Tuesday, October 2, 2018

find command examples

Find Command

Find Command:-The Linux find command is very powerful. It can search the entire Filesystem to find files and directories according to the search criteria you specify.

Basic Syntax :- find path option
 

Values for "path" :-
       1)  (.) denote current directory
       2)  (/) denote root directory or full system
       3)  (/path/) denote the path where you want to search 


"Options" for Find command :-
1) time    a) mtime
                 b) ctime
                 c) atime
 

2) min        a) amin
                    b) cmin
                    c) mmin
 

3) name       a) name
                     b) iname
4) size
5) perm
6) type
7) user
8) group

9) empty 
 

Examples:-
1) Find all empty files in /tmp directory
find /tmp -type f -empty

2) Find all empty directories in /tmp directory
find /tmp -type d -empty

3) Find all hidden files in /tmp directory
find /tmp -type f -name ".*"

4) Fill all files of User "oracle" in /tmp directory 
find /tmp -user oracle -type f

5) Fill all files of Group "developer" in /tmp directory
find /tmp -group developer -type f

6) Find all files whose modification times is 50-100 days older from today
find / -mtime +50 –mtime -100 -type f

7) Find all files those modified in last 1 hour
find / -mmin -60 -type f

8) Find all files those size is between 50MB - 100MB
find / -size +50M -size -100M -type f

9) Find all mp3 extension files those size is greater than 10MB 
find / -type f -name *.mp3 -size +10M

10) Find all mp3 extention files and copy to /tmp/Music directory
find / -type f -name "*.mp3" -exec cp {} /tmp/Music \;


11) Find files that don't have a pattern (.html)
find / -type f -not -name "*.html"
           (OR)
find / -type f ! -name "*.html"

12) Find all files in multiple directories (/opt /usr /var) with extension txt
find /opt /usr /var -type f -name "*.txt"

13) Find all files with different extensions 
find / -type f \( -name "*.c" -o -name "*.sh" \)

find / -type f \( -name "*.log" -o -name "*.xml" -o -name "*.html" \) 


Find with "exec" command :- 



14) Find all files having 777 permission and change it to 644
find / -perm 777 -type f -exec chmod 644 {} \;

15) Find all directories having 777 permission and change it to 755
find / -perm 777 -type d -exec chmod 755 {} \;

16) Find all files with extension java and having the "string" in its content
find / -type f -name "*.java" -exec grep -l string {} \;
                    (OR)
find / -type f -name "*.java" -exec grep -il string {} \;    

Note:-i option with grep search case insensitive

17) copy one file to many directories by find command
find dir1 dir2 dir3 dir4 -type d -exec cp header.html {} \;

18) Find all .err files older than 60 days and remove those files
find / -type f -name "*.err" -mtime +60 -exec rm -f {} \;

19) Find all directories older than 90 days and remove those   
find . -type d -mtime +90 -exec rm -rf {} \;


Find with "xargs" command :-  

20) Find all files older than 90 days and with extension mp3 and size is greater than 5MB, tar all those files
find / -type f -name "*.mp3" -mtime +90 | xargs tar cvf myfile.tar
                    (OR)
find . -type f -name '*.mp3' -mtime +90 -print0 | xargs -0 tar rvf music.tar

Note:-print0 helps handle spaces in filenames

No comments:

Post a Comment

Remove CTRL-M characters from a file in UNIX

 We can remove ^M by below methods 1st Method - Using sed command Type below command:    sed -e "s/^M//" filename > newfilena...