8 Steps to Becoming A `find` Fu Master — Pt 2
February 6th, 2009(This is the second part in a three part series. Review the Introduction Here)
Now for the second installment of your `find` fu mastery! We are going to cover four principles today:
- Learning to use object types
- Using command execution
- Using `find` to delete files
- Limiting search depth
With no further ado,
1. Using object types in `find`
In the Linux/Unix filesystems there are more things than plain old regular files. Among then are symbolic links, directories, named pipes, block devices, character devices, and sockets. `find` will let you specify these items.
For instance, if I am looking for the baseq3 directory of my Quake3 installation, I can do the following:
find / -type d -name 'baseq3'
If I am looking for all symbolic links in a directory, I can do the following:
find . -type s
If I am looking for only files, I can specify ‘-type f’. This is handy by itself, but becomes more useful as you are executing commands for each of your search results. Which brings us to…
2. Using Command Execution
This is probably one of the most useful things that `find` has to offer. This is also one of the easiest ways to screw up your system and/or your day’s work. You have to be careful with this. Fortunately, `find` puts options in to help with that. We’ll cover those in a moment after we give some basics of command execution in `find`.
I have a directory full of PHP source code and I want to issue a find/replace on every file in the entire hierarchy to change a setting. The setting’s value is 12345 and I want to change it to 67890. You use the -exec option , put your command in, substitute the filename with {}, and put \; to denote the end of the command. Let’s take a look:
find ./ -type f -exec perl -pi -e 's/12345/67890/g' {} \;
This works great, except how do I know I’m not executing something that I don’t want to? Well,`find` has this covered with the ‘ok’ option.
find ./ -type f -ok perl -pi -e 's/12345/67890/g' {} \;
This will now prompt you to OK everything that is done, line by line. Useful for if you’re going to do something where you’d like to know which files it’s operating on. However, `find` will truncate part of the command if it’s too long, so you don’t get to see exactly what it’s doing. You can always put an echo in front of your commands to see them in their entirety before you execute them.
`find` also has an -execdir and -okdir command, which operate in a different manner. -execdir operates as if you `cd` into the directory and then run the command. This can be handy if you don’t want to do the filename substitution with {}, maybe you want to run something inside every directory. If you have a directory full of source called src, and maybe you want to run the `make` command to compile everything, you could issue a `find` statement like so:
find ./src -type d -execdir make all \;
This is a bit messy but it illustrates my point.
3. Using `find` to delete files
Speaking of dangerous operations, here’s another one you need to be very, very careful with.
But the easiest way to illustrate this would be to say maybe you downloaded a package for installation that contained a number of files that you do not want at all. Some shared packages come with .nfo files, .txt, .installs, .sfx, .subs, all sorts of unwanted garbage that you rarely want. Let’s allow `find` to delete that garbage, shall we?
I’m going to make these .sfx files disappear…
cd ~/Downloads/package/
find ./ -type f -name '*.sfx' -delete
Ta-da!
:jokerface:
4. Limiting Search Depth
So maybe you want to only go one directory deep. Maybe you don’t want to descend into any directories with your search. Maybe you want to search a filesystem without having `find` hop onto other drives in its search. `find` has got you covered like a fire team.
Descend only one directory:
find ./ -maxdepth 1
Don’t descend at all:
find ./ -maxdepth 0
Don’t search on filesystems or drives other than the current one:
find ./ -xdev
The above is particularly useful if you have a jump drive of some sort mounting in an inappropriate place.
Wrapping up.
Can you snatch the pebble from your filesystem’s hand?
We train again later, grasshopper.
-LightningCrash



