(翻译中 by runningwater) Explanation of “Everything is a File” and Types of Files in Linux ==================================================================== ![](http://www.tecmint.com/wp-content/uploads/2016/05/Everything-is-a-File-in-Linux.png) >Everything is a File and Types of Files in Linux 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). And also the Linux system does not differentiate between files and directories, but directories do one important job, that is 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][1]. ### What are the different types of files in Linux? In Linux there are basically three types of files: - Ordinary/Regular files - Special files - 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 and they include: - Readable files - Binary files - Image files - Compressed files and so on. #### Special Files Special files include the following: 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 about 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: ``` # ls -l /dev | grep "^b" ``` Sample Output ``` brw-rw---- 1 root disk 7, 0 May 18 10:26 loop0 brw-rw---- 1 root disk 7, 1 May 18 10:26 loop1 brw-rw---- 1 root disk 7, 2 May 18 10:26 loop2 brw-rw---- 1 root disk 7, 3 May 18 10:26 loop3 brw-rw---- 1 root disk 7, 4 May 18 10:26 loop4 brw-rw---- 1 root disk 7, 5 May 18 10:26 loop5 brw-rw---- 1 root disk 7, 6 May 18 10:26 loop6 brw-rw---- 1 root disk 7, 7 May 18 10:26 loop7 brw-rw---- 1 root disk 1, 0 May 18 10:26 ram0 brw-rw---- 1 root disk 1, 1 May 18 10:26 ram1 brw-rw---- 1 root disk 1, 10 May 18 10:26 ram10 brw-rw---- 1 root disk 1, 11 May 18 10:26 ram11 brw-rw---- 1 root disk 1, 12 May 18 10:26 ram12 brw-rw---- 1 root disk 1, 13 May 18 10:26 ram13 brw-rw---- 1 root disk 1, 14 May 18 10:26 ram14 brw-rw---- 1 root disk 1, 15 May 18 10:26 ram15 brw-rw---- 1 root disk 1, 2 May 18 10:26 ram2 brw-rw---- 1 root disk 1, 3 May 18 10:26 ram3 brw-rw---- 1 root disk 1, 4 May 18 10:26 ram4 brw-rw---- 1 root disk 1, 5 May 18 10:26 ram5 ... ``` 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" ``` Sample Output ``` crw------- 1 root root 10, 235 May 18 15:54 autofs crw------- 1 root root 10, 234 May 18 15:54 btrfs-control crw------- 1 root root 5, 1 May 18 10:26 console crw------- 1 root root 10, 60 May 18 10:26 cpu_dma_latency crw------- 1 root root 10, 203 May 18 15:54 cuse crw------- 1 root root 10, 61 May 18 10:26 ecryptfs crw-rw---- 1 root video 29, 0 May 18 10:26 fb0 crw-rw-rw- 1 root root 1, 7 May 18 10:26 full crw-rw-rw- 1 root root 10, 229 May 18 10:26 fuse crw------- 1 root root 251, 0 May 18 10:27 hidraw0 crw------- 1 root root 10, 228 May 18 10:26 hpet crw-r--r-- 1 root root 1, 11 May 18 10:26 kmsg crw-rw----+ 1 root root 10, 232 May 18 10:26 kvm crw------- 1 root root 10, 237 May 18 10:26 loop-control crw------- 1 root root 10, 227 May 18 10:26 mcelog crw------- 1 root root 249, 0 May 18 10:27 media0 crw------- 1 root root 250, 0 May 18 10:26 mei0 crw-r----- 1 root kmem 1, 1 May 18 10:26 mem crw------- 1 root root 10, 57 May 18 10:26 memory_bandwidth crw------- 1 root root 10, 59 May 18 10:26 network_latency crw------- 1 root root 10, 58 May 18 10:26 network_throughput crw-rw-rw- 1 root root 1, 3 May 18 10:26 null crw-r----- 1 root kmem 1, 4 May 18 10:26 port crw------- 1 root root 108, 0 May 18 10:26 ppp crw------- 1 root root 10, 1 May 18 10:26 psaux crw-rw-rw- 1 root tty 5, 2 May 18 17:40 ptmx crw-rw-rw- 1 root root 1, 8 May 18 10:26 random ``` 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" ``` Sample Output ``` lrwxrwxrwx 1 root root 3 May 18 10:26 cdrom -> sr0 lrwxrwxrwx 1 root root 11 May 18 15:54 core -> /proc/kcore lrwxrwxrwx 1 root root 13 May 18 15:54 fd -> /proc/self/fd lrwxrwxrwx 1 root root 4 May 18 10:26 rtc -> rtc0 lrwxrwxrwx 1 root root 8 May 18 10:26 shm -> /run/shm lrwxrwxrwx 1 root root 15 May 18 15:54 stderr -> /proc/self/fd/2 lrwxrwxrwx 1 root root 15 May 18 15:54 stdin -> /proc/self/fd/0 lrwxrwxrwx 1 root root 15 May 18 15:54 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`. 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 process to communicate with each and it acts as a Linux pipe. Listing pipes sockets in a directory: ``` # ls -l | grep "^p" ``` Sample Output ``` prw-rw-r-- 1 tecmint tecmint 0 May 18 17:47 pipe1 prw-rw-r-- 1 tecmint tecmint 0 May 18 17:47 pipe2 prw-rw-r-- 1 tecmint tecmint 0 May 18 17:47 pipe3 prw-rw-r-- 1 tecmint tecmint 0 May 18 17:47 pipe4 prw-rw-r-- 1 tecmint tecmint 0 May 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][2], after that the shell became un-interactive while processing the input. Then I opened another shell and run the another command to print out what was passed to pipe. ``` # while read line ;do echo "This was passed-'$line' "; done