Friday, December 1, 2017

How to use FIND command to locate anything in Linux

How to use FIND command to locate anything in Linux

Hello Linux-fanatics in this post we will discuss find command. It is very useful utility which we can be use to easily locate files & directories with a number of search criteria to refine your search. Its usually installed by default on most of the Linux distributions.

Basic syntax for using find command is :-
find  location comparison-criteria  search-term

1- Finding files or directories

$ find /etc -type f
So, this command will find all the files in /etc folder. We can also use -type d to produce list of all the directories on /etc on screen.

2- finding files/directories with size

$ find /etc -type f -size 1M
find command
will show all the files in /etc folder with size of 1 Megabytes.

3- finding files based on name

$ find /testing -iname “*.txt”
find command
command will show all the files in /testing folder with extension .txt & will also ignore if the results are case sensitive I.e. it will show results with test1.txt or test2.TXT.
Also, if we want to find files with exact match that we can use -name instead of -iname.

4- Invertive search

$ find /testing -not -name “*.txt”
find command
above command will list all the files & directories that does not have extension “.txt” at the end.

5- Combining search criteria

$ find /testing -name ‘test*’ ! -name ‘*.txt’
find command
Above example shows combining of two search criteria i.e.it will find all the file with name test at the start in /testing folder which does not have extension .php.
“!” here is equivalent of AND operator.
Also, we can combine two search criteria & produce result when any of the two search criteria are satisfied.
$ find /testing -name ‘test*’ -o -name ‘*.txt’
find command
Here “-o” is equivalent to OR operator.

6- Search based on file permissions

$ find /testing -type f -perm 0644
find command
Also, we can find files or directories based on permissions given to them. Above command shows us just that. It will show all the files in /testing folder with permission of 0644.
$ find /testing -type f -perm /u=r
Result for the above command will show all files for user with read permissions.

7- Finding files with user & group ownership

$ find / -user dan
find command
it will find all the files that are created by user dan. Similarly we can also search for files or folders that are owned by a group by replacing -user with with -group.

8- Finding files based on their modification, access & change time

$ find / -mtime 10
It will find all the files that were modified in last 10 days. Replace mtime with -atime to find all the files that were accessed in last 10 days..
$ find / -cmin -60 will find all the files that were changed in last 60 minutes.
$ find / -mmin -60 will find all the files modified in last 60 minutes.
$ find / -amin -60 will find all the files accessed in last 60 minutes.

9- Listing all the found files

$ find . -exec ls -ld {} \;
find command
Will show all the files & will show them as would be shown by ls command.

10- Finding & deleting the found files

$ find /testing -type f -name *.txt -size -1M -exec rm -f {} \;
This command will find all the files with .txt as extension with size less than 1 Megabyte & will execute rm/delete command on found files.

No comments: