LFCS Phase 1 Part 28: Finding Files with the find Command

Master the powerful find command to locate files by name, type, size, time, and permissions. Essential file search skills for LFCS certification and system administration.

10 min read

You know how to create, move, and delete files. But what about finding them? In a Linux system with millions of files across complex directory trees, the find command is your search engine. It's one of the most powerful and essential commands for system administrators.

Understanding find is critical for the LFCS exam. You'll use it constantly for locating configuration files, finding large files consuming disk space, searching by modification time, and identifying security issues like files with incorrect permissions.

๐Ÿ’ก

๐ŸŽฏ What You'll Learn:

  • Understanding find syntax and how it works
  • Finding files by name with -name
  • Finding by file type with -type
  • Finding by size with -size
  • Finding by modification time with -mtime
  • Finding by permissions with -perm
  • Suppressing permission errors with 2>/dev/null
  • Combining find with other commands (-exec)
  • Real-world file search scenarios

Series: LFCS Certification - Phase 1 (Post 28 of 52)


Understanding the find Command

The find command recursively searches directory trees for files matching specified criteria.

Basic Syntax

find [PATH] [OPTIONS] [TESTS] [ACTIONS]

Components:

  • PATH: Where to search (default: current directory)
  • OPTIONS: How to search
  • TESTS: What to match (name, size, type, etc.)
  • ACTIONS: What to do with matches (print, delete, execute)

Simplest Example

$ find /etc -name "hosts"
/etc/hosts
/etc/avahi/hosts

Finding by Name

Exact Name Match

$ find / -name "hosts" 2>/dev/null
/tmp/hosts
/tmp/files/photos/avahi/hosts
/etc/hosts
/etc/avahi/hosts

What happened:

  • Searched entire filesystem (/)
  • Found files named exactly "hosts"
  • 2>/dev/null suppressed permission errors

Wildcard Patterns

# Files starting with "hosts"
$ find / -name "hosts*" 2>/dev/null
/etc/hosts
/etc/hosts.equiv
/usr/share/man/man5/hosts.5.gz

# Files containing "hosts"
$ find / -name "*hosts*" 2>/dev/null
/etc/hosts
/.ssh/known_hosts
/usr/bin/sss_ssh_knownhostsproxy
$ find /etc -iname "HOSTS"
/etc/hosts
/etc/avahi/hosts

Suppressing Errors with 2>/dev/null

The Permission Problem

$ find / -name "hosts"
find: '/boot/efi/EFI/centos': Permission denied
find: '/boot/grub2': Permission denied
find: '/boot/loader/entries': Permission denied
# ... many permission errors ...
/etc/hosts

Why? As a regular user, you can't read many system directories.

The Solution

$ find / -name "hosts" 2>/dev/null
/etc/hosts
/etc/avahi/hosts

What 2>/dev/null does:

  • 2> redirects stderr (error messages)
  • /dev/null is a "black hole" (discards everything)
  • Result: Clean output with only matches
โœ…

๐Ÿ’ก Pro Tip: Always use 2>/dev/null when searching system-wide as a regular user. It makes output readable and prevents clutter.


Finding by File Type

The -type Option

# Find regular files
$ find /tmp -type f

# Find directories
$ find /tmp -type d

# Find symbolic links
$ find /etc -type l
/etc/bin -> usr/bin
/etc/lib -> usr/lib

Common types:

  • f = regular file
  • d = directory
  • l = symbolic link
  • b = block device
  • c = character device
  • s = socket
  • p = named pipe

Finding by Size

The -size Option

# Find files larger than 2GB
$ sudo find / -size +2G 2>/dev/null
/proc/kcore
/var/log/journal/huge.log

# Find files exactly 1024 bytes
$ find /tmp -size 1024c

# Find files smaller than 10KB
$ find /home -size -10k

Size units:

  • c = bytes
  • k = kilobytes
  • M = megabytes
  • G = gigabytes

Operators:

  • + = greater than
  • - = less than
  • (none) = exactly

Finding by Time

Modification Time (-mtime)

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

# Files modified more than 7 days ago
$ find /home -mtime +7

# Files modified exactly 7 days ago
$ find /home -mtime 7

Time options:

  • -mtime = modification time (days)
  • -atime = access time (days)
  • -ctime = status change time (days)
  • -mmin = modification time (minutes)

Real-World Example

# Find and delete logs older than 30 days
$ find /var/log -name "*.log" -mtime +30 -delete

Finding by Permissions

# Find setuid files (security check)
$ sudo find / -perm /4000 2>/dev/null
/usr/bin/sudo
/usr/bin/passwd
/usr/bin/chsh

Permission options:

  • -perm 644 = exactly 644
  • -perm /644 = any of these bits set
  • -perm -644 = all these bits set

Combining Conditions

AND Logic (Default)

# Files named *.conf AND larger than 1MB
$ find /etc -name "*.conf" -size +1M

OR Logic (-o)

# Files ending in .txt OR .log
$ find /home -name "*.txt" -o -name "*.log"

NOT Logic (!)

# All files EXCEPT .txt
$ find /home ! -name "*.txt"

Actions with -exec

Execute Command on Each Match

# Delete all .tmp files
$ find /tmp -name "*.tmp" -exec rm {} \;

# Show detailed info for each match
$ find /etc -name "*.conf" -exec ls -lh {} \;

Syntax:

  • {} = placeholder for found file
  • \; = end of command (must be escaped)

Confirm Before Action

# Interactive deletion
$ find /tmp -name "*.bak" -ok rm {} \;
# Prompts: rm ... /tmp/file.bak?

Real-World Use Cases

1. Find Large Files

# Find files larger than 100MB
$ find /home -type f -size +100M -exec ls -lh {} \; 2>/dev/null

2. Find Old Log Files

# Find logs older than 90 days
$ find /var/log -name "*.log" -mtime +90

3. Find Empty Files/Directories

# Empty files
$ find /tmp -type f -empty

# Empty directories
$ find /tmp -type d -empty

4. Find by Owner

# Find all files owned by user
$ find /home -user centos9

# Find files owned by group
$ find /var -group apache

5. Security Audit

# Find world-writable files
$ find / -type f -perm -002 2>/dev/null

# Find files without owner
$ find / -nouser 2>/dev/null

๐Ÿงช Practice Labs

Lab 1: Basic Name Search (Beginner)

Task: Find all files named "passwd" on the system.

Show Solution
$ find / -name "passwd" 2>/dev/null
/etc/passwd
/etc/pam.d/passwd
/usr/bin/passwd

Key concept: Use 2>/dev/null to suppress permission errors.


Lab 2: Wildcard Search (Beginner)

Task: Find all files in /etc that start with "host".

Show Solution
$ find /etc -name "host*"
/etc/hosts
/etc/hostname
/etc/host.conf

Key concept: Wildcards work with -name option.


Lab 3: Find by Type (Beginner)

Task: Find all symbolic links in /etc.

Show Solution
$ find /etc -type l
/etc/bin -> usr/bin
/etc/lib -> usr/lib
/etc/lib64 -> usr/lib64

Key concept: -type l finds symbolic links.


Lab 4: Find Large Files (Intermediate)

Task: Find files in /var larger than 50MB.

Show Solution
$ sudo find /var -type f -size +50M
/var/log/journal/system.journal
/var/cache/packagekit/metadata.db

Key concept: Use + for "greater than" with -size.


Lab 5: Find by Time (Intermediate)

Task: Find files in /tmp modified in the last hour.

Show Solution
$ find /tmp -type f -mmin -60
/tmp/recent_file.txt
/tmp/session.tmp

Key concept: -mmin uses minutes, - means "less than".


Lab 6: Find and Delete (Intermediate)

Task: Find and delete all .bak files in your home directory.

Show Solution
# First, find them
$ find ~ -name "*.bak"

# Then delete
$ find ~ -name "*.bak" -delete

# Or with confirmation
$ find ~ -name "*.bak" -ok rm {} \;

Key concept: Always test with plain find first, then add -delete.


Lab 7: Find Empty Files (Intermediate)

Task: Find all empty files in /tmp and remove them.

Show Solution
$ find /tmp -type f -empty
/tmp/empty1.txt
/tmp/empty2.log

$ find /tmp -type f -empty -delete

Key concept: -empty finds files with zero size.


Lab 8: Combining Conditions (Advanced)

Task: Find .log files in /var/log that are larger than 10MB and older than 30 days.

