Find: Difference between revisions

no edit summary
No edit summary
Line 3:
<br />
 
= Time based Sort =
 
Created within 2 days
find ~/test -ctime -2 -print
 
Modified within 24 hours
find ~/test -mtime -1 -print
 
Find Text Files
Line 17 ⟶ 11:
find ~/test -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
Line 43 ⟶ 32:
Find the passwd file under root and one level down
find -maxdepth 2 -name passwd
 
Executing Commands on the Files Found by the Find Command
find -iname "MyCProgram.c" -exec md5sum {} \;
 
Inverting the match
find -maxdepth 1 -not -iname "MyCProgram.c"
 
Find files which has read permission to group
find . -perm -g=r -type f -exec ls -l {} \;
 
Find files which has read permission only to group
find . -perm g=r -type f -exec ls -l {} \;
 
Find all empty files (zero byte file)
find ~ -empty
 
List all the empty files only in your home directory
find . -maxdepth 1 -empty
 
List only the non-hidden empty files only in the current directory
find . -maxdepth 1 -empty -not -name ".*"
 
Finding the Top 5 Big Files
find . -type f -exec ls -s {} \; | sort -n -r | head -5
 
Finding the Top 5 Small Files
find . -type f -exec ls -s {} \; | sort -n | head -5
 
List the smaller files other than the ZERO byte files
find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5
 
Find only the socket files
Line 79 ⟶ 38:
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
Line 89 ⟶ 49:
find -type d -name ".*"
 
List and Export only files recursively without Directory name
Show files which are modified after the specified file
find . -maxdepth 10 -type f -printf '%f\n' > fileslist.txt
find -newer ordinary_file
 
= SizeTime based Sort =
 
Created within 2 days
Find files bigger than the given size
find ~/test -sizectime +100M-2 -print
 
Modified within 24 hours
Find files smaller than the given size
find ~/test -sizemtime -100M1 -print
 
Find files that matches the exact given size
find ~ -size 100M
 
Remove the files named a.out frequently
alias rmao="find . -iname a.out -exec rm {} \;"
$ rmao
 
Remove the core files generated by c program
alias rmc="find . -iname core -exec rm {} \;"
$ rmc
 
Removes *.zip files that are over 100M
find / -type f -name *.zip -size +100M -exec rm -i {} \;"
 
List only files recursively without Directory name
find . -maxdepth 10 -type f -printf '%f\n' > fileslist.txt
 
Find files in directory and sub-directories updated within 60 min
Line 130 ⟶ 73:
find . -cmin -60
find / -ctime -1
 
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 ".*/\..*" \)
 
Displays all files which are modified after the /etc/passwd files was modified
Line 149 ⟶ 86:
find -mmin -60 -exec ls -l {} \;
 
If multiple partitions mounted under /, this command will NOT search all mounted partitions
find / -xdev -name "*.log"
 
= Delete Files =
If you don’t want to see the errors and would like to redirect it to null
 
find -name "*.txt" 2>>/dev/null
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 _
Line 163 ⟶ 107:
Move Specific files to another location
find ./* -name *.jpg -exec mv {} ~/Public/ \;
 
 
 
 
 
 
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"
 
 
 
 
 
= Size based Sort =
 
Find files bigger than the given size
find ~ -size +100M
 
Find files smaller than the given size
find ~ -size -100M
 
Find files that matches the exact given size
find ~ -size 100M
 
Remove the files named a.out frequently
alias rmao="find . -iname a.out -exec rm {} \;"
$ rmao
 
Remove the core files generated by c program
alias rmc="find . -iname core -exec rm {} \;"
$ rmc
 
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
 
 
Line 178 ⟶ 177:
<br />
 
*List only Directories
 
Finding the Top 5 Big Files
find ~/test ! -name *.jpg ! -name *.gif ! -name *.png ! -name *.jpeg ! -name *.JPG ! -name *.bmp ! -name *.html ! -name *.htm ! -type d
find . -type f -exec ls -s {} \; | sort -n -r | head -5
 
Finding the Top 5 Small Files
find . -type f -exec ls -s {} \; | sort -n | head -5
 
List the smaller files other than the ZERO byte files
find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5
 
Find files which has read permission to group
find . -perm -g=r -type f -exec ls -l {} \;
 
Find files which has read permission only to group
find . -perm g=r -type f -exec ls -l {} \;
 
Executing Commands on the Files Found by the Find Command
find -iname "MyCProgram.c" -exec md5sum {} \;
 
<br />