How to understand and identify file types in Linux

  • 2021-01-18 06:57:19
  • OfStack

preface

As we all know, in Linux everything is a file, including hard disks, graphics cards, etc. When navigating through Linux, most of the files are plain files and directory files. But there are other types, which correspond to five different types of action. Therefore, understanding file types in Linux is important in many ways.

If you don't believe me, just read the full text and you'll see how important it is. If you don't understand file types, you can't make arbitrary changes without fear.

If you make the wrong changes that will crash your file system, be careful when you do it. Files are very important in the Linux system because all devices and daemons are stored as files.

How many types are available in Linux?

As far as I know, there are a total of seven types of files in Linux, divided into three broad categories. The details are as follows.

The & # 8226; Common file

The & # 8226; Directory file

The & # 8226; Special files (this class has 5 file types)

The & # 8226; Link to the file

The & # 8226; Character device file
The & # 8226; Socket file
The & # 8226; Named pipe file
The & # 8226; Block file

Refer to the following table for a better understanding of file types in Linux.

符号 意义
普通文件。长列表中以下划线 _ 开头。
d 目录文件。长列表中以英文字母 d 开头。
l 链接文件。长列表中以英文字母 l 开头。
c 字符设备文件。长列表中以英文字母 c 开头。
s Socket 文件。长列表中以英文字母 s 开头。
p 命名管道文件。长列表中以英文字母 p 开头。
b 块文件。长列表中以英文字母 b 开头。

Method 1: Manually identify the file type in Linux

If you know ES46en well, you can easily identify file types with the help of the table above.

How do I view plain files in Linux?

Use the following command in Linux to view a plain file. Ordinary files can be found anywhere in the Linux file system. The color of normal files is "white".


# ls -la | grep ^-
-rw-------. 1 mageshm mageshm  1394 Jan 18 15:59 .bash_history
-rw-r--r--. 1 mageshm mageshm  18 May 11 2012 .bash_logout
-rw-r--r--. 1 mageshm mageshm  176 May 11 2012 .bash_profile
-rw-r--r--. 1 mageshm mageshm  124 May 11 2012 .bashrc
-rw-r--r--. 1 root root   26 Dec 27 17:55 liks
-rw-r--r--. 1 root root 104857600 Jan 31 2006 test100.dat
-rw-r--r--. 1 root root 104874307 Dec 30 2012 test100.zip
-rw-r--r--. 1 root root  11536384 Dec 30 2012 test10.zip
-rw-r--r--. 1 root root   61 Dec 27 19:05 test2-bzip2.txt
-rw-r--r--. 1 root root   61 Dec 31 14:24 test3-bzip2.txt
-rw-r--r--. 1 root root   60 Dec 27 19:01 test-bzip2.txt

How do I view directory files in Linux?

Use the following command in Linux to view the directory file. Directory files can appear anywhere in the Linux file system. The color of the directory file is blue.


# ls -la | grep ^d
drwxr-xr-x. 3 mageshm mageshm  4096 Dec 31 14:24 links/
drwxrwxr-x. 2 mageshm mageshm  4096 Nov 16 15:44 perl5/
drwxr-xr-x. 2 mageshm mageshm  4096 Nov 16 15:37 public_ftp/
drwxr-xr-x. 3 mageshm mageshm  4096 Nov 16 15:37 public_html/

How do I view linked files in Linux?

Use the following command in Linux to view the link file. Linked files can appear anywhere in the Linux file system. There are two types of linked files available, soft links and hard links. The color of the linked files is "light turquoise".


# ls -la | grep ^l
lrwxrwxrwx. 1 root root   31 Dec 7 15:11 s-link-file -> /links/soft-link/test-soft-link
lrwxrwxrwx. 1 root root   38 Dec 7 15:12 s-link-folder -> /links/soft-link/test-soft-link-folder

How do I view character device files in ES76en?

Use the following command in Linux to view the character device file. Character device files appear only in a specific location. It appears under the directory /dev. The color of the character device file is yellow.


# ls -la | grep ^c
# ls -la | grep ^c
crw-------. 1 root root  5, 1 Jan 28 14:05 console
crw-rw----. 1 root root  10, 61 Jan 28 14:05 cpu_dma_latency
crw-rw----. 1 root root  10, 62 Jan 28 14:05 crash
crw-rw----. 1 root root  29, 0 Jan 28 14:05 fb0
crw-rw-rw-. 1 root root  1, 7 Jan 28 14:05 full
crw-rw-rw-. 1 root root  10, 229 Jan 28 14:05 fuse

