Find Command Notes and Examples
Tags:
find <location> <criteria> <action>
criteria:
-atime n accessed n days ago
-mtime n modified n days ago
-size n[c] n blocks (a block is 512 bytes) if followed by 'c', the number
is in bytes
-type type file type: f=plain text, d=directory, etc...
-fstype type file system type: ufs, nfs, etc...
-name name name of file
-user user owner of file
-group group group of file
-o or
-mtime +10 modified more than 10 days ago
-atime -5 accessed less than five days ago
-size +100 larger than 100 blocks (50KB)
! negates criteria (! -name "test" matches files NOT named "test")
action:
-print print pathnames of matching files
-exec command execute command "command" on matched file
-ok command prompt before executing the command "command" on matched file
-prune don't descend into subdirectories
*executed commands must end with \;
see Reqular Expressions Reference for info on regular expressions
---------------------------------------------------------------------------------
examples
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
Search for file with a specific name in a set of files (-name)
find . -name test.file
searches in the current working directory for a file named "test.file"
find /etc -name test.conf
searches in /etc for a file named "test.conf"
---------------------------------------------------------------------------------
Search for a file and if found, execute a command
find /etc -name test.conf -exec chmod 775 {} \;
searches in /etc for a file named "test.conf" and if found, executes
'chmod 755' on that file
---------------------------------------------------------------------------------
Search for a string in files that match criteria
find . -name \*.log -exec grep "error" {} \;
searches in the current working directory for all files ending in ".log" and
then executes grep on those files to find the string "error"
---------------------------------------------------------------------------------
search multiple directories
find /etc /usr -name \*.log
searches /etc/ and /usr for any files ending in ".log"
---------------------------------------------------------------------------------
search for files older than a certain time and remove
find . -name \*.log -mtime +10 -exec rm {} \;
searches in the current working directory for all files ending in ".log" and
last modified over 10 days ago and removes those files
---------------------------------------------------------------------------------
Search for files that have not been accessed in a certain time and remove
(with prompting)
find /apps -atime +30 -ok rm {} \;
searches in the /apps directory for all files that have not been accessed in
over 10 days and prompts the user before removing those files
---------------------------------------------------------------------------------
search for files by access and modify time, and long list them
find /apps \(-mtime +10 -o -atime +20\) -exec ls -l {} \;
searches in the /apps directory for all files that have not been accessed in
over 20 days or not modified in over 10 days and long lists those files
---------------------------------------------------------------------------------
remove core files larger than a certain size
find /apps -type f -size +104857600c -name core -exec rm {} \;
searches in the /apps directory for files (-type f) named "core" that are
larger than 100mb (104857600 bytes = 100mb) and removes them
---------------------------------------------------------------------------------
»
- Add new comment
- 4833 reads
