Find: Difference between revisions
No edit summary |
No edit summary |
||
(5 intermediate revisions by the same user not shown) | |||
Line 25: | Line 25: | ||
The <code> -type f </code> flag tells find to look for files ( <code> -type d </code> will tell find to look for directories.) | The <code> -type f </code> flag tells find to look for files ( <code> -type d </code> will tell find to look for directories.) | ||
<code>find /home/me/ -not -name "*.html"</code> This will look for all files NOT named *.html | |||
== By Date == | |||
You can find files in the current directory older than X days with: | |||
<code>find . -mtime +30 -print</code> | |||
To delete them: | |||
<code>find . -mtime +30 -exec rm -f {} \;</code> | |||
Line 43: | Line 56: | ||
<code>find -type f ! -perm 777</code> Look for anything that does NOT have 777 permissions | <code>find -type f ! -perm 777</code> Look for anything that does NOT have 777 permissions | ||
<code>find / -perm /u=r</code> Find read-only files. (Find -perm uses the same conventions as [[chmod]] ) | |||
== Find and Execute == | |||
The <code> -exec </code> flag will make find do something to what it finds. | |||
<code>find / -type f -perm 777 -print -exec chmod 644 {} \;</code> Find all files with 777 permissions. Run chmod 644 on the files that were found. | |||
What's all that stuff at then end after the command I want to run? | |||
<code> {} </code> each result of the find is put inside the brackets (The specified command (chmod 644 in this case) is run once for each matched file.) | |||
<code> \ </code> This prevents the shell from expanding the contents of <code> {} </code> | |||
<code> ; </code> Ends the execute flag | |||
== Find and Delete == | |||
<code>find . -type f -name "*.txt" | xargs rm</code> | |||
Latest revision as of 19:32, 23 April 2024
Find is used to search the directory system for files.
find location options search pattern
find /home myfile.txt
This will look in the /home folder for myfile.txt (if you leave out the location, find will look in the directory you are currently in.)
By Name
By default, find is case-sensitive. The above example will not find MYFILE.TXT. The -iname
flag tells find to ignore case.
If you want the search to be case insensitive:
find -iname myfile.txt
The above will find all of the following: myfile.txt MYFILE.TXT MyFile.txt (etc...)
By Type
find -type f -name "*.txt"
This will find all the files whose name ends in .txt in the current directory.
The -type f
flag tells find to look for files ( -type d
will tell find to look for directories.)
find /home/me/ -not -name "*.html"
This will look for all files NOT named *.html
By Date
You can find files in the current directory older than X days with:
find . -mtime +30 -print
To delete them:
find . -mtime +30 -exec rm -f {} \;
By Permission
You can have find files based on permissions:
find . -type f -perm 644
This will find files in the current directory with 0777 permissions
.
look in the current directory
-type f
look for files
-perm 644
look for permissions. What permissions? 644 (-rw-r--r--)
More Examples of finding by permissions:
find -type f ! -perm 777
Look for anything that does NOT have 777 permissions
find / -perm /u=r
Find read-only files. (Find -perm uses the same conventions as chmod )
Find and Execute
The -exec
flag will make find do something to what it finds.
find / -type f -perm 777 -print -exec chmod 644 {} \;
Find all files with 777 permissions. Run chmod 644 on the files that were found.
What's all that stuff at then end after the command I want to run?
{}
each result of the find is put inside the brackets (The specified command (chmod 644 in this case) is run once for each matched file.)
\
This prevents the shell from expanding the contents of {}
;
Ends the execute flag
Find and Delete
find . -type f -name "*.txt" | xargs rm