Finding Files in the Command Line

Unfortunately in Linux, certainly Ubuntu, the default GUI file search is not the most useful way to find files.

With just a small amount of patience you can find files quickly and easily using the command line, and your options for this are really powerful if you want to learn a bit about it.

Locate

The easy, quick command is called “locate”. To use this command at the terminal you simply type:

$ locate -i searchstring

This will search for all files and directories with “searchstring” in the name, and -i means the search is not case sensitive (i.e. it will find searchstring, Searchstring, sEaRcHsTrInG, and so on).

The results are instantaneous because the system has created a database (also known as an index) to tell you where files are located. The only problem is that newly created or moved files may not be found correctly until the next database update, and you don’t have many options to choose from for your search.

Forcing locate to update the database/index is done with sudo updatedband it doesn’t take a lot of time.

Example:

$ locate -i omgubuntu.desktop

Find

There is a much more powerful command available to you called “find”.

You can tell “find” where to look, what criteria to use in its search, and what actions to take once you have found what you are looking for.

The syntax for “find” is:

$ find <where to start searching> <search criteria> <actions to take>

If you don’t add any parameters, find will default to searching the current working directory (or “.“), uses no search criteria (defaults to showing all files), and -print (which, despite its name, displays, or “prints,” the results on screen) as the only action to take.

Two examples:

$ sudo find / -type f -mmin -10

This example will find (starting at the root directory, or /, and recursively search subdirectories) all normal files (-type f means normal files, without this it will find normal files + special files + directories) which were modified less than ten minutes ago (-mmin -10), and then display the results for you.

This would be useful if you know you edited a file recently but don’t know where you put it, or have to find a log file for a program that crashed.

You can add sudo here because find does not search files/directories that the current user does not have permissions for, and it will return error messages if you aren’t a sudoer —** just be careful!**

$ find ~ -iname "*new*" -exec mv -v {} /media/current-projects/ \;

This will find everything in your home directory (~) with a name, case insensitive (-iname), containing new (“new”) and execute (-exec) a move (mv) of the results ({}) to /media/current-projects/ ( \; is required by -exec to show the end of the command to be executed). So all your files will be moved to the same place. mv -v displays the results of the move command with (-v)erbose messages.

Another warning with -exec, though it is powerful, when used without care you can overwrite your whole home directory or whole disk – so be careful!

Catfish is a GUI Option

For those of you who simply can’t do without a GUI, you can find the program Catfish in the repositories — this enables you to run both locate and find from a graphical front-end, but it is very limited in options.

Think of Catfish as an equivalent to Windows Search. If you want the full power of find, you’ll need to run it from the command line, using the quick tips above.

Install Catfish from Ubuntu Software