Tuesday 5 February 2013

Fun with Find Command

Find a file and delete it. Replace foo.bar with the file or use wild cards *.bar
  $ find /home -name foo.bar -type f -exec rm -f "{}" ';'

When trying to find large files in / filesytem the find command will return results from other files systems. Try using the -dev option.
  $ find / -dev -size +3000 -exec ls -l {} ;
       
This will not return files in /usr/local file system. This will return files large then 3000 blocks only in the root file system.
  $ find /usr -dev -size +5000 -exec ls -l {} ;

Someone's done a 'cp -r' and filled up the filesystem. Here's a simple way to locate the unwanted files...
  $ find . -ctime -1 -print
   
Find has a partiularly powerful mechanism, -exec, which lets you execute commands on a per-file basis, while being selective about the files you're operating on. Lets say we want to delete all the *.bak or *.backup files which are older than 30 days. Type the following all on 1 line.
  $ find . ( -name '*.bak' -o -name *.backup ) -type f -atime +30 -exec rm '{}' ;

It would be even better if we only deleted the *.bak file if the original file was still available. In this case, we can use csh and test to help. Type the following all on 1 line.
  $ find . ( -name '*.bak' -o -name *.backup ) -type f -atime +30 -exec csh -c 'if ( -f $1:r ) rm $1' '{}' ;

or if you're really paraniod, use 'test -s' to verify that the original file has not been truncated. Type the following all on 1 line.
  $ find . -name '*.bak' -type f -atime +30 -exec csh -c 'test -s $1:r && rm $1' '{}' ;
        
Protecting users from themselves - file permissions
  $ find . -type l -exec gawk 'BEGIN { "ls -lag}' /dev/null ;

Find text in whole directory tree:
  $ find . -type f | xargs grep "text"

Find and Replace in whole directory tree:
  $ find . -type f [-name "..."] -exec perl -pi -e 's|xxx|yyy|g' {} ;
 

Files named filename.suffix
  $ find . -type f -exec grep -ls nohup.out {} \;
 
Greater than or equal to 100KB
  $ find . -size +100k -exec ls -l {} \; | awk '{printf $9, $5}'
 
Greater than or equal to 100KB. Sort the output numerically
  $ find . -size +100k -exec ls -l {} \; | awk '{printf "%40s %10s \n", $9, $5}' | sort -n +1
 
Changed in the last 7 hours
  $ find / -ctime -7 -print

Modified in the last 7 hours
  $ find / -daystart -mtime -7 -print
 
Files of type Directory
  $ find . -type d -print
 
Owned by user username
  $ find / -user jdoe -print 2>/dev/null

Owned by userid number
  $ find / -uid 227 -print 2>/dev/null
 
Prompt user before removing a file
  $ find / -name nohup.out -ok rm {} \;
 
Case insensitive search
  $ find . -iname -print
 
Find files under current directory named string.scr or string.prt
  $ find . \( -name "*.scr" -o -name "*.rpt" \) -print
 
Find core files. Prompt user for deletion
  $ find / -name core -ok rm {} \;

Files that end with 'log'
  $ find / -print 2>/dev/null | grep log\$

Files that start with 'law'
  $ find / -print 2>/dev/null | grep \^law
 
Files changed within the last hour
  $ find . -ctime -1 -exec ls -l {} \; 2>/dev/null
 
Files not changed within the last year. Prompt user for deletion
  $ find . -ctime +$(print $((365*20))) -exec ls -l {} \; -ok rm {} \;
 
Symbolic links
  $ find . -type -l -print
  $ find . -type -l -exec ls -l {} \;
 
Files older than 30 days
  $ cd /var/backup/logs; find . -daystart -mtime +30 -print
 
Files older than 30 days. Remove.
  $ find . -daystart -mtime +30 -print -exec rm {} \;
 
Files older than 30 days. Prompt for removal.
  $ find . -daystart -mtime +30 -print -ok rm {} \;

Edit all files that contain string
  $ vi `find . -name string -print 2>/dev/null`
  $ find . -name "1000318*" | xargs -n 10 rm


Changed in the last 20 hours, greater than 1MB in size, excluding some file systems
  $ find . -ctime -20 -size +1000k -print | grep -v \/usr | grep -v \/var | grep -v \/ora

No comments:

Post a Comment