Interpreting "Everything is a file" concept in Linux and the corresponding file type

Source: Internet
Author: User
Tags echo command

in Unix and its derivative, such as Linux, everything can be considered a file. Although it is only a general concept, it is true. If there is not a file, then it must be a running process.

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

Although the files and directories in the Linux system are not different, the directory has an important function, that is, there are structured groupings to store other files to facilitate the search for access. All hardware components are represented as files, and the system uses these files to communicate with the hardware.

These ideas are important for everything in Linux, so like documents, directories (called folders under Mac OS X and Windows), keyboards, monitors, hard drives, removable media devices, printers, modems, virtual terminals, interprocess communication (IPC), and network communication inputs /output resources are byte streams that are defined in the file system space.

Everything can be viewed as a file, and the most significant benefit is that the same set of Linux tools, utilities, and APIs are required for the input/output resources listed above.

While everything in Linux can be viewed as a file, there are special files, such as sockets and command pipelines.

There are three basic file types in the Linux system:

    • Normal/Regular file
    • Special files
    • Catalog files
Normal/Regular file

They are files that contain data such as text, data, and program directives, which are among the most common in Linux systems. Include the following:

    • Readable files
    • binary files
    • Image file
    • Compress files, etc.
Special Files

Special files include the following:

blocks file (block)

block file : A device file that provides a cache interface for accessing the system hardware part. They provide a way to communicate through a file system to a device driver.

One of the important properties about block files is that they can transmit chunks of data and information within a specified time.

To list the block files under a directory:

# Ls-l/dev | grep "^b"

Output example

BRW-RW----1 root disk 7, 0 10:26 LOOP0BRW-RW----1 root disk 7, 1 May 10:26 LOOP1BRW-RW---- 1 root disk 7, 2 10:26 LOOP2BRW-RW----1 root disk 7, 3 May 10:26 LOOP3BRW-RW----1 root D        ISK 7, 4 10:26 LOOP4BRW-RW----1 root disk 7, 5 May 10:26 LOOP5BRW-RW----1 root disk 7, 6 10:26 LOOP6BRW-RW----1 root disk 7, 7 May 10:26 LOOP7BRW-RW----1 root disk 1, 0 Ma Y 10:26 ram0brw-rw----1 root disk 1, 1 May 10:26 RAM1BRW-RW----1 root disk 1, may 10:26 R AM10BRW-RW----1 root disk 1, 10:26 RAM11BRW-RW----1 root disk 1, 10:26 RAM12BRW-RW ----1 root Disk 1, 10:26 RAM13BRW-RW----1 root disk 1, 10:26 RAM14BRW-RW----1 ro        OT Disk 1, 10:26 RAM15BRW-RW----1 root disk 1, 2 May 10:26 RAM2BRW-RW----1 root disk 1, 3 May 18 10:26RAM3BRW-RW----1 root disk 1, 4 10:26 RAM4BRW-RW----1 root disk 1, 5 May 10:26 ram5 ... 
character file (Character)

character file (Character): Also a device file that provides a non-buffered serial interface to access the system hardware components. They work with devices in a way that transmits only one character of data at a time.

List the character files under a directory:

# Ls-l/dev | grep "^c"

Output example

CRW-------1 root root, 235 15:54 autofscrw-------1 root root, 234 may 15:54 Btrfs-controlc RW-------1 root root 5, 1 May 10:26 consolecrw-------1 root root, 10:26 Cpu_dma_latenc YCRW-------1 root root, 203 may 15:54 cusecrw-------1 root root, 10:26 ecryptfscrw-rw- ---1 root video, 0 10:26 fb0crw-rw-rw-1 root root 1, 7 May 10:26 fullcrw-rw-rw-1 root R       Oot, 229 10:26 fusecrw-------1 root root 251, 0 may 10:27 hidraw0crw-------1 root root  228 10:26 hpetcrw-r--r--1 root root 1, 10:26 kmsgcrw-rw----+ 1 root root, 232 may 10:26 KVMCRW-------1 root root, 237 10:26 loop-controlcrw-------1 root root, 227 May 18 1 0:26 MCELOGCRW-------1 root root 249, 0 may 10:27 media0crw-------1 root root and 0 may 10:26 Mei 0crw-r-----1 Root Kmem        1, 1 May 10:26 memcrw-------1 root root, 10:26 memory_bandwidthcrw-------1 root root  10:26 NETWORK_LATENCYCRW-------1 root root, May 10:26 network_throughputcrw-rw-rw-       1 root root 1, 3 10:26 nullcrw-r-----1 root kmem 1, 4 May 10:26 portcrw-------1 root root   108, 0 10:26 pppcrw-------1 root root, 1 May 10:26 psauxcrw-rw-rw-1 root TTY 5, 2 17:40 ptmxcrw-rw-rw-1 root root 1, 8 May 10:26 random
Symbolic Link file (symboliclink)

Symbolic Link file (symboliclink) : A symbolic link is a reference to another file on the system. Therefore, the symbolic link file is a file that points to other files, which can be directories or regular files.

List the symbolic link files under a directory:

# ls-l/dev/| grep "^l"

Output example

