Q&A – 6 Useful find Command Options In Linux

By | 29/08/2013

The find command in Linux is one of the most basic but still one of the most widely used command. Not only you can do basic search operations with it but when combined with xargs command, it becomes even more useful command line utility. The find command provides a huge list of options and most of the times a user might not have knowledge of every option provided by this command. In this article, we will discuss six useful find command options that can save your time while using this utility.

Useful find Command Options

Q1. How to find files modified (or accessed) a particular number of days back?

Ans. Use -mtime , -atime  options to find files modified and accessed a specified number of days back.

Here is an example of -mtime option:

find-1

So we see that the find command along with option -mtime 2 produced names of files that were modified 2 days back in the directory /home/himanshu/practice.

Here is an example of -atime option:

find-1-atime

So we see that the find command along with option -atime 1 produced names of files that were accessed 1 day back in the directory /home/himanshu/practice

Q2. How to find files based on their permissions?

Ans. To find files based on their permissions, use -perm option.

Here is an example :

find-2-perm

So you can see that all the files with permission level 777 were produced in the output.

Q3. How to restrict find command from searching subdirectories?

Ans. To restrict find command from searching subdirectories, use -maxdepth option.

Here is an example :

find-5-maxdepth

So you can see that no results from any subdirectory was shown in the output.

Q4. How to pipe the output of find command to some other command if file names contains white spaces?

Ans. Suppose we have a file with name ‘file name with spaces‘.

find-7-ls

Now, observe that the file ‘file name with spaceswas not processed properly due to the spaces within the file name.

find-4

So, to overcome this problem, use -print0 option with find command and -0 option with xargs command.

find-7-3

Now, the file name was processed properly.

Q5. How to find files owned by a particular user account?

Ans. To search for files owned by a particular user account, use the user option which expects a user name.

Here is an example :

find-8-user

So we see that all the files owned by the user himanshu were displayed in the output.

Q6. How to find only directories?

Ans. In order to search for only directories, use the type option with value d (signifies directories).

Here is an example:

find-9-type

So you can see that only directories were listed in the output.

 

One thought on “Q&A – 6 Useful find Command Options In Linux

  1. Luca Bellarosa

    Great, GREAT job Himanshu! You saved me tons of minutes of scripting!!! Thanxxx! ;-)

    Reply

Leave a Reply

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