Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the concept of "everything is a file" and the corresponding file types in Linux

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how to analyze the concept of "everything is a file" in Linux and the corresponding file types, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

In Unix and its derivatives such as Linux, everything can be seen as a file. Although it is only a general concept, it is true. If there is one that is not a file, it must be a running process.

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

Although there is no difference between files and directories in Linux systems, directories also have an important function, that is, there are structural grouping to store other files for easy search and access. All hardware components are represented as files that the system uses to communicate with the hardware.

These ideas are important expositions of various things in Linux, so input / output resources such as documents, directories (called folders in Mac OS X and Windows systems), keyboards, monitors, hard drives, removable media devices, printers, modems, virtual terminals, and interprocess communication (IPC) and network communications are all byte streams defined in the file system space.

Everything can be thought of as a file, and the most obvious benefit is that you only need the same set of Linux tools, utilities, and API for the input / output resources listed above.

Although everything can be thought of as a file in Linux, there are also special files, such as sockets and command pipes.

What are the differences in Linux file types?

There are three basic file types in the Linux system:

Normal / regular file

Special document

Catalog file

Normal / regular file

They are files containing text, data, program instructions and other data, which are the most common in Linux systems. These include the following:

Readable file

Binary file

Image file

Compressed files, etc.

Special document

Special files include the following:

Block file (block): device file that provides a cache interface for accessing system hardware components. They provide a way to communicate with the device driver through the file system.

An important performance of block files is that they can transfer large chunks of data and information within a specified period of time.

List the block files in a directory:

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

Output example

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 file (Character): also a device file that provides an unbuffered serial interface for accessing system hardware components. They communicate with devices by transmitting only one character of data at a time.

List the character files in a directory:

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

Output example

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 (Symbolic link): symbolic links are references to other files on the system. Therefore, symbolic link files are files that point to other files, which can be directories or regular files.

List the symbolic link files in a directory:

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

Output example

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

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 file]

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

Pipes (Pipe) and command pipes (Named pipe): files that connect the output of one process to the input of another process, thereby allowing inter-process communication (IPC).

A named pipe is actually a file used to make two processes communicate with each other, just like a Linux pipe.

List the pipe files in a directory:

# ls-l | grep "^ p"

Examples of 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 tool in Linux to create a named pipe, as follows:

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

In the example above, we created a named pipe called pipe1, and then used the echo command to add some data, after which shell became non-interactive when processing the input data. (LCTT)

Then we open another shell terminal and run another command to print out the data that has just joined the pipe.

# while read line; do echo "This was passed-'$line'"; done

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report