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
logsdirectory that have not been accessed for over a year?
Solution:
<code>find . -type f -atime +365 -exec rm -rf {} \;</code>1. Search by name or regular expression
Find files matching a specific name:
<code>find . -name test.txt</code>Find all PDF books using a pattern:
<code>find ./yang/books -name "*.pdf"</code>Specify file type for clarity:
<code>find ./yang/books -type f -name "*.pdf"</code>2. Search different file types
Search directories:
<code>find . -type d -name "yang*"</code>Search symbolic links:
<code>find . -type l -name "yang*"</code>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:
<code>find . -type f -atime +365</code>Find files whose modification time is exactly 5 days ago (no
+sign):
<code>find . -type f -mtime 5</code>Find files with ctime between 5 and 10 days ago:
<code>find . -type f -ctime +5 -ctime -10</code>4. Search by size
Use
-sizewith units (b, c, w, k, M, G). Example: files between 10 MB and 1 GB:
<code>find . -type f -size +10M -size -1G</code>5. Search by permissions
Find files with specific permission bits, e.g., 777:
<code>find . -type f -perm 777</code>6. Search by ownership
Find files owned by a particular user:
<code>find -type f -user yang</code>7. Execute a command on found files
Use
-execto run a command for each matched file. The command must end with an escaped semicolon (
\;).
<code>find . -type f -atime +5 -exec ls {} \;</code>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.
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.