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:
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:
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:
The first loki is a file and the second loki is a folder.
Execute the following command would return both files:
find . -name "loki"
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:
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:
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 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
These conditions are implicity AND. If you want to do OR condition, you can use the following command:
find . \( -name "sdk*" -o -name "post*" \)
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:
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:
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*"
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]
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
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:
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"
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.
I build softwares that solve problems. I also love writing/documenting things I learn/want to learn.