Share the latest technology trends or photos of gadgets you love.

How to Find a File in Linux

How to Find a File in Linux

It is very easy to find files in Linux. There are certain commands that can be used for it. Let us go through them in detail in this article...
Shah Newaz Alam
Linux is an operating system known for its features which protect the computer. Users who are new to it may find it difficult to operate. However, anyone can master it with time. Given below are some commands which can help you open files.

How to Search for a File in Linux

The find Command

The find command can be used for searching all files within a directory hierarchy. It checks each expression from left to right, trying to match it with the names in the present directory. If it finds a match, it displays it, else the result is false and it goes to the next filename. This command finds out files with all the approximate names as well. Let us 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 this 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 file systems.
  • -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. The file in such cases does not belong to the /etc/group.
  • -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.
The locate Command

This command is used to list files in a database. It is also used along with various arguments.
  • -d path or --database=path: Instead of searching for the files in the default database, it will search for them 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 ones that no longer exist.
  • -i or --ignore: This argument is used to ignore the case sensitivity, else all the searches will be case-sensitive.
  • --help: It is used to display the syntax and all the possible arguments of the locate command.
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 binary, source, 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 in the manual sections.
  • -s: Using this, you can specify that you want to limit the search to sources.
  • -u: It's used to specify that you want to search for files which do not have any usual entries or documentation.
  • -B: Using it 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 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 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.