Sandbox99 Chronicles

Level Up Your Linux Skills: Find Command Reference

find-command

Written by Jose Mendez

Hi, I’m Jose Mendez, the creator of sandbox99.cc. with a passion for technology and a hands-on approach to learning, I’ve spent more than fifteen years navigating the ever-evolving world of IT.

Published Jul 9, 2025 | Last updated on Jul 9, 2025 at 8:20AM

Reading Time: 4 minutes

Introduction

The find command is one of the most powerful and versatile tools in the Linux command line arsenal. It allows you to search for files and directories based on various criteria such as name, size, type, permissions, and modification time. Beyond just locating files, find can execute commands on the results, making it an essential tool for system administration, file management, and automation tasks.

This cheatsheet covers everything from basic file searches to advanced operations, providing you with practical examples and best practices to master the find command efficiently.

Basic Syntax

find [path] [expression]

Common Options

Search by Name

find /path -name "filename"           # Exact match (case-sensitive)
find /path -iname "filename"          # Case-insensitive match
find /path -name "*.txt"              # Files ending with .txt
find /path -name "file*"              # Files starting with "file"
find /path -name "*pattern*"          # Files containing "pattern"

Search by Type

find /path -type f                    # Files only
find /path -type d                    # Directories only
find /path -type l                    # Symbolic links only
find /path -type b                    # Block devices
find /path -type c                    # Character devices
find /path -type p                    # Named pipes (FIFOs)
find /path -type s                    # Sockets

Search by Size

find /path -size +100M                # Files larger than 100MB
find /path -size -50k                 # Files smaller than 50KB
find /path -size 1G                   # Files exactly 1GB
find /path -empty                     # Empty files and directories

Size units: c (bytes), k (kilobytes), M (megabytes), G (gigabytes)

Search by Time

find /path -mtime -7                  # Modified in last 7 days
find /path -mtime +30                 # Modified more than 30 days ago
find /path -mtime 0                   # Modified today
find /path -atime -1                  # Accessed in last 24 hours
find /path -ctime +7                  # Status changed more than 7 days ago

Time units: -mmin (minutes), -mtime (days), -newer file (newer than file)

Search by Permissions

find /path -perm 755                  # Exact permissions
find /path -perm -755                 # At least these permissions
find /path -perm /755                 # Any of these permissions
find /path -executable                # Executable files
find /path -readable                  # Readable files
find /path -writable                  # Writable files

Search by User/Group

find /path -user username             # Files owned by user
find /path -group groupname           # Files owned by group
find /path -uid 1000                  # Files with specific user ID
find /path -gid 100                   # Files with specific group ID
find /path -nouser                    # Files with no valid user
find /path -nogroup                   # Files with no valid group

Advanced Options

Depth Control

find /path -maxdepth 2                # Max 2 levels deep
find /path -mindepth 1                # Skip the starting directory
find /path -maxdepth 1 -type d        # Only immediate subdirectories

Logical Operators

find /path -name "*.txt" -o -name "*.log"    # OR condition
find /path -name "*.txt" -a -size +1M        # AND condition
find /path ! -name "*.tmp"                   # NOT condition
find /path \( -name "*.txt" -o -name "*.log" \) -size +1M

Regular Expressions

find /path -regex ".*\.(txt|log)$"    # Files ending with .txt or .log
find /path -iregex ".*\.JPG$"         # Case-insensitive regex

Actions

Execute Commands

find /path -name "*.txt" -exec cat {} \;              # Execute for each file
find /path -name "*.txt" -exec cat {} +               # Execute with multiple files
find /path -name "*.txt" -execdir cat {} \;           # Execute in file's directory
find /path -name "*.log" -exec rm {} \;               # Delete files
find /path -name "*.txt" -ok rm {} \;                 # Ask before executing

Print Information

find /path -name "*.txt" -print                       # Print filenames (default)
find /path -name "*.txt" -print0                      # Null-separated output
find /path -name "*.txt" -printf "%f\n"               # Print filename only
find /path -name "*.txt" -printf "%s %p\n"            # Print size and path
find /path -name "*.txt" -ls                          # Print detailed info

Printf Format Specifiers

  • %f – filename
  • %p – full path
  • %s – size in bytes
  • %t – modification time
  • %u – username
  • %g – group name
  • %m – permissions in octal

Common Examples

Find and Delete

# Delete all .tmp files
find /tmp -name "*.tmp" -type f -delete

# Delete empty directories
find /path -type d -empty -delete

# Delete files older than 30 days
find /path -type f -mtime +30 -delete

Find Large Files

# Find files larger than 100MB
find /path -type f -size +100M

# Find top 10 largest files
find /path -type f -printf "%s %p\n" | sort -rn | head -10

Find and Copy/Move

# Copy all .txt files to backup directory
find /path -name "*.txt" -exec cp {} /backup/ \;

# Move old log files to archive
find /logs -name "*.log" -mtime +7 -exec mv {} /archive/ \;

Find by Content

# Find files containing specific text
find /path -type f -exec grep -l "search_term" {} \;

# Find files and search content
find /path -name "*.txt" -exec grep "pattern" {} +

System Administration

# Find SUID files
find /path -type f -perm -4000

# Find world-writable files
find /path -type f -perm -002

# Find broken symbolic links
find /path -type l -exec test ! -e {} \; -print

Performance Tips

  1. Specify path: Use specific paths instead of searching from root
  2. Use -name early: Place -name conditions early in the expression
  3. Limit depth: Use -maxdepth to avoid deep recursion
  4. Use -type: Specify file type to reduce search scope
  5. Combine conditions: Use logical operators efficiently

Common Patterns

# Find configuration files
find /etc -name "*.conf" -o -name "*.cfg"

# Find recently modified files
find /path -type f -mtime -1

# Find duplicate files by size
find /path -type f -printf "%s %p\n" | sort -n

# Find files with specific extension, ignoring case
find /path -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png"

# Find and count files
find /path -type f | wc -l

Xargs Integration

# Process files with xargs
find /path -name "*.txt" -print0 | xargs -0 grep "pattern"

# Remove files safely
find /path -name "*.tmp" -print0 | xargs -0 rm

# Change permissions
find /path -type f -print0 | xargs -0 chmod 644

Final Thoughts

The find command is an indispensable tool that becomes more powerful as you combine its various options and integrate it with other Unix tools. Start with simple searches and gradually incorporate more complex expressions as you become comfortable with the syntax.

Remember these key principles when using find:

  • Always test your commands on a small dataset first, especially when using -delete or -exec actions
  • Use specific paths and conditions to improve performance and avoid unintended results
  • Combine find with other tools like xargs, grep, and shell scripts for powerful automation
  • Keep security in mind when searching for files with specific permissions or ownership

With practice, the find command will become second nature, enabling you to efficiently locate, manage, and process files across your Linux system. Whether you’re cleaning up disk space, auditing security, or automating maintenance tasks, find provides the flexibility and power to handle virtually any file-related operation.

Related Post

Docker & Docker Compose Cheatsheet

Docker & Docker Compose Cheatsheet

Introduction to Docker In the world of software development, the phrase "it works on my machine" used to be a common, frustrating refrain. This is precisely the problem Docker set out to solve. Introduced in 2013, Docker has revolutionized how applications are built,...

read more