find Command in Linux: Search Files and Directories

When you need to locate a configuration file buried somewhere under /etc, clean up old logs by date, or change permissions on hundreds of files at once, the find command does the job. It walks a directory tree and matches files based on name, type, size, date, permissions, ownership, and more. It can also run a command on every match.
You can combine find with other tools such as grep
, sed
, or xargs
for more complex operations.
This guide covers the most common find use cases with practical examples.
To assemble these tests interactively, try our find command builder : set the filters you need and copy the finished command.
find Command Syntax
The general syntax for the find command is as follows:
find [options] [path...] [expression]- The
optionsattribute controls the treatment of the symbolic links, debugging options, and optimization method. - The
path...attribute defines the starting directory or directories where find will search the files. - The
expressionattribute is made up of options, search patterns, and actions separated by operators.
To search for files in a directory, the user invoking the find command needs to have read and execute permissions on that directory.
Here is an example that puts these parts together:
find -L /var/www -name "*.js"- The option
-L(options) tells thefindcommand to follow symbolic links. - The
/var/www(path…) specifies the directory that will be searched. - The expression
-name "*.js"tellsfindto search files ending with.js(JavaScript files).
If you want to limit how deep find searches, use -maxdepth. For example, to search only the top level of /var/www:
find /var/www -maxdepth 1 -type f -name "*.js"Find Files by Name
Finding files by name is probably the most common use of the find command. To find a file by its name, use the -name option followed by the name of the file you are searching for.
For example, to search for a file named document.pdf in the /home/linuxize directory, you would use the following command:
find /home/linuxize -type f -name document.pdfTo run a case-insensitive search, change the -name option with -iname:
find /home/linuxize -type f -iname document.pdfThe command above will match “Document.pdf”, “DOCUMENT.pdf”, etc.
To match part of the path, use -path:
find /var/www -type f -path "*/cache/*"Search the Entire System for a File
When you know the name of a file but have no idea where it lives, start the search from the root directory /:
find / -type f -name "nginx.conf"As a regular user, you do not have access to many system directories, so the real matches get buried under lines like these:
find: ‘/lost+found’: Permission denied
find: ‘/boot/lost+found’: Permission denied
find: ‘/boot/efi’: Permission deniedThese messages are printed to standard error. To hide them and see only the files that matched, redirect
standard error to /dev/null:
find / -type f -name "nginx.conf" 2>/dev/nullKeep in mind that this only hides the errors; find still skips the directories you cannot read. To search those as well, run the command with sudo:
sudo find / -type f -name "nginx.conf"A search from / also walks through virtual filesystems such as /proc and /sys, and any mounted network shares. Add the -xdev option to stay on the filesystem where the search started:
sudo find / -xdev -type f -name "nginx.conf"If you search the whole system by file name often, the locate command
is much faster because it queries a prebuilt index instead of reading the disk.
Find Files by Extension
Searching for files by extension is the same as searching for files by name. For example, to find all files ending with .log.gz inside the /var/log/nginx directory, you would type:
find /var/log/nginx -type f -name '*.log.gz'You must either quote the pattern or escape the asterisk * with a backslash \ so that the shell does not interpret it as a glob.
To find all files that do not match a pattern, use the -not option. For example, to find all files that do not end in *.log.gz:
find /var/log/nginx -type f -not -name '*.log.gz'Find Files by Type
Sometimes you might need to search for specific file types such as regular files, directories, or symlinks. In Linux, everything is a file.
To search for files based on their type, use the -type option and one of the following descriptors to specify the file type:
f: a regular filed: directoryl: symbolic linkc: character devicesb: block devicesp: named pipe (FIFO)s: socket
For instance, to find all directories in the current working directory , you would use:
find . -type dA common example is to recursively change the website file permissions to 644 and directory permissions to 755 using the chmod
command:
find /var/www/my_website -type d -exec chmod 0755 {} \;
find /var/www/my_website -type f -exec chmod 0644 {} \;When the -exec expression ends with {} + instead of {} \;, find passes many files to a single command invocation rather than running the command once per file, which is much faster on large directory trees:
find /var/www/my_website -type f -exec chmod 0644 {} +Find Directories by Name
To search for directories instead of files, combine -type d with -name:
find / -type d -name "config"This returns every directory named config anywhere on the filesystem. To limit the search depth, add -maxdepth:
find /etc -maxdepth 3 -type d -name "nginx"The command above searches no more than three levels deep under /etc.
Find Files by Size
To find files based on the file size, pass the -size parameter along with the size criteria. You can use the following suffixes to specify the file size:
b: 512-byte blocks (default)c: bytesw: two-byte wordsk: kibibytes (1024 bytes)M: mebibytes (1024 kibibytes)G: gibibytes (1024 mebibytes)
The following command will find all files of exactly 1024 bytes inside the /tmp directory:
find /tmp -type f -size 1024cThe find command also allows you to search for files that are greater
or less than a specified size.
Before comparing, find rounds the size of each file up to the next whole unit. Because of that, -size -1M does not mean “less than 1 MiB”. A 500 KiB file is rounded up to 1M, which is not less than 1M, so the test matches only empty files. Use the c suffix for exact byte comparisons without rounding.
In the following example, we search for all files smaller than 1 MiB (1,048,576 bytes) inside the current working directory. Notice the minus - symbol before the size value:
find . -type f -size -1048576cIf you want to search for files with a size greater than 1 MiB, then you need to use the plus + symbol:
find . -type f -size +1MYou can even search for files within a size range. The following command will find all files larger than 1 MiB and smaller than 2 MiB:
find . -type f -size +1048576c -size -2097152cThe same rounding rule is why -size +1M -size -2M returns nothing: no file can round up to more than 1M and less than 2M at the same time.
Find Files by Modification Date
The find command can also search for files based on their last modification, access, or change time.
Same as with size, use the plus and minus symbols for “greater than” or “less than”.
Suppose you modified one of the dovecot configuration files a few days ago but cannot remember which one. You can filter all files under the /etc/dovecot/conf.d directory that end with .conf and have been modified in the last five days:
find /etc/dovecot/conf.d -name "*.conf" -mtime -5Here is another example of filtering files based on the modification date using the -daystart option, which measures times from the beginning of today instead of 24 hours ago. The -daystart option is positional and affects only the tests that come after it, so it must be placed before -mtime. The command below will list all files in the /home directory that were modified more than 30 days ago:
find /home -daystart -mtime +30Find Files by Permissions
The -perm option allows you to search for files based on the file permissions.
For example, to find all files with permissions of exactly 644 inside the /var/www/html directory, you would use:
find /var/www/html -perm 644You can prefix the numeric mode with minus - or slash /.
Prefix the mode with / to match files with any of the specified permission bits set.
Consider the following example command:
find . -perm /444The above command will match all the files with read permissions set for either user, group, or others.
If minus - is used as the prefix, then for the file to match, at least the specified bits must be set. The following command will search for files that have read and write permission for the owner and group and are readable by other users:
find . -perm -664Find Files by Owner
To find files owned
by a particular user or group, use the -user and -group options.
For example, to search for all files and directories owned by the user linuxize, you would run:
find / -user linuxizeBefore changing ownership, preview the files owned by www-data in your website directory. Replace /var/www/my_website with the directory you want to update:
sudo find /var/www/my_website -user www-data -type f -printAfter you confirm the list, run the following command to change those files’ ownership to nginx. Changing a file’s owner requires root privileges, so both commands use sudo to search with the same permissions:
sudo find /var/www/my_website -user www-data -type f -exec chown nginx {} \;Find Files by Content
The find command locates files by their metadata (name, size, date), not by what is inside them. To search within file contents, combine find with grep
:
find /etc -type f -name "*.conf" -exec grep -l "listen" {} \;This searches every .conf file under /etc and prints the path of each file that contains the word “listen.” The -l flag tells grep to print only the filename, not the matching line.
When you are searching file contents rather than file metadata, ripgrep
is a faster alternative that combines recursive search, filename output, and ignore-file handling.
For better performance on large directory trees, pipe the results through xargs
instead of running -exec ... \; once per file:
find /var/log -type f -name "*.log" -print0 | xargs -0 grep -l "error"The -print0 and -0 pair handles file names that contain spaces or special characters.
Find and Delete Files
To delete all matching files, append the -delete option to the end of the match expression.
Ensure you are using this option only when you are confident that the result matches the files you want to delete. It is always a good idea to print the matched files before using the -delete option.
For example, to delete all files ending with .temp from the /var/log/, you would use:
find /var/log/ -name '*.temp' -delete-delete option with extreme caution. The find command is evaluated as an expression and if you add the -delete option first, the command will delete everything below the starting points you specified.The -delete action handles file names with spaces directly. When you need to pass matching paths to another command, use -print0 and xargs -0. Preview the matches first:
find . -type f -name "*.log" -printAfter you confirm the list, pass the same matches to rm:
find . -type f -name "*.log" -print0 | xargs -0 rm -fWhen it comes to directories, find can delete only empty directories, same as rmdir
.
Quick Reference
For a printable quick reference, see the find cheatsheet .
| Command | Description |
|---|---|
find /path -name "*.log" | Find files by name |
find /path -iname "readme*" | Case-insensitive name search |
find /path -type d -name "config" | Find directories by name |
find /path -type f -size +100M | Find files larger than 100 MiB |
find /path -mtime -7 | Files modified in the last 7 days |
find /path -perm 644 | Files with exact permissions |
find /path -user john | Files owned by a user |
find /path -name "*.conf" -exec grep -l "term" {} \; | Search file contents |
find /path -name "*.tmp" -delete | Delete matching files |
find /path -maxdepth 2 -type f | Limit search depth |
FAQ
What is the difference between find and locate?find walks the directory tree in real time, so it always returns current results but can be slow on large filesystems. locate
queries a pre-built database (updated by updatedb), which makes it much faster but may return stale results if the database has not been refreshed recently. Use find when accuracy matters and locate when speed is the priority.
How do I find empty files or directories?
Use the -empty option. To find empty files: find /path -type f -empty. To find empty directories: find /path -type d -empty. You can combine this with -delete to clean them up.
How do I find files modified in the last N minutes instead of days?
Use -mmin instead of -mtime. For example, find /var/log -type f -mmin -30 finds files modified in the last 30 minutes.
Can I combine multiple search conditions?
Yes. By default, find treats multiple conditions as AND. To use OR logic, add -o between conditions: find /path -name "*.log" -o -name "*.txt". Use parentheses (escaped as \( and \)) to group expressions.
Conclusion
The find command handles everything from simple name lookups to complex searches combining size, date, permissions, and content. For related tools, see the guide on finding large files
and the xargs command
for processing find output efficiently.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page