Sysadmin

Linux find Command: 20 Practical Examples

Linux find Command: 20 Practical Examples

The find command on Linux is a fundamental tool for every sysadmin, a true Swiss Army knife for file system management. It’s not just about locating a file by name; it’s about scanning entire directory hierarchies to identify elements based on complex criteria like size, modification date, owner, permissions, and even executing actions on the results. In enterprise environments, where thousands of servers and millions of files are managed, mastering find means optimizing time, automating cleanup processes, identifying space or security issues, and promptly responding to incidents. Its versatility makes it indispensable for audits, troubleshooting, and preventive maintenance, allowing you to transition from manual, time-consuming operations to precise, scalable execution.

Tested on: Ubuntu 24.04 LTS · CentOS Stream 9 · Debian 12 · October 2026

Prerequisites / Test Environment

To follow this guide, you will need a Linux operating system (Ubuntu, Debian, CentOS, RHEL, or any other compatible distribution) with terminal access. No additional packages are required, as find is a standard command present in all Linux installations. It is advisable to run commands in a test environment or with example directories and files to avoid unintended modifications on production systems, especially when using options like -delete or -exec with modifying commands. Ensure you have read permissions for the directories you intend to search; for some operations, root permissions or the use of sudo might be necessary.

Basic find Syntax

The general syntax for the find command is find [path] [expression]. The path specifies the starting directory for the search, while the expression defines the search criteria and actions to perform. If the path is omitted, find assumes the current directory (.). The expression is composed of options, tests, actions, and operators.

Here’s a simple example to get started:

find /var/log -name "*.log"

This command searches for all files with the .log extension within the /var/log directory and its subdirectories. The -name option is case-sensitive. For a case-insensitive search, you can use -iname.

Search by Name and Extension

In addition to -name and -iname, you can use wildcards like * (any sequence of characters) and ? (any single character). It’s good practice to enclose patterns in quotes to prevent the shell from interpreting them.

To find all .conf files in the /etc directory:

find /etc -name "*.conf"

To find files named report (case-insensitive):

find /home -iname "report*"

Read also: 15 Essential journalctl Commands for Linux Sysadmins

Find Files Larger Than a Certain Size

This is a crucial feature for disk space management. The -size option allows you to specify the size. The + prefix indicates ‘larger than’, - indicates ‘smaller than’, and the absence of a prefix indicates ‘exactly’. Units include c (bytes), k (kilobytes), M (megabytes), G (gigabytes).

To find all files larger than 1GB, excluding mounted filesystems (-xdev) and suppressing permission errors (2>/dev/null):

find / -xdev -type f -size +1G 2>/dev/null

To find files exactly 100MB:

find /tmp -type f -size 100M

Files Modified in the Last Few Days (mtime, mmin, newer)

The -mtime (modified time) and -mmin (modified minutes) options are used to filter files based on their last modification. Again, + means ‘more than N’, - means ‘less than N’, and the absence of a prefix means ‘exactly N’.

To find files modified more than 30 days ago in the /tmp directory:

find /tmp -type f -mtime +30

To find files modified in the last 60 minutes:

find /var/log -type f -mmin -60

The -newer option is useful for comparing a file’s modification date with that of another reference file.

To find files modified more recently than /etc/passwd:

find /etc -newer /etc/passwd

Search by Owner and Permissions

The -user, -group, and -perm options are essential for security checks and access management.

To find all files owned by user john in their home directory:

find /home -user john

To find files owned by the www-data group:

find /var/www -group www-data

With -perm, you can specify permissions in octal format. -perm MODE searches for files with EXACTLY MODE permissions. -perm -MODE searches for files with AT LEAST MODE permissions (all bits in MODE must be set). -perm /MODE searches for files with AT LEAST ONE of the bits in MODE set.

To find files with 644 permissions:

find . -type f -perm 644

To find files executable by everyone (+x):

find . -type f -perm /111

Finding SUID Files: A Security Check

SUID (Set User ID) files are executable files that, when run, temporarily assume the permissions of the file’s owner (often root). They can pose a security risk if improperly configured or exploited by attackers. It is crucial to monitor them. Read also: SSH Hardening Linux: Complete Guide 2026 (10 Critical Settings)

To find all SUID files across the entire filesystem (excluding mounted filesystems):

find / -xdev -perm -4000 -type f

The -perm -4000 option searches for files with the SUID bit set. The -type f ensures we only look for regular files.

Execute a Command on Results with -exec

The -exec option is one of find‘s most powerful features, allowing you to execute an external command on each found file. It concludes with \; to execute the command for each individual file, or with + to group multiple files and pass them as single arguments to the command, making it more efficient.

To find all .php files in /var/www and search for the string eval within them, printing only the filename (-l):

find /var/www -type f -name "*.php" -exec grep -l eval {} +

The {} is a placeholder for the current filename. The + at the end groups the filenames, making grep more efficient. If we had used \;, grep would be executed once for each file.

Safely Delete with -delete

