Thursday 26 March 2020

60 Examples of Linux find command

Using find command in Linux

Find becomes extremely useful when combined with other commands,

man find
info find
find . -type d -print  #list directories
find . -type f -print  #regular files
find . -type l -print  #symbolic files

find . -name
find . -iname #case-insensitive search
find /home -iname      #both capital and small letters
find . -exec ls -ld {} \;         #List out the found files
find . \( -name "*.mp4" -o -name "*.txt" \) -print
find /home/test -path "*/public/*" -print
find . -iregex ".*\(\.py\|\.sh\)$"
find . -type f -not -name "*.sh" #invert match
find . ! -name "*.txt" -print

find /tmp -type f -empty         #find Empty files
find /tmp -type d -empty find   #Empty directories
find /tmp -type f -name ".*"  #find hidden files
find ./ -type f -name "ab.txt" -exec grep 'This' {} \; #print lines which have ‘This’
find ./test -name 'abc*' ! -name '*.php' #combine multiple search criteria
find -name '*.php' -o -name '*.txt'
find ./test ./dir2 -type f -name "abc*"       #search multiple directories
find . -maxdepth 1 -name "f*" -print
find . -mindepth 2 -name "f*" -print

find . -type f -atime -7 -print    #accessed in last 7 days
find . -type f -atime 7 -print     #accessed in last 7th day
find . -type f -atime +7 -print    #accessed older that 7 days
find / -mtime +7 –mtime -10     #modified 7 days back and less than 10 days
find / -cmin -60  #files which are changed in last 1 hour
find / -mmin -60 #the files which are modified in last 1 hour
find / -amin -60 #files which are accessed in last 1 hour
find . -type f -amin +7 -print                  #accessed in last 7 mins
find . -type f -newer -print    #newer than a file

find . -type f -size +2k                      #file size
find / -size +50M -size -100M             #greater than 50MB and less than 100MB
find . -type f -exec ls -s {} \; | sort -n -r | head -5  #find largest
find . -type f -exec ls -s {} \; | sort -n | head -5     #smallest file
find . -type f -name "a.txt" -exec cp {} test/ \;     #find and move
find / -type f -size +100M -exec rm -f {} \;               #find all 100MB files and delete
find / -type f -name *.mp3 -size +10M -exec rm {} \;#find all .mp3 & delete more than 10MB
find / -name name.txt -exec rm -i {} \; #delete a file with confirmation
find . -type f -name *.swp -delete                 #delete

find / -perm 1551 #sticky bit files
find / -perm /u=s #SUID
find / -perm /g=s #SGID
find / -perm /u=r #read only
find / -perm /a=x # executable

find /etc -maxdepth 1 -perm /u=r
find . -type f -perm 644 -print    #permission
find / -type f ! -perm 777 #with out permission
find . -type f -name "*.php" ! -perm 644 -print #don't have permission levels
find / -type f -perm 0777 -print -exec chmod 644 {} \;  #change permission
find . -type f -user root -print #find based on user
find /data -owner smith #find all files owned by smith
find /home -group developer #find based on group
find . -type f -user root -exec chown test {} \; #find and change ownership

find ./ -type f -exec sed -i 's/find/replace/g' {} \; #find and replace
find / -name \*.txt -exec sed -i "s/one/five/g" {} \;
find . -type f -name "*.c" -exec cat {} \;>all_c_files.txt #all .c to .txt
find . -type f -mtime +10 -name "*.txt" -exec cp {} OLD \; #cp 10 days old OLD dir
find . -type f -name "*.txt" -exec printf "Text file: %s\n" {} \;  #list .txt

find / -type f -name "*.sh" -print0 | xargs -0 wc -l           #cout no of lines
find . -type f -name "*.txt" -print0 | xargs -0 rm -f          # remove files
find . -type f -name “*.java” | xargs tar cvf myfile.tar    #find and tar files
find [paths] [expression] [actions] 2>/dev/null          #finding error messages
find ~/documents -type f -name '*.txt' \ -exec grep -s DOGS {} \; -print   #grep

#remove duplicate files
find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD
find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate

No comments:

Post a Comment