Master the Linux find Command: 7 Powerful Ways to Locate and Manage Files
This article explains the essential Linux find command, walks through a common interview problem of deleting log files older than a year, and demonstrates seven practical usages—including searching by name, type, timestamps, size, permissions, ownership, and executing actions on matched files—complete with clear code examples.
The find command is a must‑know tool for Linux system administrators, allowing powerful file searches and actions.
Interview Question
How to delete log files in a logs directory that have not been accessed for over a year?
Solution:
find . -type f -atime +365 -exec rm -rf {} \;1. Search by name or regular expression
Find files matching a specific name: find . -name test.txt Find all PDF books using a pattern: find ./yang/books -name "*.pdf" Specify file type for clarity:
find ./yang/books -type f -name "*.pdf"2. Search different file types
Search directories: find . -type d -name "yang*" Search symbolic links:
find . -type l -name "yang*"3. Search by timestamps
Linux tracks three timestamps:
atime – last access time
mtime – last modification time
ctime – last status change time
Find files accessed more than a year ago: find . -type f -atime +365 Find files whose modification time is exactly 5 days ago (no + sign): find . -type f -mtime 5 Find files with ctime between 5 and 10 days ago:
find . -type f -ctime +5 -ctime -104. Search by size
Use -size with units (b, c, w, k, M, G). Example: files between 10 MB and 1 GB:
find . -type f -size +10M -size -1G5. Search by permissions
Find files with specific permission bits, e.g., 777:
find . -type f -perm 7776. Search by ownership
Find files owned by a particular user:
find -type f -user yang7. Execute a command on found files
Use -exec to run a command for each matched file. The command must end with an escaped semicolon ( \;). find . -type f -atime +5 -exec ls {} \; Without the placeholder {}, the command would run on all files, not just the matches.
Summary
After learning these seven practical uses of find, the original interview question becomes straightforward: simply run the provided command to delete log files older than a year.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.
