Monday, 20 August 2012

Master on Find Command in Linux


This command is an extremely handy tool for programmers in shell scripting and various other system administrative tasks. In fact, you will save a lot of time using the find command which would have otherwise been wasted trying to find the file. With the option of imposition of various criteria whilst searching for files, find is the ideal command to look for.
There are several versions of find e.g. POSIX find, AIX find, GNU find etc. Since we are concerned with Linux, this post will be based on GNU find.

1. Using find command
$ find myFile.txt
Search for myfile.txt in the current directory.
$ find . -name myFile.txt
Search for myFile.txt in the current directory and its sub-directories.
Here, ‘.’ represents the current directory.
You can specify many places to search, for e.g.
$ find  /home  /usr .  -name “*.txt”
Search all files with .txt extension in /home, current directory and /usr.
To search files without case sensitivity, use
$find . -iname myFile.txt

2. With Wildcards
$ find /home -type f -name myFile*
Search for all the files whose filename starts with myFile in the home directory and its sub-directories.
-type f : to search for files only
$ find /home -type d -name *john
Similarly, it will search for all the directories with directory name ending with john.
$find /home -type f -name [ldt]uck
It will search for files with filenames luck or duck or tuck in tje home directory and its sub-directories.
$find . -type f -name ?uck
By introducing ‘?’ in the above example, find command searches for a 4 digit filename whose initial letter can be any character.
Check out:
$find . -type f -executable -name f*ball
$find . -type f -name f*b*
$find . -type f -writable -name *woo*
$find . -type l -readable -name *.jpg   (-type l : for link )
3. With Date / Time
Find command permits you to search for files based on
(*) last data modification time ( mtime or mmin )
(*) last access time ( atime or amin )
(*) last status changed time ( ctime or cmin)
a. mmin / mtime
$ find /home -mmin -10
Search for all the files whose data was modified less that 10 minutes ago.
$ find /home -mmin 10
Search for files whose data was modified exactly 10 minutes ago
$ find /home -mmin +10
Search for files whose data was modified more than 10 minutes ago
Similarly , if you use mtime instead of mmin, find command will search for files modified in 24 hour periods.
$ find /home -mtime -1
Search for files modified in the last 24 hrs.
b. amin / atime
$find /home -amin -10
Similarly, it will search for files that were accessed within 10 minutes ago.
$find /home -atime +10
Search for files accessed more than 10 days ago.
c. cmin / ctime
$find /home -cmin 10
It will list the files whose status (i.e. change in the ownership or access permissions) was changed exactly 10 minutes ago.
$find /home -ctime -10
Search for files whose status was changed less than 10 days ago.
Remember: You always have the option of combining these options.
$find /home -atime +2 -amin -10
(search for files that were were last accessed 2 to 10 minutes ago)
4.  –exec parameter
The -exec parameter defines what to do with the file. This is indeed a handy and an important option to learn.
$find /home -empty -exec rm {} \;
( search for empty files in home directory and its sub-directories and remove them using rm command )
$find /home -name “*.doc” -exec ls {} \;
( search for document files and list them );
$find /home -name “*.doc” -ok rm {} \;
( search for document files and remove them. But it will prompt a question mark after each file).
5. With Permissions
$find . -perm 644
(search for files with permissions 644 ie read and write permission for owner, read permission for group and other users.)
$find . -perm -644
( same as above but without regard to the presence of any extra permission bits, eg. the executable bit )
$find . -perm /444
( search for files which are readable by somebody ie. owner or group or anybody else )
Remember :      $find . -perm -440
$find . -perm -u+r,g+r
$find . -perm -u=r,g=r
All three commands search for the files which are readable by both their owner and their group.
Similarly,          $find . -perm /440
$find . -perm /u+r,g+r
$find . -perm /u=r,g=r
All three commands search for the files which are readable by either their owner or their group.
6. Operators
Listed in order of decreasing precedence:
a. ( expr )
$ find /home \( -size +200c \)
Search for files with size greater than 200 bytes (c is for bytes).
b. ! expr  :  True if expr is false
$ find /home \! -perm 644
It will not list the files or directories with permission 644.
c. expr1 expr2  :  expr2 is not evaluated if expr1 is false
$ find /home -perm 644 -size -2k
Search for files with permission 644 and size less than 2 kilobytes.
( expr1 -a expr is same as expr1 expr2
i.e. $find /home -perm 644 -a -size 2k  is same as
$find /home -perm 644 -size 2k )
d.  expr1 -o expr2 : expr2 is not evaluated if expr1 is true.
$find /home -size 6M -o -size 1G
Search for files which is 6 Megabytes or files with 1 Gigabytes.
e.  expr1 , expr2  : both expr1 and expr2 are always evaluated.
$find /home -name “*.doc” -print , -name “*.pdf” -print

7. With I/O Redirection and Pipes
$find / -size +4M > list_of_files.txt
Search for files greater than 4 Megabytes and redirect the standard output to the file list_of_files.txt.
$ find /home -size +4M | wc -l
Search for files greater than 4 Megabytes and count number of files (by counting number of lines).

8. With Users and Groups
Search for files belonging to a user
$find / -user rabi “*.doc”
Search for files belonging to a group
$find / -group fortystones “*.doc”
Search for files that do not belong to any user
$find / -nouser “*.pdf”
Search for files that do not belong to any group
$find / -nogroup “*.pdf”

9.
$find / -newer mydoc.doc
Search for files that were modified more recently than mydoc.doc.
$find / -anewer mydoc.doc
Search for files that were last accessed more recently than file mydoc.doc was modified.
$find / -cnewer mydoc.doc
Search for files’ status was last changed more recently than file mydoc.doc was modified.
$find / -used 2
Search for files that were last accessed 2 days after its status was last changed.
10. With -print, -print0, -printf
$find / -name “*.pdf” -print
(print to the standard output, followed by a newline )
$find / -name “*.pdf” -print0
(print to the standard output, followed by a null  character instead of a newline )
$find / -name “*.pdf” -printf “%g %s %p\n”
Search for pdf files and print to the standard output, its group name , size in bytes and the filename. Here \n is for new line.
For more options regarding printf, refer manual page of find.
e.g. %a : file’s last access time
%c  : file’s last status change time
%d  : file’s depth in the directory tree
%m : file’s permission bits
%t   : file’s last modification time
%u  : file’s user name etc.
Until you do not play with the find command by going through its manual page, experimenting with its options, mixing these options together, you will not be able to use this wonderful command confidently.

No comments:

Post a Comment