Tuesday 7 February 2012

find command in linux

It searches through the root filesystem ("/") for the file ctechz
# find / -iname ctechz -type f -print

If it finds -print the location to the screen.

And if you change the type option f to d it search only the directoy called ctechz
# find / -name ctechz -type d -print

modern linux system no longer need the argument -print
-f return files
-d return directory

Searches only /opt nad /home for files or directory
# find /opt /home -iname ctechz -type d -print

To search in the current directory
# find . -iname ctechz -type d  -print

Search anything that starting with ctechz
# find / -iname "ctechz*" -type d  -print

Finding files that do not match a pattern
 provide a -not argument with it
# find . -type f -not -name '*.txt'

Find files by modification time
# find . -mtime -7 -type f
# find . -mtime -7 -type d
# find /var/spool -mtime +60
                  -mmin
 Find every file under the directory /var/spool that was modified more than 60 days ago and 7 respectively

# find . -mtime 1 
find files modified between 24 and 48 hours ago

Find files with different file extensions
# find ctechz/ -type f \( -name "*.sh" -o -name "*.c" \)

add more "-o" options for each filename extension.
Find the passwd file under root and one level down
 # find -maxdepth 2 -name passwd

(i.e root — level 1, and two sub-directories — level 2 and 3 )

Find all empty files (zero byte file) in your home directory and its subdirectory
# find . -maxdepth 1 -empty

Finding the Top 5 Big Files
# find . -type f -exec ls -s {} \; | sort -n -r | head -5

Find Files by Size
# find ~ -size +100M
# find ~ -size 100M
# find ~ -size -100M

Find with exec and xargs
Note the use of xargs, a handy utility that coverts a stream of input (in this case the output of find) into command line arguments for the supplied command (in this case tar, used to create a backup archive).

The -exec action takes a Unix command (along with its options) as an argument. 

# find . -type f -name "file*" -exec grep -l \<iframe {} \;
files starting with file and contain character <iframe. here \ before <iframe to ignore the special character. 

Take an action on files you find
# find /usr/ -name "*.html" -type f -exec chmod 644 {} \;
# find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755{}\;

listing files using exec and ls 
# find ctechz/ -iname "file*" -type f -exec ls -ld {} \; 

exec ls -l {}' means run the "ls -l" command on this set of files.

The '\;' at the end of the command is an obscure syntax that is required by the find command when used with exec. 
 
 

Find and delete files or directory
# find ctechz/ -iname "file*" -type f -exec rm -rf {} \;

Find and copy
# find ctechz/ -iname "file*" -type f -exec cp {} /bkp/ \;

# find / -type f -mtime -7 | xargs tar -rf weekly.tar

This command will efficiently remove all files named core from your system
# find / -name core | xargs /bin/rm -f
# find / -name core -exec /bin/rm -f '{}' \; # same thing
# find / -type f -name *.zip -size +100M -exec rm -i {} \;"


No comments:

Post a Comment