Show Solution
$ sudo find /var/log -name "*.log" -size +10M -mtime +30
/var/log/old-huge.log

Key concept: Multiple conditions use AND logic by default.


Lab 9: Find by Permissions (Advanced)

Task: Find all setuid binaries (potential security concern).

Show Solution
$ sudo find / -type f -perm /4000 2>/dev/null
/usr/bin/sudo
/usr/bin/passwd
/usr/bin/su

Key concept: Setuid files run with owner's permissions.


Lab 10: Find and Execute (Advanced)

Task: Find all .conf files in /etc and count lines in each.

Show Solution
$ find /etc -name "*.conf" -exec wc -l {} \;
127 /etc/ssh/sshd_config
45 /etc/logrotate.conf

Key concept: -exec runs command on each match.


๐Ÿ“ Command Cheat Sheet

# By name
find / -name "filename" 2>/dev/null
find / -name "*.txt" 2>/dev/null
find / -iname "FILENAME" 2>/dev/null

# By type
find /etc -type f          # Regular files
find /etc -type d          # Directories
find /etc -type l          # Symbolic links

# By size
find /var -size +100M      # Larger than 100MB
find /tmp -size -1k        # Smaller than 1KB
find /home -size 1M        # Exactly 1MB

# By time
find /var/log -mtime +30   # Modified >30 days ago
find /tmp -mmin -60        # Modified <60 min ago
find /home -atime +7       # Accessed >7 days ago

# By permissions
find / -perm 777 2>/dev/null      # Exactly 777
find / -perm /4000 2>/dev/null    # Setuid files

# By owner
find /home -user username
find /var -group groupname

# Combining
find /var/log -name "*.log" -size +10M -mtime +30

# Actions
find /tmp -name "*.tmp" -delete
find /etc -name "*.conf" -exec ls -l {} \;
find /tmp -name "*.bak" -ok rm {} \;

๐ŸŽฏ Key Takeaways

โœ…

Master These Concepts:

  1. find searches recursively from the starting path
  2. 2>/dev/null suppresses permission errors
  3. -name finds by filename (exact or wildcard)
  4. -type finds by file type (f/d/l)
  5. -size finds by file size (+/- operators)
  6. -mtime finds by modification time (days)
  7. -perm finds by permissions
  8. Multiple tests = AND logic by default
  9. -exec runs commands on matches
  10. Test before using -delete!

Quick Decision Guide:

  • Know exact name โ†’ -name "filename"
  • Know pattern โ†’ -name "*.txt"
  • Need by size โ†’ -size +100M
  • Need by time โ†’ -mtime +30
  • Need by type โ†’ -type f/d/l

๐Ÿš€ What's Next?

Coming Up Next:

  • Post 29: Viewing File Contents (cat, less, more, head, tail)
  • Post 30: Introduction to Text Editors (vi/vim basics)

โœ…

๐ŸŽ‰ Congratulations! You've mastered the find command! You can now:

  • Search the entire filesystem efficiently
  • Find files by name, type, size, time, and permissions
  • Suppress errors for clean output
  • Combine multiple search criteria
  • Execute actions on found files

The find command is indispensable for system administration. Practice these labs until searching for files becomes second nature! ๐Ÿš€

Owais

Written by Owais

I'm an AIOps Engineer with a passion for AI, Operating Systems, Cloud, and Securityโ€”sharing insights that matter in today's tech world.

I completed the UK's Eduqual Level 6 Diploma in AIOps from Al Nafi International College, a globally recognized program that's changing careers worldwide. This diploma is:

  • โœ… Available online in 17+ languages
  • โœ… Includes free student visa guidance for Master's programs in Computer Science fields across the UK, USA, Canada, and more
  • โœ… Comes with job placement support and a 90-day success plan once you land a role
  • โœ… Offers a 1-year internship experience letter while you studyโ€”all with no hidden costs

It's not just a diplomaโ€”it's a career accelerator.

๐Ÿ‘‰ Start your journey today with a 7-day free trial

Related Articles

Continue exploring with these handpicked articles that complement what you just read

21 min read

LFCS Part 38: Text Transformation with tr

Master the tr command for character-by-character text transformation. Learn case conversion, character deletion, squeezing repeats, and complement sets for efficient text processing.

#Linux#LFCS+6 more
Read article

More Reading

One more article you might find interesting