File Search

Overview

In Linux systems, we often need to find files and directories. This chapter introduces several commonly used file search tools: find, locate, which, whereis, etc.

find Command

find is the most powerful file search command that can search for files in real-time based on various conditions.

Basic Syntax

find [search_path] [conditions] [actions]

Search by Name

# Search by filename
$ find /home -name "file.txt"

# Case-insensitive
$ find /home -iname "file.txt"

# Use wildcards
$ find /home -name "*.txt"
$ find /home -name "file?.txt"

# Search for specific directory names
$ find /home -name "project*" -type d

Search by Type

# Search only for files
$ find /home -type f

# Search only for directories
$ find /home -type d

# Search only for symbolic links
$ find /home -type l

# Search for empty files
$ find /home -type f -empty

# Search for empty directories
$ find /home -type d -empty

File Types

TypeDescription
fRegular file
dDirectory
lSymbolic link
bBlock device
cCharacter device
sSocket
pPipe

Search by Size

# Files larger than 100MB
$ find /home -size +100M

# Files smaller than 1KB
$ find /home -size -1k

# Files exactly 100 bytes
$ find /home -size 100c

# Files between 10MB and 100MB
$ find /home -size +10M -size -100M

Size Units

UnitDescription
cBytes
kKB
MMB
GGB

Search by Time

# Files modified in the last 7 days
$ find /home -mtime -7

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

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

# Files modified in the last 60 minutes
$ find /home -mmin -60

# Files accessed in the last 24 hours
$ find /home -atime -1

# Files with status changed in the last 7 days
$ find /home -ctime -7

Time Types

OptionDescription
-mtimeModification time (content)
-atimeAccess time
-ctimeStatus change time (permissions, ownership)
-mminModification time (minutes)
-aminAccess time (minutes)
-cminStatus change time (minutes)

Search by Permissions

# Find files with permissions 644
$ find /home -perm 644

# Find files with at least specified permissions
$ find /home -perm -644

# Find files with any of the specified permissions
$ find /home -perm /644

# Find SUID files
$ find /usr -perm -4000

# Find writable files
$ find /home -perm -222

Search by Ownership

# Search by user
$ find /home -user maxwell

# Search by group
$ find /home -group developers

# Find files without an owner
$ find /home -nouser

# Find files without a group
$ find /home -nogroup

Combine Conditions

# AND (default)
$ find /home -name "*.txt" -size +1M

# OR
$ find /home -name "*.txt" -o -name "*.md"

# NOT
$ find /home ! -name "*.txt"

# Use parentheses for grouping (needs escaping)
$ find /home \( -name "*.txt" -o -name "*.md" \) -size +1M

Limit Search Depth

# Search only in current directory
$ find /home -maxdepth 1 -name "*.txt"

# Limit maximum depth to 2 levels
$ find /home -maxdepth 2 -name "*.txt"

# At least depth 2
$ find /home -mindepth 2 -name "*.txt"

Execute Actions on Results

-exec execute command

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

# Modify permissions
$ find /var/www -type f -exec chmod 644 {} \;

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

# Show detailed information
$ find /home -name "*.txt" -exec ls -l {} \;

-exec use + for efficiency

# Process multiple files at once (more efficient)
$ find /home -name "*.txt" -exec ls -l {} +

# Batch delete
$ find /tmp -name "*.tmp" -exec rm {} +

-ok interactive confirmation

# Confirm before deletion
$ find /tmp -name "*.tmp" -ok rm {} \;

Use xargs

# Basic usage
$ find /home -name "*.txt" | xargs ls -l

# Handle filenames with spaces
$ find /home -name "*.txt" -print0 | xargs -0 ls -l

# Limit number of files processed each time
$ find /home -name "*.txt" | xargs -n 10 ls -l

Practical Examples

# Find and delete logs 7 days old
$ find /var/log -name "*.log" -mtime +7 -delete

# Find large files
$ find / -type f -size +100M 2>/dev/null

# Find recently modified configuration files
$ find /etc -name "*.conf" -mtime -1

# Find and compress old files
$ find /backup -mtime +30 -exec gzip {} \;

# Count files
$ find /home -type f | wc -l

# Find duplicate filenames
$ find /home -type f -name "*.txt" -printf "%f\n" | sort | uniq -d

locate Command

locate uses a pre-built database for fast file searching.

Installation

# Debian/Ubuntu
$ sudo apt install mlocate

# Fedora
$ sudo dnf install mlocate

Basic Usage

# Search for files
$ locate file.txt

# Case-insensitive
$ locate -i FILE.TXT

# Limit result count
$ locate -n 10 "*.txt"

# Show only existing files
$ locate -e file.txt

# Show match count
$ locate -c "*.txt"

Update Database

# Manually update database
$ sudo updatedb

find vs locate

Featurefindlocate
SpeedSlower (real-time search)Very fast (database query)
Real-timeReal-timeNeeds database update
FeaturesRich (condition filtering)Simple (name matching)
Resource usageHigherVery low

which Command

Find location of executable commands.

# Find command location
$ which ls
/usr/bin/ls

$ which python
/usr/bin/python

# Find all matches
$ which -a python
/usr/bin/python
/usr/local/bin/python

whereis Command

Find command's binary, source, and manual pages.

$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

$ whereis python
python: /usr/bin/python /usr/lib/python3.10 /usr/share/man/man1/python.1.gz

# Find only binary
$ whereis -b ls

# Find only manual
$ whereis -m ls

type Command

Display command type and location.

$ type ls
ls is aliased to `ls --color=auto'

$ type cd
cd is a shell builtin

$ type python
python is /usr/bin/python

# Show all locations
$ type -a ls

grep Search File Content

Although grep is mainly for searching file content, it can also be used for file search combined with other commands.

# Search content in files
$ grep "pattern" file.txt

# Recursive search in directories
$ grep -r "pattern" /path/to/dir/

# Show only filenames
$ grep -l "pattern" *.txt

# Show line numbers
$ grep -n "pattern" file.txt

# Case-insensitive
$ grep -i "pattern" file.txt

# Reverse match (not containing)
$ grep -v "pattern" file.txt

Combine with find

# Search in .txt files
$ find /home -name "*.txt" -exec grep -l "keyword" {} \;

# Use xargs
$ find /home -name "*.txt" | xargs grep "keyword"

fd - Modern find Alternative

fd is a modern alternative to find, faster and more intuitive.

Installation

# Debian/Ubuntu
$ sudo apt install fd-find
# Command name is fdfind, can create alias
$ alias fd=fdfind

# Fedora
$ sudo dnf install fd-find

# Arch Linux
$ sudo pacman -S fd

Basic Usage

# Simple search (recursive by default)
$ fd pattern

# Specify directory
$ fd pattern /path/to/dir

# Search specific extensions
$ fd -e txt

# Search specific type
$ fd -t f pattern    # Files
$ fd -t d pattern    # Directories

# Execute command
$ fd -e txt -x rm {}

ripgrep is one of the fastest content search tools.

Installation

# Debian/Ubuntu
$ sudo apt install ripgrep

# Fedora
$ sudo dnf install ripgrep

# Arch Linux
$ sudo pacman -S ripgrep

Basic Usage

# Recursive search (default)
$ rg "pattern"

# Search in specific directory
$ rg "pattern" /path/to/dir

# Specify file types
$ rg -t py "pattern"    # Python files
$ rg -t js "pattern"    # JavaScript files

# Show only filenames
$ rg -l "pattern"

# Show context
$ rg -C 3 "pattern"    # 3 lines before and after

# Ignore case
$ rg -i "pattern"

Practical Search Tips

Find Largest Files

# Top 10 largest files
$ find / -type f -exec du -h {} + 2>/dev/null | sort -rh | head -10

# Or use du
$ du -ah /home | sort -rh | head -20

Find Recently Modified Files

# Files modified in last 24 hours
$ find /home -type f -mtime -1

# Sort by modification time
$ find /home -type f -printf "%T@ %p\n" | sort -n | tail -10

Find Duplicate Files

# Use fdupes
$ sudo apt install fdupes
$ fdupes -r /home
$ find /home -xtype l

Find Files in Specific Size Range

# Files between 1MB and 10MB
$ find /home -type f -size +1M -size -10M

Exclude Directories

# Exclude .git directory
$ find /home -name ".git" -prune -o -name "*.txt" -print

# Exclude multiple directories
$ find /home \( -name node_modules -o -name .git \) -prune -o -name "*.js" -print

Summary

This chapter introduced Linux file search tools:

  • find: Most powerful, supports complex condition search
  • locate: Fast search, uses database
  • which/whereis/type: Find command locations
  • grep: Search file content
  • fd: Modern alternative to find
  • ripgrep: Fast content search

In daily use:

  • Simple filename search with locate
  • Complex condition search with find
  • Content search with grep or ripgrep
  • Find commands with which

Mastering these tools will allow you to quickly find the files you need in a vast filesystem.


Previous chapter: File Permissions

Next chapter: Shell Introduction