Useful Linux Commands – Finding Files from the Command Line

Below are a few command line tips and tricks for finding files that can make your life a little easier and save you time.

Finding Files

Finding files is always important on your system, whether you are looking for the location of a configuration file, or trying to remember where your stored your address list. There are multiple command line utilities that make finding files easy.

locate

The locate command is very useful in quickly finding files. Locate maintains a database which is easily and quickly searchable for finding files by name. Before using the locate command, you must do the following (from the root shell):

# yum-install mlocate
# updatedb

yum-install mlocate will install the locate package on your machine. If you are using Ubuntu or another system which uses apt, instead of yum, you will need to do the following:

# apt-get install mlocate
# updatedb

The updatedb command initially builds the database. Additionally, if you have made additional changes to your system, you may wish to run updatedb at any later time from the command line to update the locate database. This should automatically update according to your crontab scheduler.

When you want to find a file, all you need to type is:

$ locate myfile.txt

And locate will respond with all of the files that match the string “myfile.txt” You can also search for partial strings, such as:

$ locate myfile

To find all references to myfile.txt, myfile.dat, myfile.ods

I also find it helpful to combine locate with grep (easier than using regular expressions). So, if I want to find files were “http” is connected with “conf” which would include “httpd.conf” or “/var/conf/httpd”, I can use the following:

$ locate http | grep conf

If you are not familiar with it, the | is a “pipe,” which send the results of your first command into your second command. In this case, it sends the result from the locate command directly to “grep” which then filters out only the lines containing “conf”.

find

In a similar manner find is a useful command, which is typically part of the installed base. find is more readily available on certain system, particularly embedded systems and Raspberry Pi distributions.

To find files within the current directory and subdirectories, simply type:

$ find . -name *myfile*

In this command, find is the name of the command. The . says to look in the current directory and all subs. The -name options directs find to search by filename, and myfile indicates the name of the file, with wildcards to include “myfile.txt” or “not_myfile.txt”. Note that find requires the use of wildcards, otherwise exact matches are expected.

You can also search for files based on size:

find . -size +100000k -exec ls -lh {} \;

In this command, -size indicates that we will search by size. +100000k indicates that we are looking for files larger than 100MB, and the -exec ls -lh {} \; tells find to run ls with options lh on each line found. This will do a listing of the file, showing all attributes in human readable form.

Similar, you can search for files based on the time they have been modified. To find all files within the current directory modified in the last day, do:

find . -mtime -1 -exec ls -lh {} \;

and to find all files modified in the last three hours do:

find . -mmin -180 -exec ls -lh {} \;

Please be aware that any of these commands will also show the contents of directories which have been modified.

These commands, particularly by time, can be especially useful if you have forgotten the name of a file you have just downloaded (and its location), or if you are attempting to track down malicious changes to your operating system and want to see what files have been modified.

Happy computing!

For other useful commands take a look at these:

http://www.barryhubbard.com/linux/useful-linux-commands-working-with-text/
http://www.barryhubbard.com/linux/useful-linux-commands-basic-navigation/

Leave a Reply

Your email address will not be published. Required fields are marked *