Archive

Posts Tagged ‘which command’

Searching files in Linux

October 7th, 2009 No comments

No doubt every single user from time to time uses search function because it’s almost impossible to remember where all the files are saved. This function is even more useful when you need to find a system file, library and so on.

Most likely the great majority of you are familiar with Windows Search Function that is pretty easy to use. In fact all Linux distributions have such an option (a graphical one) as well, so you won’t lack any functionality. However if you want to be able to find absolutely everything you need, it’s better to choose command line that has some really powerful tools to use.

In the given post we are going to review some of these tools in detail. Nevertheless it should be emphasized that there is no need to worry even if you don’t really like using command line, because no scripts or complex expressions will be discussed – just easy and understandable methods.

First of all we need to define common types of search requests. Here are the most important ones:

  • Search by file name or mask (this type also includes file searching according to a certain path and exclusion of a particular path from the search)
  • Search by file type (extension)
  • Search by file access/creation/modification date/time
  • Search by file size
  • Search by file owner and access permissions
  • Search of system and executable files

As far as the commands used to find files are concerned there are four of them to be reviewed:

1) find

This command is considered to be the most powerful one. It checks the file system in real time based on certain criteria. That means you will always get the most up-to-date results without the necessity to update a database. Of course, you can perform all sorts of operations on the files that were found. In addition to that is should be emphasized that file command allows you to check temp folders as well (in comparison with locate command).

However due to the fact that file command does search through the file system hierarchy, it’s considerably slower than other commands. That’s why it’s recommended to narrow your search (of course, if there is such a possibility) by searching in certain directories.

2) locate

The locate command uses a database (instead of file system itself) for searching, so it’s significantly faster than file command. However using database means that the database has to be updated in order to ensure proper search results (all new files have to be included and deleted ones mustn’t be considered). Once your database is updated you are ready to search. To update the database the only thing you should do is to run the following command:

updatedb

Taking into consideration the fact that many users may forget to update the database each and every time, it’s recommended to set up a cron job for that purpose. Here you can learn more about the cron.

3) whereis

Given command is useful in case you need to find source, binary and/or executable files which are associated with manual pages. As a return of this command you get the path.

Let’s check an example. Suppose that you need to find out where Firefox is installed. Here is what you enter and what you get:

whereis firefox

firefox: /usr/bin/firefox /etc/firefox.cfg

4) which

The which command being pretty simple is very similar to the previous one (whereis command) but it shows you the full path of shell commands instead.

It’s very useful for finding out “which” binary the system would execute if you typed the command out. Since some programs have multiple versions installed the which command comes in handy to tell you which version it is using and where it is located.

Read more…