Skip to Content

4 Ways to check file type in Linux

In Linux, there are a few different ways that you can check the type of a file. This is helpful if you want to determine what program to use to open the file, or if you just want to know more about the file itself.

In this blog post, we will discuss four different methods that you can use to check file type in Linux. We will also provide instructions on how to easily identify a file’s format!

 

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

 

understanding file type in Linux

Most filesystem implementations define seven types of files. Even when developers add something new and wonderful to the file tree (such as the process information under /proc), it must still be made to look like one of these seven types:

  • Regular files
  • Directories
  • Character device files
  • Block device files
  • Local domain sockets
  • Named pipes (FIFOs)
  • Symbolic links

 

The most common file types are regular files and directories.

Regular files
Regular files consist of a series of bytes; filesystems impose no structure on their contents. Text files, data files, executable programs, and shared libraries are all stored as regular files. Both sequential access and random access are allowed.

You can use touch, cat or echo commands to create files in Linux.

Directories
A directory contains named references to other files. You can create directories with mkdir and delete them with rmdir if they are empty. You can recursively delete nonempty directories—including all their contents—with rm -r.

The special entries “.” and “..” refer to the directory itself and to its parent directory; they cannot be removed. Since the root directory has no real parent directory, the path “/..” is equivalent to the path “/.” .

Procedure to check file type in Linux

  1. Open the terminal and navigate to the directory where the file is located.
  2. Type in the command stat -c “%F” filename to check the file type.
  3. press enter to run the command
  4. The file type will be listed on the command output

 

Check file type with stat command in Linux

The easiest way to check file type in Linux is using stat command. To use this method, you will need to open up a terminal window and change into the directory where the file is located.

Once you are in the correct directory, you will need to type the following command:

stat filename

The output of stat command in Linux can be a little confusing if you are not familiar with it. Here is a breakdown of what each column in the output means:

File: test
Size: 9      Blocks: 8     IO Block: 4096 regular file
Device: fd06h/64774d Inode: 6291591   Links: 1
Access: (0600/-rw-------) Uid: (50290/ocp)  Gid: (50290/ocp)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2022-09-15 13:38:55.788502818 +0000
Modify: 2022-09-15 13:38:55.788502818 +0000
Change: 2022-09-15 13:38:55.788502818 +0000
Birth: -

 

  • Access: This column displays the permissions for the file.
  • Inode: This is the unique identifier for the file.
  • Device: This column displays the device name of the file system where the file is located.
  • Size: This column displays the size of the file in bytes.
  • Blocks: This column displays the number of blocks that are allocated to the file.

 

From this information, you can see that the Type column displays the type of file, which in this case is a regular text file. You can also see that the Size column displays the size of the text file, which is 9 bytes.

The stat command with -c option allows you to use a particular or custom format instead of the default, it prints a newline after each use of the format sequence.

stat -c "%F" filename

If you want more information about the stat command and what it can do, you can type the following command: man stat. This will open up the man page for stat, which will provide you with more information about this command.

Get file type with file command in Linux

To check file type in Linux, we can use file command. It can be used to check the type of a text file, an image file, or any other type of file.

To use this method, you will need to open up a terminal window and change into the directory where the file is located.

Once you are in the correct directory, you will need to type “file” followed by the name of the file that you want to check.

For example, if we wanted to check the type of the file “readme.txt”, we would type “file readme.txt”.

In the example above, we are checking the file type of a text file.

The file command in Linux is used to identify the type of a file. It can also be used to determine whether a file is a binary or text file.

If you want more information about the file command and what it can do, you can type “man file” to open up the man page for this command.

List file type with ls command in Linux

To get the file type in Linux, we can also use ls command with -l option. For example, if we wanted to check the type of the file “readme.txt”, we would type “ls -l readme.txt”.

This command will give you useful information about the file, including the type.

The -l ( lowercase L) option tells ls to print files in a long listing format.

When the long listing format is used, you can see the following file information:

  • The file type.
  • The file permissions.
  • Number of hard links to the file.
  • File owner.
  • File group.
  • File size.
  • Date and Time.
  • File name.

 

-rw-r--r-- 1 root root 337 Oct 4 11:31 readme.txt

Let’s explain the most important columns of the output.

The first character shows the file type. In this example, the first character is -, which indicates a regular file. Values for other file types are as follows:

  • – – Regular file.
  • b – Block special file.
  • c – Character special file.
  • d – Directory.
  • l – Symbolic link.
  • n – Network file.
  • p – FIFO.
  • s – Socket.

 

As you can see, a – character indicates that the file is a regular file, while a d character indicates that the file is a directory.

If you want more information about the ls command and what it can do, you can type the following command: man ls

Search file type with find command in Linux

To search for files with a specific file type in Linux, we can use using the find command with -type option.

For example, if we wanted to find all of the files and names with txt in the current directory, we would type

find . -type f -name "*.txt"

This will search through all of the files in the current directory and return any files that have a .txt extension.

You can also use the -type option to find other types of files, such as directories (-type d) or symbolic links (-type l).

There are many other methods that can be used to check file type in Linux.

However, these are four of the most common methods. If you are having difficulty identifying a file’s format, these methods should help you out.

 

Do you have any other tips for checking file type in Linux? Let us know in the comments below! And don’t forget to check out our other Linux tutorials! Thanks for reading! =)

KR

Thursday 7th of December 2023

I like to use the "file" command. It gives a bit more detail about what's in the file, which can be helpful if the extension is wrong or missing. But really, it's pretty easy to upload a file to a file-checking website these days.