How to Find a File in Linux: Simple and Easy
Finding a file in Linux can be a little confusing if you’re used to the Windows interface. However, you’ll find that Linux offers several efficient search commands with which you can find exactly the file you’d been looking for. In many ways, Linux file finding commands are even better than anything that Windows has to offer.
In this tutorial we’ll take a look at some of the commands with which you can quickly find the file you’ve been looking for. There are two main ways to find files in Linux: using the find command and the locate command. Let’s take a look at the find command first.
Finding a File by its Name
You can find a file if you know its exact name. The syntax to find a file by its name is:
find –name “name_of_file”
Here, the –name keyword tells Linux to look for files by its name. The name of the file should be specified without the quotes. For example, if you wanted to search for a file called players.txt, simply type:
find –name players.txt
Remember that this command is case sensitive. This means that if you were looking for PLAYERS.txt and searched for players.txt, you won’t be able to find it.
You can ask Linux to ignore the letter case of the name of your file by typing the following command:
find –iname “name_of_file”
Now you can search for players.txt even by typing:
find –iname PLAYERS.txt
If you know your file is located in a certain directory, you can navigate to that directory and then find the file to speed up the search. The command to search for a file in the directory you’re currently in varies very slightly (notice the dot):
find . –name “name_of_file”
You can search for files that don’t match a certain pattern – that is, you will be given a list of all files that don’t match what you’ve typed. The syntax for this is:
find –not –name “name_of_file_you_don’t_want”
Another command that achieves the same thing is as follows:
find \! –name “name_of_file_you_don’t_want”
If you know the partial name of the file, you can still search for it. This command is also useful if you need to find multiple files that begin with the same name:
find –iname “file\*”
For example, if you wanted to find 3 files named players1.txt, players2.txt and players3.txt, type the following command:
find –iname “play\*”
Remember, you can always search the directory you’re currently in by putting a dot (.) after find.
If you want to find a file located in a specific directory, you can use the following command:
find /directory_name –name “name_of_file”
If you wanted to search for the players.txt file that was located in your home directory, type the following:
find /home –name players.txt
Finding a File by its Type
There are many types of files in Linux, like regular files, character devices, directories, symbolic links, and block devices. You can search for files by their specific type:
find –type type_of_file
If you wanted to search for all regular files on your pc, type the following command:
find / -type f
The f instructs Linux to search for regular files. You should get a long list of regular files after your system finishes processing the command. The short forms for other common file types include c (character devices), d (directories), b (block devices) and l (symbolic link).
If you wanted to find all .txt files located on your system, you can type the following command:
find / -type f –name “*.txt”
This command is something of a combination of the commands we gave earlier.
Finding a File by Time Modified, Accessed and Changed
Whenever a file is changed, its modification time is saved in Linux. You can find a file that you modified two days ago by typing the following command:
find / -mtime 2
You can find a file that you modified less than thirty minutes ago by typing the following command:
find / -mmin -30
Linux also saves the time when you last accessed a particular file. If you accessed the file you’re looking for 3 days ago, type in the following command:
find / -atime 3
You can also search for files by when their meta information (descriptive data) was last modified 3 days ago with the following command:
find /-ctime +3
If you want to find a file that you modified just before a certain file, you can use the following command:
find / -newer name_of_file
For example, if you modified players2.txt just before players1.txt, you can find players2.txt with the following command:
find / -new players1.txt
Finding a File by Owners
You can find files by their owners. For example, if “john” is a user that owns the players.txt file, type in the following command to search for players.txt (along with other files he owns):
find / -user john
If there is a group that owns a set of files, you can search for those files if you know the group name. If the name of the group is team, you can find all files they own by:
find / -group name_of_file
The Locate Command
The locate command is faster at finding files than the find command, but it requires that your database that indexes your files is updated to find recent files. You may need to install the command through apt-get before you can first run it:
sudo apt-get update sudo apt-get install mlocate
The database that stores the file locations is auto updated once a day, so you can find all files that you updated yesterday with the locate command. You can also find all files that you created or updated recently if you manually update the database. For this, type:
Now you can finally use the locate command (it’s faster than find, really):
The other commands with which you can search for files in Linux are the whereis command and the which command. You will find that the find command is usually sufficient for most of your needs, however.
Top courses in Linux
Linux students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.