lrwxrwxrwx  1 root root             3 10:26 cdrom-sr0lrwxrwxrwx  1 root root one may            15:54 core-/PR  oc/kcorelrwxrwxrwx  1 root root            15:54 FD-/proc/self/fdlrwxrwxrwx  1 root root             4 May 18 10:26 RTC-rtc0lrwxrwxrwx  1 root root             8 May 10:26 SHM-/run/shmlrwxrwxrwx  1 root root            18 15:54 stderr-/proc/self/fd/2lrwxrwxrwx  1 root root *            15:54 stdin-/proc/self/fd/0lrwxrwxrwx< c14/>1 root root            15:54 stdout-/PROC/SELF/FD/1

Using the ln tool in Linux, you can create a symbolic link file as follows:

# Touch file1.txt# ln-s file1.txt/home/tecmint/file1.txt  [Create symbolic Link file]# ls-l/home/tecmint/| grep "^l"         [list Symbolic link text Thing

In the above example, first we create a file named File1.txt in the/tmp directory, then create a symbolic link file, and point the/home/tecmint/file1.txt to the/tmp/file1.txt file.

Piping (pipe) and command pipes (Namedpipe)

Piping (pipe) and command pipes (Namedpipe) : The output of one process is connected to the input of another process, thus allowing interprocess communication (IPC) files.

A named pipe is actually a file that enables two processes to communicate with each other, just like a Linux pipeline.

To list the pipeline files under a directory:

# Ls-l | grep "^p"

Output Example:

prw-rw-r--1 Tecmint tecmint    0 may 17:47 pipe1prw-rw-r--1 tecmint tecmint    0 may 17:47 pipe2prw-rw-r--1 t Ecmint tecmint    0 17:47 pipe3prw-rw-r--1 tecmint tecmint 0 may    17:47 pipe4prw-rw-r--1 tecmint tecmint    0 May 17:47 Pipe5

In Linux, you can use the Mkfifo tool to create a named pipe, as follows:

# Mkfifo pipe1# echo "This is named Pipe1" > Pipe1

In the example above, we created a named pipe named Pipe1, and then added some data using the echo command, which then became noninteractive when processing the input data (LCTT: The pipe was occupied).

Then we open another shell terminal and run another command to print out the data that we just added to the pipeline.

# while the read line;d o echo "This is passed-' $line '"; Done<pipe1
Socket file (socket)

socket file (socket) : A file that provides interprocess communication methods that can transfer data and information between processes running in different environments.

This means that sockets can provide data and information transfer for processes running on different machines in the network.

An example of a socket run is the process by which a Web browser connects to a Web server.

# ls-l/dev/| grep "^s"

Output Example:

srw-rw-rw-  1 root root             0 may 10:26 log

The following is an example of a call to the socket () system that was written using the C language.

int socket_desc= socket (af_inet, sock_stream, 0);

In the example above:

    • Af_inet refers to the Address field (IPV4)
    • Sock_stream refers to the type (the TCP protocol connection is used by default)
    • 0 means protocol (IP protocol)

Use Socket_desc to refer to the pipe file, which is the same as the file descriptor, and then use the system function read () and write () to read and write data from the pipe file separately.

Catalog Files

This is a special file that can contain both ordinary files and other special files, which are hierarchically organized in the Linux file system starting from the root (/) directory.

List the directory files under a directory:

Output Example:

Drwxr-xr-x 2 root root 4096 5 15:49 bindrwxr-xr-x 4 root root 4096 May 5 15:58 bootdrwxr-xr-x 2 root root 4 096 Apr cdromdrwxr-xr-x root root 4400 may 10:27 devdrwxr-xr-x 168 root root 12288 may 10:28 etcdrwxr- Xr-x 3 root root 4096 Apr homedrwxr-xr-x root root 4096 May 5 15:44 libdrwxr-xr-x 2 root root 4096 Ma Y 5 15:44 lib64drwx------2 root root 16384 Apr lost+founddrwxr-xr-x 3 root root 4096 Apr mediadrw Xr-xr-x 3 root root 4096 Feb 17:54 mntdrwxr-xr-x root root 4096 Apr 16:01 optdr-xr-xr-x 223 root root 0    15:54 procdrwx------root root 4096 Apr 9 11:12 rootdrwxr-xr-x root root 920 may 10:54 rundrwxr-xr-x  2 root root 12288 5 15:57 sbindrwxr-xr-x 2 root root 4096 Dec 1 srvdr-xr-xr-x root root 0 May 18 15:54 SYSDRWXRWXRWT root root 4096 may 17:55 tmpdrwxr-xr-x root root 4096 Mar 16:00 usrdrwxr-xr-x T root 4096 Nov 12 2015 var 

You can use the mkdir command to create a directory.

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

It's time to get a clear idea of why everything in a Linux system is a file and what types of files can exist in a Linux system.

You can add more knowledge by reading more articles about each file type and creating the corresponding process. I hope this tutorial is helpful to you. If you have any questions or have additional knowledge, please leave a comment and discuss it together.

This article reprinted address: http://www.linuxprobe.com/linux-always-files.html

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

Interpreting "Everything is a file" concept in Linux and the corresponding file type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.