Introduction
find Command in Linux is used to find files and directories and take action on them. It is one of the most versatile commands in UNIX-like OSes(Linux, BSD). This command should be in the toolset of every developer, sys-admin or anyone interacting with the system.
Basic mastery over this command can simplify a developer’s (DevOps, sys-admins) life. On the surface, it might seem like a difficult and complex command but it is well-structured and fun to use.
In this post you will also learn how find command can be used to delete many files and directories that can’t be done through rm command.
Prerequisites
Introduction to Linux Commands
See How easy, simple, and short it is to use the find command.
Synopsis of the find command
in Linux
find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]
Understanding the Basics of the Find Command
At its core, the find
command scans through one or more directories and evaluates each file and directory against an expression you provide. It returns every file name that matches this expression. The basic syntax of the find
command is as follows:
find [options] [path...] [expression]
- options: Modify the behavior of the
find
command. - path: Specify the directory to start the search. If no path is specified, it defaults to the current directory.
- expression: Dictate what to search for and what to do with the matched items.
Key Options and How to Use Them
1. Searching by Name
The -name
option allows you to specify the name of the files you want to search for. It’s case-sensitive by default, but you can use -iname
for a case-insensitive search.
find /home/user -name "sample.txt" find /home/user -iname "Sample.TXT"
2. Searching by Type
You can find files of a specific type using the -type
option. Common types include:
f
for regular filesd
for directoriesl
for symbolic links
find /var/log -type f find /home -type d
3. Searching by Modification Date
The find
command can search files based on when they were last modified:
-mtime n
: Files modified n*24 hours ago.-mtime +n
: Files modified more than n days ago.-mtime -n
: Files modified less than n days ago.
find /home/user -mtime -7 # Files modified in the last week
4. Searching by Size
To find files based on their size, use the -size
option. You can specify sizes in kilobytes (k), megabytes (M), gigabytes (G), and others.
find / -size +100M # Files larger than 100 MB
5. Using Logical Operators
Combine conditions using logical operators:
-and
: Both conditions must be true (implicit if omitted).-or
: Either condition is true.-not
: Negates a condition.
find /home/user -type f -not -name "*.txt"
6. Executing Commands on Found Files
The -exec
option allows you to run a command on each file found. Use {}
to represent the current file, and \;
to terminate the command.
find /tmp -type f -name "*.tmp" -exec rm {} \;
7. Finding Files by Permissions
To find files with specific permissions, use the -perm
option.
find /home/user -perm 644
8. Advanced Searches
Combine multiple search criteria for more refined searches. For instance, finding all .jpg files that are larger than 1MB and modified in the last 30 days:
find / -type f -name "*.jpg" -size +1M -mtime -30
Usage
Example 1 – Find all text files (.txt) extension in the current directory
Solution
find . -name "*.txt"
Explanation – We are using the find command to find files, next we are specifying. (. means current directory) to find in the current directory only, further, we are filtering the search for text files only (with *.txt).
Challenges in using the find command
Not able to correctly break down the problem statement
Not able to solve those sub-problems broken in the previous steps
Difficulty in constructing the find command for the given problem
Right approach for using the find command
Understand the problem well. Any difficult or complex problem related to Linux commands should be approached with a divide-and-conquer strategy.
Divide – Break the problem into sub-problems
Conquer – Solve sub-problems and merge sub-solutions to arrive at the final solution.
In the context of find command, you should first solve the base problem (finding the required files), then keep adding difficult (taken from the problem) one at a time and finally construct the complete find command (solution).
Let us learn this with an example.
Let us understand this with an example
Q1 – find all log files (.log extension) greater than 5MB and older than 10 days in /var/log directory
Approach
Break down the problem
find all log files (.log extension)
greater than 5MB
older than 10 days
directory – /var/log
Tip – Breaking down the problem is 80% of the solution
Solution:
We need to find files -> Use the find command
Command -> find
We need to find files in a given directory -> search only in the given directory
Command -> find /var/log
We need to find only log files -> Use pattern matching to filter log files -> -iname “*.log”
Commad -> find /var/log -iname "*.log"
We need to find files greater than 5 MB -> add a filter for file size
Command -> find /var/log -iname "*.log" -size +5M
We need to find files older than 10 days -> add a filter for time
Command -> find /var/log -iname "*.log" -size +5mb -mtime +10
Final Command
find /var/log -iname "*.log" -size +5mb +mtime 10
# if you face permission issue in /var/log directory then elevate priviledge using sudo
Command - find /var/log -iname "*.log" -size +5mb +mtime 10
Practical Examples and Common Use Cases of Find Command
Example 1: Cleanup Temporary Files
Automatically find and remove temporary files that haven’t been accessed in the last 10 days:
find /var/tmp -type f -atime +10 -exec rm {} \;
Example 2: Archiving Old Files
Find all log files older than a year and archive them:
find /var/log -name "*.log" -mtime +365 -exec tar -rvf yearly_logs.tar {} \;
Example 3: Permission Auditing
Audit files to ensure sensitive files are not world-readable:
find /secure -type f -perm -o=r
Usecases
Find a file in a given directory or its sub-directory
Question 1 – Find a file example.txt in the ~/Downloads directory.
Answer – find ~/Downloads -iname "example.txt"
find command power over other commands
Find older log files and delete them
Question 1 – find more than 2 days older system.log files (files matching with system.log) in /var/log directory and delete them
Answer – First let us find system.log files older than 2 days (modified more than 2 days ago)
find /var/log -iname "system.log.*" -mtime +2
Note: Please don’t use trailing / in the directory name above (e.g /var/log/)
Now, to delete the required found files use -delete option in the find command
find /var/log -iname "system.log.*" -mtime +2 -delete
The above command will throw an error due to a permission issue, so use the sudo
Final command – sudo find /var/log -iname "system.log.*" -mtime +2 -delete
Validation – Again run the find command without the delete option to confirm whether the required files were deleted or not.
Validation command – find /var/log -iname "system.log.*" -mtime +2
See the below screenshot for the complete run of the command
How to use chatgpt or Gemini to solve problems related with find command
Chatgpt or Gemini is a great AI assistant to make our lives easy. If used correctly it can offload 80% of our tasks related to coding, DevOps, and other general tasks as well.
The advent of chatgpt is a paradigm shift in the landscape of programming or system-related tasks. This is going to change the way we solve programming problems, design systems, and troubleshoot issues for always. So please give it a try.
If you can master prompt engineering then it can increase your output 10x.
Let us learn prompt engineering to solve problems related to the find command.
Problem 1 – Find empty files in the ./test folder.
Solution – Simply prompt the above problem to chatgpt as below.
Copy the command and execute on the system
Problem 2 – find files more than 10 MB and older than 5 days in /var/log/nginx and zip them and move them to ~/archive/nginx folder
Solution – First prompt chatgpt with the above problem, understand the solution, then try it
If the provided solution has any issues then again prompt chatgpt the issue until the solution completely works.
FAQs
What is the basic syntax of the find
command in Linux?
The basic syntax is find [path] [expression]
, where you specify the directory path and search criteria.
How can I use find
to search by file type?
Use -type
followed by f
for files or d
for directories, e.g., find /path -type f
.
Can find
search by modification date?
Yes, use options like -mtime
or -mmin
to filter by modification time.
How do I search for files by name?
Use -name
or -iname
(case-insensitive) with the filename pattern, e.g., find /path -name "*.txt"
.
Can I delete files directly with find
?
Yes, by adding -exec rm {} \;
to the command, but be cautious as this is irreversible.
Conclusion
In this post, You learned about the find command, how to solve related problems, and finally how to construct the find command for the given Problem.