How to Find a File in Linux

Well, so you are searching for another very common question in Linux. How to find a file in Linux is not a question that needs a complicated answer. There are certain commands that can be used for finding files in Linux. Let us go through them in detail in this article...
We will straight away take a look at the various commands that can be used for searching files in Linux. We will also try to understand the difference between the various commands to search for files in Linux. Here we are going through the command line approach, as it is this interface which makes Linux the wonderful operating system that it is. Let us now straight away go through the various commands that can be used to answer the question, how to find a file in Linux.

How to Search a File in Linux

The find Command
The find command can be used for searching all files within a directory hierarchy. find command checks each expression from left to right, trying to match it with the filenames in the present directory. If it finds a match it displays it, else the result is false and it goes to the next filename. The find command finds out files with all the approximate filenames as well. Let us now take a look at some of the arguments that we can use with the find command.
  • -depth: This command is used to process the directory contents before the directory itself.
  • -help: It is used for displaying the syntax and different arguments that can be used with this command.
  • -name: Suppose you want to find the files whose names start with a particular set of characters. In such cases you can use the find -name command.
  • -path: It is used to provide a starting point in the directory hierarchy to start the search.
  • -atime n: You can specify this argument if you have a fair idea, when had you last accessed the file.
  • -mount: Keeps the search limited to the directory specified. It does not go into other filesystems.
  • -ntime n: Here n stands for the number of days, where you can specify approximately how many days before you are executing this search, had you modified the file's data.
  • -nogroup: For files that do not belong to any group, you can use this argument to search. The file in such cases does not belong to the /etc/group file.
  • -size n[c]: Here you can specify if the file is 'n' blocks long. 1 block is 512 bytes. If you specify [c] along with the command then the size can be mentioned in bytes.
  • -user: Using this command you can specify the username of the person to whom the file belongs.
  • -xdev: This command works the same as the -mount parameter.
These were a few of the arguments that you can use along with the find command. Remember, the find command is case-sensitive.

The locate Command
The locate command is used to list files in a database. It is also used along with various arguments. Let us take a look at all these arguments.
  • -d path or --database=path: Instead of searching for the files in the default database, it will search for the files in the database paths listed along with this parameter. Different database filenames can be specified with this command by separating them with ":" [colon sign].
  • -e or --existing: Using this command you can specify to locate only those filenames which existed when the database was created. It will not locate those files whose names exist in the database but the files no longer exist.-i or --ignore: This argument is used to ignore the case sensitivity, else all the searches will be case-sensitive.
  • --help: This argument is used to display the syntax and all the possible arguments of the locate command.
All these arguments that are used along with the locate command, help in producing a refined search.

The whereis Command
This command is used for searching different types of files. Using the different arguments, you can specify if the file you are searching for is a binary file, source file, etc.
  • -b: This argument can be specified to mention that you only want to search for binary files.
  • -m: This argument, if specified, will only search for the file in the manual sections.
  • -s: Using this argument, you can specify that you want to limit your search to sources.
  • -u: You can use this argument to specify that you want to search for files which do not have any usual entries or documentation.
  • -B: Using this argument you can limit the places where the whereis command will search for binaries.
  • -M: Just as in the case of -B argument, you can use this argument to limit the places to be searched in by the command for manual sections.
  • -S: Another argument in the same series, where you can mention the places to search in by the whereis command for sources.
A simple command like the whereis -help will enlist all the different options that you can try out along with the whereis command.

The whichis Command
This is a simple command which is used to search for other commands. In short, those files which have been stored as commands. The simple syntax which [filename], will locate the executable file you are looking for.

This was a little insight into the different types of search that you can try out with Linux. The above explanation will not only help you in solving your most basic query in Linux, 'how to find a file in Linux', but also provide you with the difference between the different types of commands that are used for searching for files in Linux.
By
Last Updated: 10/13/2011
Like This Article?
Follow:
Post Comment
Your Comments:
Your Name: