Part of mastering the Linux file system is knowing how to search and find files in Linux. The find command in Linux is an excellent way of quickly searching for files. You can use it with numerous options to optimize your search for better results.
This article explores the Linux find command more. We will start by checking the syntax for the find command in Linux and then cover examples of how you can use the find command.
Syntax for the Find Command in Linux
The find command is a handy choice for searching for files and directories in Linux. It follows a hierarchical structure, which you can use with various options to customize your search.
Below is the syntax for the Linux find command
$ find [path] [option] [expression]
From the above syntax, the path specifies the location where you want to search. The options are how you want to customize the search, and the expression is an optional criterion for filtering the search.
10 Practical Examples of the Linux Find Command
The find command in Linux has different options. You can access them by checking the find command help page. Besides, we’ve given a few examples to get you started on how to use the command.
Example 1: Find File in Current Directory
For instance, if you want to search for a particular file in the current directory quickly. You would execute the find command using the below syntax.
$ find . -name <file_name>
The period (.) is the current directory, and our search file is “names.txt.” The -name flag lets you specify the search term. You will get an output showing the path to the found file, if any.
Example 2: Find File By Specifying the Search Path
The previous example searched for the file in the current directory. However, you can specify the search path for the file.
$ find [path] -name [file_name]
We’ve added the path to our home directory for this example, and our search file is “file3.”
Example 3: Ignoring Cases with the Find Command in Linux
The default behavior of the find command is that it is case-sensitive. However, if you are still determining the cases for your file or directory or want to get all matching results irrespective of their cases, adding the -i option does the trick.
Here’s an example.
Example 4: Finding Based on a Pattern
It’s possible to define a pattern that acts as the condition for your search. For instance, you can set the pattern to be the file extension.
The example below returns all files with the “.txt” extension in their name.
$ find [path] -name *.txt”
Suppose you don’t want to focus your search on a specific file type and instead want to search for all files in a given path. Using the -type with the -f flag will only display files and exclude directories.
You can also do the same for searching for directories. The type to use will be the d flag, and your search results will now display directories in the specified path. Using this option with the find command is similar to displaying the hierarchy of a target path.
$ find [path] -type d
Example 5: Find Empty Files
The find command has the -empty flag that searches for all the empty files and directories, as in the example below.
To search for empty files, excluding the directories, combine the -empty with the -type option.
Example 6: Finding Files Based on Permission
Sometimes, you may wish to find files based on their permissions. The -perm option lets you specify the target file permission and will return all the matching files in the search path.
$ find [path] -perm [permission]
Example 7: Search Text within Files
The find command in Linux also helps with searching for text within files. It will check all files in the search path and return all lines containing the search text.
In this case, you must combine it with the Linux grep command with the below syntax.
$ find [path] -type f -exec grep [search_text] {} \;
Example 8: Find Files Based on Time
To search for files based on the last edit time, use the -mtime and specify the modification time. The example below finds all files whose modification has been within the previous two days.
Example 9: Find and Delete Files Using the Find Command in Linux
The Linux find command can also help you interactively find and delete a file. Combine the find command with the rm command, and you will get a prompt requesting that you confirm the deletion.
$ find [path] -type f -name [file_name] -exec rm -i {} \;
You will get an output showing the previous location of the deleted file.
We can run the ls command to verify that the file has been deleted.
Example 10: Find Hidden Files
Our last example demonstrates how the find command can be used to search for hidden files. For this example, our search path is the current directory, and our command is as follows.
$ find [path] -type f -name “.*”
Conclusion
The find command in Linux can be used in numerous ways to search for files in the specified path. Although we’ve only given 10 find command examples, there are multiple ways of tweaking the command to better your find query. The examples presented help you understand the find command and give you an easy time mastering it.