On this page
- When should I use find, and when locate?
- How do I search by name?
- How do I find files by size or age?
- How do I search by owner or permissions?
- How do I run a command on every result?
- How do I limit how deep find searches or skip folders?
- How do I install and use locate?
- How do I find where a program or package file lives?
- Questions
When should I use find, and when locate?
Use find when you need fresh results or want to filter by anything other than the name, and locate when you only remember part of a file name and want an answer in milliseconds. find walks the directories at the moment you run it, so it always sees the current state but can take a while on large trees. locate looks names up in a database that is rebuilt once a day, so it is fast but misses files created since the last update.
find --version | head -n 1
command -v locate || echo "locate not installed"find comes from the findutils package and is on every image. locate has to be installed, as shown further down.
How do I search by name?
Give find a starting directory, then -name with a pattern in quotes. The quotes stop the shell from expanding the wildcard before find sees it.
find /etc -name '*.conf'
find ~ -iname 'readme*'
find /var/www -type d -name uploads
sudo find / -name sshd_config 2>/dev/null-iname ignores case, -type f limits results to files, -type d to directories and -type l to symbolic links. The 2>/dev/null at the end hides the "Permission denied" lines that appear when a normal user searches system folders. To match against the full path rather than the last name part, use -path '*/nginx/*.conf'.
How do I find files by size or age?
Use -size with a sign and a unit, and -mtime (days) or -mmin (minutes) for modification time. A plus means more than, a minus means less than.
sudo find / -xdev -type f -size +500M -exec ls -lh {} +
find /var/log -type f -mtime -1
find /tmp -type f -mmin +120
find . -newer deploy.stamp -type f
find /srv -type d -emptyThe first command is a quick way to hunt for whatever is filling a disk; -xdev keeps find on one filesystem so it does not descend into /proc or mounted volumes. Units are k, M and G. For a ranked view of space by folder, the disk usage guide shows du and ncdu.
How do I search by owner or permissions?
Use -user, -group and -perm. These tests are a staple of small security audits.
sudo find /var/www -not -user deploy
sudo find / -xdev -nouser
sudo find / -xdev -perm -4000 -type f
find /var/www -type f -perm -o+wThe lines show, in order: files in the web root not owned by deploy, files whose owner account no longer exists, programs with the setuid bit, and files that anyone may write to. -perm -4000 means "at least these bits set". World writable files in a web root are a common weakness; fix them with chmod as described in the file permissions guide.
How do I run a command on every result?
Add -exec with {} as the place holder for each path and finish with +, which hands many paths to one command run, or with an escaped semicolon, which runs the command once per file.
find /var/www -type f -name '*.php' -exec chmod 644 {} +
find /var/www -type d -exec chmod 755 {} +
find . -name '*.bak' -exec rm -i {} \;
find /var/log/app -name '*.log' -mtime +14 -deleteBefore using -delete, run the same command without it and read the list. Put -delete last: find evaluates its arguments left to right, and an action placed before the tests would delete everything under the start directory. For long pipelines, -print0 together with xargs -0 keeps names with spaces intact; the xargs guide covers that.
How do I limit how deep find searches or skip folders?
-maxdepth sets how many levels to descend, and -prune drops a directory from the walk entirely.
find /etc -maxdepth 1 -name '*.conf'
find /srv/app -path /srv/app/node_modules -prune -o -name '*.js' -print
find / -xdev \( -path /proc -o -path /sys \) -prune -o -name core -printIn the prune form, the -o -print part is required: without an explicit -print, find also prints the pruned folder names. The escaped parentheses group two paths so both are skipped. Put -maxdepth before the other tests to avoid a warning.
How do I install and use locate?
Install the package for your family, build the first index, and search. The daily refresh runs from a systemd timer the package sets up.
sudo apt install plocate
sudo dnf install mlocate
sudo updatedb
locate nginx.conf
locate -i -c readme
locate -b '\sshd_config'The first line is for Ubuntu and Debian, the second for Rocky and Alma 9; both provide the locate and updatedb commands. -i ignores case, -c prints only the number of matches, -l 20 limits output, and -b with a leading backslash matches the exact base name. Check the timer with systemctl list-timers | grep updatedb. Files a user cannot read are hidden from that user in the results, so search with sudo for system paths.
How do I find where a program or package file lives?
For commands, ask the shell; for files from packages, ask the package manager. Neither needs a filesystem search.
command -v nginx
type -a python3
whereis sshd
dpkg -S /usr/sbin/nginx
rpm -qf /usr/sbin/nginx
dpkg -L openssh-server | grep sshd_configdpkg answers on Ubuntu and Debian, rpm on Rocky and Alma. The package managers guide lists more of these queries. If find or locate returns nothing for a file you are sure exists, check the spelling and case, try -iname, run sudo updatedb for locate, and confirm you are searching the right starting directory.
Questions about Find files with find and locate
Why does find print so many Permission denied lines?
A normal user cannot enter many system directories. Run find with sudo, or add 2>/dev/null to hide error output.
Why does locate not show a file I just created?
The index is updated once a day. Run sudo updatedb and search again.
What is the difference between -exec with + and with a semicolon?
With + find passes many paths to one command run, which is much faster. The semicolon form runs the command once for each file.
Can find search inside files?
find matches names and attributes. To search file contents, combine it with grep, or use grep -r directly, as the grep guide shows.
Is plocate compatible with old locate options?
The common options (-i, -c, -l, -b) work the same, so scripts written for mlocate usually run unchanged.
More in Linux command line
- Copy files and directories with cp
- Move and rename with mv
- Create directories and empty files
- List files with ls, find your place with pwd
- Read files with cat, less and tail
- Search text with grep
- Edit and report text with sed and awk
- echo, tee and wc
- Edit files with nano
- Edit files with Vim
- Read manual pages
- Users and groups
- Ownership with chown and chgrp
- Symbolic and hard links
- Pipes, redirection and xargs