Connect with us

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. 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

Sample Output

Find command in Unix/Linux

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

For example, to search for files in /etc directory run the command below

Sample Output

find files in a particular directory

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

In our previous example in (2) above, to find files labeled apache2 in /etc directory, execute the command

Sample Output

find command- find specific files in directories

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

Sample Output

find command find files only

To search files only bearing certain names include the -type f argument as shown

For example, to search for ssh files only in /etc directory, execute

Sample Output

find particular file names using find command

To search directories only use the -type d argument

For example, to search for apache2 directories only located in /etc directory, execute

Sample Output

find directories only

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

For instance, to find files with file a permission value of 600 in /etc directory, run

Sample Output

find files with certain file permissions

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

For example, to find all read-only files in the /etc directory execute the command

Sample Output

If you want to search for executable files, the command will be

Sample Output

search executable files

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

For example, if you are wondering how to find a file with a .conf extension in /etc directory execute

Sample Output

find command recursively search for file extensions

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

In this example, the command will be

Sample Output

invert find rsults using find command

OR

Sample Output

invert find results with ! -name flag

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

Sample output

searching hidden files

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

For instance, to find files owned by a user ‘ james’ in the home directory run

Sample output

find files owned by a specific user

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

Sample output

maxdepth search option -1

To go up to the 2nd directory run

Sample output

maxdepth search option -2

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

For instance, to find all files with 5kb file size in /etc directory, run

Sample output

search files of certain file sizes

If you want to search files in a given range e.g between 5kb and 10kb run

Sample output

search file size between file ranges

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.

Continue Reading
Advertisement
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending