Beginner’s Guide: How to Use the Find Command in Linux

HomeLinuxBeginner's Guide: How to Use the Find Command in Linux

Beginner’s Guide: How to Use the Find Command in Linux

Want to be a Linux master? One of the basic commands you need to learn is find. Just like the name suggests, this command is used to perform a find over the files or folders. You will so much need this command when you are working with a Linux that has no desktop environment installed, typically server machines.

find is the standard search tool in Linux. You don’t need to install it anymore as it’s installed by default.

findworks on the current directory. When you run this command without adding any path, it will only scan all the files and directories under the current directory where you run it. For instance, if you run find *.JPG under the “Pictures” directory, findwon’t display .JPG files outside of the “Pictures” directory. Instead, it will only displays all the .JPG files under the “Pictures” directory.

Same as other commands in Linux, find also has a bunch of options and parameters you can use to get the most out it. To learn more about this command, you can read the manual page by typing find manon the terminal. But for now, let’s take a look at the examples below on how to use this useful command.

Find files by name

Probably, the most common use of findis to search for files by name. However, you need ensure you really remember the name of the files you want to search for. For instance, if you want to search for a file named “slack.png”, then you need to type the following command on the terminal.

find -name slack.png

The option of -nameis case-sensitive. Which means, it will only displays the files with the name like exactly what you typed. If you want to find a file by name that contains both capital and small letters, you can replace the option of -namewith -iname.

If you are not sure the directory name of the file you are looking for under, you can widened the search by adding a path. For instance, the command below will scan all of the files under the home directory.

find /home -name slack.png

What if you don’t remember the name of the file?

It’s complicated enough, but you can at least recall the extension of the file. Once you did, you can use the wildcard to search for the file. For instance, if you want to search for a text file with the extension of .txt, you can type the following command.

find /home -name *.txt

Find empty files

To search for empty files, you can type the following command.

find -type f -empty

Find directories

To find directories, you can use the typeoption followed the parameter of d. For instance, if you type the following command, all of the displayed results are directories instead of files.

find -type d

Same as file, you can also use the option of nameif you remember the name of the folder you want to search. The example below is used to search for a folder with the name of “logos”.

find -type d -name logos

Find files by date and time

You can narrow the search results to show only files modified or accessed during certain period. There are three useful options you can use to do so: mtime, atimeand cmin.

mtime is used to display the last modified files during certain period, while atimeis used to display files last accessed files during certain period. Here are the examples to use this options.

  • Find files last modified less than 60 days ago.
find -mtime -60
  • Find files last modified more than 60 days.
find -mtime +60
  • Find files last accessed less than 60 days ago.
find -atime -60
  • Find files last accessed more than 60 days.
find -atime +60
  • Find files that are modified more than thirty days earlier and less than fifty days after.
find -mtime +30 -mtime -50
  • Find files that are changed in the last two hours.
find -cmin -120

Find files by size

To find files by size, you can use the option of sizefollowed the size of files you are searching for. Following are the examples of the uses of this option.

  • Find files with the sizes less than 4MB
find * -size -4M
  • Find files with the sizes more than 4MB
find * -size +4M

Conclusion

The example uses above are basic uses of find. You can do more things with this command based on your own cases. As I said, you can read the manual page of findby typing the find manon the terminal.

Note

If you want to find files under the root directory, be sure to type sudobefore the findcommand.

hand-picked weekly content in your inbox

.

related posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here