Find: Difference between revisions

696 bytes added ,  6 years ago
(updated)
 
(16 intermediate revisions by the same user not shown)
Line 3:
<br />
 
= Time based Sort =
 
= General Usage =
Created within 2 days
find ~/test -ctime -2 -print
 
Modified within 24 hours
find ~/test -mtime -1 -print
 
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)
= Delete Files =
find ~ -empty
 
List all the empty files only in your home directory
Delete empty folders
find ~/test. -typemaxdepth d1 -empty -exec rmdir {} \;
find ~/ -type d -empty -delete
 
List only the non-hidden empty files only in the current directory
Find and Delete .ini & .db files
find ~/test. -maxdepth 1 -empty -not -name *".ini*"
find ~/test -name *.ini -delete
find ~/test -name *.db
find ~/test -name *.db -delete
 
Search all jpg images in the system and archive it
find ~/test -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
 
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 all directories
Executing Commands on the Files Found by the Find Command
find ~ -type d
find -iname "MyCProgram.c" -exec md5sum {} \;
 
Find only the normal files
Inverting the match
find . -type f
find -maxdepth 1 -not -iname "MyCProgram.c"
 
Find all the hidden files/directories
Find files which has read permission to group
find . -perm -g=r~ -type f -execname ls -l {} \;".*"
find ~ -type d -name ".*"
 
= Time based Sort =
Find files which has read permission only to group
find . -perm g=r -type f -exec ls -l {} \;
 
Created within 2 days
Find all empty files (zero byte file)
find ~/test -emptyctime -2 -print
 
Modified within 24 hours
List all the empty files only in your home directory
find .~/test -maxdepthmtime -1 -emptyprint
 
Find files in directory and sub-directories updated within 60 min
List only the non-hidden empty files only in the current directory
find . -maxdepth 1mmin -empty -not -name ".*"60
 
Finds all the files updated within 1 day
Finding the Top 5 Big Files
find / -mtime -1
find . -type f -exec ls -s {} \; | sort -n -r | head -5
 
search files which are modified between last 10 to 20 days
Finding the Top 5 Small Files
find ./ -type fmtime -exec ls20 -smtime {} \; | sort -n | head -5+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
List the smaller files other than the ZERO byte files
find /tmp -atime -20 -atime +10
find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5
 
Find files which changed within 60 minutes
Find only the socket files
find . -typecmin s-60
find / -ctime -1
 
Displays all files which are modified after the /etc/passwd files was modified
Find all directories
find . -typenewer d/etc/passwd
 
Displays all files which are accessed after modifying /etc/hosts
find -anewer /etc/hosts
 
Displays all files whose status got changed after modifying the /etc/fstab
find -cnewer /etc/fstab
 
Long list the files which are edited within the last 1 hour
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 =
 
Delete empty folders
find ~/test -type d -empty -exec rmdir {} \;
find ~/ -type d -empty -delete
 
Find and Delete .ini & .db files
find ~/test -name *.ini
find ~/test -name *.ini -delete
find ~/test -name *.db
find ~/test -name *.db -delete
 
Replaces space in all the *.mp3 files with _
find . -type f -iname “*.mp3″ -exec rename “s/ /_/g” {} \;
 
Delete 100 days old file
find * -mtime +100 -exec rm {} \;
 
Move Specific files to another location
find ./* -name *.jpg -exec mv {} ~/Public/ \;
 
Find only the normal files
find . -type f
 
Find all the hidden files
find . -type f -name ".*"
 
Find all the hidden directories
find -type d -name ".*"
 
Show files which are modified after the specified file
find -newer ordinary_file
 
= Size based Sort =
 
Find files that matches the exact given size
find ~ -size 100M
 
Find files bigger than the given size
Line 100 ⟶ 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 112 ⟶ 132:
 
Removes *.zip files that are over 100M
find / -type f -name *.zip -size +100M -exec rm -i {} \;"
 
= Exec Commands =
List only files recursively without Directory name
*Perform Any Operation on Files Found From Find Command
find . -maxdepth 10 -type f -printf '%f\n' > fileslist.txt
find <CONDITION to Find files> -exec <OPERATION> \;
 
Execute any Unix shell command/custom shell script/command on find command output files:
Find files in directory and sub-directories updated within 60 min
rm : remove the files found by find command.
find . -mmin -60
mv : rename the files found.
ls -l : get details of the find command output files.
md5sum : find command output files
wc : count the total number of words on find command output files.
<br />
 
Finds all the files updated within 1 day
find / -mtime -1
 
Finding the Top 5 Big Files
Find files which got accessed within 60 minutes
find . -type f -exec ls -s {} \; | sort -n -r | head -5
find -amin -60
find / -atime -1
 
Finding the Top 5 Small Files
Find files which changed within 60 minutes
find . -type f -exec ls -s {} \; | sort -n | head -5
find . -cmin -60
find / -ctime -1
 
RestrictingList the findsmaller outputfiles onlyother tothan the ZERO byte files
find /etc/sysconfig. -aminnot -30empty -type f -exec ls -s {} \; | sort -n | head -5
 
Find files which has read permission to group
Do not display hidden files in find output
find . -mminperm -15g=r \(-type !f -regexexec ".*/\..*"ls -l {} \);
 
Displays allFind files which arehas modifiedread afterpermission the /etc/passwd filesonly wasto modifiedgroup
find . -perm g=r -type f -exec ls -l {} \;
find -newer /etc/passwd
 
Executing Commands on the Files Found by the Find Command
Displays all files which are accessed after modifying /etc/hosts
find -iname "MyCProgram.c" -exec md5sum {} \;
find -anewer /etc/hosts
 
Displays all files whose status got changed after modifying the /etc/fstab
find -cnewer /etc/fstab
 
= Advanced Options =
Long list the files which are edited within the last 1 hour
find -mmin -60 -exec ls -l {} \;
 
List and Export only files recursively without Directory name
If multiple partitions mounted under /, this command will NOT search all mounted partitions
find . -maxdepth 10 -type f -printf '%f\n' > fileslist.txt
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
 
Search all jpg images in the system and archive it
Replaces space in all the *.mp3 files with _
find ~/test -name *.jpg -type f -inameprint “*.mp3″| -execxargs renametar “s/-cvzf /_/g” {} \;images.tar.gz
 
Inverting the match
Delete 100 days old file
find * -mtimemaxdepth +1001 -execnot rm {}-iname \;"MyCProgram.c"
 
Move SpecificFind files toof anotherspecific locationExtensions:
find ./* -name '*.jpg' -execname mv'*.gif' {}-name ~/Public/'*.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
= Exec Commands =
find / -type f -perm 444
*Perform Any Operation on Files Found From Find Command
find <CONDITION/ to-type Findf files>-perm /u=r -execperm <OPERATION>/g=r -perm \;/o=r
rm : remove the files found by find command.
If you are suspecting some user is spamming files on server, you can search files with his ownership:
mv : rename the files found.
find / -type f -user aman
ls -l : get details of the find command output files.
md5sum : find command output files
Similarly, files owned by specific group can be searched
wc : count the total number of words on find command output files.
find / -type f -group dba
Execute any Unix shell command/custom shell script/command on find command output files.
 
<br />
;References
<references/>
<br />
<br />
<br />
 
*List only Directories
<pre style="width: 97%; overflow-x: scroll;">
find ~/test ! -name *.jpg ! -name *.gif ! -name *.png ! -name *.jpeg ! -name *.JPG ! -name *.bmp ! -name *.html ! -name *.htm ! -type d </pre>
<br />
 
{{DISQUS}}