Monday, July 09, 2012

'find' command with examples

The find command is used to locate files on a Unix or Linux system. 'find' will search any set of directories you specify for files that match the supplied search criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria. The search is recursive in that it will search all sub-directories too.
 
Working with files & directories
 
# find / -name my_text_file -print  
/* will search for a file 'my_text_file' in the root and its subdirectories */
# find . -type f -print
/* will search for all files in the current and its subdirectories */
# find . -name rman\*dbuat01
/* will search for files or directories that begins with rman and ends with dbuat01 e.g. 'rman_full_bkp_dbuat01'. don't forget to include '-name' clause. otherwise won't work. */
# find . -name \*.sh
/* will search for files that ends with '.sh' extention. */
# find . -iname \*.sh
/* will search for files (case insensitive) that ends with '.sh' extention */
# find . -type d -print
/* will search for all directories in the current and its subdirectories */
# find . /tmp /var/tmp $ORACLE_HOME -name dbuat01 -print
/* will search for a file named 'dbuat01' in multiple directory locations */
# find . -type d -mtime -7 | xargs tar -rf weekly_backup.tar # gzip weekly_backup.tar # tar -zxvf weekly_backup.tar.gz
/* 1. will tar all files or folders. '-r' option appends files to an archive 2. will zip the target tar file 3. will unzip and untar the archive to the current location from where it is run */
# find . -name dbuat01_bkp | xargs /bin/rm -f # find . -name dbuat01_bkp -exec /bin/rm -f '{}' \;
/* These two commands are the same. These commands will force the removal of files named 'dbuat01_bkp' from the current or sub-directory locations. */
# find / -mmin -10
/* will search for files modified in less than 10 minutes ago. */
# find . -name \r* -user oracle -print
/* will search for files or directories that begins with r* and owned by user oracle */
# find . -perm -o=w
/* will display files or directories having write permission for other users */
# find . -mtime 0
/* files modified between now and 1 day ago (i.e. within the past 24 hrs) */
# find . -mtime -1
/* files modified less than 1 day ago (i.e. within the past 24 hrs */
# find . -mtime 1
/* find files modified between 24 and 48 hrs ago */
# find . -mtime +1
/* files modified more than 48 hrs ago */
# find . -mmin +5 -mmin -10
/* files modified more than 5 minutes ago and less than 10 minutes ago */
# find -type d -exec ls -d1 {} \; | cut -c 3-
/* will search for only directories and will display with full details */
# find . -mtime 1
/* find all files modified exact 1 day */
# find . -mtime -1
/* find all files modified less than 1 day */
# find . -mtime +1
/* find all files modified more than 1 day */
# find . -perm 644
/* find all files with file permission 644 */
# find . -name "*.tmp" -print | xargs rm -f
/* search for *.tmp and remove it */
# find . -maxdepth 1 -type f -newer first_file
/* using -maxdepth */
# find . -type f -cmin 15 -prune
/* */
# find . -size +1000c -exec ls -l {} \;
/* */
# find . -size +10000c -size -50000c -print
/* 10000 bytes, 50000 bytes */
# find . -mtime +10 -size +50000c -exec ls -l {} \;
/* */
# find . -type l -print | xargs ls -ld | awk '{print $10}'
/* */
# find . -name "*equity*" -print
/* */
# find . -name "*equity*" -print0 | xargs -0 ls -l
/* */
# find . -name "*arc_jul12*" -print | xargs -0 ls -l
/* */
# !find
/* will execute the last find command */
# find . -type f -exec ls -s {} \; | sort -n -r | head -1
/* largest file in the current directory and sub-directories */
# find . -type f -exec ls -s {} \; | sort -n | head -1
/* another method */
# find . -type f -exec ls -s {} \; | sort -n -r | tail -1
/* smallest file in the current directory and subdirectories */
# find . -maxdepth 1 -type d
/* only directories in the current location */
# find . -size 10M
/* files having size exactly 10M */
# find . -size +10M
/* files having more than 10M */
# find . -size -10M
/* files having less than 10M */
# find . -type f -mtime -2 -exec du -sk {} \; | sort -rn
/* displays the files modified in reverse order by size modified in the last 24 hrs */
# find /u01/oradata/dbuat01/archive -iname *.arc -type f -mmin +15 -exec gzip {} \;
/* will zip archives modified more than 15 minutes ago */
# find /u01/oradata/dbuat01/backup/ -type f -mtime +6 -exec rm {} \;
/* */
 
[ Top ]
 
Last modified: Jul 03, 2012 10:45 IST

No comments: