Linux
Find command in Unix/Linux with 30+ Examples [2023]
Let’s Learn Find command in Unix/Linux with Practical Examples :
The find command in Unix/Linux is a nifty tool and comes in handy when searching files on the terminal or the command line. It helps users find files based on a wide spectrum of criteria such as file size, file ownership, date of modification, file permissions to mention just but a few. In this tutorial, you will learn how to search a file in Linux using the various parameter as mentioned earlier.
The find is installed by default on all Linux distributions, therefore there’s no need of installing it. Let’s dive in and see how you can use the command which is considered essential for beginners and advanced users alike.
The find command in Unix/Linux syntax structure is as shown below
1 |
$ find [where to start searching from] [ search criteria] [what to find] |
1. How to List files in Current directory and sub-directory
To list files in the current directory inclusive of sub-directories run the command below
1 |
$ find . |
Sample Output
2. How to List files & subdirectories in a specific path or directory
You can also search a file or files in a specific directory using the syntax below
1 |
$ find /path/to/directory |
For example, to search for files in /etc directory run the command below
1 |
$ find /etc |
Sample Output
3. How to List specific files in a specific path or directory
If you wish to narrow down your search for files and print out files according to their names, use the -name argument as shown
1 |
$ find /path/to/directory -name file_name |
In our previous example in (2) above, to find files labeled apache2
in /etc
directory, execute the command
1 |
$ find /etc -name apache2 |
Sample Output
4. How to search files only or directories only
To search files only in a directory, use the -type f
argument. For example, to search for files only in /etc directory run the command below and pipe results into less attribute
1 |
$ find /etc -type f | less |
Sample Output
To search files only bearing certain names include the -type f
argument as shown
1 |
$ find /path/to/directory -type f -name “file_name” |
For example, to search for ssh files only in /etc directory, execute
1 |
$ find /etc -type f -name “ssh” |
Sample Output
To search directories only use the -type d
argument
1 |
$ find /path/to/directory -type d -name “directory_name” |
For example, to search for apache2 directories only located in /etc directory, execute
1 |
$ find /etc -type d -name “*apache2” |
Sample Output
5. How to search files with certain file permissions
The find command can also be used to search files with certain file permission using the -perm option followed by the octal value denoting the file permission. The syntax will be
1 |
$ find /path/to/directory -type f -perm octal-value |
For instance, to find files with file a permission value of 600 in /etc directory, run
1 |
$ find /etc -type f -perm 0600 |
Sample Output
Also, you can play around with the permissions syntax and search for files which are read-only, files which are executable and so on.
For example, to find files which are read-only in a certain directory execute
1 |
$ find /path/to/directory -type f -perm /a=r |
For example, to find all read-only files in the /etc directory execute the command
1 |
$ find /etc -type f -perm /u=r |
Sample Output
If you want to search for executable files, the command will be
1 |
$ find /etc -type f -perm /u=x |
Sample Output
Feel free to add more options on the file permissions
6. How to recursively list files with a certain file extension
You can also search files with a specific file extension using the wildcard symbol (*) as shown below
1 |
$ find /path/to/directory -name “*. extension” |
For example, if you are wondering how to find a file with a .conf extension in /etc directory execute
1 |
$ find /etc -name “*. conf” |
Sample Output
7. Inverting search results
Suppose you want to search for files that do not fit a certain pattern. Let’s say you want to search for files that do not have a .conf extension in /etc directory. How do you go about it?
To accomplish that, use the -not -name or ! -name flags as shown
1 |
$ find /path/to/directory -not -name “*. extension” |
In this example, the command will be
1 |
$ find /etc -not -name “*. conf” | less |
Sample Output
OR
1 |
$ find /etc -! -name “*. conf” |
Sample Output
8. Searching hidden files
To search for hidden files in a directory, use the “.*”
option. For example, to search for hidden files in /etc directory execute the command
1 |
$ find /etc -type f -name ".*" |
Sample output
9. Searching files owned by a particular user
The find command in Unix/Linux also allows you to find files owned by a certain user in the system
The syntax for achieving this is
1 |
$ find /path -user username |
For instance, to find files owned by a user ‘ james’ in the home directory run
1 |
$ find . -user james |
Sample output
10. Searching files up to a certain directory depth
By default, the find command traverses down the entire directory tree structure in a recursive manner. I’m sure you must have noticed that at this point. However, you can dictate the search depth and specify the number of directories that you’d want your search to go down to. For instance, you may decide that you want your search to be restricted up to 2 or 3 directories. The option for specifying the depth of search is the -maxdepth option.
For instance, to limit the search of apache2to the 1st directories in the /etc directory run
1 |
$ find /etc -maxdepth 1 -name apache2 |
Sample output
To go up to the 2nd directory run
1 |
$ find /etc -maxdepth 2 -name apache2 |
Sample output
As you can clearly see, the search depth in the first result is limited to the /etc directory. However, in the second instance, the search has extended to /etc/init.d
. /etc/cron.daily
and /etc/logrotate
directories.
11. Searching files of a given file size
Find can also be used to search files of specified file size. The syntax for this is
1 |
$ find /path/to/directory -size {x kb/MB/GB} |
For instance, to find all files with 5kb file size in /etc directory, run
1 |
$ find /etc -size 5k |
Sample output
If you want to search files in a given range e.g between 5kb and 10kb run
1 |
$ find /etc -size +5k -size -10k |
Sample output
Wrapping Up
That was a summary of the find command in Unix/Linux. Find is an essential tool that is a must know for beginners and seasoned system administrators and which makes searching files in Linux easy. Thank you for taking the time in this tutorial. Your feedback will be appreciated.
-
DevOps55 years ago
Saltstack Tutorial for beginners [2023]
-
DevOps55 years ago
How to build a Docker cron job Container easily [2023]
-
Linux55 years ago
mail Command in Linux/Unix with 10+ Examples [2023]
-
DevOps55 years ago
Docker ADD vs COPY vs VOLUME – [2023]
-
DevOps55 years ago
How to setup Pritunl VPN on AWS to Access Servers
-
Linux55 years ago
Grep Command In Unix/Linux with 25+ Examples [2023]
-
Linux55 years ago
How To setup Django with Postgres, Nginx, and Gunicorn on Ubuntu 20.04
-
Linux55 years ago
Whereis command in Linux with 10+ Examples [2023]