People just starting out with Linux often claim that they get confused on where their files are located. This is because most migrate their files over from Windows, which has a more clear-cut directory layout. And while there is some truth to this, Linux gives users much more options on how to search for files compared to Windows. Once you get a hang of these commands, searching for files on your Linux platform will be extremely easy. In short, you will be able to execute commands and find files by name, format, size, modification time and much more.
In this tutorial, we will see how to utilize the file-searching capabilities of Linux. We will cover two separate commands for searching files: find and locate. Detailed usage and examples for both commands will be presented so that you can choose what’s best for you. Also, we are using Ubuntu VPS, but this will work for Debian, CentOS, or any other distribution of Linux you might be sporting.
Using the find Command in Linux
Let us begin with the find command, and how to use it to the maximum capability.
The Basic Syntax
The most common command used to find and filter your files on Linux is through the find command. The basic layout of this command is as follows:
find <startingdirectory> <options> <search term>
It begins with the keyword find, which alerts Linux that whatever follows is concerned with finding a file. The <startingdirectory> argument is the origin point of where you want to start the search. This is useful if you have a rough idea of where your desired file might be located, as it narrows down the search. Most of the time, however, you will want to search the whole file system for the files. You can do this by replacing your path with a slash `/`, which is the symbol for the root directory. Sometimes you might want to start the search from the current working directory i.e. the directory where the terminal is open. This can be done with the dot `.` argument. To find out your current directory, use the pwd command. Finally, to begin the file search from your home folder, use the tilde `~` symbol.
The second argument is the filter that you want to use to search your file. This could be the file’s name, type, date of creation or modification etc. The third argument is a follow-up to the second, where you will specify the relevant search term. Let’s take a look at the various options Linux provides users when looking for their files:
Searching by name
Of course, the most common and obvious method to look for a file is using its name. To run a simple search query using the name of the file, use the find command as follows:
find . -name “my-file”
In the above command, we used the -name option, and searched for a file called my-file. Note that we started the search in our current directory.
An important thing to remember when using the vanilla is -name argument, which looks for case-sensitive terms in Linux. Thus, if you know the name of the file, but are not sure about its case-sensitivity, use the find command like this:
find . -iname "my-file"
Another way of using the name option is searching for all files without a certain keyword in their names. In Linux, you can do this in two ways. The first method involves using the –not keyword in the following manner:
find . -not -name "my-file"
We can also use `!`, though it has to be preceded by the escape identifier to let Linux know that this is the part of the find command and not an independent.
find . \! -name "my-file"
You can look for multiple files with a common format like .txt as well, which could be useful in some instances:
find . -name “*.txt”
This will list down all the text files starting with the current folder.
Finally, if you want to find a certain file by name and remove it, use the -delete argument after the file name:
find . -name "my-file" -delete
Searching by type
For most users, just knowing how to find files by their names is enough. However, it is always useful to know all the tools on offer to take the full advantage of Linux.
This is where the -type argument comes into play. Linux gives users the following options when searching files by type:
- f – normal file
- d – directory or folder
- l – symbolic link
- c – character devices
- b – block devices
A simple example of using file type for searching can be seen below:
find / -type d
This will list all the directories present on your file system, as we started the search from our root directory with the slash symbol.
You can also chain together the -type and -name options to narrow down your searches further:
find / -type f -name "my-file"
This will look for files named my-file, excluding directories or links.
Searching by time
If you want to search for files based on its access time and modification time footprints, Linux has just the tools for you. There are 3 timestamps Linux keeps track of with respect to files:
- Access Time (-atime) – Most recent time the file was either read or written into.
- Modification Time (-mtime) – Most recent time the file was modified.
- Change Time (-ctime) – Most recent time the file’s meta-data was updated.
This option has to be used with a number. This number specifies the number of days since the file was accessed, modified or changed. The simplest way of searching files by time is:
find / -atime 1
This will find all files that were accessed a day ago from the current time. This means every file that was either read, written into, or both since a day ago will be listed.
We can make our queries more fine-grained by the plus (+) and minus (–) signs preceding the number of days. For instance:
find / -mtime +2
This will list down all the files that have a modification time of more than two days ago.
To find all files whose inode meta-data was updated less than a day ago, run the following:
find / -ctime -1
Finally, there are some additional arguments other than these 3 that are also related to timed-searches. The -<time-descriptor>min argument looks for how many minutes have passed. It can be used like this:
find / -mmin -1
This will look for files that were modified less than a minute ago. Also, the -newer argument can be used to compare the age of two files and find the newer one.
Searching by size
Just like Linux gives you the option to search for files based on timestamps, it also lets you do the same with sizes. The basic syntax for searching files by size is:
find <startingdirectory> -size <size-magnitude> <size-unit>
You can specify the following size units:
- c – bytes
- k – kilobytes
- M – megabytes
- G – gigabytes
- b – 512-byte chunks
A simple example of how to use the find command for file sizes is as follows:
find / -size 10M
This will search for files in your system that are exactly 10 Megabytes in size. Just like when searching based on time, you can further filter your searches through the plus and minus signs:
find / -size +5G
The above command will list down all the files on your drive that are more than 5 Gigabytes in size. You can similarly achieve this with the minus sign for denoting ‘less than’ in your queries.
Searching by ownership
The privilege hierarchy of Linux can also be utilized when searching for files. Linux gives you the ability to narrow down your searches based on file ownership, as well as permissions given to different users.
To find files of a certain owner, the following command should be executed:
find / -user dummy
This will return a list of all files for which the user named dummy owns. Similar to usernames, we can also search files through group names:
find / -group classroom
This will look for those files that are owned by the group named classroom.
Searching by permissions
Users who want to search for files based on file permissions can do so using the -perm option of the find command. For instance:
find / -perm 644
In Linux, 644 corresponds to read and write permission. Which means that this command will search for all the files that have only read and write permissions. You can play with this option some more, like this:
find / -perm -644
This will return all the files that have at least 644 permission.
To read more on permissions, and the various codes corresponding to other Linux permissions, visit this web page.
Other Useful Options
In addition to all these methods of searching for files, there are other useful options that one should remember.
For example, to look for empty files and folders on your system, use the following:
find / -empty
Similarly, to look for all the executables saved on your drive, utilize the -exec option:
find / -exec
To look for readable files, you can run the following command:
find / -read
And that is all as far as find goes. As you can see, there is a ton of options at hand so that users can tailor their queries perfectly according to their needs. Let us now look at the other command which can be used instead of find to search for files in Linux.
Using the locate Command in Linux
At this point, you might question the need for an alternative to the find command. After all, it does everything needed for searching files. However, the locate is a useful alternative, as it is faster than find when searching system-wide.
By default, Linux does not come with the locate command pre-installed. To get the package, run the following commands on your terminal:
sudo apt-get update
sudo apt-get install mlocate
Now that you’ve acquired locate, you can use it to search for files like this:
You can use the -b argument to narrow down your search. It will only look for the file’s ‘basename’ – effectively listing only those files that have the search term rather than returning directories that lead to the files.
locate -b my-file
Other available arguments include:
- -e returns entries of existing files at the time the locate command is executed.
- -q disables the display of errors encountered the finding process.
- -c shows the number of matching files, instead of the file names
The vanilla locate command can sometimes return files that have been deleted. This is because the locate command searches the main Linux OS database. If that database isn’t updated, even deleted files may appear in your search results. You can manually update the database by running the following:
In this tutorial, we have learned how to:
- Use find to search for files based on name, type, time, size, ownership and permissions, in addition to some other useful options
- Install and use locate to perform faster system-wide searches for files
Linux users are given two extremely powerful tools to traverse through your drive and look for files. It entirely depends on the user’s individual need which method is more suitable. We recommend giving both a go to see which one is right for you.
If you have any cool tips or ideas that you wish to share, feel free to do so in the comments!