How do I view block files in Linux?

Use the following command in Linux to view the block file. Block files only appear in specific locations. It appears under the directory /dev. The color of the block file is "yellow".


# ls -la | grep ^b
brw-rw----. 1 root disk  7, 0 Jan 28 14:05 loop0
brw-rw----. 1 root disk  7, 1 Jan 28 14:05 loop1
brw-rw----. 1 root disk  7, 2 Jan 28 14:05 loop2
brw-rw----. 1 root disk  7, 3 Jan 28 14:05 loop3
brw-rw----. 1 root disk  7, 4 Jan 28 14:05 loop4

How do I view Socket files in Linux?

Use the following command in Linux to view the Socket file. The Socket file can appear anywhere. The color of the Scoket file is pink. LCTT: The original description of where Socket files and named pipe files can be found has been corrected.


# ls -la| grep ^s
srw-rw-rw- 1root root0 Jan 5 16:36system_bus_socket

How do I view named pipe files in Linux?

Use the following command in Linux to view the named pipe file. Named pipe files can appear anywhere. The color of the named pipe file is "yellow."


# ls -la | grep ^p
prw-------. 1 root root  0 Jan 28 14:06 replication-notify-fifo|
prw-------. 1 root root  0 Jan 28 14:06 stats-mail|

Method 2: How do I use the file command to identify file types in Linux

The file command allows us to determine different file types in Linux. There are three test sets, with three sets of tests in that order: file system tests, magic byte tests, and language tests for identifying file types.

How to use the file command to view normal files in Linux

Simply type the file command on your terminal along with the plain file. The file command will read the contents of the provided file and display the exact type of the file.

This is why we see different results for each plain file. Refer to the following common file for different results.


# file 2daygeek_access.log
2daygeek_access.log:ASCII text, with very long lines
 
# file powertop.html
powertop.html:HTML document,ASCII text, with very long lines
 
# file 2g-test
2g-test:JSON data
 
# file powertop.txt
powertop.txt:HTML document,UTF-8 Unicode text, with very long lines
 
# file 2g-test-05-01-2019.tar.gz
2g-test-05-01-2019.tar.gz:gzip compressed data, last modified: Sat Jan 5 18:22:20 2019, from Unix,original size 450560

How to use the file command to view directory files in Linux?

On your terminal simply type the file command following the directory. See the results below.


# file Pictures/
Pictures/:directory

How do I use the file command to view link files in Linux?

Simply type the file command on your terminal and follow the link file. See the results below.


# file log
log:symbolic link to/run/systemd/journal/dev-log

How do I use the file command to view character device files in Linux?

On your terminal simply type the file command followed by the character device file. See the results below.


# ls -la | grep ^d
drwxr-xr-x. 3 mageshm mageshm  4096 Dec 31 14:24 links/
drwxrwxr-x. 2 mageshm mageshm  4096 Nov 16 15:44 perl5/
drwxr-xr-x. 2 mageshm mageshm  4096 Nov 16 15:37 public_ftp/
drwxr-xr-x. 3 mageshm mageshm  4096 Nov 16 15:37 public_html/
0

How do I use the file command to view block files in Linux?

On your terminal simply type the file command following the block file. See the results below.


# ls -la | grep ^d
drwxr-xr-x. 3 mageshm mageshm  4096 Dec 31 14:24 links/
drwxrwxr-x. 2 mageshm mageshm  4096 Nov 16 15:44 perl5/
drwxr-xr-x. 2 mageshm mageshm  4096 Nov 16 15:37 public_ftp/
drwxr-xr-x. 3 mageshm mageshm  4096 Nov 16 15:37 public_html/
1

How to use the file command to view Socket files?

On your terminal, simply type the file command following the Socket file. See the results below.


# ls -la | grep ^d
drwxr-xr-x. 3 mageshm mageshm  4096 Dec 31 14:24 links/
drwxrwxr-x. 2 mageshm mageshm  4096 Nov 16 15:44 perl5/
drwxr-xr-x. 2 mageshm mageshm  4096 Nov 16 15:37 public_ftp/
drwxr-xr-x. 3 mageshm mageshm  4096 Nov 16 15:37 public_html/
2

How to use the file command to view named pipe files in Linux?

Simply type the file command on your terminal followed by the named pipe file. See the results below.


# file pipe-test
pipe-test:fifo(named pipe)

Method 3: How do I use the stat command to identify file types in Linux?

The stat command allows us to see the file type or file system status. This utility provides more information than the file command. It displays a lot of information about the file, such as the size, block size, IO block size, Inode values, links, file permissions, UID, GID, file access/update and modification time details, etc.

How to use the stat command to view ordinary files in Linux?

On your terminal simply type the stat command followed by the plain file. See the results below.


# ls -la | grep ^d
drwxr-xr-x. 3 mageshm mageshm  4096 Dec 31 14:24 links/
drwxrwxr-x. 2 mageshm mageshm  4096 Nov 16 15:44 perl5/
drwxr-xr-x. 2 mageshm mageshm  4096 Nov 16 15:37 public_ftp/
drwxr-xr-x. 3 mageshm mageshm  4096 Nov 16 15:37 public_html/
4

How to use the stat command to view directory files in Linux?

On your terminal simply type the stat command following the directory file. See the results below.


# ls -la | grep ^d
drwxr-xr-x. 3 mageshm mageshm  4096 Dec 31 14:24 links/
drwxrwxr-x. 2 mageshm mageshm  4096 Nov 16 15:44 perl5/
drwxr-xr-x. 2 mageshm mageshm  4096 Nov 16 15:37 public_ftp/
drwxr-xr-x. 3 mageshm mageshm  4096 Nov 16 15:37 public_html/
5

How do I use the stat command to view linked files in Linux?

Simply type the stat command on your terminal and follow the link file. See the results below.


# ls -la | grep ^d
drwxr-xr-x. 3 mageshm mageshm  4096 Dec 31 14:24 links/
drwxrwxr-x. 2 mageshm mageshm  4096 Nov 16 15:44 perl5/
drwxr-xr-x. 2 mageshm mageshm  4096 Nov 16 15:37 public_ftp/
drwxr-xr-x. 3 mageshm mageshm  4096 Nov 16 15:37 public_html/
6

How do I use the stat command to view character device files in Linux?

On your terminal simply type the stat command followed by the character device file. See the results below.


# ls -la | grep ^d
drwxr-xr-x. 3 mageshm mageshm  4096 Dec 31 14:24 links/
drwxrwxr-x. 2 mageshm mageshm  4096 Nov 16 15:44 perl5/
drwxr-xr-x. 2 mageshm mageshm  4096 Nov 16 15:37 public_ftp/
drwxr-xr-x. 3 mageshm mageshm  4096 Nov 16 15:37 public_html/
7

How do I use the stat command to view block files in Linux?

On your terminal simply type the stat command following the block file. See the results below.


# stat /dev/sda1
 File: /dev/sda1
 Size: 0      Blocks: 0     IO Block: 4096  block special file
Device: 6h/6d  Inode: 250     Links: 1   Device type: 8,1
Access: (0660/brw-rw----) Uid: (  0/  root)  Gid: ( 994/  disk)
Access: 2019-01-05 16:36:31.596666806 +0530
Modify: 2019-01-05 16:36:31.596666806 +0530
Change: 2019-01-05 16:36:31.596666806 +0530
 Birth: -

How to use stat to view Socket files in Linux?

On your terminal, simply type the stat command followed by the Socket file. See the results below.


# ls -la | grep ^d
drwxr-xr-x. 3 mageshm mageshm  4096 Dec 31 14:24 links/
drwxrwxr-x. 2 mageshm mageshm  4096 Nov 16 15:44 perl5/
drwxr-xr-x. 2 mageshm mageshm  4096 Nov 16 15:37 public_ftp/
drwxr-xr-x. 3 mageshm mageshm  4096 Nov 16 15:37 public_html/
9

How do I use the stat command to view named pipe files in Linux?

Simply type the stat command on your terminal followed by the named pipe file. See the results below.


# stat pipe-test 
 File: pipe-test
 Size: 0      Blocks: 0     IO Block: 4096  fifo
Device: 10301h/66305d  Inode: 1705583   Links: 1
Access: (0644/prw-r--r--) Uid: ( 1000/ daygeek)  Gid: ( 1000/ daygeek)
Access: 2019-01-06 02:00:03.040394731 +0530
Modify: 2019-01-06 02:00:03.040394731 +0530
Change: 2019-01-06 02:00:03.040394731 +0530
 Birth: -

conclusion


Related articles: