Linux FIND command with an example: Do you know how many files are in a fresh Linux installation? If you are using PopOS! Linux distribution is an example, there are over 31,000 files. That’s before you start creating documents, storing music, uploading PDFs, or organizing images.
For this reason, finding the right file or folder in Linux when you need it becomes a challenge. In this article, you will learn how to use the Linux FIND command and we will give you as many examples as possible.
Linux FIND Command Syntax
Syntax refers to how words or commands are put together. Just as a normal sentence can become nonsense just by mixing up the words, commands can fail if not used with the proper syntax.
to find [path] [conditions] [actions]
This is what it means:
to find – start the search utility in Linux
path – where to look
terms – the arguments you want to apply to the search
behviour – what do you want to do with the results
A simple example using all three looks like this:
to find . -example-filename.rtf -print
As you might have guessed, this will find the filename sample-file.rtf.
Period (.) path tells find to search the current directory and all directories within it.
the -name state says find to get the file with that specific name.
the -impression asks FIND to display the results on the screen.
The dot and -print are default values for the search command. It will therefore continue to do the same thing if you do not use them. Next, find -sample-filename.rtf will give you the same results.
Linux FIND in another directory
You can search in a different directory than the one you are in. Just insert the directory path after SEARCH. If you are at root and you know the file is somewhere in the home user directory you would use:
find home/user -sample-filename.rtf
This is still a recursive search, so it will go through all the directories under username.
Linux FIND Find multiple directories
If you want to search multiple directories at once, just list them in the command, separated by a space.
find /lib /var /bin -sample-filename.rtf
Linux FIND no recursion or limited recursion
If you used the FIND command above at the root level, it would search all directories on the system. So if you want to stick only to the current directory, use the -Maximum depth option. The number after -max depth indicates how deep to go before stopping.
using – maximum depth 1 just means this directory.
find -sample-filename.rtf -max depth 1
using -maximum depth 2 or a higher number means going much further.
find -max depth 5 -sample-filename.rtf
Linux FIND Wildcard Example
The FIND command uses the asterisk (*) as a wildcard character. Use it for any part of the name that you are unsure of. It can be used more than once in the name. Without the file type in the file name, the results will also include the matching directories.
find home/file username *shows*
Linux FIND example by type
To search for a single file or directory, use the -type option and the appropriate descriptor. There are a few, but file and directory are the most common:
b – block the device
c – character device
l – symbolic link
s – socket
find home/user -filename*sample* -type d
Linux FIND example which is not case sensitive
Unlike Windows, Linux doesn’t care whether a letter is uppercase or lowercase. So if you want it to search for both File-Sample.rtf and file-sample.rtf, use the -name option.
find home/user -iname Sample-File.rtf
Linux FIND Multiple Example Files
Suppose you want to find the .rtf and .html versions of a file. This can be done in one command using the -Where (u) operator. On some distros you may need to enclose the names in square brackets, like ( -sample-filename.rtf -o -sample-filename.html).
find home/user -sample-filename.rtf -o -sample-filename.html
Linux FIND files that don’t match a name
Maybe you know that the .html version of a file exists, but not if there are others. You can filter the .html version of the search using the -no option.
find home/user -sample-filename* -no -name *.html
Linux FIND no error results
In the non-recursive search example, note that it listed all the directories it couldn’t search for and the correct result. It’s boring. Let’s stop it from showing all those “Permission Denied” directories. Combine it with another Linux terminal command, grep. You can also use Search with grep to find files with specific words.
find -max depth 5 -name file-sample.rtf 2>&1 | grep -v “Permission denied”
let’s break up 2>&1.
of them – This represents Standard which is short for standard error out.
1 – This represents standard output which is short for standard output
> – means redirect the output on its left to the one on its right.
& – means to gather.
Next 2>&1 this means taking standard errors and redirecting them, then merging them with the standard output into a single output.
Let’s see now | grep -v “Permission denied”.
| (called a pipe) – Tells Linux to pass results from whatever is to its left to whatever is to its right. It is sent to the grep command.
grep – is a text search utility.
-v – tells grep to search for anything that doesn’t match the text to the left of -v. In this case, you are telling grep to only search for anything that does not contain the text or string “Permission denied”. So grep will only show you the results you’re looking for and any errors that don’t match “Permission Denied”.
Linux FIND permissions example
To use it well, you need to learn Linux permissions.
All sample files have 664 permissions except one with 775 permissions. Use the -permanent option to find it.
search for documents/ -sample-filename* -type f -perm 775
Linux FIND example by size
Searching for files by size is useful for those large files to fill up your hard drive. Use the -size option, the desired size, and one of the following suffixes. If no suffix is used, -size defaults to B. To search for files that are equal to or larger than a certain size, place a plus sign (+) in front of the size.
M – Megabytes
G – Gigabytes
k – Kilobytes
b – blocks (512 bytes – default)
w – words (two bytes together)
find -size +500k
Linux FIND by owner
There are two ways to search for files by owner. One is for the owner’s username and the other is for the user’s group. To search by username, use the -Username option, followed by the username. To search by user group, use -group followed by the name of the group.
find -usergroup name Where find -user username
Linux FIND files for last modified example
To find files that have been modified or edited in the last X days, use -mtime followed by a number. Put a minus sign (–) in front of the number you will find something modified so many days ago. A plus sign (+) means so many days before now.
find -name “file-sample*” -mtime +5 (more than 5 days ago)
find -name “file-sample*” -mtime -5 (less than 5 days ago)
To search by last modified in minutes, use the -mmin option followed by the number of minutes. Use the + and – as above.
find -name “example-file*” -mmin -5
find -name “example-file*” -mmin +5
Example of searching Linux files by last access time
The option used to find files based on when they were last opened is -one moment for days and -amin for minutes. Follow it with the number of days or minutes to go back and use the + and – signs as greater than and less than.
find -name “example-file*” -atime -5
find -name “example-file* -amin -5
Combine FIND with other Linux commands
There is a previous example of using find with the grep command, and you can use it with many more. You can see that using find and other commands can be very powerful and save you a lot of time. Imagine having to delete a bunch of a particular type of file.
Instead of digging through File Explorer, just create the right command and you’ll be ready to go in seconds. How are you going to use the search command now?
I am Bhumi Shah, a highly skilled digital marketer with over 11 years of experience in digital marketing and content writing in the tech industry.