Saturday, 28 March 2015

Find command in Linux (Examples)

The find command is used to locate files on a Unix or Linux system. Find will search any set of directories you specify for files that match the supplied search criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria. The search is recursive in that it will search all sub-directories too. The syntax looks like this: 

find <where-to-look> <criteria> <what-to-do>


All arguments to find are optional, and there are defaults for all parts.  (This may depend on which version of find is used.  Here we discuss the freely available Gnu version of find
For example,
where-to-look  defaults to . (that is, the current working directory),
criteria defaults to none (that is, select all files),
what-to-do
(known as the find action) defaults to ‑print (that is, display the names of found files to standard output).

For example: 
1) find 
will display the path-names of all files in the current directory and all sub-directories.  The commands 
find . -print 
find -print 
find . 
do the exact same thing.

2) Here's an example find command using a search criterion and the default action:
find / -name foo
This will search the whole system for any files named foo and display their path-names.  Here we are using the criterion ‑name with the argument foo to tell find to perform a name search for the file-name foo.  The output might look like this:
/home/wpollock/foo
/home/ua02/foo
/tmp/foo
If find doesn't locate any matching files, it produces no output.
The above example said to search the whole system, by specifying the root directory (“/”) to search.  If you don't run this command as root, find will display a error message for each directory on which you don't have read permission.  This can be a lot of messages, and the matching files that are found may scroll right off your screen.  A good way to deal with this problem is to redirect the error messages so you don't have to see them at all:
find / -name foo 2>/dev/null
You can specify as many places to search as you wish:
find /tmp /var/tmp . $HOME -name foo

3) How to run the last executed find command?
!find 
It also displays the last find command executed along with the result on the terminal.

4) How to find for a file using name?
find -name "sum.sv" 
This will find all the files with name "sum.sv" in the current directory and sub-directories. The output might look like this:
./bkp/sum.sv
./sum.sv

5) How to find for files using name and ignoring case?
find -iname "sum.sv"
This will find all the files with name "sum.sv" while ignoring the case in the current directory and sub-directories. The output might look like this:
./SUM.sv
./bkp/sum.sv
./sum.sv

6) How to find for a file in the current directory only?
find -maxdepth 1 -name "test.sv"
This will find for the file "test.sv" in the current directory only. The output might look like this:
./test.sv

7) How to find for files containing a specific word in its name?
find -name "*sv*"
It displayed all the files which have the word "sv" in the filename. The output might look like this:
./SUM.sv
./bkp/sum.sv
./sum.sv
./multiply.sv

8) How to find for files in a specific directory?
find  /etc -name "*.sv"
This will look for the files in the /etc directory with ".sv" in the filename.

9) How to find the files whose name are not "test.sv"?
find -not -name "test.sv"
This is like inverting the match. It prints all the files except the given file "test.sv". The output might look like this: 
./TEST.sv
./bkp
./multiply.sv

10) How to limit the file searches to specific directories?
find -name "test.sv"
You can see here the find command displayed all the files with name "test.sv" in the current directory and sub-directories.
. /tmp/test.sv
./bkp/var/tmp/files/test.sv
./bkp/var/tmp/test.sv
./bkp/var/test.sv
./bkp/test.sv 
./test.sv

a) How to print the files in the current directory and one level down to the current directory?
find -maxdepth 2 -name "test.sv"
./tmp/test.sv
./bkp/test.sv
./test.sv

b) How to print the files in the current directory and two levels down to the current directory? 
find -maxdepth 3 -name "test.sv"
./tmp/test.sv
./bkp/var/test.sv
./bkp/test.sv
./test.sv

c) How to print the files in the subdirectories between level 1 and 4? 
find -mindepth 2 -maxdepth 5 -name "test.sv"
./tmp/test.sv
./bkp/var/tmp/files/test.sv
./bkp/var/tmp/test.sv
./bkp/var/test.sv
./bkp/test.sv

11) How to find the empty files in a directory? 
find . -maxdepth 1 -empty ./empty_file

12) How to find the largest file in the current directory and sub directories 
find . -type f -exec ls -s {} \; | sort -n -r | head -1
The find command "find . -type f -exec ls -s {} \;" will list all the files along with the size of the file. Then the sort command will sort the files based on the size. The head command will pick only the first line from the output of sort.

13) How to find the smallest file in the current directory and sub directories 
find . -type f -exec ls -s {} \; | sort -n -r | tail -1
Another method using find is 
find . -type f -exec ls -s {} \; | sort -n | head -1

14) How to find files based on the file type? 
a) Finding socket files 
find . -type s
b) Finding directories 
find . -type d
c) Finding hidden directories
find -type d -name ".*"
d) Finding regular files
find . -type f
e) Finding hidden files
find . -type f -name ".*" 


15) How to find files based on the size?
a) Finding files whose size is exactly 10M
find . -size 10M
b) Finding files larger than 10M size
find . -size +10M
c) Finding files smaller than 10M size
find . -size -10M

16) How to find the files which are modified after the modification of a give file.
find -newer "test.sv"
This will display all the files which are modified after the file "test.sv"

17) Display the files which are accessed after the modification of a give file.
find -anewer "test.sv"

18) Display the files which are changed after the modification of a give file.
find -cnewer "test.sv"

19) How to find the files based on the file permissions?
find . -perm 777
This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command "ls -l".

20) Find the files which are modified within 30 minutes.
find . -mmin -30

21) Find the files which are modified within 1 day.
find . -mtime -1

22) How to find the files which are modified 30 minutes back
find . -not -mmin -30

23) How to find the files which are modified 1 day back.
find . -not -mtime -1

24) Print the files which are accessed within 1 hour.
find . -amin -60

25) Print the files which are accessed within 1 day.
find . -atime -1

26) Display the files which are changed within 2 hours.
find . -cmin -120

27) Display the files which are changed within 2 days.
find . -ctime -2

28) How to find the files which are created between two files.
find . -cnewer f1 -and ! -cnewer f2

So far we have just find the files and displayed on the terminal. Now we will see how to perform some operations on the files.

1) How to find all 777 permission directories and use chmod command to set permissions to 755.
find ./ -type d -perm 777 -print -exec chmod 755 {} \;
Another method using xargs is
find ./ -type d -perm 777 - print | xargs chmod 755

2) How to find a single file called tecmint.txt and remove it.
find . -type f -name "tecmint.txt" -exec rm {} \;
Another method using xargs is
find ./ -type d -perm 777 - print | xargs rm

3) Find the files which have the name ".sv" in it and then display only the files which have "class" word in them?
find -name "*.sv" -exec grep -H class {} \;
Another method using xargs is
find -name "*.sv" | xargs grep "class"

4) How to find  files which have the name ".svi" or ".svh" in it?
find ./ -type f  \( -name "*.svi" -o -name "*.svh" \)
Explanation from the POSIX spec:
! ( expression ) : Negation of a primary; the unary NOT operator.
( expression ): True if expression is true.
expression -o expression: Alternation of primaries; the OR operator. The second expression shall not be evaluated if the first expression is true.
Note that parenthesis, both opening and closing, are prefixed by a backslash (\) to prevent evaluation by the shell. 
Another method is
find /path/ -name '*.png' -or -name '*.jpg'
which will find the files with .png or .jpg extensions.

5) How to find  files whose name are not ".swp" or ".swo"?
find ./ -type f ! \( -name "*.swp" -o -name "*.swo" \)



No comments:

Post a Comment