Linux Find Command with Practical Examples

linux find command

The find command is a powerful search utility used to locate files in Linux and Unix like operating system. It can find files and directories based on different criteria like name, extension, type, modification and creation date, permissions, size, owner.

Find can also perform an action based on command for example search files and execute delete, count, etc.

This tutorial will focus on explaining the way to use find command and walk you through examples.

Syntax:

The basic syntax of find command usage is as follows:

find <directory-to-search> [OPTIONS] <find name,file,extension>

The -H and -L options are used to manage symbolic links while searching, -P (don't follow symbolic links) and -L (follow symbolic links).

1. Find a file by name

In order to find a file by its name, use the find command with -name option.

Assuming that you want to search for the file have name 'nginx.yaml' in Documents directory, you can run the following command:

$ find /Documents -name nginx.yaml

This will find the file named "nginx.yaml" in the Documents/ and all sub-directories:

Moreover, in order to search for the file "nginx.yaml" in current directory and its all subdirectories, run:

$ find -name nginx.yaml

The period (.) after the find command indicate to start searching from the current directory:

$ find . -name nginx.yaml

2. Find files by extension

If you want to search for all files by extension, you can use the find command with -name option following with the *.<file-extension>

For example, you can search for all files which have extension '.png' in the home directory by running the following command:

$ find $HOME -name *.png

3. Find only directories

If you want to search for only directories, you can run the find command with -type d option.

Suppose you want to search for all directories in the folder Documents/, let's run:

$ find /Documents -type d

4. Find only files

There are many different types of files in Linux such as: directories, symbolic links, socket, regular files, etc. If you want to search for only regular files, you can run the find command with '-type f' option.

For example, assuming that you find only files in the current directory, let's run:

$ find . -type f

5. Find and delete

In some situations you want to find and delete files/directories which are no longer needed, you can run the find command with '-delete' option.

For instance, finding all the images have *.png extension in folder Documents/ and remove them:

$ find /Documents -name *.png -delete

6. Find files having specific permissions

If you want to search for all files which have specific permission, you can use the Linux find command with '-perm' option.

For example, in order to find files which have '755' permission, let's run:

$ find . -type f -perm 775 -exec ls -l {} \;

7. Find files and directories by date/time

Linux find command allows you to search for files and directories by date/time stamp.

You can use find command with '-mtime -N' option to find files that was last modified 'N' days ago. For example, in order to search for all files which are modified in the last 2 days in the current directory, let's run:

$ find . -type f -mtime -2 -exec ls -l {} \;

Another way to use find command to search for files/directories by date/time is using '-newerXY' option. Where X and Y can be:

a - the access time of the refer file

B - the birth time of the refer file

c - the inode status change time of refer file

m - the modification time of refer file

t - refer is interpreted directly as a time

For example, in order to search for all files modified on the July 4th, 2020 in the current directory, let's run:

$ find . -type f -newermt 2020-07-04 -ls

8. Find files and directories by size

Linux find command allows you to search for files/directories by size by running command with '-size' option.

Assuming that you want to find files that are larger than 1GB, you can run the following command:

$ find . -type f -size +1G

Some Practical Examples

01) Find an empty files/directories

This output will lists all empty files and folders in the current directory and sub-directories:

$ find . -empty -ls

02) Find all .jpg files

$ find . -name *.jpg

03) Find all directories with 644 permission

The output shows you all the folders in the current directory or sub-directory which have 644 permission:

$ find . -type d -perm 644 -ls

04) Find files owned by a specific user

If you want to check for all files owned by the user "cas" under the current directory, you can do it as follows:

$ find . -user cas -ls

05) Find files by inode number

In order to search for a file by its inode number, you can use find with -inum option. For example:

$ find -inum 534937

06) Count number of files

$ find -type f -exec basename {} \; | wc -l

07) Count number of directories

$ find -type d -exec basename {} \; | wc -l

08) Search for files with a particular depth

Assuming that you want to search for the file "Dockerfile" in HOME folder and its subdirectories and not in the next level subdirectories, you need to set '-maxdepth' option to 2 as follows:

$ find $HOME -maxdepth 2 -name Dockerfile

Conclusion

Another powerful tool is fd command which is an alternative to find command.

In this tutorial, we learned how to use Linux find command to search for files and directory. Thanks for reading and please leave your suggestion in the below comment section.

Bobbin Zachariah 11:08 am

Comments

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

1 Comment