find

linux

http://www.thegeekstuff.com/2009/03/15-practical-linux-find-command-examples/
http://www.linux.ie/newusers/beginners-linux-guide/find.php
http://content.hccfl.edu/pollock/unix/findcmd.htm
http://www.devdaily.com/unix/edu/examples/find.shtml
https://www.hackersgarage.com/find-linux-command-cheat-sheet.html

How can we find and delete files?

find -type f -name *.jsp.bak -delete
find -type f -delete // This is dangerous
find . -type f -empty

find . -type f -not -empty

find . -type d -empty
find . -type d -empty -exec rmdir {} \;

How can we find files that match a particular pattern in the name?

find ./ | grep '5.06.0.12'

How can we find files with size greater than 1000k?

find / -size +1000k

How can we find files that have been modified within 2 days?

find / -mtime 2

How can we find files with size greater than 1000k and list files in 'ls' listing format?

find / -size +1000k -ls

How can we find files with size greater than 1000k and output extra information about these files?

find / -size +1000k -printf "%r %k\n"

How can we find files older than a certain file and delete them?

find $PWD -type f ! -cnewer filename -exec rm {} \;

How can we find files older than a certain file and delete them (will ask you for confirmation)?

find $PWD -type f ! -cnewer filename -ok rm {} \;
find $PWD -type f -mtime +4 -name "*.log*" ! -name "submitter.log" -ok rm {} \;

How can we find files that are older than 3 days old and delete them?

find . -name "*.trc" -ctime +3 -exec rm {} \;

How can we find files using regex?

find $PWD -regex '.*apache_error.*' -print
find $PWD -regex '.*apache_access.*' -print -exec rm {} \;

How can we list all files and directories using the find command?

find directoryName
find directoryName -follow

The -follow option directs find to follow symbolic links to directories.

How can we delete each matching file as it is found?

-exec rm -f {}\;

What are the options for the find command?

Option Meaning
-atime n File was last accessed n days ago
-mtime n File was last modified n days ago
-newer file File was modified more recently than the specified file was
-size n File exactly n 512-byte blocks long
-type c Specifies file type: f=plain file, d=directory, etc
-fstype type Specifies filesystem types
-name name File has the given name
-perm p The permission mode is p
-user user The file owner is given
-group grp The file group owner is given
-nouser The file owner is not listed in /path/file
-nogroup The file group owner is not listed in /etc/group
-print Display the pathname of matching file
-exec cmd Execute the specified command on the file
-ok cmd Execute the specified command on the file after prompting for confirmation
-xdev Restrict the search to the filesystem of starting directory
-mount Same as -xdev for IRIX and SCO
-prune Do not descend into directories encountered
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License