![]() |
|
![]() |
|
Stupid find Tricks |
Home > Resources > HACMP Resources Collection > Stupid find Tricks This page contains various examples of things that can be done with the find command. We call it Stupid find Tricks since some of them are pretty strange (I still have a ways to go so don't get too worried if none of the current ones strike you as pretty strange). Some of these can be quite useful to have in your mental toolkit when you're doing system administration on a HACMP cluster (or any Unix system, for that matter). Feel free to help expand and improve this list of find tricks. This page is part of the Matilda Team's HACMP Resources Collection. The home page of the collection is located here. IMPORTANT: read the disclaimer BEFORE you use any information provided in this collection. General TechniquesAvoiding NFS filesystems
Finding files which don't match a search request
Using search criteria that find doesn't support directlySometimes, you want to find files which match complex criteria which are difficult or impossible to express directly with the find command. In this case, write a shell script or program that performs the desired tests and use the find command's -exec option. For example, assuming that our criteria testing shell script is called doit, the following would do the trick:
A simple (and relatively common) variant of this is to use the grep command to perform the test. For example, the following will print the names of all .c files in the current directory which contain the word hello:
There's a better way to just get a list of the files that match the grep request in the above example. For example, the following will list the files with names ending in .c and containing the word hello:
The /dev/null is provided because some versions of grep don't list the file name unless at least two file names are specified (the /dev/null is not required on AIX's grep (since at least AIX 4.3.2) although we still provide it because we prefer to use techniques which are portable (the script may never be ported but we use lots of different kinds of Unix and can't keep track of these sorts of subtle differences between them)).
Note that the structure of the first example is still valid for many contexts. For example, the following will compile each of the .c files containing the word hello (yes, rather contrived but . . .):
Also, keep in mind that invoking a command or especially a shell script against each file in a directory hierarchy can be rather expensive. Use conventional find options (like in the hello example above) to eliminate as many candidates as possible before invoking the command or shell script with the -exec option.
An arguably better way of invoking grep on a whole pile of files is: find . -type f -print | xargs grep hello This results in far fewer invocations of grep (i.e. it runs faster). Doing complicated things with the files that you findfind is great at finding files matching particular criteria. What may not be obvious is that it is easy to then perform complex operations on the files that find finds for you. Write yourself a shell script that does the complex operations on a file which is passed as the first parameter and then invoke the script against each file that find finds like this:
Using grep to print certain lines in the files you find One common operation performed on found files is to use grep to extract certain lines from the files. At first glance, the following appears to print the lines containing the word hello in each of the found files:The problem is that since you've only specified a single file to the grep command, grep doesn't prefix each line with the name of the file that the line was found in. Since this is often quite important in this context, trick grep into showing the file name by giving it two files to look in but being sure that it won't find the pattern in the second file. Obviously, an empty file works best for this so we use everyone's favourite empty file /dev/null in our example: Applying a single invocation of a command on the list files that are foundHere's one of our favourites:This invokes vi on all of the .c files in the current hierarchy. Once invoked, you move on to the next file using the :n command from within vi.vi ` find . -type f -name '*.c' -print ` Getting cron and find to be friendsOne sometimes wants to put a find command into a crontab file. The problem is that find, the shell and cron parse their input lines in ways which aren't quite compatible. For example, the following crontab entry:is supposed to delete any file in /tmp which hasn't been modified for over 30 days (this may or may not be a good idea depending on your context so be careful). The problem is that cron strips off the \ before the line is handed to the shell which then treats the ; as a command separator and executes the find command as Solving Specific ProblemsWhy is this filesystem full?Here's a simple invocation which presents you with a list of all the files in the root filesystem with the largest files appearing first.IMPORTANT: If you lack the appropriate skills, experience and/or competency, are unwilling to take responsibility for your actions, or if you don't like these disclaimers then don't use this information.Replacing the / with the name of another filesystem will get you a list for that filesystem.find / -xdev -type f -ls | sort +6n -r | more
|
| © 2004 Matilda Systems Corporation |