Linux Commands

How to Find a File in Linux from the Command Line

Finding files in Linux is difficult if you are using the command line interface. The easiest and simplest way of finding the file in Linux is via the find command. The Linux find command filters the files using a conditional mechanism. This article covers all the information for finding the files in Linux.

How to Find a File in Linux from the Command Line

There is one easiest and simplest way to find a file on Linux through the command line and that is by using the find command and here are some ways to find a file using the find command:

1: Finding a File through Name

find is a utility in Linux, for finding the files and directories. You can find the files using the various parameters including name, type, and date. Use the following command syntax for searching a file with a specific name:

sudo find -name <name-of-file>

For illustration I have created a simple test file and use the find command to get its address:

sudo find -name testfile.txt

2: Finding a File through Format

To find all the files through their formats then use the below given syntax:

find <directory-path> -name "*.<format>"

For illustration I have searched for all the text file in the home directory by using the above-mentioned syntax:

find /home/zainab/ -name "*.txt"

You can also search for bash script files as well by just replacing the format from .txt to .sh

3: Finding a File through Data

To look for the files that are empty in any directory use this command:

find . -type f -empty

4: Finding a File through Modification Time

The find command can also filter files based on the time they were last modified so here is the syntax for the command that can be used to find a file based on its last modification:

find / -name "*conf" -mtime <number-of-days>

For illustration I have searched for all the files that are modified less than 7 days so in the syntax I have replaced number of days with “-7”:

sudo find / -name "*conf" -mtime -7

5: Finding a File through Size:

Another way to find a file using this find command is by setting a filter of file size and for that here is the syntax for it:

find <path> -type f -size <size-of-file>

For illustration, I have set the filter of 5 MB in the above syntax and the command below gives the files whose size is either equal to 5 MB or are less than it:

find /home -type f -size -5M

6: Finding the hidden Files

Following command searches for the hidden files in the specific directory:

find . -type f -name ".*"

The dot in the above command signifies the current directory.

Conclusion

While it might seem difficult to locate a file in Linux at first but learning how to find a file in Linux will make it easier. The find command is the easiest way of finding the files within the command line. The find command efficiently searches for the files and directories.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.