find command in Linux with Examples.

find command is a very essential command in the linux operating system. We need to use this amazing command to find files within our system hierarchy. In a windows operating system we can use search option very simply in the GUI. Likewise we can use find linux command here to find and grab the files as our need. Same as other linux commands find command also having so many command options, like to search recursively in the files, to find the files with considering modified dates, accessed dates, files considering their sizes, files considering ownerships and permissions,  and with so many options.

Also we can use the pipeline and redirection to have the output of the find command and pass it to another operation. As an example we can type the find command to find some files and delete those files with a single linux command. For that purpose we are using pipeline with -exec or xarg command. We will discuss about -exec later.


After this we will discuss the find command and it's examples with the options. 

Syntax -

find [location] [options] [what to find]

  • location : The directories where you need to search. This can be a single location or multiple locations.
    eg - if we need to find a file under root directory, the location should be root directory.
    So, the command should start like find /
  • options : find command has so many options to optimise the search. Will discuss on below of the article. 
  • What to find : The name of the file which you need to find.
    eg - if we need to find all files having the extention of .cpp under root directory, the command should be find / -name *.cpp. Here -name is the option we used to determine the file name. If we use to find a file giving a name of the file, we must use the option name. 

Examples

1) find files named "example.txt" in your current location

find . -name "example.txt"

2) find files named "passwd" under root directory

find / name "passwd"

find command in linux
 
 3) find files named with case insensetively.

Guess we have two files named, Text_file and text_file. These two words differ with capital T.If we need to ignore case sensitivity we need to use option name as -iname. 

find / -name "text_file" ( Case sensitive )


find / -iname "text_file" ( Case Insensitive )

find command in linux

 
 4) find file "example.txt" under your home directory

find /home -iname "example.txt"

5) find files which having the extention of .php under your home directory

find /home -type f -name "*.php"

here -type option is used to determine the type of the file, to recognize is it a file or a directory which you are searching. if you are finding a directory the option should be -type d on the above example.

6) find directories named example under your home directory

find /home -type d -name "example"

7) find files in more than one location

find / /home -iname "student"

Above command find the files named student in both places of root directory and /home directory.

8) find emty files

find / -type f -empty

9) find empty directories


find / -type d -empty

10) find files which have permissions of 777

find / -type f -perm 777 

-perm option is used to determine permissions with the find command. if you want to find permissions of 644, it should be like -perm 644.

11) find files not having 777 permissions

find / -type f ! -perm 777

! is used to mention NOT.

12) find files which are set to SETUID

find / -perm /u=s

13) find files which are set to SETGID

find / -perm /g=s

14) find files read only files

find / -perm /u=r

15) find files and remove them

find /home -iname "*.cpp" -exec rm -rf {} \;

We will break this command into two parts.
1) find part is ok for you. It finds all files having .cpp extension under your home directory.
2) -exec rm -rf {} \;  This part is taking the output of the find section and executes the rm -rf command to remove the searched files.  So the output of the find command is going to store inside of {} as an input to rm -rf command and -exec option makes the command as executable.

16) find files having 755 permissions and change back to 644 permissions.

find / -iname "*.cpp" -perm 755 -exec chmod 644 {} \;

17) find files based on users

find / -iname "example" -user student

Above command finds files with example named which is owned to user student.

18) find files based on group

find / -iname "example" -group user

This finds files with name example with group name user.

19) find modified files with given dates

find / -iname "*.txt" -mtime 7

find files having .txt extention under root directory which is modified on 7 days back.

find / -iname "*.txt" -mtime +7

find files having .txt extention under root directory which is modified on more than 7 days.

 
find / -iname "*.txt" -mtime -7

find files having .txt extention under root directory which is modified within 7 days.

find / -iname "*.txt" -mmin 7

find files having .txt extention under root directory which is modified before 7 minutes from now.

* mtime is used to mention in days and mmin is used to mention in minutes. 

20) find files which accessed on 10 days back

find / -iname "*.txt" -atime 10

21) find files which are changed on before 10 minutes

find / -iname "*.txt" -cmin 10 

22) find files with size

find / -size +50M

find files which are more than 50M in size.

find / -size +50M -size -200M 

find files which sizes are more than 50M and less than 200M.



So, we have discussed alot of options can be used with find command. You can refer the internet to find more and more.


Cheers. 

 

Comments

Popular posts from this blog

Why i can't ping to my AWS EC2 Instances ?

Different types of users in linux

How to setup a wordpress website on AWS EC2 within few minutes