![]() |
Using the find Command
Command line skills are something you pick up over time. When something needs to be done, you work out how, and from then on you know how to do it. Few take the time to systematically learn the ins and outs of the tools at their disposal, however, and so may not be aware of all the possibilities of even the most basic utilities. Here we'll be looking at one of the staples of any shell toolbox: the find utility.
As the name suggests, find is a program for searching your disk for files and directories satisfying a given criteria. By default, it starts in the current directory and traverses down to all lower directories. Find is able to search based upon a number of different file attributes and also perform actions on the results, usually running some program for each result. Let's take a look at a few examples: firstly, to find all html files in the current directory or lower you would use: Code:
find -name "*.html" -type fIn this example we don't specify a location since we were looking in the current directory. You can start the search in any directory (or directories) on your system, for example if we know that the html files will be in one of two places then we could make the search quicker and more accurate by specifying a start point: Code:
find /var/www /home/pradeep/public_html -name "*.html" -type fFind traverses down all subdirectories by default, but you can control this behaviour by specifying the maximum depth. If in the previous case you wanted only to search those two directories but go no further you can add "-maxdepth 1" to the command. Setting the max depth to 0 means that only the files given on the command line will be tested. Similarly you can set a minimum depth, so that you can avoid files sitting in the root. Another use for find is to search for files belonging to a given user. So to search for all files belonging to me on my system I use: Code:
find / -user pradeepThe next category of tests relates to time, allowing you to search for files based on when they were last created, accessed or modified by using "-ctime", "-atime" and "-mtime" respectively. For example to find files created in the past day: Code:
find -ctime -1Code:
find -mmin -5Code:
find -user pradeep -iname "*.html" -lsLastly, you can get find to run any program on each result by using the "-exec" action. The following program will delete all files in your home directory with the extension ".tmp": Code:
find -name ".tmp" -exec rm {} \; |
Re: Using the find Command
ty for this man
|
Re: Using the find Command
Great article! People really underestimate the power of theses powerful usually overlooked apps.
|
Re: Using the find Command
I usually use "find . -name"
|
| All times are GMT +5.5. The time now is 10:31. |