If you are new to Linux, finding files via “Command Line Interface” (CLI) is different from browsing them through a desktop file browser. Fortunately, there is a Linux command called ‘find’ that helps you find the files you are looking for. The command comes with different options you can use to easily find the files you are looking for.
The purpose of this blog post is to introduce new Linux users to the command find. Below is the default syntax of this command:
find [path] [expression]
Where path is the folder (directory) where the command will start looking for files (including all directories inside) and expression is the expression matching the files we have to find.
Examples:
1.) Finding files based on their file extensions.
You can use the command below if you want to search for all .jpg files in /root directory:
# find /root -name ‘*.jpg’
Use the command below if you want to search for all .php and .jpg files in /root directory:
# find /root -name ‘*.php’ -o -name ‘*.jpg’
If you are expecting a case sensitive results then you can use the -iname switch:
# find /home -iname ‘*.php’ -o -iname ‘*.jpg’
2.) Adding more search criteria:
The example commands we used above might display a lot of files on your screen, making it a bit hard for you to locate the files you are looking for. But that is not a problem, you can narrow down your search by adding more criteria like file size and file modification date.
Use the command below if you want to find .jpg files bigger than 3MB in /root directory:
# find /root/ -name ‘*.jpg’ -a -size +3M
You can still narrow down your search by adding the file modification date.Use the command below if you want to search for .jpg files bigger than 3MB and were modified less than 9 days ago in /root directory:
# find /root/ -name ‘*.jpg’ -a -size +3M -mtime -9
3.) Adding actions to the search results.
Now that you know the basic on how to find files using the find command, the next step is to add actions to the result of the find command that you used. Action can be done using the -exec switch.
Based on the example we used above, it is easy for us to find all .jpg files larger than 3MB and modified less than 9 days ago in the /root folder. You might want to move the results of the command to folder /home/dumbuser/ folder. You can use the command below to achieve this:
# find /home/ -name ‘*.jpg’ -a -size +3M -mtime -9 -exec mv ‘{}’ /home/dumbuser/ \;
Please take note of the ‘{}’ and \;
NOTE: there is a space before \;
The ‘{}’ matches the file that was found
The \; terminate the exec statement.

Popularity: 1% [?]