Find: Difference between revisions

845 bytes added ,  6 years ago
No edit summary
 
(14 intermediate revisions by the same user not shown)
Line 4:
 
 
= General Usage =
 
Find Text Files
find ~/test -name *.txt
 
Find empty files/folders
find ~/test -type df -empty
find ~ -type d -empty
 
Find all empty files (zero byte file)
Line 19 ⟶ 21:
List only the non-hidden empty files only in the current directory
find . -maxdepth 1 -empty -not -name ".*"
 
 
Find Files Using Name
find -name "MyCProgram.c"
 
Find Files Using Name and Ignoring Case
find -iname "MyCProgram.c"
 
Limit Search To Specific Directory
find / -name passwd
 
Find the passwd file under root and one level down
find -maxdepth 2 -name passwd
 
Find only the socket files
find . -type s
 
Find all directories
find .~ -type d
find ~/test ! -name *.jpg ! -name *.gif ! -name *.png ! -name *.jpeg ! -name *.JPG ! -name *.bmp ! -name *.html ! -name *.htm ! -type d
 
Find only the normal files
find . -type f
 
Find all the hidden files/directories
find .~ -type f -name ".*"
find ~ -type d -name ".*"
 
Find all the hidden directories
find -type d -name ".*"
 
List and Export only files recursively without Directory name
find . -maxdepth 10 -type f -printf '%f\n' > fileslist.txt
 
= Time based Sort =
Line 66 ⟶ 52:
find / -mtime -1
 
search files which are modified between last 10 to 20 days
find / -mtime -20 -mtime +10
Find files which got accessed within 60 minutes
find / -amin -60
find / -atime -1
 
Search files which are accessed between last 10 to 20 days
find /tmp -atime -20 -atime +10
 
Find files which changed within 60 minutes
Line 86 ⟶ 78:
find -mmin -60 -exec ls -l {} \;
 
Restricting the find output only to files
find /etc/sysconfig -amin -30 -type f
 
Do not display hidden files in find output
find . -mmin -15 \( ! -regex ".*/\..*" \)
 
= Delete Files =
Line 112 ⟶ 109:
 
 
= Size based Sort =
 
Find files that matches the exact given size
Search all jpg images in the system and archive it
find ~ -size 100M
find ~/test -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
 
 
Inverting the match
find -maxdepth 1 -not -iname "MyCProgram.c"
 
 
 
 
 
= Size based Sort =
 
Find files bigger than the given size
Line 132 ⟶ 120:
find ~ -size -100M
 
Search files whose size is greater than 10MB and less than 20MB
Find files that matches the exact given size
find ~/tmp -size 100M+10M -size -20M
 
Remove the files named a.out frequently
Line 144 ⟶ 132:
 
Removes *.zip files that are over 100M
find / -type f -name *.zip -size +100M -exec rm -i {} \;"
 
 
 
 
Restricting the find output only to files
find /etc/sysconfig -amin -30 -type f
 
Do not display hidden files in find output
find . -mmin -15 \( ! -regex ".*/\..*" \)
 
 
 
If multiple partitions mounted under /, this command will NOT search all mounted partitions
find / -xdev -name "*.log"
 
If you don’t want to see the errors and would like to redirect it to null
find -name "*.txt" 2>>/dev/null
 
 
 
= Exec Commands =
Line 195 ⟶ 164:
Executing Commands on the Files Found by the Find Command
find -iname "MyCProgram.c" -exec md5sum {} \;
 
 
= Advanced Options =
 
List and Export only files recursively without Directory name
find . -maxdepth 10 -type f -printf '%f\n' > fileslist.txt
 
If you don’t want to see the errors and would like to redirect it to null
find -name "*.txt" 2>>/dev/null
 
Search all jpg images in the system and archive it
find ~/test -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
 
Inverting the match
find -maxdepth 1 -not -iname "MyCProgram.c"
 
Find files of specific Extensions:
find . -name '*.jpg' -name '*.gif' -name '*.png' -name '*.jpeg' -name '*.JPG' -name '*.bmp' -name '*.html' -name '*.htm'
 
Find all files except for specific Extensions:
find . ! -name '*.jpg' ! -name '*.gif' ! -name '*.png' ! -name '*.jpeg' ! -name '*.JPG' ! -name '*.bmp' ! -name '*.html' ! -name '*.htm'
 
Searching all files with 777 permission
find / -type f -perm 0777
 
Search world readable files - everyone has only read access on that file (444 or -r–r–r– permission); numeric as well as u-g-o (user, group, others) format can be used with -perm switch
find / -type f -perm 444
find / -type f -perm /u=r -perm /g=r -perm /o=r
If you are suspecting some user is spamming files on server, you can search files with his ownership:
find / -type f -user aman
Similarly, files owned by specific group can be searched
find / -type f -group dba
 
<br />
;References
<references/>
<br />
<br />
<br />
 
 
{{DISQUS}}