Find
m (Reverted edits by 58.185.16.210 (Talk); changed back to last version by Jimbo) |
|||
(4 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | == The basic command == | ||
Used to [[find]] files or directories. For bare-bones help in finding a particular file or directory, use in conjunction with the '''-name''' argument: | Used to [[find]] files or directories. For bare-bones help in finding a particular file or directory, use in conjunction with the '''-name''' argument: | ||
Line 7: | Line 8: | ||
'''samizdata#''' find /usr/home -name "string" | '''samizdata#''' find /usr/home -name "string" | ||
− | With the above, you won't waste time and valuable system resources thrashing all hard drives looking for a file you know can't be anywhere other than /usr/home. | + | With the above, you won't waste time and valuable system resources thrashing all hard drives looking for a file you know can't be anywhere other than /usr/home. |
+ | |||
+ | == Refine results by [[pipe|piping]] through [[grep]] == | ||
+ | If you're receiving too many results and you want to narrow them down somewhat, you can pipe the results through [[grep]] for easier review - let's say you're looking for a file that contains "string", and you also know it contains "silly", but you don't know whether it's "sillystring" or "string,silly" or "silly_string" or... you get the idea. Try this: | ||
'''samizdata#''' find /usr/home -name "string" | grep "silly" | '''samizdata#''' find /usr/home -name "string" | grep "silly" | ||
Line 14: | Line 18: | ||
'''samizdata#''' find /usr/home -name "string" | grep "silly" | more | '''samizdata#''' find /usr/home -name "string" | grep "silly" | more | ||
+ | |||
+ | == Using find to delete files == | ||
+ | Let's say you want to delete all files and directories created more than 30 days ago in the current directory (notice that maxdepth is '''one''', not zero as you might think): | ||
+ | |||
+ | server# '''find . -ctime +30 -maxdepth 1 -delete''' | ||
+ | |||
+ | What if you only want to delete all ''files'', but not directories, in the current directory ''and'' all of its subdirectories? | ||
+ | |||
+ | server# '''find . -ctime +30 -type f -delete''' | ||
+ | |||
+ | What if we want to delete everything in the current directory that hasn't been accessed this year? | ||
+ | |||
+ | server# '''find . -atime +365 -maxdepth 1 -delete''' | ||
+ | |||
+ | == Doing things to the files you find == | ||
+ | |||
+ | The [[xargs]] command is a great tool to use with [[find]]: it allows you to do stuff to or with the files you've found. For example, if you wanted to place all files named .tmp in the /tmp directory: | ||
+ | |||
+ | server# '''find -name "*.tmp" | xargs -J % mv % /tmp | ||
Even after all this, we've barely scratched the surface of what you can do with [[find]] - it's a complicated beast that is well worth the time spent in investigating further via the [[man]] pages. | Even after all this, we've barely scratched the surface of what you can do with [[find]] - it's a complicated beast that is well worth the time spent in investigating further via the [[man]] pages. | ||
− | See also [[locate]] | + | == See also: == |
+ | [[locate]] is a far more optimized method of searching for files by substring, using a database generated by the weekly [[periodic]] routines rather than a brute-force search of the filesystem itself. | ||
+ | |||
+ | [[grep]] searches within files to find matches for a pattern that you give it, and with some care can even be made to give results pipeable directly to other commands, like [[find]]'s. | ||
[[category: System Commands]] | [[category: System Commands]] | ||
[[Category : New_User_Tips_and_FAQs]] | [[Category : New_User_Tips_and_FAQs]] |
Latest revision as of 12:45, 19 November 2007
Contents |
[edit] The basic command
Used to find files or directories. For bare-bones help in finding a particular file or directory, use in conjunction with the -name argument:
samizdata# find / -name "string"
will execute find on / and then only show you the results that match "string". Note that this is not a fast search and can take a very long time and burn a lot of system resources when used on large (or multiple) filesystems - in this example, every single device and filesystem on the machine will be groveled over directly looking for anything with "string" in the filename. If you knew that the file you wanted to find was in a user's home directory, for example, you would be much better off with the following:
samizdata# find /usr/home -name "string"
With the above, you won't waste time and valuable system resources thrashing all hard drives looking for a file you know can't be anywhere other than /usr/home.
[edit] Refine results by piping through grep
If you're receiving too many results and you want to narrow them down somewhat, you can pipe the results through grep for easier review - let's say you're looking for a file that contains "string", and you also know it contains "silly", but you don't know whether it's "sillystring" or "string,silly" or "silly_string" or... you get the idea. Try this:
samizdata# find /usr/home -name "string" | grep "silly"
Still getting more results than fit on the screen at once? Pipe it through more:
samizdata# find /usr/home -name "string" | grep "silly" | more
[edit] Using find to delete files
Let's say you want to delete all files and directories created more than 30 days ago in the current directory (notice that maxdepth is one, not zero as you might think):
server# find . -ctime +30 -maxdepth 1 -delete
What if you only want to delete all files, but not directories, in the current directory and all of its subdirectories?
server# find . -ctime +30 -type f -delete
What if we want to delete everything in the current directory that hasn't been accessed this year?
server# find . -atime +365 -maxdepth 1 -delete
[edit] Doing things to the files you find
The xargs command is a great tool to use with find: it allows you to do stuff to or with the files you've found. For example, if you wanted to place all files named .tmp in the /tmp directory:
server# find -name "*.tmp" | xargs -J % mv % /tmp
Even after all this, we've barely scratched the surface of what you can do with find - it's a complicated beast that is well worth the time spent in investigating further via the man pages.
[edit] See also:
locate is a far more optimized method of searching for files by substring, using a database generated by the weekly periodic routines rather than a brute-force search of the filesystem itself.
grep searches within files to find matches for a pattern that you give it, and with some care can even be made to give results pipeable directly to other commands, like find's.