Archive for the ‘find’ Category

8 Steps to Becoming A `find` Fu Master — Pt 2

Friday, 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:

  1. Learning to use object types
  2. Using command execution
  3. Using `find` to delete files
  4. 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

8 Steps to Becoming A `find` Fu Master — Introduction

Thursday, February 5th, 2009

(This is the first part in a three part series. Read part two here.)

Introduction:

Among the tools you will use on a Linux system, few are more valuable than the find utility from findutils. Forget search bars: This is a more highly tailored search system that can help you find exactly what you’re looking for with great ease. It will also permit you to perform operations upon the files that you find, delete them, or just list them. If you do more than just browse the Internet with your Linux box, you need some sort of proficiency with find.

Basics:
Find, at its simplest, will list everything in the filesystem beneath your current directory. This includes files, directories, symbolic links, FIFOs, sockets, and more other crap than you probably care to know about. Over 95% of your usage is going to be finding files and directories, so we’ll focus there.
If you open up a terminal and type
cd
find

You’ll receive a list of everything in the filesystem below your current directory.
Let’s limit that some. Let’s search for files that end in mp3.
find -name '*.mp3'

But wait, I know I had a file that ended in .MP3, why didn’t it show up? Well, the -name test is case sensitive. -iname is not. Let’s do the same thing with iname.

find -iname '*.mp3'

Ahh, there it is.
I want to search my videos directory for the video of my friend’s birthday party. I know it was named Fred-something. Now I can introduce a directory in the search.

find ~/Videos/ -name '*Fred*'

Hah. Fred is silly.

Well, that’s the most basic that you’ll get with find, grasshopper. Now you must prove your skills to the sensei!

Well, sort of. You just have to wait for the next part of the series.

-LightningCrash

(This is the first part in a three part series. Read part two here.)