Linux Blog

GNU find – A Multidimensional Tool

Filed under: Linux Software — TheLinuxBlog.com at 6:30 am on Tuesday, November 30, 2010

This is a guest post by Usama Hashimi. Usama is currently a student of MPhil in Economics and enjoys Learning about Linux, Surfing the Internet, watching movies and Listening to Music.

Beginners are mostly afraid of the command prompt. Whenever they see one, they immediately say “it’s very difficult”. But it’s not true. The command prompt is just as friendly as GUI, provided you use it with proper procedure.
Most people use GUI tools to search for their lost files. They don’t even know that they can use command line tools to search their files. GNU ‘find’ is such like a tool which can not only search files but can even copy, move or delete these files on the fly.
So let’s see that how ‘find’ works.

Find your lost files!

Let’s start from a simple example.
Suppose you want to search for a file named ‘master.txt’ in your home directory.
Open a Terminal and issue the following command:

find . -name "master.txt"

‘find’ will immediately show the results. If ‘find’ does not show any result, this means that the file, in our case, ‘master.txt’ does not exist.
It is not always the case that you want to find something in your home directory. The lost/desired file could be anywhere in your computer.
Suppose you want to find a file named ‘space-01.jpg’ and you only know that it’s located somewhere in the /usr directory. You can find it by issuing following command in a Terminal:

find /usr -name "space-01.jpg"

and ‘find’ will tell you that this is located under /usr/share/backgrounds.

You don’t know the exact name of the file? Use Wildcards.

So, you want to search for a file but you don’t know the exact name. Don’t worry! You can still locate the file using ‘GNU find’ and wildcards. Wildcards are a way of searching files when you don’t know much about the desired file.
One of the commonly used wildcard is asterisk (*).
Consider the following example to better understand the things.
Suppose you want to search a file named ‘Jumping_Flowers’ but you only remember the ‘Jumping’ part of the file name. So issue the following command in Terminal:

find . -name "Jumping*"

And it will bring all the files starting with the word ‘Jumping’.

You can use asterisk (*) anywhere with file name. For example:

find . -name "*Jumping*"

And it will bring all the files which contain the word ‘Jumping’.
Here are some more examples of use of a wildcard:

find . -name "Jumping*Flowers*"
find . -name "*Jumping*Fowres.mp3"

How to search files of different types?

Sometimes you are not looking for some specific file but you are looking for a group of files. For example, you may be looking for all the .txt files in your home directory.
To find all the .txt files, you will give the following command in Terminal:

find . -name *.txt

In case of mp3 files, the above command will be:

find . -name *.mp3

And so on…

When you want to search with respect to time.

If you want to search for files which where accessed last time, you can use -amin option with ‘find’. In this case you have to put a minus (-) sign before the time. The time here is in minutes. In order to search for .doc files which were accessed in last 10 minutes, you will give the following command:

find . -amin -10 -name "*.doc"

Similarly, to search for .doc files which were modified in last 20 minutes, you will use -mmin option as follows:

find . -mmin -20 -name "*.doc"

Search for files which are eating your hard disk space.

There may be files on your system which are not only huge in size but also located at a place where you can not manually locate them and you don’t even know that when they were last accessed. You have to use -size option with ‘find’ to locate them.
Let’s see how we can do this:

find . -size +100M

It will list all those files which are greater than 100 Megabytes. You can replace ‘M’ with ‘G’ (for Gigabyte) or with ‘k’ (for Kilobyte)

Copy, move or delete the unwanted files on the fly.

Copy – ‘find’ can also be used to copy or backup your files. You can use ‘find’ to copy certain files from one location to other with one simple command.
Suppose you want copy all of your mp3 songs from your home directory to your Windows Partition. Enter the following command in Terminal:

find . -name "*.mp3" -exec cp {} /path/to/Windows_Drive \;

And all of your mp3 files will be copied to the desired Drive/Folder.
Move – There may be situation that you quickly want to move all of your document files from your Hard Disk to your USB drive to keep them safe. To move all of your documents from your home directory to your USB drive, you will issue the following command:

find . -name "*.doc" -exec cp {} /path/to/USB \;

Delete – Suppose there are a lot of .tmp files and you want to get rid of them at once. Again ‘GNU find’ is at your service and will do the work for you. Issue the following in a terminal and all of the .tmp files are gone…

find . -name '*.tmp' -exec rm {} \;

Which files are owned by you and which are not?

There may be a situation when you want to know that which files in some other directories (or even in your home directory) are owned by some other user of your computer.
Suppose there is another user named ‘blackstar’ with whom you are sharing your PC. Now you want to know that which .doc files in Windows Directory are owned by the user ‘blackstar’. You can do this by issuing the following command:

find /path/to/Windows_Drive -user blackstar -name "*.doc"

Direct the output of ‘find’ to a file

You can save the results of your ‘find’ command to a text file which will allow you to examine the results in detail at some later time (or to create playlist of your songs). To do this a greater than (>) sign is used.
Suppose you want to save the list of all the mp3 songs in your home directory to a text file (which you can later share with your friend), you can do this by:

find . -name "*.mp3" >> mp3s.txt

It will save the complete path to all of your mp3 songs in the file named mp3.txt.

This article is basically directed towards new users of Linux which are not much familiar with command prompt. This is a small but comprehensive article about ‘GNU find’ . The man pages of ‘find’ list a huge number of options which are difficult to explain in detail in one small article. I tried to cover those options which are commonly used. Obviously, to know more about such a powerful tool, one has to visit its man pages again and again and spend a lot time with ‘find’ :)

Man Pages for commands in this post »

find

2 Comments »

Comment by Xyzzy

December 6, 2010 @ 5:34 am

Thanks for posting this… I have a couple of pocket reference books, but for some reason their “find” explanation/examples never worked properly for me — I’ve just copied your info down into my little hand-written terminal-howto notebook instead. This will be SO much faster/easier than firing up one of the slow GUI-based search functions…

Comment by TheLinuxBlog.com

December 6, 2010 @ 10:19 am

Hey, thanks for the comment :) Here is another article on the linux find command

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>