Linux VPS

The find command: search for files in Linux

Search your Linux server for files by name, type, size, age, owner or permissions with find, act on the results with -exec or -delete, and skip folders you do not need.

BeginnerLinux server4 min read

find walks through folders and lists every file that matches your conditions. It is the tool for questions like "where is that config file?", "what is filling the disk?" or "which files did the app change today?". This guide covers the options you will use most on a server, with examples you can copy.

Before you start

  • A Linux VPS with Ubuntu 22.04 or 24.04, Debian 12, or AlmaLinux or Rocky Linux 8 or 9. find is installed on all of them.
  • An SSH session. Searching system folders such as /etc or /var/log fully needs sudo, or you will see "Permission denied" lines.

The basic shape

Bash
find [where to look] [conditions] [action]

With no condition, find lists everything under the starting folder:

Bash
find /etc/nginx

With no action, it prints the matching paths. Conditions are combined with AND by default.

Search by name

Find a file by exact name:

Bash
sudo find / -name nginx.conf

Use wildcards in quotes, so the shell does not expand them first:

Bash
find /var/www -name "*.php"

-iname ignores upper and lower case:

Bash
find /home -iname "*.jpg"

To hide "Permission denied" noise when searching as a normal user, send errors away:

Bash
find / -name "*.conf" 2>/dev/null

Search by type

  • -type f: regular files.
  • -type d: directories.
  • -type l: symbolic links.
Bash
find /var/www/example.com -type d -name cache

Search by size

-size uses k, M and G. A + means larger than, a - smaller than:

Bash
sudo find /var -type f -size +100M

List them with sizes:

Bash
sudo find /var -type f -size +100M -exec ls -lh {} +

For a full disk-space hunt, see Find the largest files and folders.

Search by age

  • -mtime -1: content modified in the last 24 hours.
  • -mtime +30: modified more than 30 days ago.
  • -mmin -60: modified in the last 60 minutes.
Bash
find /var/www/example.com -type f -mmin -60

More on timestamps is in Find recently changed files.

Search by owner or permissions

Files owned by a user or group:

Bash
sudo find /var/www -user deploy
sudo find /srv -group webteam

Files no current user owns, often left after deleting an account:

Bash
sudo find / -xdev -nouser

Files with exact permissions, or with a bit set:

Bash
find /var/www -type f -perm 777
find /var/www -type f -perm -o+w

The second example finds anything writable by everyone, which on a web server deserves a closer look.

Combine conditions

Conditions next to each other must all match. Use -o for OR and ! for NOT, with escaped brackets to group:

Bash
find /var/log -type f \( -name "*.gz" -o -name "*.1" \)
find /var/www -type f ! -name "*.php"

Exclude folders

-prune skips a folder entirely, which also makes the search faster:

Bash
find /var/www -path /var/www/example.com/node_modules -prune -o -type f -name "*.js" -print

The -print at the end is needed so the pruned folder itself is not printed.

To stay on one filesystem and skip /proc, /sys and other mounts, add -xdev:

Bash
sudo find / -xdev -type f -size +500M

Act on the results

-exec runs a command on each match. {} stands for the path:

Bash
find /var/www/example.com -type f -name "*.sh" -exec chmod 755 {} +
  • Ending with + passes many files to one command call. It is fast.
  • Ending with \; runs the command once per file. Use it when the command takes only one file.

To be asked before each action, use -ok instead of -exec.

Delete files

-delete removes every match:

Bash
find /tmp/myapp -type f -name "*.tmp" -mtime +7 -delete

Warning: -delete acts at once, with no recycle bin. Always run the same command without -delete first and read the list. Put -delete last; placed before your conditions, it deletes everything under the starting folder.

Check it works

Create a small test tree and search it:

Bash
mkdir -p ~/findtest/sub && touch ~/findtest/a.log ~/findtest/sub/b.log ~/findtest/c.txt
find ~/findtest -name "*.log"

The output lists a.log and sub/b.log, but not c.txt. Remove the test folder with rm -r ~/findtest.

Troubleshooting

  • Many "Permission denied" lines: you searched folders your user cannot read. Use sudo, or add 2>/dev/null to hide the errors.
  • "paths must precede expression": a wildcard was not quoted, so the shell expanded it. Write -name "*.log", with quotes.
  • Search takes very long: you started at / and included network or virtual filesystems. Add -xdev or start in a narrower folder.
  • Nothing found with -size -1M: sizes round up to whole units, so only empty files are smaller than 1M. Use -size -1024k.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.