Linux Tutorials

Everything Is File In Linux – Part 1

everything is file in linux part 1

Divided into 2 parts, in this first part I will introduce the concept that everything is file and present the special devices /dev/null, /dev/zero, /dev/random and /dev/full. Part 2 will be to present didactically interesting features about this, for example, how to turn a file into a partition!

Introduction

  • What is it?

Everything is File is the concept of how the system records and keeps everything in its structure. Unlike other operating systems, UNIX-like operating systems treat everything as a common file.

This means that not only partitions are mounted as files but directories of specific devices such as RAM, smartphones, external disks, and optical media discs are all files. Besides these, sockets and pipes are also files!

In addition, each TTY terminal – the ones you open with CTRL ALT F1 – are “files” in the eyes of the system.

Directory /dev

If you browse the / dev directory, you will see all files corresponding to system mounts relative to other media. It is as if you plugged a cell phone into the computer, an external hard drive or even a USB stick and all became “a file” for the operating system.

System partitions are the files started with “sd” in the /dev folder.​In the same folder, TTYs are TeleTypewriter devices, the shell terminal that is accessed by the “CTRL ALT F1” command. Already the assembly blocks used by the optical media system (DVD, CD and etc) are also in this folder, in the files listed “loop“.

linux archive dev blocks

Special Devices

The biggest curiosity of this post is because of these 4 small files in the folder /dev:  full, zero, random and null. Can you differentiate the function of each of them?  What they have in common lies in the fact that they are “special files” and not mere directories.

Device Null: /dev/null

The null device (device null) is typically used to discard the output stream of a process whose result does not matter for some reason. It also serves as an empty file of convenience. Usually, this operation is performed through a redirection command on the system console. Any file sent to the device “/dev/null” will disappear forever because of the way it operates, ditching the data instantly.

It’s like a big black hole, a junk that never fills.

​Poetically, programmers make lots of jokes about /dev/null because of this peculiarity.

Try the following command:$ echo “Hello World” You will see “Hello World” as the command output.

Now try:

$ echo “Hello World”> /dev/null

Will return empty output. That is, nothing will happen.

Because the command output “Hello world” went to Nowhere.
It was swallowed by a Black Hole.

He went to visit Courage the Cowardly Dog in Nowhere! Or any other crazy place your imagination allows.

black hole file in linux

​/dev/null for UNIX-based systems works like a black hole:

What is sent there does not come back!​Leaving the jokes aside, basically, the command output of the process was not stored, it was a direct discard as if it had never been generated.

Device Full: /dev/full

On Linux, the always full device is a special file that always returns the same error code when it is accessed for writing: ENOSPC – which means “There is no space available on the device“. 

In addition, it has another function: Because it is always full, it provides an infinite number of null characters (NULL) to any process that accesses it as read. This device is commonly used to test the behavior of a program when encountering an error due to a full memory disk!

Try the following command:$ echo “Hello world”> /dev/fullIt will give this device full error, imitating a crowded HDD!
bash: echo: write error: No space left on device

Zero Device: /dev/zero

On Unix-like operating systems, /dev/zero is a special file that provides how many null characters are read from it – the NULL of the ASCII table, 0x00; and not the character “zero digit”, “0”, whose value is 0x30–.

The null character stream generated by this device can, for example, be used to overwrite information on a disk (to clean it) or to generate a clean file of size X to infinity. The BSD system uses it by implementing shared memory through the mmap function, which maps/dev / zero to RAM.

Creates a large empty file called a ‘test’:

$ dd if=/dev/ zero of=/home/
$ USER /test count=1000 bs=1000

If you do not impose a limit to the command, getting as “dd if=/dev/zero of=/home / $ USER/test”, you will see a file that will fatten tending to infinity; it only stops growing when the disk drive is full, resulting in a “no disk space available” error.

This command is interesting when you want to recover a pen drive that has the partition table corrupted and is unrecoverable. The dd command forces writing, and /dev /zero will populate each storage block, with zeros. It will be in a newly-manufactured state, requiring you to later use a program such as Disks or Gparted to rebuild your partition table!

​Be careful with this command, if the output is of = /dev /sda1, you will zero your entire sda1 disk (fills it with zeros) making the files unrecoverable.

Random Device: /dev/random

It works by using the same logic as /dev/zero, but filling a disk or creating a file of size n with random values, generated from computer processing, collecting GPU information characters, RAM memory and so on.

/Dev/random should be appropriate for uses that require high-quality random numbers such as a one-time pad or public key generation for SSH and other sharing networks.

Quick Curiosity!

What happens if we send the nowhere? That is if we send the contents of /dev/zero to /dev/null

$ sudo dd if =/dev/zero of=/dev/null

This command will generate a file that tends to infinity, which will be recorded in the infinite space device – Our black hole! In practice, it does not damage the hardware disks because no data is actually generated and written to disk. But under the kernel’s perspective, there is a data transfer there: it will generate high CPU consumption, which will select one of the cores to generate such processing occupying 100% use. 

This type of command is used for performance testing, for example, to check how the computer behaves with the CPU at 100% usage.

Conclusion

In this post, you learned more details about how UNIX-based systems recognize devices and how everything is “file”. They also knew details about the special devices.

In the next post in the series, I’ll talk about some interesting utilities for these devices, for example, creating a partition from a file, among other curiosities that I do not want to comment on not to spoil!



LinuxAndUbuntu hosting is sponsored by massiveGRID

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This website uses cookies to improve your experience. We'll assume you're ok with this. Thank you! Ok Read More