Everything Is a File and Types of Files in Linux

If you are new to Linux or have used it for a few months, then you must have heard or read statements such as “In Linux, everything is a File”.

That is in fact true although it is just a generalization concept, in Unix and its derivatives such as Linux, everything is considered as a file. If something is not a file, then it must be running as a process on the system.

To understand this, take for example the amount of space on your root (/) directory is always consumed by different types of Linux files. When you create a file or transfer a file to your system, it occupies some space on the physical disk and it is considered to be in a specific format (file type).

Everything is a File and Types of Files in Linux
Everything is a File and Types of Files in Linux

Also, the Linux system does not differentiate between files and directories, but directories do one important job, which is to store other files in groups in a hierarchy for easy location. All your hardware components are represented as files and the system communicates with them using these files.

The idea is an important description of a great property of Linux, where input/output resources such as your documents, directories (folders in Mac OS X and Windows), keyboard, monitor, hard drives, removable media, printers, modems, virtual terminals, and also inter-process and network communication are streams of bytes defined by file system space.

A notable advantage of everything being a file is that the same set of Linux tools, utilities, and APIs can be used on the above input/output resources. Although everything in Linux is a file, there are certain special files that are more than just a file for example sockets and named pipes.

Different Types of Files in Linux?

In Linux there are basically three types of files:

  1. Ordinary/Regular files
  2. Special files
  3. Directories

Ordinary/Regular Files

These are files data contain text, data, or program instructions and they are the most common type of files you can expect to find on a Linux system they include:

  1. Readable files
  2. Binary files
  3. Image files
  4. Compressed files and so on.

Special Files

Special files include the following:

Linux Block Files

These are device files that provide buffered access to system hardware components. They provide a method of communication with device drivers through the file system.

One important aspect of block files is that they can transfer a large block of data and information at a given time.

Listing block files sockets in a directory using the ls command:

# ls -l /dev | grep "^b" brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 loop0
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 loop1
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 loop2
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 loop3
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 loop4
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 loop5
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 loop6
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 loop7
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 sda
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 sda1
brw-rw---- root disk 0 B Wed Apr 26 08:39:10 2023 sda2
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 sdb
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 sdb1
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 sdb2
brw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 sdb3
...
Linux Character Files

These are also device files that provide unbuffered serial access to system hardware components. They work by providing a way of communication with devices by transferring data one character at a time.

Listing character files sockets in a directory:

# ls -l /dev | grep "^c" crw-r--r-- root root 0 B Wed Apr 26 08:39:09 2023 autofs
crw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 btrfs-control
crw--w---- root tty 0 B Wed Apr 26 08:39:09 2023 console
crw------- root root 0 B Wed Apr 26 08:39:09 2023 cpu_dma_latency
crw------- root root 0 B Wed Apr 26 08:39:09 2023 cuse
crw------- root root 0 B Wed Apr 26 08:39:09 2023 ecryptfs
crw-rw---- root video 0 B Wed Apr 26 08:39:09 2023 fb0
crw-rw-rw- root root 0 B Wed Apr 26 08:39:09 2023 full
crw-rw-rw- root root 0 B Wed Apr 26 08:39:09 2023 fuse
crw------- root root 0 B Wed Apr 26 08:39:09 2023 hidraw0
crw------- root root 0 B Wed Apr 26 08:39:09 2023 hidraw1
crw------- root root 0 B Wed Apr 26 08:39:09 2023 hidraw2
crw------- root root 0 B Wed Apr 26 08:39:09 2023 hidraw3
crw------- root root 0 B Wed Apr 26 08:39:09 2023 hpet
crw------- root root 0 B Wed Apr 26 08:39:09 2023 hwrng
crw------- root root 0 B Wed Apr 26 08:39:09 2023 i2c-0
crw------- root root 0 B Wed Apr 26 08:39:12 2023 i2c-1
crw------- root root 0 B Wed Apr 26 08:39:12 2023 i2c-2
crw------- root root 0 B Wed Apr 26 08:39:12 2023 i2c-3
crw-r--r-- root root 0 B Wed Apr 26 08:39:09 2023 kmsg
crw-rw----+ root kvm 0 B Wed Apr 26 08:39:09 2023 kvm
crw-rw---- root disk 0 B Wed Apr 26 08:39:09 2023 loop-control
crw-rw---- root lp 0 B Wed Apr 26 08:39:09 2023 lp0
crw------- root root 0 B Wed Apr 26 08:39:09 2023 mcelog
crw------- root root 0 B Wed Apr 26 08:39:09 2023 mei0
...
Linux Symbolic Link Files

A symbolic link is a reference to another file on the system. Therefore, symbolic link files are files that point to other files, and they can either be directories or regular files.

Listing symbolic link sockets in a directory:

