Find a File in Linux using Command Line

Channel: Linux
Abstract: you use file type -empty. For example to find all empty files in /tmp directoryfind -O3 -L /etc -name "*.conf" The option -03 (options) tells to perfo

Linux comes with a powerful tool named find to find a file (or files). By default, it searches into subdirectories for a file (or files) based on certain conditions.

In this tutorial, we learn how to find a file in Linux using the command line.

Find a File in Linux

The syntax of find command to find a file in Linux:

How to unzip zip files in Linux

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

How to unzip zip files in Linux
find starting-point options expression

starting-point -This can be the current directory (indicate by .) or directory path where to begin filtering. If no paths are given, the current directory is used.

options - This controls the treatment of symbolic links, debug options, and performance optimization.

expression - what is to be searched for.

For example:

find -O3 -L /etc -name "*.conf"
  • The option -03 (options) tells to perform an efficient search to give better performance.
  • The -L (options) tells find command to follow symbolic links
  • The /etc (starting-point) specifies the directory from where to start seaching
  • The -name "*.conf" (expression) tells find command what to search for.
Find a file in Linux by name

You can use find command to find a file by name. For example to search the file named file1.txt in the current directory and subdirectories.

find . -name file1.txt

Here . (dot) indicates the current directory.

To search the file named file2.txt in the directory path /home/bob and its subdirectories.

find /home/bob -name file2.txt

To perform a case-insensitive search, use -iname option instead of -name:

find /home/bob -iname File2.txt
Find files in Linux by extension

Use find command to find a file (or files) using file extensions. For example to search file (or files) with an extension ".txt", type:

find /home/bob -name "*.txt"
Find an empty file

To find empty file or files, you use file type -empty.

For example to find all empty files in /tmp directory, type:

find /tmp -type f -empty
Find a File by modification time

To search file or files under a directory by the last modified, use -mtime option. For example to find text files that were last modified 60 days ago, type:

find /home/username -name "*.txt" -mtime -60
Find File containing specific string

If you want to find the file containing a specific string you can combine find and grep together.

To search all *.js files containing the string 'example', type:

find . -name '*.js' -exec grep -i 'example' {} \; -print

Alternatively, you can achieve the same results by the grep command as well.

Find file and execute commands

On occasion you need to find the file and execute a process such as deletion, change permission, etc, you can use -exec option.

find /var/www/html -name "wp-config.php" -exec chmod 0644 {} \;
Useful Find options to find file

By default, find ignore symbolic links, use -L to follow symbolic links.

-maxdepth X - Search the current directory along with all sub-directories X levels deep.
-iname - Ignoring text case.
-not - Outputs only results that don’t match the test case

Conclusion

In this tutorial, we learned to find a file in Linux using the command line. Linux structure makes to efficiently find the file by searching through directories and subdirectories.

Thanks for reading, please let us know your feedback and suggestion in the comment section.

Ref From: linoxide
Channels:

Related articles