find
MASTERY

// Locate. Filter. Execute.

KNOW WHERE YOUR FILES ARE.

On a Linux system with millions of files, find is your GPS. It locates anything instantly by name, type, size, date, or any criteria you specify.

SEARCH LIKE A PRO.

Whether finding a config file buried deep in /etc or locating large log files eating up disk space—find handles it all with ease.

BEGIN YOUR JOURNEY

// Your Training Path

Click a lesson to begin

LESSON 01

Introduction to find

What is find? Basic syntax and usage.

Beginner
LESSON 02

Search by Name

Find files using -name and -iname.

Beginner
LESSON 03

Search by Type

Find files, directories, symlinks, and more.

Beginner
LESSON 04

Search by Size

Find files by size with -size option.

Beginner
LESSON 05

Search by Time

Find by modification, access, change time.

Beginner
LESSON 06

Search by Permissions

Find files by permission modes.

Intermediate
LESSON 07

Search by Owner

Find by user and group ownership.

Intermediate
LESSON 08

Combining Tests

Use -and, -or, -not to combine conditions.

Intermediate
LESSON 09

Execute on Results

-exec and -ok to run commands on results.

Intermediate
LESSON 10

Delete Found Files

Safely delete files matching criteria.

Advanced
LESSON 11

find in Scripts

Use find in bash for automation.

Advanced
LESSON 12

xargs Power

Chain find results with xargs.

Advanced

// Lesson 01: Introduction to find

×

What is find?

The find command searches for files and directories in a directory hierarchy based on criteria you specify. It's one of the most powerful and essential commands on Linux.

Basic Syntax

find [path] [options] [expression]

Simple Examples

# Find all files in current directory
find .

# Find all files in /home
find /home

# Find specific file by name
find / -name "myfile.txt"

Why find Matters

  • Precision: Search by any criteria imaginable
  • Speed: Real-time filesystem search
  • Power: Execute commands on results
  • Ubiquity: Available on every Linux system

Quiz

1. What does the find command do?

Show Answers
  1. Searches for files and directories based on criteria

// Lesson 02: Search by Name

×

Find by Name

# Find file named exactly "error.log"
find /var/log -name "error.log"

# Find all .conf files
find /etc -name "*.conf"

# Find file regardless of case
find /home -iname "readme.txt"

Wildcards

*  Matches any characters
?  Matches any single character

# Find files starting with "backup"
find / -name "backup*"

# Find files ending with ".log"
find /var/log -name "*.log"

# Find files with exactly 4 character name
find /home -name "????.txt"

Quiz

1. Which option makes find case-insensitive?

Show Answers
  1. -iname (instead of -name)

// Lesson 03: Search by Type

×

File Types

f  Regular file
d  Directory
l  Symbolic link
c  Character device
b  Block device
p  Named pipe (FIFO)
s  Socket

Examples

# Find all directories
find /home -type d

# Find all regular files
find /var/log -type f

# Find all symbolic links
find /usr/local -type l

# Find all block devices
find /dev -type b

Quiz

1. What type denotes a directory?

Show Answers
  1. d (use -type d)

// Lesson 04: Search by Size

×

Size Units

c  Bytes
k  Kilobytes (1024 bytes)
M  Megabytes
G  Gigabytes
b  512-byte blocks (default)

Examples

# Files exactly 100MB
find / -size 100M

# Files larger than 1GB
find / -size +1G

# Files smaller than 10KB
find /tmp -size -10k

# Files between 100MB and 500MB
find /var -size +100M -size -500M

Find Large Files

# Find files larger than 100MB in /var
find /var -type f -size +100M -exec ls -lh {} \;

Quiz

1. How do you find files larger than 1GB?

Show Answers
  1. find -size +1G

// Lesson 05: Search by Time

×

Time Options

-mtime  Modified time (content change)
-atime  Access time (last read)
-ctime  Change time (metadata/sermissions change)
-mmin   Modified time in minutes
-amin   Access time in minutes
-cmin   Change time in minutes

Examples

# Files modified in last 24 hours
find /var/log -mtime -1

# Files accessed more than 30 days ago
find /home -atime +30

# Files modified in last hour
find /var/log -mmin -60

# Files changed in last 7 days
find /etc -ctime -7

Practical Use Case

# Find recently modified config files
find /etc -name "*.conf" -mtime -7

# Find all files accessed today
find /home -atime 0

Quiz

1. Which time option checks when file content was modified?

Show Answers
  1. -mtime (or -mmin for minutes)

// Lesson 06: Search by Permissions

×

Permission Options

-perm  Exact permission match
-perm -  At least these permissions (AND)
-perm /  Any of these permissions (OR)

Examples

# Files with exactly 644 permissions
find /home -perm 644

# Files readable by everyone
find /home -perm -444

# Files writable by owner
find /home -perm -200

# Files with execute permission
find /home -perm -111

# SUID files (security risk!)
find / -perm -4000 2>/dev/null

Quiz

1. How do you find SUID files?

Show Answers
  1. find -perm -4000