# ls -l /dev/ | grep "^l" lrwxrwxrwx root root 13 B Wed Apr 26 08:39:04 2023 fd ⇒ /proc/self/fd
lrwxrwxrwx root root 11 B Wed Apr 26 08:39:04 2023 core ⇒ /proc/kcore
lrwxrwxrwx root root 12 B Wed Apr 26 08:39:09 2023 initctl ⇒ /run/initctl
lrwxrwxrwx root root 28 B Wed Apr 26 08:39:09 2023 log ⇒ /run/systemd/journal/dev-log
lrwxrwxrwx root root 4 B Wed Apr 26 08:39:09 2023 rtc ⇒ rtc0
lrwxrwxrwx root root 15 B Wed Apr 26 08:39:04 2023 stderr ⇒ /proc/self/fd/2
lrwxrwxrwx root root 15 B Wed Apr 26 08:39:04 2023 stdin ⇒ /proc/self/fd/0
lrwxrwxrwx root root 15 B Wed Apr 26 08:39:04 2023 stdout ⇒ /proc/self/fd/1
...

You can make symbolic links using the ln utility in Linux as in the example below.

# touch file1.txt
# ln -s file1.txt /home/tecmint/file1.txt [create symbolic link]
# ls -l /home/tecmint/ | grep "^l" [List symbolic links]

In the above example, I created a file called file1.txt in /tmp directory, then created the symbolic link, /home/tecmint/file1.txt to point to /tmp/file1.txt.

Linux Pipes or Named Pipes

These are files that allow inter-process communication by connecting the output of one process to the input of another. A named pipe is actually a file that is used by two processes to communicate with each and it acts as a Linux pipe.

Listing pipes sockets in a directory:

# ls -l | grep "^p" prw-rw-r-- 1 tecmint tecmint 0 Apr 18 17:47 pipe1
prw-rw-r-- 1 tecmint tecmint 0 Apr 18 17:47 pipe2
prw-rw-r-- 1 tecmint tecmint 0 Apr 18 17:47 pipe3
prw-rw-r-- 1 tecmint tecmint 0 Apr 18 17:47 pipe4
prw-rw-r-- 1 tecmint tecmint 0 Apr 18 17:47 pipe5

You can use the mkfifo utility to create a named pipe in Linux as follows.

# mkfifo pipe1
# echo "This is named pipe1" > pipe1

In the above example, I created a named pipe called pipe1, then I passed some data to it using the echo command, after that the shell became un-interactive while processing the input.

Then I opened another shell and run another command to print out what was passed to the pipe.

# while read line ;do echo "This was passed-'$line' "; done<pipe1
Linux Socket Files

These are files that provide a means of inter-process communication, but they can transfer data and information between processes running on different environments.

This means that sockets provide data and information transfer between processes running on different machines on a network.

An example to show the work of sockets would be a web browser making a connection to a web server.

# ls -l /dev/ | grep "^s" srw-rw-rw- 1 root root 0 Apr 18 10:26 log

This is an example of a socket created in C by using the socket() system call.

int socket_desc= socket(AF_INET, SOCK_STREAM, 0 );

In the above:

  • AF_INET is the address family(IPv4)
  • SOCK_STREAM is the type (connection is TCP protocol oriented)
  • 0 is the protocol(IP Protocol)

To refer to the socket file, use the socket_desc, which is the same as the file descriptor, and use read() and write() system calls to read and write from the socket respectively.

Linux Special Directories

These are special files that store both ordinary and other special files and they are organized on the Linux file system in a hierarchy starting from the root (/) directory.

Listing sockets in a directory:

# ls -l / | grep "^d" drwxr-xr-x root root 4.0 KB Fri Apr 21 10:21:42 2023 boot
drwxr-xr-x root root 4.0 KB Wed Oct 6 22:55:05 2021 cdrom
drwxr-xr-x root root 4.4 KB Wed Apr 26 08:39:12 2023 dev
drwxr-xr-x root root 12 KB Sat Apr 22 10:59:02 2023 etc
drwxr-xr-x root root 4.0 KB Wed Oct 6 22:55:12 2021 home
drwx------ root root 16 KB Wed Oct 6 22:52:31 2021 lost+found
drwxr-xr-x root root 4.0 KB Thu Oct 7 18:03:09 2021 media
drwxr-xr-x root root 4.0 KB Mon Jul 18 11:34:54 2022 mnt
drwxr-xr-x root root 4.0 KB Tue Apr 4 15:09:01 2023 opt
dr-xr-xr-x root root 0 B Wed Apr 26 08:39:04 2023 proc
drwx------ root root 4.0 KB Fri Apr 7 11:28:42 2023 root
drwxr-xr-x root root 1.1 KB Wed Apr 26 08:44:09 2023 run
drwxr-xr-x root root 4.0 KB Wed Dec 15 13:23:40 2021 srv
dr-xr-xr-x root root 0 B Wed Apr 26 08:39:04 2023 sys
drwxrwxrwt root root 4.0 KB Wed Apr 26 10:14:23 2023 tmp
drwxr-xr-x root root 4.0 KB Sat Jul 3 22:29:26 2021 usr
drwxr-xr-x root root 4.0 KB Thu Mar 30 10:46:04 2023 var

You can make a directory using the mkdir command.

# mkdir -m 1666 tecmint.com
# mkdir -m 1666 news.tecmint.com
# mkdir -m 1775 linuxsay.com
Summary

You should now be having a clear understanding of why everything in Linux is a file and the different types of files that can exist on your Linux system.

You can add more to this by reading more about the individual file types and they are created. I hope you find this guide helpful and for any questions and additional information that you would love to share, please leave a comment and we shall discuss more.

Similar Posts