Spreadfirefox Affiliate Button
Custom Search

Saturday, October 11, 2008

Finding Files

You now know what each major directory holds, but it still doesn’t really help you find things. I mean, you could go looking through directories, but there are quicker ways. There are four main file search commands.

which
The first is the which(1) command. which is usually used to locate a program quickly. It just searches your PATH and returns the first instance it finds and the directory path to it. Take this example:

% which bash
/bin/bash

From that you see that bash is in the /bin directory. This is a very limited command for searching, since it only searches your PATH.

whereis
The whereis(1) command works similar to which, but can also search for man pages and source files. A whereis search for bash should return this:

% whereis bash
bash: /bin/bash /usr/bin/bash /usr/man/man1/bash.1.gz

This command not only told us where the actual program is located, but also where the online documentation is stored. Still, this command is limited. What if you wanted to search for a specific configuration file? You can’t use which or whereis for that.

find
The find(1) command allows the user to search the filesystem with a rich collection of search predicates. Users may specify a search with filename wildcards, ranges of modification or creation times, or other advanced properties. For example, to search for the default xinitrc¤ FORMTEXT
file on the system, the following command could be used.

% find / -name xinitrc
/var/X11R6/lib/xinit/xinitrc

find will take a while to run, since it has to traverse the entire root directory tree. And if this command is run as a normal user, there will be permission denied errormessages for directories that only root can see. But find found our file, so that’s good. If only it could be a bit faster...

slocate
The slocate(1) command searches the entire filesystem, just like the find command can do, but it searches a database instead of the actual filesystem. The database is set to automatically update every morning, so you have a somewhat fresh listing of files on your system. You can manually run updatedb(1) to update the slocate database (before running updatedb by hand, you must first su to the root user). Here’s an example of slocate in action:

% slocate xinitrc # we don’t have to go to the root
/var/X11R6/lib/xinit/xinitrc
/var/X11R6/lib/xinit/xinitrc.fvwm2
/var/X11R6/lib/xinit/xinitrc.openwin
/var/X11R6/lib/xinit/xinitrc.twm

We got more than what we were looking for, and quickly too.With these commands, you should be able to find whatever you’re looking for on your Linux system.

No comments: