Master Linux Find: 8 Powerful Ways to Locate and Manage Files
This article explains why the Linux find command is essential for operations, walks through an interview question about deleting year‑old logs, and then details eight practical find usages—including name patterns, file types, timestamps, sizes, permissions, ownership, executing actions, and directory searches—complete with example commands.
It can be said with confidence that the find command is one of the essential operations every Linux operator must know.
If your Linux server has a directory named .logs , how would you delete log files whose last access time is more than one year ago?
The answer is to change to the directory and run:
<code>find . -type f -atime +365 -exec rm -rf {} \;</code>The article then introduces eight practical uses of the
findcommand.
1. Find files by name or regular expression
Search by a specific name:
<code>find . -name test.txt</code>Search for all PDF books using a pattern:
<code>find ./yang/books -name "*.pdf"</code>Specify the file type for clarity:
<code>find ./yang/books -type f -name "*.pdf"</code>2. Find different types of files
Search for directories:
<code>find . -type d -name "yang*"</code>Search for symbolic links:
<code>find . -type l -name "yang*"</code>3. Find files by timestamps
Linux tracks three timestamps:
Access time (
atime): last time the file was read.
Modification time (
mtime): last time the file content changed.
Change time (
ctime): last time metadata (owner, permissions, etc.) changed.
To find files with an access time older than a year:
<code>find . -type f -atime +365</code>To find files whose modification time is exactly five days ago (no
+sign):
<code>find . -type f -mtime 5</code>To find files with a change time between 5 and 10 days ago:
<code>find . -type f -ctime +5 -ctime -10</code>4. Find files by size
The
-sizeoption accepts the following units:
b – 512‑byte blocks (default)
c – bytes
w – two‑byte words
k – kilobytes
M – megabytes
G – gigabytes
Example: find files between 10 MB and 1 GB:
<code>find . -type f -size +10M -size -1G</code>5. Find files by permissions
Use
-permto match specific permission bits, e.g., files with 777 permissions:
<code>find . -type f -perm 777</code>6. Find files by ownership
Search for files owned by a specific user with
-user:
<code>find -type f -user yang</code>7. Execute a command on found files
After locating files, you can run a command on each of them using
-exec. For the interview question:
<code>find . -type f -atime +365 -exec rm -rf {} \;</code>Note that the placeholder
{}represents each found file; omitting it would apply the command to all files.
8. Search by directory name
Find directories by name (case‑insensitive):
<code>find /path/to/search -type d -iname "directory_name"</code>Find the most recently modified directory:
<code>find /path/to/search -type d -printf '%T+ %p\n' | sort -n | tail -1</code>Summary
Besides
find, Linux also offers commands like
grep,
locate,
which, and
fdfor file discovery.
After reviewing the eight usages above, the interview question becomes straightforward; the answer is:
<code>find . -type f -atime +365 -exec rm -rf {} \;</code>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.