How to find files and directories in Linux
In Linux operating systems, the find command can search for files and directories on your computer. To proceed, select a link from the following list or go through each section in order.
To use find, begin by opening a terminal session to access the command line.
Basic functionality of find
Running find without any options produces a list of every file and directory in and beneath the working directory. For instance, if your working directory is /home/hope/Documents, running find outputs the following:
- Every file in /home/hope/Documents.
- Every subdirectory in /home/hope/Documents.
- Every file in each of those subdirectories.
Let's see it in action. First, let's check our working directory using the pwd command:
pwd
/home/hope/Documents
Now let's run find without any options:
find
. ./images ./images/hp ./images/hp/snape.jpg ./images/hp/harry.jpg ./images/memes ./images/memes/winteriscoming.jpg ./images/memes/goodguygary.JPG ./images/memes/picard.jpg ./gimp-2.8.16.tar.bz2 ./hp-fanfic ./hp-fanfic/malfoys-revenge.doc ./hp-fanfic/weekend-at-hagreds.doc ./hp-fanfic/dumbledores-lament.doc ./archlinux-2016.02.01-dual.iso
In this example, we see a total of ten files and four subdirectories in and beneath our Documents folder.
Notice that the output starts with a single dot, which represents the working directory. Running find with no options is the same as specifying that the search should begin in the working directory, like this:
find .
The example above is the "proper" way to use find. If you try to use it on another Unix-like operating system, such as FreeBSD, specifying a directory is required. It's good practice to use this form of the command.
Specifying where to search
To only list files and subdirectories that are contained in the directory /home/hope/Documents/images, specify the first argument of the command as:
find /home/hope/Documents/images
/home/hope/Documents/images /home/hope/Documents/images/hp /home/hope/Documents/images/hp/snape.jpg /home/hope/Documents/images/hp/harry.jpg /home/hope/Documents/images/memes /home/hope/Documents/images/memes/winteriscoming.jpg /home/hope/Documents/images/memes/goodguygary.JPG /home/hope/Documents/images/memes/picard.jpg
Notice that the full path is also shown in the results.
If our working directory is /home/hope/Documents, we can use the following command, which finds the same files:
find ./images
But this time, the output reflects the starting location of the search and looks like this:
./images ./images/hp ./images/hp/snape.jpg ./images/hp/harry.jpg ./images/memes ./images/memes/winteriscoming.jpg ./images/memes/goodguygary.JPG ./images/memes/picard.jpg
By default, the search looks in every subdirectory of your starting location. To restrict how many levels of subdirectory to search, you can use the -maxdepth option with a number.
For instance, specifying -maxdepth 1 searches only in the directory where the search begins. If any subdirectories are found, they are listed, but not searched.
find . -maxdepth 1
. ./images ./bigfiles.txt ./gimp-2.8.16.tar.bz2 ./hp-fanfic ./archlinux-2016.02.01-dual.iso
Specifying -maxdepth 2 searches the directory and one subdirectory deep:
find . -maxdepth 2
. ./images ./images/hp ./images/memes ./gimp-2.8.16.tar.bz2 ./hp-fanfic ./hp-fanfic/malfoys-revenge.doc ./hp-fanfic/weekend-at-hagreds.doc ./hp-fanfic/dumbledores-lament.doc ./archlinux-2016.02.01-dual.iso
Specifying -maxdepth 3 searches one level deeper than that:
find . -maxdepth 3
. ./images ./images/hp ./images/hp/snape.jpg ./images/hp/harry.jpg ./images/memes ./images/memes/winteriscoming.jpg ./images/memes/goodguygary.JPG ./images/memes/picard.jpg ./gimp-2.8.16.tar.bz2 ./hp-fanfic ./hp-fanfic/malfoys-revenge.doc ./hp-fanfic/weekend-at-hagreds.doc ./hp-fanfic/dumbledores-lament.doc ./archlinux-2016.02.01-dual.iso
Finding by name
To restrict your search results to match only files and directories with a certain name, use the -name option and put the name in quotes:
find . -name "picard.jpg"
./images/memes/picard.jpg
You can also use wildcards as part of your file name. For instance, to find all files whose name ends in .jpg, you can use an asterisk to represent the rest of the file name. When you run the command, the shell globs the file name into anything that matches the pattern:
find . -name "*.jpg"
./images/hp/snape.jpg ./images/hp/harry.jpg ./images/memes/winteriscoming.jpg ./images/memes/picard.jpg
Notice that our command didn't list the file whose extension (in this case, JPG) is in capital letters. That's because unlike other operating systems, such as Microsoft Windows, Linux file names are case-sensitive.
To perform a case-insensitive search instead, use the -iname option:
find . -iname "*.jpg"
./images/hp/snape.jpg ./images/hp/harry.jpg ./images/memes/winteriscoming.jpg ./images/memes/goodguygary.JPG ./images/memes/picard.jpg
Finding only files, or only directories
To list files only and omit directory names from your results, specify -type f:
find . -type f
./images/hp/snape.jpg ./images/hp/harry.jpg ./images/memes/winteriscoming.jpg ./images/memes/goodguygary.JPG ./images/memes/picard.jpg ./gimp-2.8.16.tar.bz2 ./hp-fanfic/malfoys-revenge.doc ./hp-fanfic/weekend-at-hagreds.doc ./hp-fanfic/dumbledores-lament.doc ./archlinux-2016.02.01-dual.iso
To list directories only and omit file names, specify -type d:
find . -type d
. ./images ./images/hp ./images/memes ./hp-fanfic
Finding files based on size
To display only files of a certain size, you can use the -size option. To specify the size, use a plus or minus sign (for "more than" or "less than"), a number, and a quantitative suffix, such as k, M, or G.
For instance, to find files that are "bigger than 50 kilobytes", use -size +50k:
find . -size +50k
./images/memes/winteriscoming.jpg ./gimp-2.8.16.tar.bz2 ./archlinux-2016.02.01-dual.iso
For files "bigger than 10 megabytes", use -size +10M:
find . -size +10M
./gimp-2.8.16.tar.bz2 ./archlinux-2016.02.01-dual.iso
For "bigger than 1 gigabyte", use -size +1G:
find . -size +1G
./archlinux-2016.02.01-dual.iso
For files in a certain size range, use two -size options. For instance, to find files "bigger than 10 megabytes, but smaller than 1 gigabyte", specify -size +10M -size -1G:
find . -size +10M -size -1G
./gimp-2.8.16.tar.bz2
Finding files based on modification, access, or status change
The -mtime option restricts search by how many days since the file's contents were modified. To specify days in the past, use a negative number. For example, to find only those files which were modified in the past two days (48 hours ago), use -mtime -2:
find . -mtime -2
The -mmin option does the same thing, but in terms of minutes, not days. For instance, this command shows only files modified in the past half hour:
find . -mmin -30
A similar option is -ctime, which checks when a file's status was last changed, measured in days. A status change is a change in the file's metadata. For instance, changing the permissions of a file is status change.
The option -cmin searches for a status change, measured in minutes.
You can also search for when a file was last accessed — in other words, when its contents were most recently viewed. The -atime option is used to search for files based upon their most recent access time, measured in days.
The -amin option performs the same search restriction, but measured in minutes.
Redirecting output to a text file
If you are performing a large search, you may want to save your search results in a file, so that you can view the results later. You can do this by redirecting your find output to a file:
find . -iname "*.jpg" > images.txt
You can then open your results in a text editor, or print them with the cat command.
cat images.txt
./images/hp/snape.jpg ./images/hp/harry.jpg ./images/memes/winteriscoming.jpg ./images/memes/goodguygary.JPG ./images/memes/picard.jpg
Alternatively, you can pipe your output to the tee command, which prints the output to the screen and write it to a file:
find . -size +500M | tee bigfiles.txt
./archlinux-2016.02.01-dual.iso
cat bigfiles.txt
./archlinux-2016.02.01-dual.iso
Suppressing error messages
You may receive the error message "Permission denied" when performing a search. For instance, if you search the root directory as a normal user:
find /
find: `/var/lib/sudo/ts': Permission denied find: `/var/lib/sudo/lectured': Permission denied find: `/var/lib/polkit-1': Permission denied find: `/var/lib/container': Permission denied find: `/var/lib/gdm3/.dbus': Permission denied find: `/var/lib/gdm3/.config/ibus': Permission denied ...
You receive that error message if find tries to access a file your user account doesn't have permission to read. You can perform the search as the superuser (root), which has complete access to every file on the system. But it's not recommended to do things as root, unless there are no other options.
If all you must do is hide the "Permission denied" messages, you can add 2&>1 | grep -v "Permission denied" to the end of your command, like this:
find / 2>&1 | grep -v "Permission denied"
The above example filters out the "Permission denied" messages from your search. How?
2>&1 is a special redirect that sends error messages to the standard output to pipe the combined lines of output to the grep command. grep -v then performs an inverse match on "Permission denied", displaying only lines which do not contain that string.
Redirecting and using grep to filter the error messages is a useful technique when "Permission denied" is cluttering your search results and you can't perform the search as root.
Examples
find ~/. -name "*.txt" -amin -120
Find all files in your home directory and below which end in the extension ".txt". Display only files accessed in the past two hours.
find . -name "*.zip" -size +10M -mtime -3
Find all files in the working directory and below whose name has the extension ".zip" and whose size is greater than 10 megabytes. Display only files whose contents were modified in the last 72 hours.
find . -iname "*report*" -type f -maxdepth 2
Perform a case-insensitive search for files that contain the word "report" in their name. If the search finds a directory with "report" in its name, do not display it. Search only in the working directory, and one directory level beneath it.
find / -name "*init*" 2>&1 | grep -v "Permission denied" | tee ~/initfiles.txt
Find all files on the system whose name contains "init", suppressing error messages. Display results on the screen and output them to a file in your home directory named "initfiles.txt".