Using the Unix/Linux Find command
Find command is a extremely useful command. you can search for any file anywhere using this command provided that file and directory you are searching has read write attributes set to you ,your, group or all. Find descends directory tree beginning at each pathname and finds the files that meet the specified conditions.Some Examples:
find $HOME -print will lists all files in your home directory.
find /work -name chapter1 -print will list all files named chapter1 in /work directory.
find / -type d -name 'man*' -print will list all manpage directories.
find / -size 0 -ok rm {} \; will remove all empty files on system.
conditions of find
- -atime +n |-n| n will find files that were last accessed more than n or less than -n days or n days.
- -ctime +n or -n will find that were changed +n -n or n days ago.
- -depth descend the directory structure, working on actual files first and then directories. You can use it with cpio command.
- -exec commad {} \; run the Unix command on each file matched by find. Very useful condition.
- -print print or list to standard output (screen).
- -name pattern find the pattern.
- -perm nnnfind files whole permission flags match octal number nnn.
- -size n find files that contain n blocks.
- -type c Find file whole type is c. C could be b or block, c Character special file, d directory, p fifo or named pipe, l symbolic link, or f plain file.
More Examples
Find 10 days old files of size 0
#/bin/ksh
echo " /tmp in /tmp/checkold.txt file"
find /tmp -size 0 -atime +10 -exec ls -l {} \; > /tmp/checkold.txt
find /tmp -size 0 -atime +10 -exec rm -f {} \;
Find large files in system
#!/bin/sh
# Script to find the big files on a disk.
# Defaults and temporary info.
# This script send the output to a default printer
DiskName=${1:?Disk name not supplied.}
TMPFILE=/tmp/$LOGNAME.$$.txt
SysName=`uname -n`
echo \
"Checking for large files on $SysName $DiskName. " \
"Output will be sent to default printer."
echo "\nNewer big files on $SysName $DiskName" >> $TMPFILE
find $DiskName -type f -mtime -3 -size +5000 -exec ls -ld {} \; \
2>/dev/null | sort -n -k 5.1,5 >> $TMPFILE
echo "\nOlder big files on $SysName $DiskName" >> $TMPFILE
find $DiskName -type f -mtime +3 -size +5000 -exec ls -ld {} \; \
2>/dev/null | sort -n -k 5.1,5 >> $TMPFILE
#change the following line if you want to put output to a file
#or anything.
lp -onb -olandscape $TMPFILE
rm $TMPFILE
# End of script
Find all the files named core and remove them. Saving output in mycompcore.txt file
#!/bin/ksh
find / -name core -group mycomp -print -exec rm -f {} \; >> mycompcore.txt

No comments:
Post a Comment