Find Files and Directories

Importance of a Search

It can be crucial to be able to find the different files and folders in Linux efficiently. We do not have to manually browse through every single folder and check as there are multiple tools we can use to make this work easier.

Which

One of the common tools is which. This tool returns the path to the file or link that should be executed. This allows us to determine if specific programs, like curl, netcat, wget, python, gcc, are on the operating system. Below is an example for curl.

└─$ which curl
/usr/bin/curl

If the program that we search for does not exist, no results will be displayed.

Find

Another handy tool is find. Besides the function to find files and folders, this tool also contains the function to filter the results. We can use filter parameters like the size of the file or the date. We can also specify if we only search for files or folders.

$ find <location> <options>

Let us look at an example of what such a command with multiple options would look like.

$ find / -type f -name *.conf -user root -size +20k -newermt 2020-03-03 -exec ls -al {} \; 2>/dev/null
-rw-r--r-- 1 root root 34498 Dec 1 13:01 /usr/share/drirc.d/00-mesa-defaults.conf
-rw-r--r-- 1 root root 195027 Aug 23 2020 /usr/share/alsa/topology/hda-dsp/skl_hda_dsp_generic-tplg.conf
-rw-r--r-- 1 root root 66635 Aug 23 2020 /usr/share/alsa/topology/sklrt286/skl_i2s.conf
-rw-r--r-- 1 root root 74488 Aug 23 2020 /usr/share/alsa/topology/bxtrt298/bxt_i2s.conf
-rw-r--r-- 1 root root 30436 Jan 8 2021 /etc/cups/cups-browsed.conf

Now let us take a closer look at the options we used in the previous command. If we hover the mouse over the respective options, a small window will appear with an explanation. These explanations will also be found in other modules, which should help us if we are not yet familiar with one of the tools.

OptionDescription
-type fHere, we define the type of the searched object. In this case, ‘f‘ stands for ‘file‘.
-name *.confWith ‘-name‘, we indicate the name of the file we are looking for. The asterisk (*) stands for ‘all’ files with the ‘.conf‘ extension.
-user rootThis option filters all files whose owner is the root user.
-size +20kWe can then filter all the located files and specify that we only want to see the files that are larger than 20 KiB.
-newermt 2020-03-03With this option, we set the date. Only files newer than the specified date will be presented.
-exec ls -al {} \;This option executes the specified command, using the curly brackets as placeholders for each result. The backslash escapes the next character from being interpreted by the shell because otherwise, the semicolon would terminate the command and not reach the redirection.
2>/dev/nullThis is a STDERR redirection to the ‘null device‘, which we will come back to in the next section. This redirection ensures that no errors are displayed in the terminal. This redirection must not be an option of the ‘find’ command.

Locate

It will take much time to search through the whole system for our files and directories to perform many different searches. The command locate offers us a quicker way to search through the system. In contrast to the find command, locate works with a local database that contains all information about existing files and folders. We can update this database with the following command.

$ sudo updatedb

If we now search for all files with the “.conf” extension, you will find that this search produces results much faster than using find.

$ locate *.conf
/etc/GeoIP.conf
/etc/NetworkManager/NetworkManager.conf
/etc/UPower/UPower.conf
/etc/adduser.conf
<SNIP>

However, this tool does not have as many filter options that we can use. So it is always worth considering whether we can use the locate command or instead use the find command. It always depends on what we are looking for.

Leave a Reply

Your email address will not be published. Required fields are marked *