The -delete option allows you to remove found files. It is extremely powerful and must be used with utmost caution. It is always recommended to test the command without -delete first, to verify that the file selection is correct.

To delete temporary files older than 30 days in the /tmp directory:

find /tmp -type f -mtime +30 -delete

This command is often used in maintenance scripts to clean up log or temporary files. Ensure you have the necessary permissions.

Limit Depth and Exclude Directories

For more targeted searches, you can control the scan depth or exclude specific directories.

-maxdepth N limits the search to N levels of subdirectories. -mindepth N starts the search from N levels of subdirectories.

To search for directories only within the first 2 levels (from the current directory):

find . -maxdepth 2 -type d

The -prune option is useful for excluding entire directories from the search. It must be used carefully and often combined with -o (OR) and -print.

To find .conf files in /srv but excluding the /srv/cache directory:

find /srv -path /srv/cache -prune -o -name "*.conf" -print

Here, -path /srv/cache -prune tells find not to descend into /srv/cache. The -o (OR) operator means “or”, and -print is necessary to print the results of the subsequent expression, as -prune has the side effect of not printing anything for the excluded directory.

find and xargs: When They’re Needed Together

While -exec is powerful, it has limitations, particularly regarding handling spaces in filenames or the number of arguments it can pass to an external command. xargs is a complementary tool that solves these issues by building and executing command lines from standard input.

To pass filenames with spaces, use -print0 with find and -0 with xargs:

find /tmp -name "*.tmp" -print0 | xargs -0 rm

This is the safest way to delete files found by find when filenames may contain spaces or special characters, as -print0 and -0 use the null character as a delimiter.

Read also: iloader Linux: Dynamic Library Management

Common Errors and Troubleshooting

  • Permission denied: Often find encounters directories for which the current user does not have read permissions. You can ignore these errors by redirecting error output: 2>/dev/null. If you need to search in protected areas, use sudo.
  • Incorrect wildcard usage: Remember to enclose wildcard patterns (*, ?) in quotes ("") to prevent the shell from expanding them before find processes them.
  • find and -delete: Never use -delete directly in production without first testing the command without it. Run the command first, then add -delete only when you are sure of the selection.
  • -exec and \; vs +: Understanding the difference is crucial for performance. \; executes the command once for each result, + executes it once for a block of results. Use + whenever possible for greater efficiency.
  • -xdev and filesystems: If you are searching on a specific filesystem and do not want to traverse mount points of other filesystems, -xdev is your ally. Without it, find can slow down or give unexpected results.

FAQ — Frequently Asked Questions

How can I search for files based on their content, not just their name?

To search for files based on their content, you can combine find with grep using the -exec option. For example, find . -type f -exec grep -l "string to search" {} \; will search for “string to search” in all regular files in the current directory and print the names of the files containing it. The -l option of grep is used to print only the filename.

What is the difference between -mtime, -ctime, and -atime?

-mtime (modification time) refers to the last time the file’s content was modified. -ctime (change time) refers to the last time the file’s metadata (such as owner, permissions, or content) was changed. -atime (access time) refers to the last time the file was read or accessed. Each of these can be specified with +N, -N, or N days.

Can I combine multiple search criteria with AND/OR?

Yes, find supports logical operators. By default, tests are combined with an implicit AND operator. For an OR, use -o. To negate a test, use !. For example, find . -type f \( -name ".log" -o -name ".txt" \) will search for .log or .txt files. Parentheses must be escaped (\) or enclosed in quotes to prevent shell interpretation.

How do I find empty directories?

To find empty directories, you can use the -empty option combined with -type d. For example, find /path/to/search -type d -empty will list all empty subdirectories within the specified path. This is an excellent option for filesystem cleanup.

Is there a way to display find‘s output in a more readable format?

find prints file paths by default. If you want more detailed output, you can combine it with ls via -exec. For example, find . -type f -name "*.conf" -exec ls -lh {} \; will show the details (permissions, owner, size, date) of each .conf file found, in a human-readable format.

Conclusions with Operational Takeaways

Mastering the find command is more than just a sysadmin skill; it’s a true superpower that allows you to efficiently navigate and manage even the most complex Linux environments. From simple use cases like searching by name to advanced automated cleanup operations with -delete or executing commands on thousands of files via -exec and xargs, find offers unparalleled flexibility. Always remember to test your commands in safe environments, especially those that modify or delete files, and leverage its options to limit search depth or exclude specific directories. Dedicate time to exploring its numerous options, and you will see a significant improvement in your productivity and the reliability of your system operations. A sysadmin who knows find well is a more effective and secure sysadmin.

Sources

Updated: October 2026

Share this article:

Written by

Rosario Giordano

Rosario Giordano is a system administrator and IT consultant specializing in cybersecurity and cloud, with over 20 years of experience managing enterprise Linux infrastructures. His areas of expertise include SSH hardening, Kubernetes platforms, PostgreSQL databases, VMware/ Proxmox virtualization, and compliance with NIS2 and ISO 27001 security frameworks