// Lesson 07: Search by Owner

×

Owner Options

-user USERNAME   Files owned by USERNAME
-group GROUPNAME Files owned by GROUPNAME
-nouser          Files with no valid owner
-nogroup         Files with no valid group

Examples

# Files owned by root
find / -user root

# Files owned by www-data
find /var/www -user www-data

# Files owned by a specific group
find /home -group developers

# Files with no valid owner (orphaned)
find /home -nouser

Combined Search

# Large files owned by root
find / -user root -size +100M

Quiz

1. How do you find files with no valid owner?

Show Answers
  1. -nouser

// Lesson 08: Combining Tests

×

Logical Operators

-a  AND (default between conditions)
-o  OR
-not or !  NOT

Examples

# Files owned by www-data AND .log files
find /var/www -user www-data -name "*.log"

# .conf OR .ini files
find /etc -name "*.conf" -o -name "*.ini"

# Files NOT modified in last 30 days
find /var/log -not -mtime -30

# Files that are 0 bytes OR .tmp files
find /tmp -size 0 -o -name "*.tmp"

Parenthesis for Grouping

# Must escape parenthesis \( \)
find /var \( -name "*.log" -o -name "*.tmp" \) -mtime +7

Quiz

1. What operator combines conditions with OR?

Show Answers
  1. -o (or -or)

// Lesson 09: Execute on Results

×

The -exec Option

# Execute command on each result
find /path -name "*.log" -exec rm {} \;

# {} is replaced with the filename
# \; terminates the command

Examples

# List details of all .conf files
find /etc -name "*.conf" -exec ls -lh {} \;

# Change permissions on results
find /var/www -name "*.php" -exec chmod 644 {} \;

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

The -ok Option

# Like -exec but asks for confirmation
find / -name "core" -ok rm {} \;

# Answer y for yes, n for no

Quiz

1. What does {} represent in -exec?

Show Answers
  1. The current filename being processed

// Lesson 10: Delete Found Files

×

Delete Safely

# First test with -ok (asks confirmation)
find /tmp -name "*.tmp" -ok rm {} \;

# Then use -delete directly
find /tmp -name "*.tmp" -delete

Practical Examples

# Delete core dumps older than 7 days
find / -name "core.*" -mtime +7 -delete

# Delete empty files
find /tmp -type f -empty -delete

# Delete .pyc files
find . -name "*.pyc" -delete

# Delete files owned by deleted user
find /home -nouser -delete

WARNING

Always test with ls first before deleting:

# Test first (list files)
find /tmp -name "*.tmp" -ls

# Then delete when confirmed
find /tmp -name "*.tmp" -delete

Quiz

1. What should you do before deleting files?

Show Answers
  1. Test first with ls or -ok

// Lesson 11: find in Scripts

×

Cleanup Script Example

#!/bin/bash
# Clean old log files

LOGDIR="/var/log/myapp"
DAYS=30

# Find and delete old .log files
find "$LOGDIR" -name "*.log" -mtime +$DAYS -type f -delete

echo "Cleaned log files older than $DAYS days"

Backup Script

#!/bin/bash
# Backup all .sql files

find /var/www -name "*.sql" -type f | while read -r file; do
    backup_file="${file}.bak.$(date +%Y%m%d)"
    cp "$file" "$backup_file"
    echo "Backed up: $file"
done

Disk Usage Report

#!/bin/bash
# Find largest directories

du -sh /var/* 2>/dev/null | sort -rh | head -10

Quiz

1. What does the -delete option do?

Show Answers
  1. Deletes files that match the find criteria

// Lesson 12: xargs Power

×

What is xargs?

xargs takes input from stdin and converts it into arguments for a command. Combined with find, it efficiently processes many files.

Basic Usage

# Instead of -exec which runs per file:
find . -name "*.txt" -exec rm {} \;

# xargs can batch process:
find . -name "*.txt" | xargs rm

Advantages of xargs

# More efficient (fewer process spawns)
# Better handling of filenames with spaces
# Can control number of args per command

# Limit to 10 files per command
find / -name "*.log" | xargs -n 10 rm

# Use null delimiter for safe filenames
find . -name "*.txt" -print0 | xargs -0 rm

# Show commands without executing
find . -name "*.txt" | xargs -p rm

Congratulations!

You've mastered find! You now understand:

  • Search by name, type, size, time
  • Search by permissions and ownership
  • Combining conditions with AND, OR, NOT
  • Executing commands on results
  • Safe deletion of files
  • Using find in bash scripts
  • Power of xargs for batch processing

// Why find

find is the most powerful file search tool on Linux. It can locate any file based on any criteria you can imagine.

Master find and you'll never waste time hunting for files again. Combined with -exec and xargs, it becomes an automation powerhouse.

Locate. Filter. Execute. Automate.

// Tools & References

find Man Page

Official documentation

man find

find Tutorial

Linuxize find guide

Linuxize

xargs Man Page

Official documentation

man xargs

Find Cheatsheet

Quick reference

QuickRef