Table of Contents
- 1 Overview
- 2 Basic usages of the find command
- 3 Case Insensitive search
- 4 Find directory only & file only
- 5 Find by size (greater than, less than)
- 6 Find finds by permission
- 7 Find files with logical condition
- 8 Execute commands on found files
- 9 Negate search
- 10 Find files by users and groups
- 11 Find with limit depth of search
- 12 Find and delete files
- 13 Conclusion
Overview
The find command is one of the most useful commands in Linux (for me). This cheat sheet serves as a reference so you (and I) can quickly find the correct command to get what we search.
Basic usages of the find command
This is the pattern of the find command:
find [path] -name [filename]
For example, I have the following folder:
![The folder structure](https://datmt.com/wp-content/uploads/2023/12/image-3-1024x754.png)
For example, if I want to find the file “sdk.json”, this is the command:
find . -name "sdk.json"
Sure enough, I got the file back:
![find result](https://datmt.com/wp-content/uploads/2023/12/image-4.png)
As you can see, the find command also finds in the nested folder.
Case Insensitive search
By default, the find command treats the search term case-sensitively. That means, if I run:
find . -name "SDK.json"
There would be no result returns.
If I want to ignore case sensitivity, I can use iname
instead of name
find . -iname "SDK.json"
Find directory only & file only
Consider the following structure:
![](https://datmt.com/wp-content/uploads/2023/12/image-5-925x1024.png)
The first loki is a file and the second loki is a folder.
Execute the following command would return both files:
find . -name "loki"
![](https://datmt.com/wp-content/uploads/2023/12/image-6.png)
You can specify the type you want to return with -type
#returns only the directory find . -name "loki" -type d
# returns only the file find . -name "loki" -type f
Find by size (greater than, less than)
Let’s consider the following files in a folder:
![](https://datmt.com/wp-content/uploads/2023/12/image-7-1024x256.png)
I can execute the following commands to find the files that are less than 50KB
# returns only the file find . -size -50k # returns # ./dockermetrics.json # ./sdk.json
This command would result in the files that are larger than 50KB:
# returns only the file find . -size +50k # returns #./postgres.json #./temporal.json
Find finds by permission
You can find files by permission with:
find [path] -perm [permissions]
Let’s say I have the following files:
![Files with permissions](https://datmt.com/wp-content/uploads/2023/12/image-8-1024x228.png)
As you can see, they are all 664.
I can issue a command to find by permission 664 and sure enough, I can see the 4 files:
![find files with permission](https://datmt.com/wp-content/uploads/2023/12/image-9-1024x185.png)
Find files with logical condition
You are not limited to just one condition. Let’s say you want to find JSON files with a size > 50KB. You can do the following:
find -name "*.json" -size +50k
![](https://datmt.com/wp-content/uploads/2023/12/image-10-1024x139.png)
These conditions are implicity AND. If you want to do OR condition, you can use the following command:
find . \( -name "sdk*" -o -name "post*" \)
![](https://datmt.com/wp-content/uploads/2023/12/image-11-1024x121.png)
Here, I use the wildcard to match the file names. Essentially, what I did was to ask the command to return the files that either start with sdk
or post
. The result was quite expected.
Execute commands on found files
if you want to execute a command on found files, you have it covered. For example, I want to find all the shell files and make them executable.
First, here are the nonexecutable files:
![](https://datmt.com/wp-content/uploads/2023/12/image-13-1024x142.png)
They are not executable as you can see.
Let’s make them all executable:
find *sh -exec chmod +x {} \;
If I list the files again, they are all executable:
![Files are now executable](https://datmt.com/wp-content/uploads/2023/12/image-14-1024x139.png)
in the command above, the {}
is the placeholder for the found files. the \; at the end signifies this is the end of the command that needs to be executed.
Negate search
There are times you want to find files that don’t match a pattern, you can precede the -name with !
find . ! -name "sdk*"
![negate search with find](https://datmt.com/wp-content/uploads/2023/12/image-15-1024x285.png)
As you can see, all files returned except for sdk.json.
Find files by users and groups
Similar to finding by names, you can find files by user and group. The syntaxes are familiar:
find [path] -user [username] find [path] -group [groupname]
![](https://datmt.com/wp-content/uploads/2023/12/image-16-1024x200.png)
In the list of files above, you can see that the run1.sh file is owned by the user jenkins-agent. The following command return that single file:
find . -user jenkins-agent
![](https://datmt.com/wp-content/uploads/2023/12/image-17-1024x77.png)
Find with limit depth of search
There are times you don’t want to search through all levels of the directory tree. For example, in the following directory tree:
![](https://datmt.com/wp-content/uploads/2023/12/image-18-1024x531.png)
You only want to find all the ‘loki’ files in the first level but not the ones inside sub folders.
You can specify the depth with -maxdepth
find [path] -maxdepth N -name [pattern] find -maxdepth 1 -name "loki"
![](https://datmt.com/wp-content/uploads/2023/12/image-19-1024x96.png)
Find and delete files
Combining the find command with the -delete
can be very useful (and dangerous).
Let’s say I want to find all .sh files in the current folder and delete them:
find -name "*sh" -exec rm {} \;
Conclusion
In this post, I’ve introduced you to the usage of the find command in Linux. This is a very powerful and useful command. Using it right can serve you well.
![](https://datmt.com/wp-content/uploads/2023/02/1585613908477.jpeg)
I build softwares that solve problems. I also love writing/documenting things I learn/want to learn.