Human Readable   

 

     
   
     

A Slackware Desktop Enhancement Guide

Finding Files

© Copyright Darrell Anderson.

One of the more common tasks associated with computers is finding files. Two useful commands exist for GNU/Linux users to help find files.

One command is called slocate and Slackware comes packaged with a soft link to that command called locate. However, before this command becomes useful, users need to create the database that slocate uses to immediately find files.

After first installing Slackware this database does not exist. As root create the database by typing the following at a command line:

touch /var/lib/slocate/slocate.db

That command will create a zero byte file that will become the database. As root next type the following:

updatedb

Depending upon your hardware and the number of files installed, this command will require a couple of minutes or more to create the search database.

After creating the search database, to find a file with slocate is a simple as typing locate filename. Often that is all that you need to do, but also often the result is far too many files to discern the actual file you seek. Thus, a nice trick is to use grep to narrow the search field. For example, if you know you are looking for an executable binary file, you might type locate filename | grep /bin.

One caveat associated with using slocate is that users must continually update the search database. However, for many people this is easily accomplished by created a cron table entry to automagically run updatedb daily.

Another caveat is that users might not want every file on their system indexed in the search database. This is easily avoided by adding some entries in the /etc/updatedb configuration file. A typical config file might look like this:

# /etc/updatedb.conf: slocate configuration file

PRUNEFS="devpts nfs afs proc smbfs autofs iso9660 udf"
PRUNEPATHS="/dev /proc /sys /tmp /usr/tmp /var/tmp /afs /net /home/archive /home/ftp /mnt /media"
export PRUNEFS
export PRUNEPATHS

Another useful file search tool is the find command. Unlike slocate, the find command can search for files in real-time and needs no database or search index. Thus, if slocate fails to find a file that you know exists, the find command comes to the rescue. Additionally, the find command provides a useful feature in that the command can also perform additional tasks based upon the search results. The syntax for using the find command is a wee bit lengthy:

find search-path -type filetype -name filename options

search-path is just that—where to look for files. In a system-wide search this would be nothing more than /. However, the search path could be more limited, such as /home, /usr/local, or /media/cdrom.

The type options informs find whether to look for files (f) or directories (d).

The name option is the file name to find. Thus, a typical search might look like this:

find /usr/local -type f -name deleteoldfiles

This will look for a file called deleteoldfiles and restrict the search to the /usr/local directory. Another example might look for all directories called shutdown within the /home directory:

find /home -type d -name shutdown

The find command supports running additional commands after completing the search. For example, to delete all files older than three days within the /tmp directory, perform the following:

find /tmp ! -type d -mtime +3 -exec rm -f {} \;

The command will specifically ignore directories and execute the rm (remove) command when finding files older than 3 days in the /tmp directory.

Because the find command searches in real-time and uses no search index, finding files with the find command often takes longer than when using slocate.

Finis.

Table of Contents