Comprehensive Guide to Using the Linux find Command for File Search and Management
This article provides a detailed tutorial on the Linux find command, covering how to locate files by name, type, size, time, and permissions, combine conditions, execute actions, and compare it with the locate utility, offering practical examples for system administrators.
The find command is one of the most powerful tools in Linux for searching files based on various attributes such as name, size, permissions, owner, modification time, and access time.
Case studies demonstrate practical usage:
1. Search by file name
$ find . -name "*.go"
$ find /etc -name "[A-Z]*.txt" -print
$ find . -name "out*" -prune -o -name "*.txt" -print
$ find . -path "./git" -prune -o -name "*.txt" -print
$ find . -num 1389562. Search by file type
$ find . -type l -print
$ find . -type f -name "*.log"3. Search by file size
$ find . -size -64k -print
$ find . -size +200M -type f -print4. Search by time
$ find . -mtime -2 -type f -print
$ find . -mtime +2 -type f -print
$ find . -atime -1 -type f -print
$ find . -ctime -1 -type f -print
$ find . -newer "chopin.txt" -type f -print
$ find . ! -newer "chopin.txt" -type f -print5. Search by permissions
$ find . -type f -perm 644
$ find /etc -type f -perm /222
$ find /etc -perm -111 -ls6. Combine conditions
$ find . -type f -a -user chopin -print
$ find . -size +2M -o -mtime +2 -print
$ find . -not -type f
$ find . ! -empty7. Action options
After locating files, find can perform actions such as:
-print (default, prints the path)
-ls (outputs in ls long format)
-delete (removes matched files)
-exec (passes files to any command, e.g., ls -lh {} )
-ok (like -exec but asks for confirmation)
$ find . -name "*.txt" -exec ls -lh {} \;
$ find . -size +100M -delete8. locate utility
For faster searches, locate uses an indexed database:
$ locate file.txt
$ locate /etc/httpdRemember to run updatedb to refresh the index for newly created files.
Warning: Scanning large directories (especially / ) can heavily load the CPU; avoid running extensive find queries on production systems without assessing impact.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.