How to find files with Linux

Newbies to Linux often wonder how to find files. In windows the search function is pretty much part of the O/S but on Linux things can be trickier and there are several ways to do things.  There are GUI options, for example the magnifying glass in the Caja file manager (I use Linux Mint for my main desktop). There is also Catfish (install it through apt-get or Synaptic). Catfish with its basic search uses the ‘locate’ command and for more complex searches it uses ‘find’. Both of these can be used at the command line.

To use find:

Open the Linux Terminal.

# find / -type f -name myfile.doc
(Find the file called myfile.doc)

# find / -mount -mtime  +2 “*.log”
(Find all .log files that more than 7 days old on *this* mountpoint only)

# find /home -mount -size +100000k -type f  -name “*.avi” -exec ls -lh {} \;
(Find all avi files on the mountpoint where /home resides, which are greater than 100 MB and use the ls -lh command to show a long human readable listing)

user1@pc01 / $ find /home -mount -size +100000k -type f -name “*avi” -exec ls -lh {} \;
-rw-r–r– 1 user1 user1 359M Dec  6 22:43 /home/user1/xmtest_1.avi
-rw-r–r– 1 user1 user1 160M Dec  6 22:51 /home/user1/xmtest_2.avi

For more complex options, other switches can used but find is not always the fastest command and on large disks or filesystems can take a long time. The enormous amount of options can also be disconcerting.

If you want something quicker and simpler, an alternative approach is to use the ‘locate’ command.

The ‘locate’ command uses a database that has been pre-populated in the background. Finding files with locate is almost instant. One caveat is that it will only search local disks – so if you have something on a NAS drive you will need to use the find command anyway.

# cd /
# locate myfile.doc


# locate *doc  (to find all doc files on the system)

# locate -c *docx (to count all of the docx files)

As you can see, locate  has various switches, -i for case insensitive for instance, -c for count. It is not as powerful as the find command and may also be out of date (if you want to find a very recent file, it may not be in the database yet) but for general use it is a super-fast alternative to ‘find’.

Here is an example of locate in action.

Note: when using a * at in the search string, ‘locate’ looks at the whole path so you need to specify ‘-b’ or –basename to search for a filename (e.g. the whole path starts with “/” so “xm*avi” would not match. “*xm*avi” would but it is easier to specify we are searching for the ‘basename’ (the actual filename) with the -b command).

user1@pc01 /home $ locate -b xm*avi


Below is the help page for ‘locate’.

Usage: locate [OPTION]… [PATTERN]…
Search for entries in a mlocate database.

-A, –all              only print entries that match all patterns
-b, –basename         match only the base name of path names
-c, –count            only print number of found entries
-d, –database DBPATH  use DBPATH instead of default database (which is
-e, –existing         only print entries for currently existing files
-L, –follow           follow trailing symbolic links when checking file
existence (default)
-h, –help             print this help
-i, –ignore-case      ignore case distinctions when matching patterns
-l, –limit, -n LIMIT  limit output (or counting) to LIMIT entries
-m, –mmap             ignored, for backward compatibility
-P, –nofollow, -H     don’t follow trailing symbolic links when checking file
-0, –null             separate entries with NUL on output
-S, –statistics       don’t search for entries, print statistics about each
used database
-q, –quiet            report no error messages about reading databases
-r, –regexp REGEXP    search for basic regexp REGEXP instead of patterns
–regex            patterns are extended regexps
-s, –stdio            ignored, for backward compatibility
-V, –version          print version information
-w, –wholename        match whole path name (default)

Posted in IT Tagged with: , , , , , , , ,

Leave a Reply

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