Mastering the Linux find Command: 7 Powerful Use Cases for Devs
This article explains why the Linux find command is essential for backend developers and interviewees, demonstrates how to delete log files older than a year, and walks through seven practical find usages—including name patterns, file types, timestamps, size, permissions, ownership, and executing actions on matched files.
It can be said with confidence that the find command is one of the operations every Linux backend developer must know, unless you are using Windows Server.
It is also a hot topic in technical interviews. Consider this interview question:
If your Linux server has a directory named logs, how would you delete log files in that directory whose last access time is more than one year ago?
The answer is to first cd into the directory, then run the following command: find . -type f -atime +365 -exec rm -rf {} \; If you haven’t fully understood the command above, don’t worry. This article introduces seven practical uses of the find command, and you will master it by the end. If you already know it, the article serves as a good review.
Find by name or regular expression
Start with the simplest usage. To search for a file by a specific name: find . -name test.txt To find all PDF books using a regular expression: find ./yang/books -name "*.pdf" Specifying the file type makes the intent clearer:
find ./yang/books -type f -name "*.pdf"Find different types of files
Besides regular files, you can search for other file types using the -type option.
For directories: find . -type d -name "yang*" For symbolic links:
find . -type l -name "yang*"Find files by timestamp
Linux tracks three timestamps for each file:
atime : last time the file was read.
mtime : last time the file content was modified.
ctime : last time the file’s metadata (owner, permissions, etc.) changed.
To search for files whose access time is older than one year: find . -type f -atime +365 To find files whose modification time is exactly five days ago (no + sign): find . -type f -mtime 5 To find files whose change time is between five and ten days ago:
find . -type f -ctime +5 -ctime -10Find files by size
The -size option lets you search by file size. Units are:
b: 512‑byte blocks (default)
c: bytes
w: two‑byte words
k: kilobytes
M: megabytes
G: gigabytesFor example, to find files between 10 MB and 1 GB:
find . -type f -size +10M -size -1GFind files by permissions
Controlling file permissions is a key task for Linux administrators. Use -perm to locate files with specific permissions: find . -type f -perm 777 This command finds all files that have read, write, and execute permissions for owner, group, and others.
Find files by ownership
Specify the owner with -user. For example, to find all files owned by user yang:
find -type f -user yangExecute a command on found files
Often you want to perform an action on each matched file, such as deleting or inspecting it. The -exec option makes this easy.
Returning to the interview question, you can combine -exec with rm -rf: find . -type f -atime +365 -exec rm -rf {} \; The placeholder {} represents each file found; without it, the command would act on all files in the directory.
Try the following two commands to see the difference:
With placeholder: find . -type f -atime +5 -exec ls {} \; Without placeholder: find . -type f -atime +5 -exec ls \; The -exec command must end with a semicolon ( \;), which is escaped with a backslash to prevent the shell from interpreting it.
Conclusion
After learning the seven uses of the find command, the interview question becomes easy to solve. You can now write the answer clearly and explain it.
find . -type f -atime +365 -exec rm -rf {} \;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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
