Imagine you have recently joined a new project as a software developer. You get an email from your manager telling you that the database keeps crashing and asking you to check the logs. They have just mentioned the name of the log file you need to check, db.log, but not the path where it is located. The project contains hundreds of directories and thousands of files. How do you find db.log in this huge code base? Of course by using the find command! And that's what we are going to explore in this topic.
The find command
The find command line utility is one of the most useful tools you will use daily in your work with Linux. It can search through hundreds of files and directories in seconds to locate the file you want.
find can be used to locate both files and directories (since directories are also a type of file in Linux). This command provides you with a lot of options with which you can search for files. But before we look at these options, let's first familiarize ourselves with the basic syntax of the find command.
Basic syntax
find [path] [options]find allows you to specify the folder in which you want to search for the file. The path to this folder is given as an argument to the find command. If you don't specify a path, the command will do the search in the current directory and its sub-directories. [options] allow you to specify various filter conditions while searching for the file.
Let's learn the different ways in which find can be used through examples.
find ./projectThis is a basic find command without any options. The only argument we've given to the command is the path to the folder where to search. This command will print out all the files and directories in the ./project folder and its sub-folders.
The real power of find comes from its options. Let's go back to the log file your manager asked you to check. Since you know the name of the log file, you can use the -name option to find it in the ./project directory.
find ./project -name db.log-name option can match globbing patterns as well. This is especially useful when getting all the files with a particular extension. The following example shows how to get all the .log files from the ./project directory:
find ./project -name "*.log"Other useful options
Some other useful options used with -find are:
-type to specify if the thing we are looking for is a file or a directory;
-user to search for files belonging to a particular user;
-size to search for files by size.
find ./project -name "*log*" -type dThis command searches for all the sub-directories of ./project that contain the word "log" in them.
To search only for the files, excluding directories, use the -type f option.
To find photos owned by the user "anna", you can type:
find / -name "*.jpg" -user "anna"If you want to find all the files in the project directory which weigh more than 2GiB, you can use:
find ./project -size +2GGibibytes are units of 1 GiB = 1024 MiB = 2^30 bytes, not gigabytes, which are units of 1GB = 1000 Mb = 10^9 bytes.
The + sign indicates "greater than". Similarly, if you want to search for files smaller than 2GiB, you would use "-2G". For files that weigh precisely 2GiB you would use "2G" without any sign in front of it.
You can use the k, M and G suffixes with the -size flag to denote sizes in kibibytes, mebibytes, and gibibytes respectively.
To find files that have a size between two given sizes, use the -size flag twice to denote the maximum and minimum file size you want to search for.
# Finds all the files in the ./project directory which have
# a size greater than 10MB and less than 500MB
find ./project -size -500M -size +10M -type fIf you want to find all the empty files/sub-directories in a directory, use the -empty flag.
# This finds all the empty files and subdirectories in the /home directory.
find /home -emptyThere are a lot of other find options which we haven't covered here like time and permissions. You can get a detailed list of all the options which can be used with find by typing the man find command.
Conclusion
In this topic, we've explored the find command, one of the most useful and popular commands in Linux. You now know how to navigate through huge file systems and find the file you want in seconds if you know its name! We've also learned how to narrow down our search for a file using options when the exact name of the file is unknown but some of its other characteristics are known. Head on over to the tasks to see how well you've understood this tool.