Find Command in Linux

Find Command in Linux

The Linux find command is being used when looking for files directly from the command line. The find command is given with search criteria such as permissions, ownership, modification, size, time, and date among others to locate the file or directory.

The find command is available in all Linux distros by default, no need of installing special packages to use it. Due to its significance, the find command is an essential command to learn if you want to know more about the command line navigations on any Linux distribution.

Let’s see some of the find command examples and explain the various options that we use.

Syntax

$ find location comparison-criteria search term

Listing files in the current directory

To list all files in a directory including files inside folders, run the command below.

$ find .

Searching files within a specified directory

If you want to search all files in a given directory, use the find command as follows

$ find directory_name

For example, to search for all files in /etc execute the command

$ find /etc

Searching files using the filename within a specified directory

If you want to specify the search criteria using the name of the file in a directory, the syntax will be as follows

$ find directory_name -name "file_name"

For example, to search for Apache2 files in /etc directory run

$ find /etc -name "apache2"

Recursively find all files with a specified file extension

If you want to search for particular files with a specific extension, in a given directory, the syntax will be as follows

$ find directory_name -name "*.extension"

For example, to search for all configuration files (.conf) in /etc directory, execute

$ find /etc -name "*.conf"

Limiting depth of search

You can also decide to limit the depth of your file search in directories. If you want to limit your file search to the first level of the directory, the syntax will be

$ find directory_name -maxdepth 1 -name "*.conf"

So, if you want to limit the file search to the first level directory in /etc for files with .conf extension execute:

$ find /etc -maxdepth 1 -name "*.conf"

Sample output:

Find Command in Linux

As seen in the output above, the file search is limited to the /etc directory level. If you want to perform a more intensive search and go deeper within other directories within the /etc directory, increase the maxdepth value.

To search for files with .conf extension up to the 3rd directory run

$ find /etc -maxdepth 3 -name "*.conf"

Sample output

Find Command in Linux

From the above output, the search goes up to the 2nd and 3rd directories.

Invert search results

You can also search for files that do not meet given criteria with the find command. This mode is helpful when you want to eliminate known files from the search pattern.

To do this, use the -not -name attribute as shown

$ find /etc -maxdepth 1 -not -name "*.conf"

Sample output

Find Command in Linux

The above output prints all the files that do not have the .conf file extension.

Using find with OR operator

You can choose to combine search results with find by using the OR operator which is symbolized by -o flag shown in the example below

$ find /etc -maxdepth 3 -name "cron" -o -name "ssh"

Sample output

Find Command in Linux

The above command searches for files bearing the name ssh OR cron in the </etc directory>

If you want to search for directories only, use the -type d attribute as shown in the example below.

$ find /etc -type d -name "ssh"

Searching for files owned by a particular user

To search for files owned by a particular user in a specific directory, use the syntax:

$ find /path -user username

For instance, to find files owned by user webhostc in /home directory run the command below

$ find /home -user webhostc

Sample output

/home/webhostc
/home/webhostc/tmp
/home/webhostc/imap
/home/webhostc/Maildir
/home/webhostc/.bash_logout
/home/webhostc/public_html
/home/webhostc/domains

Searching for files with certain file permissions

To search for files with specific file permissions, use the syntax below

$ find /directory_name -type f -perm value

For example, to search for files with permissions 755 in /etc directory, run:

$ find /etc -type f -perm 755

Sample output

Find Command in Linux

Searching for files with certain files sizes or a range of files

Linux find command also allows users a chance to search files according to their file sizes.

Search files of N size

For example, to search for files which are 10kb run:

$ find /etc -type f -size 10k

Find Command in Linux

To search files greater than 10kb run

$ find /etc -type f -size +10k

Find Command in Linux

To search files less than 10kb run

find /etc -type f -size -10k

Find Command in Linux

That’s it now for find command. If you know any other commands related to find command please comment us so that we can add it with your name.

To know about Top 50 Linux Commands, Click here

You may also like...

1 Response

  1. Nageshwaran says:

    You guys really posting the important topics.
    Kindly post 2 or 3 blogs in a week.

    Now I learned more about find command.

    Thankyou

Leave a Reply

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