Beyond Finding Stuff with the Linux find Command

1053

Continuing the quest to become a command-line power user, in this installment, we will be taking on the find command.

Jack Wallen already covered the basics of find in an article published recently here on Linux.com. If you are completely unfamiliar with find, please read that article first to come to grips with the essentials.

Done? Good. Now, you need to know that find can be used to do much more than just for search for something, in fact you can use it to search for two or three things. For example:


find path/to/some/directory/ -type f -iname '*.svg' -o -iname '*.pdf'

This will cough up all the files with the extensions svg (or SVG) and pdf (or PDF) in the path/to/directory directory. You can add more things to search for using the -o over and over.

You can also search in more than one directory simultaneously just be adding them to the route bit of the command. Say you want to see what is eating up all the space on your hard drive:


find $HOME /var /etc -size +500M

This will return all the files bigger than 500 Megabytes (-size +500M) in your home directory, /var and /etc.

Additionally, find also lets you do stuff with the files it… er… finds. For example, you can use the -delete action to remove everything that comes up in a search. Now, be careful with this one. If you run


# WARNING: DO NOT TRY THIS AT $HOME

find . -iname "*" -delete

find will erase everything in the current directory (. is shorthand for “the current directory“) and everything in the subdirectories under it, and then the subdirectories themselves, and then there will be nothing but emptiness and an unbearable feeling that something has gone terribly wrong.

Please do not put it to the test.

Instead, let’s look at some more constructive examples…

Moving Stuff Around

Let’s say you have bunch of pictures of Tux the penguin in several formats and spread out over dozens of directories, all under your Documents/ folder. You want to bring them all together into one directory (Tux/) to create a gallery you can revel in:


find $HOME/Documents/ ( -iname "*tux*png" -o -iname "*tux*jpg" -o -iname "*tux*svg" ) 
  -exec cp -v '{}' $HOME/Tux/ ;

Let’s break this down:

  • $HOME/Documents is the directory (and its subdirectories) find is going to search in.
  • You enclose what you want to search for between parentheses (( ... )) because, otherwise -exec, the option that introduces the command you want to run on the results, will only receive the result of the last search (-iname "*tux*svg"). There are two things you have to bear in mind when you do this: (1) you have to escape the parentheses using backslashes like this: ( ... ). You do that so the shell interpreter (Bash) doesn’t get confused (parentheses have a special meanings for Bash); and (2) there is one space between the opening bracket ( and -iname ... and another space between "*tux*svg" and the closing bracket ). If you don’t include those spaces, find will exit with an error.
  • -exec is the option you use to introduce the command you want to run on the found files. In this case it is a simple cp (copy) command. You use cp‘s -v option to see what is going on.
  • '{}' is the shorthand find uses to say “the file or directory I have found that matches the criteria you gave me“. '{}' gets swapped for each file or directory as it is found and, in this case, then gets copied to the Tux/ directory.
  • ; tells find to execute the command for each result sequentially, that is, one after another. There is another option, + which runs the command adding each result from find to the end of the command, making a long sausage of a string. But (1) this is not helpful for you here, and (2) you need the '{}' to be at the end of the command for this to work. You could use + to make executable all the files with the .sh extension tucked away under your Documents/ folder like this:
    
    find $HOME/Documents/ -name "*.sh" -exec chmod a+x {} +
    
    

Once you have the basics of modifying files using find under your belt, you will discover all sorts of situations where it comes in handy. For example…

A Terrible Mish-Mash

Client X has sent you a zip file with important documents and images for the new website you are working on for them. You copy the zip into your ClientX folder (which already contains dozens of files and directories) and uncompress it with unzip newwebmedia.zip and, gosh darn it, the person who made the zip file didn’t compress the directory itself, but the contents in the directory. Now all the images, text files and subdirectories from the zip are all mixed up with the original contents of you folder, that contains more images, text files, and subdirectories.

You could try and remember what the original files were and then move or delete the ones that came from the zip archive. But with dozens of entries of all kinds, you are bound to get mixed up at some point and forget to move a file, or, worse, delete one of your original files.

Looking at the files’ dates (ls -la *) won’t help either: the Zip program keeps the dates the files were originally created, not when they were zipped or unzipped. This means a “new” file from the zip could very well have a date prior to some of the files that were already in the folder when you did the unzipping.

You probably can guess what comes next: find to the rescue! Move into the directory (cd path/to/ClientX), make a new directory where you want the new stuff to go (mkdir NewStuff), and then try this:


find . -cnewer newwebmedia.zip -exec mv '{}' NewStuff ;

Breaking that down:

  • The period (.) tells find to do its thing in the current directory.
  • -cnewer tells find to look for files that have been changed at the same time or after a certain file you give as reference. In this case the reference file is newwebmedia.zip. If you copied the file over at 12:00 and then unpacked it at 12:01, all the files that you unpacked will be tagged as changed at 12:01, that is, after newwebmedia.zip and will match that criteria! And, as long as you didn’t change anything else, they will be the only files meeting that criteria.
  • The -exec part of the instruction simply tells find to move the files and directories to the NewStuff/ directory, thus cleaning up the mess.

If you are unsure of anything find may do, you can swap -exec for -ok. The -ok option forces find to check with you before it runs the command you have given it. Accept an action by typing y or reject it with n.

Next Time

We’ll be looking at environmental variables and a way to search even more deeply into files with the grep command.