Search for files

Search for files

  • whereis
$ whereis who

You use this command to find three paths. Two of them are the paths of executable files and third one is the path of a man online help file. This search is really quick because it does not look up from the hard disk; instead it queries directly from the database. whereis can only search for binary files (-b), man help files (-m), and source code files (-s). If you want to get more comprehensive search results you can use the locate command.

  • locate

This command finds files through the varlibmlocatemlocate.db database, but this database is not updated in real time. The system will automatically update once a day. So it may not find the file that you just added. You need to manually update it using the updatedb command. locate can be used to find different file types, such as finding all files that start with ‘sh’:

$ locate etcsh

Note that it is not only in the etc directory to find files, but also automatically recursive subdirectory to find.

Find all jpg files under usrshare:

$ locate usrshare.jpg

You can add the -c parameter to count the number of files. Adding -i parameter can search ignoring cases. The parameters -b-m-s of whereis can also be used.

  • which

which is a shell built-in command. We usually use which to determine whether to install a software package, because it only searches from the PATH environment variable to search for a command. To search for man, run this command:

$ which man
  • find

Find should be the most powerful of these commands. It can not only find files by file type or file name, but can also search for files based on file attributes (such as file timestamp, file permissions etc.). The following command can search for a file or directory named interfaces in the etc directory. This is the most common form of find. Remember the first parameter of find is the search destination:

$ sudo find etc -name interfaces

Time-related parameters:

ParameterDescription
-atimeLast visit time (Last access time)
-ctimeThe last time when the contents of the file were modified
-mtimeThe last time when the file attributes were modified

Take -mtime for example:

  • -mtime n:The file that was modified on the day n days ago
  • -mtime +n:List the files that were modified n days ago (not including the nth day)
  • -mtime -n:List the files that have been modified within n days (including the day)
  • -newer file:List all the files that are more recent than the existing file named file

List all the files that have changed within 24 hours:

$ find ~ -mtime 0

List all the files that are more recent than the Code folder in the user directory:

$ find ~ -newer /home/shiyanlou/Code

Tags: