How to Identify File Types in Linux

As we all know that everything is a file in Linux, which includes Hard Disk, Graphics Card, etc.

When you are navigating to the Linux file system most of the files are fall under regular files and directories.

But it has other file types as well for different purpose. It has five types as per my knowledge.

So, it’s very important to understand the file types in Linux, that helps you in many ways.

In Linux, a file can use an extension, but the actual purpose may be different. Linux does not use file extensions, instead the file type is part of the file name.

If you can’t believe this, check out the full article to know how important it is.

To understand the color coding of Linux files go to the article below.

Make a note: Take a backup whenever you make any changes to the configuration file, as if you made the changes incorrectly, it could seriously damage your system, so be careful when you do this.

What Type of Files are Available in Linux

As per my knowledge, totally 7 types of files are available in Linux with 3 Major categories. The details are below.

  • Regular File
  • Directory File
  • Special Files (There are five types of files in the special category)
  • Link File
  • Character Device File
  • Socket File
  • Named Pipe File
  • Block File

Refer the below table for a better understanding of file types and their symbols in Linux.

+--------------+------------------------+
|    Symbol    |      File Types        |
+--------------+------------------------+
|      -       | Regular File           |
|      d       | Directory              |
|      l       | Link File              |
|      c       | Character Device File  |
|      s       | Local Socket File      |
|      p       | Named Pipe File        |
|      b       | Block Device File      |
+--------------+------------------------+

Method-1: How to Identify File types in Linux Using the ls Command

The ls command helps you to identify and classify all kind of the file types found on a Linux system.

Regular file

The regular file is a common file type found everywhere on Linux system. These include text files, script files, images, binary files, and shared libraries,etc.

“-” This refers to the identification symbol for the regular file. You can use the rm command to remove a regular file.

# 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

Directory

The directory is the second most common file type found on a Linux system. This can be created with the mkdir command.

# 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/

Link File

A link is a mechanism for creating a shortcut to the original file or directory. It contains information about another file or directory.

Links allow more than one filename to reference the same file.

There are two types of link files available, it’s soft link and hard link.

# 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

Character Device file

Character device files allow the user and application program to communicate directly with the hardware device. It’s not allow programs to read or write single characters at a time.

It is available under the /dev directory.

# 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

Block Device file

Block devices provide buffered access to hardware devices, it’s similar to character devices. Unlike character devices, block devices will always allow the programmer to read or write a block of any size at a time.

# 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

Socket file

A socket is a special file used for inter-process communication, which enables communication between two processes.

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

Named Pipe file (FIFO)

a named pipe (also known as a FIFO) is one of the methods for inter-process communication.

Named pipes are special files that can exist anywhere in the file system. They can be created with the command mkfifo.

A named pipe is marked with a p as the first letter of the mode string.

# 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 to Identify File types in Linux Using the file Command

The file command allows you to determine various file types in Linux. There are three sets of tests will be performed against a file in the below order to identify the file types.

  • Filesystem tests: Filesystem tests checks to see if the file is empty, or if it’s a special file. If the file type is found in the system header file, it is displayed as the correct file type.
  • Magic Tests: Magic tests check the magic number stored in a specific location near the beginning of the file to determine the file type. Magic tests use the following files to match a file to its file type. The magic files are /usr/share/misc/magic.mgc, /usr/share/misc/magic and /etc/magic. If a file does not match any of the entries in the magic file, it is examined to see if it is a text file. If the file character set is reported like ASCII, ISO-8859-x, UTF-8, and extended-ASCII are identified as a ‘text’ file. UTF-16 and EBCDIC are text files, but translation will be required before reading them.
  • Language Tests: Once the file type is determined as a text file. Language tests look for specific strings to determine in what language the file is written. It will appear anywhere in the first few blocks of a file. These tests are less reliable than the previous two tests, so they are performed last.

Regular file

Enter the file command on your terminal, followed by the regular file. The file command reads the contents of the given file and shows what kind of file it is.

That’s why you see below the different results for each regular file.

# 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

Directory

# file Pictures/
Pictures/: directory

Link File

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

Character Device file

# file vcsu
vcsu: character special (7/64)

Block Device file

# file sda1
sda1: block special (8/1)

Socket file

# file system_bus_socket 
system_bus_socket: socket

Named Pipe file

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

Method-3: How to Identify File types in Linux Using the stat Command

The stat command allow us to check file types or file system status. This utility giving more information than file command.

It shows lot of information about the given file such as Size, Block Size, IO Block Size, Inode Value, Links, File permission, UID, GID, File Access, Modify and Change time details.

Regular files

# stat 2daygeek_access.log
  File: 2daygeek_access.log
  Size: 14406929  	Blocks: 28144      IO Block: 4096   regular file
Device: 10301h/66305d	Inode: 1727555     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/ daygeek)   Gid: ( 1000/ daygeek)
Access: 2019-01-03 14:05:26.430328867 +0530
Modify: 2019-01-03 14:05:26.460328868 +0530
Change: 2019-01-03 14:05:26.460328868 +0530
 Birth: -

Directory

# stat Pictures/
  File: Pictures/
  Size: 4096      	Blocks: 8          IO Block: 4096   directory
Device: 10301h/66305d	Inode: 1703982     Links: 3
Access: (0755/drwxr-xr-x)  Uid: ( 1000/ daygeek)   Gid: ( 1000/ daygeek)
Access: 2018-11-24 03:22:11.090000828 +0530
Modify: 2019-01-05 18:27:01.546958817 +0530
Change: 2019-01-05 18:27:01.546958817 +0530
 Birth: -

Link file

# stat /dev/log
  File: /dev/log -> /run/systemd/journal/dev-log
  Size: 28        	Blocks: 0          IO Block: 4096   symbolic link
Device: 6h/6d	Inode: 278         Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-01-05 16:36:31.033333447 +0530
Modify: 2019-01-05 16:36:30.766666768 +0530
Change: 2019-01-05 16:36:30.766666768 +0530
 Birth: -

Character Device file

# stat /dev/vcsu
  File: /dev/vcsu
  Size: 0         	Blocks: 0          IO Block: 4096   character special file
Device: 6h/6d	Inode: 16          Links: 1     Device type: 7,40
Access: (0660/crw-rw----)  Uid: (    0/    root)   Gid: (    5/     tty)
Access: 2019-01-05 16:36:31.056666781 +0530
Modify: 2019-01-05 16:36:31.056666781 +0530
Change: 2019-01-05 16:36:31.056666781 +0530
 Birth: -

Block Device file

# 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: -

Socket file

# stat /var/run/dbus/system_bus_socket 
  File: /var/run/dbus/system_bus_socket
  Size: 0         	Blocks: 0          IO Block: 4096   socket
Device: 15h/21d	Inode: 576         Links: 1
Access: (0666/srw-rw-rw-)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-01-05 16:36:31.823333482 +0530
Modify: 2019-01-05 16:36:31.810000149 +0530
Change: 2019-01-05 16:36:31.810000149 +0530
 Birth: -

Named Pipe file

# 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: -

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...