Skip to Content

4 ways to check if a file is a regular file or a directory in Linux

In Linux, there are four ways to check if a file is a regular file or a directory. In this blog post, we will discuss each of these methods in detail. The first method is the “file” command. The second method is the “ls” command with the “-l” flag. The third method is the “stat” command. The last method is the find command. Let’s get started!

what is file type in Linux?

 In Linux, everything is considered as a file. This is one of the major differences between Linux and other operating systems. Even a directory is considered as a file in Linux.

A file type helps us in identifying the type of content that is saved in the file. Linux supports seven different types of files. These file types are the Regular file, Directory file, Link file, Character special file, Block special file, Socket file, and Named pipe file.

check if a file is a regular file or a directory with file command in Linux

The “file” command is the most common way to check if a file is a regular file or a directory. To use the “file” command, you need to specify the path of the file that you want to check.

For example, if you want to check if the “/etc/passwd” file is a regular file or a directory, you would use the following command: file /etc/passwd

The output of the above command will be like this: /etc/passwd: ASCII text. so the “/etc/passwd” file is a regular file.

The “/etc/” is a directory, the output of the file /etc/ command will be like this: /etc/: directory

check if a file is a regular file or a directory with ls command in Linux

The second method to check if a file is a regular file or a directory is to use the ls command with the “-l” flag. The “-l” flag stands for “long listing”. If the first column of the command output starts with a “-“, this file is a regular file. If the first column of the command output starts with a “d”, this file is a directory.

For example, if you want to check if the “/etc/passwd” file is a regular file or a directory, you would use the following command:

ls -l /etc/passwd

The output of the above command will be like this:

-rw-r--r-- root root 1436 Nov 25 2017 /etc/passwd

The first column shows the file type. The second column indicates the file permissions. The third column shows the owner of the file. The fourth column shows the group owner of the file. The fifth column indicates the size of the file in bytes. The sixth column shows the date and time when the file was last modified. The seventh column indicates the path of the file.

As you can see from the output, the first column starts with a “-“. So /etc/passwd is a regular file.

The “/etc/” file is a directory, the output of the ls -l /etc/ command will be like this:

drwxr-xr-x root root 4096 Nov 25 2017 /etc/

As you can see from the output, if a file is a directory, the first column will start with a “d”.

check if a file is a regular file or a directory with stat command in Linux

The third method to check if a file is a regular file or a directory is to use the “stat” command. The “stat” command can be used to display detailed information about a file.

The “stat” command prints a lot of information about a file. In particular, it prints the device name, inode number, size of the file, block size, number of blocks used by the file, byte size of each block, filename, permissions, user id, group id, and timestamp.

To use the “stat” command, you need to specify the path of the file that you want to check. For example, if you want to check if the “/etc/passwd” file is a regular file or a directory, you would use the following command:

stat /etc/passwd

The output of the above command will be like this:

File: ‘/etc/passwd’
Size: 1436 Blocks: 24 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 2570257 Links: 11
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-11-25 21:21:53.000000000 -0500
Modify: 2017-11-25 21:21:53.000000000 -0500
Change: 2017-11-25 21:21:53.636059480 -0500
Birth: –

As you can see from the output, the “stat” command displays a lot of information about the file. /etc/passwd is a regular file.

The “/etc/” file is a directory, the output of the stat /etc/ command will be like this:

File: '/etc/'
Size: 4096 Blocks: 16 IO Block: 4096 directory
Device: fd01h/64769d Inode: 2570256 Links: 11
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-11-25 21:21:53.000000000 -0500
Modify: 2017-11-25 21:21:53.000000000 -0500
Change: 2017-11-25 21:21:53.636059480 -0500
Birth: -

we can also use -c option to format the output in stat command. This will give us the file type and name in one line which can be useful for scripts.

stat -c "%F %n" /etc/passwd
regular file /etc/passwd
stat -c "%F %n" /etc/
directory /etc/

check files with find command in Linux

The fourth and final method to check if a file is a regular file or a directory is to use the find command. The “find” command can be used to search for files in a directory hierarchy.

To use the “find” command, you need to specify the path of the directory that you want to search. For example, if you want to search the “/etc” directory for regular files, you would use the following command:

find /etc -type f

If the “/etc” directory contains regular files, the output of the above command will be a list of all the regular files in the “/etc” directory.

If the “/etc” directory does not contain any regular files, the output of the above command will be an empty list.

As you can see, there are four different ways to check if a file is a regular file or a directory in Linux. Each method has its own advantages and disadvantages. Choose the method that best suits your needs. Thanks for reading! I hope this has been helpful.