Using find command in linux

0 / 335
How to use linux find command

Overview and introduction

The “find” command is one of the important commands in Linux. It makes easy for us to find a file or a directory.We can also find the unused files from last few days and delete it. “find” command also makes it easy to search for files which have been modified few hours or days back.

 
Below are the some of the examples of using  find command :
 
 
 
Find files using name :
 
 
[root@localhost ~]#find /root -name anaconda-ks.cfg
 
 
Find files ignoring the case
 
 
[root@localhost ~]#find /usr/src -iname  makefile
 
Find with mindepth and maxdepth i.e limit search to specific directory
 
 
The command below gives the list of passwd file in whole /
 
 
 
[root@localhost ~]#find / -name passwd
 
 
/usr/bin/passwd
 
 
/usr/share/doc/nss_ldap-264/pam.d/passwd
 
 
/usr/lib/news/bin/auth/passwd
 
 
/etc/passwd
 
 
/etc/pam.d/passwd
 
 
Find files with inode numbers
 
 
Lets create the test file and note down its inode number
 
 
[root@localhost ~]# touch test-file
 
 
 
[root@localhost ~]# ls -il test-file
 
 
 
1632299 -rw-r–r– 1 root root 0 2011-02-22 22:44 test-file
 
 
 
Now lets find the file by inode number
 
 
[root@localhost ~]# find -inum 1632299
 
 
 
./test-file
Find the  file by inode number  and rename it
 
 
[root@localhost ~]# find -inum 1632299 -exec mv {} new-test-file \;
 
 
 
[root@localhost ~]# ls -il
 
 
 
1632299 -rw-r–r–  1 root root     0 2011-02-22 22:44 new-test-file
 
 
 
Now if we want to search files just one directory level down
 
 
 
[root@localhost ~]#find / -maxdepth 2 -name passwd
 
 
 
 
/etc/passwd
 
 
 
 
If we want to search files two directories level down
 
 
 
/usr/bin/passwd
 
 
 
 
/etc/passwd
 
 
 
 
/etc/pam.d/passwd
 
 
 
 
If we want to find the files between sub-directory level 2 and 4
 
 
 
 
[root@localhost ~]#find / -mindepth 2 -maxdepth 5 -name passwd
 
 
/usr/bin/passwd
 
 
 
/etc/passwd
 
 
 
/etc/pam.d/passwd
 
 
 
 
Find the files and execute commands on them
 
 
 
 
first we create some files with .tmp and .c extension in /tmp directory 
 
 
 
 
[root@localhost ~]#cd /tmp
 
 
 
[root@localhost tmp]# touch file{1,2,3,4}.tmp
 
 
 
[root@localhost tmp]#touch file{1,2,3,4}.c
 
 
 
[root@localhost tmp]#ls 
 
 
 
 
file1.c    file1.tmp  file2.cpp  file3.c    file3.tmp  file4.cpp
file1.cpp  file2.c    file2.tmp  file3.cpp  file4.c    file4.tmp
 
 
 
Now lets search .tmp files and remove them
 
 
 
[root@localhost ~]#find /tmp -name *.tmp -exec rm -fr {} \;
 
 
 
Find all the files which has rw permission to owner/user
 
 
 
[root@localhost ~]#find  /root -perm -u=rw -type f -exec ls -l {} \;
 
 
 
 
Find files world writable and list them
 
 
 
[root@localhost ~]#find /root -perm -o=rw -exec ls -ld {} \;
 
 
Find file which has 666 permission:
 
 
[root@localhost ~]# find /root -perm 666 -type f -exec ls -l {} \;
 
 
 
No result as no file with 666 permission
Let’s create a file with with permission 666
 
 
[root@localhost ~]# touch file11
 
 
 
[root@localhost ~]# chmod 666 file11
 
 
 
[root@localhost ~]# find /root -perm 666 -type f -exec ls -l {} \;
 
 
 
-rw-rw-rw- 1 root root 0 2011-02-22 23:24 /root/file11
 
 
 
That’s all for this article. Stay tuned for more interesting stuff
 
 

Comments

comments


***Linux, Cloud & Devops Architect & Technical Content Writer*** I am a Linux Enthusiast and Supporter/Promoter of Open Source Technology with over 12+ years of experience in Linux, Cloud and Devops. I am A Technical Content writer for various sites like : Hostbread & Golibrary

Related Posts