4
Posted October 26, 2009 by Spyros in Linux Tips
 
 

Use The Find Command Like a Unix Professional

helping-the-locals-dig
helping-the-locals-dig

The true beauty about Unix commands is that they can give so much power to the users who spend some time to learn more about them. There are actually dozens of commands that are pretty useful for unix/linux users, so that if one were to write a separate article for each one of them, it would take some serious writing to cover up everything.

As you can understand, i cannot really be writing about every unix command that exists but i’ve decided to create some posts in order to help people use some of the most important commands in a good manner. At this post, i will be discussing the usage of the very powerful find command that if used properly can really do some heavy work on our behalf.

The Find Command – Simple Usage

The find command in its simplest form does exactly what you can imagine. It helps us locate files on our operating system. Its usage is pretty simple. You first specify the path that you want to search for file(s) and then you provide the name of that file using the switch -name. So, for instance let’s say that you have a folder named “subversion” somewhere in your home folder and want to find out where it is exactly. You would simply need to execute a command like this :

find ~ -name subversion

That could return something like “/Users/hthought/subversion” if you’re on a Mac OS or “/home/hthought/subversion” if you are on Linux. So, now you now the complete path of that folder.

Search Using Regular Expressions

Most times that we need to use find is because we need to find a file that we partly remember its full name. Let’s suppose that there is a file that we need to locate that we remember that had the word “picture” inside its filename. We don’t know anything about it but we need to find where it resides to. Find can help us do that quite easily. We just have to specify a simple regular expression like:


find / -name "*picture*"

Notice that we begin the search from the root path of our os installation. This actually means 2 things. First of all, we will be searching for that file in the entire system and secondly, we will need to run it as superusers (root), because it would need to read root protected files too. Then, we use -name and provide a simple regular expression like “*picture*”. Please notice that whenever you provide a regular expression to find, you will need to use quotes. Now, this expressions means something like : “Search for files or folders that are named under whatever character(s) prefix, followed by picture and then whatever character(s) postfix”. Therefore, results like “picture123d” or “asdapicturecda” would be valid searches that find would print.

Search According to File Size, User of Group

Sometimes you need to find a file of certain size constraints or just one that belongs to a certain user or group. This is actually pretty easy. Let’s suppose that we need to locate all jpg files of size less than 500KBytes that belong to the user hthought or/and the group photo. We would just execute this simple command:  (make sure the group and user exist)

find / -name "*.jpg" -size 500k -name hthought -group photo

Isn’t that really simple but effective ?

The Power of Exec – Advanced Find Usage

Now comes the real importance of the find command and that is the powerful exec switch. What this does is execute something that you specify it to execute to the files that find has located. So, suppose that you have a folder which contains lots of files and folders with even more files. What you would want to do is erase every file that ends in “.txt”. One could argue that using rm is possible in this situation but this is really not the case. rm cannot erase recursively given a certain condition like this. Therefore, find comes to the rescue, but how can it be used to do such a thing ?

First of all, whenever you are about to do something like erasing be sure to echo first. So, what we will do is first locate the whereabouts of the files we need to erase and just print them. Suppose that the folder we need to search for these files is located at ~/myfiles. The command to use would be like :


find ~/myfles -name "*.txt" -exec echo {} \;

This command will just echo the files that end in “.txt”. Now, once we are sure that these are the files we really want to remove, just execute the real remove command. You may also want to use the switch -ok in order to have the shell ask you whether you want to execute a certain command. Now that we know that we are about to remove what we really intended to remove at first place, we execute the real rm command :


find ~/myfiles -name "*.txt" -exec rm {} \;

Notice that {} actually refers to the filenames returned by the command find and these are passed to the execution of the rm command and the \; is actually the way to finish the command execution. What this does is that it erases every file in the folder myfiles (or its subfolders), that ends with “*.txt”. Each filename that find discovers is fed to the exec switch that consecutively executes rm and thus removing that file.

Exec is the real power behind find and it would be a good idea for you to do some practise on it. It can simplify your tasks in certain situations. Remember that it can do lots of things for you executing every possible command affecting the files you order it to affect. I would like to hear any questions about that or any situations and problems that arise with that and i will certainly try to help.

Thanx for reading another long tutorial, hope it helps you a bit.


Spyros