Tuesday, March 12, 2013

Linux Find Command With Example

ABOUT FIND COMMAND :-
:->>  Find command is used to locate files on Linux system. Find command can search any set of directories you specify for files that match provided search criteria. We can search file by name size user permission etc. find provide the accurate result
                                        
The Linux Find Command is one of the most important and much used command in Linux systems. Find command used to search and locate list of files and directories based on conditions you specify

 1 -To Find all Passwd File in the system
 #find / -name passwd

2 -Find File Under A particular Directory   (Taking an example of directory /etc)
#find /etc -name passwd


3 -Find File using name Ignoring the upper-case and lower-case
#find / -iname PassWD          (Upper Case and Lower Case)

-Find Directories by Using Name
[root@station110 ~]# mkdir aa            (Create a directory aa)
[root@station110 ~]# cd /home/
[root@station110 home]# mkdir aa      (Create a directory in /home)
[root@station110 home]# cd
[root@station110 ~]# find / -type d -name aa
the output will be:-
/usr/share/locale/aa
/root/aa
/home/aa

5-find all .txt file
[root@station110 ~]# find . -type f -name "*.txt"
./.subversion/README.txt
./abc.txt
./vinod.txt

6-Find all files with using Permission
if we know only permission of the file then
#find / -perm 642
      or
# find . -type f -perm 642 -print
[root@station110 ~]# find / -perm 642
/root/ab.txt

 7-Find all file without permission of 642
# find  / -type f! -perm 642 -print

8 -Find Read Only Files
# find / -perm /u=r
Find Executable file
#find / perm /a=x

9 -Find the Empty file
[root@station110 ~]# find /tmp/ -type f -empty

10-Find and remove the Multiple File in single step
#Find . -type f -name "*.mp3" -exec rm -f {} \;
          or
#Find . -type f -name "*.gpg" -exec rm -f {} \;

11-Find all empty directories
 # find /tmp/ -type d -empty

12 -Find all the hidden files in the system
#find /tmp -type f -name ".*"

13 -Find files based on a particular user name
#find / -user root -name vinod.txt

14 -Find all the files based on the user (username)
#find /home -user vinod  (Here Vinod is a username)

15 -Find a particular file of a user
#find /home -user  redhat -iname "*.txt" (Redhat is username)

16 -Find all file based on group
#find /home -group developer(Group Name)

17 -Find last 50 days modified Files
#find / -mtime 50(find all files which are modified 50 days back)
     or
#find / -atime 50 (accessed)
   or
#find  / -mtime +50 -mtime -100 (find all file b/w 50-100 days)

18 Find -changed file in Last 1 Hour
#find / -cmin -60


19 -find modified files in last 1 hour
#find / -mmin -60
                 or
#find / -amin -60 (access file)

20 -Find files by using a particular size
#find / -size +32M   
                  or
# find / -size +50M -size -100M  (file size b/w 50-100MB)
          
21- Find the file with size and delete (all in single step)
find / -size +100M -exec rm -rf {} \; (find and delete)

3 comments: