Search file in Linux command line terminal

There are many commands which can be used to search file in Linux command line terminal. In this tutorial, you will learn how to use Linux whereis command, Linux which command, Linux find command and Linux locate command to find file in Linux system.

Search file using Linux whereis command

The Linux whereis command can be used to find the location of a binary, source, and manual page files. Below are examples on how to use whereis command to find file location:

If whereis command is used without any option, it will return all the subject (file) paths:

[email protected]:~# whereis inetd.conf
inetd: /usr/sbin/inetd /etc/inetd.conf
/usr/man/man8/inetd.8.gz
/usr/share/man/man8/inetd.8.gz
[email protected]:~#

There are some options that you can use with Linux whereis command to customize your searching term. Let's say you want to find a binary file named 'nmap', then you can use whereis with -b option for binary. Here is the example:

[email protected]:~# whereis -b nmap
nmap: /usr/bin/nmap /usr/share/nmap
[email protected]:~#

If you are looking for a Linux command manual page, use whereis with -m option. Let's say we want to search a whereis command manual page:

[email protected]:~# whereis -m whereis
whereis: /usr/man/man1/whereis.1.gz
/usr/share/man/man1/whereis.1.gz
[email protected]:~#

Use Linux whereis command with -s option to look for a source file:

[email protected]:~# whereis -s kernel
kernel: /usr/src/linux-2.4.29/kernel
/usr/src/linux/kernel
[email protected]:~#

Search file using Linux which command

The next command which can be used to search file in Linux system is Linux which command. Actually the Linux which command shows the full path of commands executed when an argument had been entered at the shell prompt. It searching for an executable or script in the directories listed in the environment variable `PATH' using the same algorithm as `bash(1)'.

The synopsis to invoke which is:

which options [--] programname [...]

Search file using Linux which command example:

[email protected]:~# which ls
/usr/bin/ls
[email protected]:~# which -a ls
/usr/bin/ls
/bin/ls
[email protected]:~# which -a whereis
/usr/bin/whereis
[email protected]:~#

Search file using Linux find command

The Linux find command is a very powerful command to find the location of a file. The find command provides some useful options so that user can give specific information about the subject they are looking for.

Type info find to read a very good documentation about Linux find command. Here's a quote from overview section of the documentation:

`find' searches for files in a directory hierarchy and prints information about the files it found. It is run like this:

find [FILE...] [EXPRESSION]

Here is a typical use of `find'. This example prints the names of all files in the directory tree rooted in /usr/src' whose name ends with `.c' and that are larger than 100 Kilobytes.

find /usr/src -name '*.c' -size +100k -print

[email protected]:~# find /usr/src -name '*.c' -size +500k -print
/usr/src/linux-2.4.29/fs/nls/nls_cp936.c
/usr/src/linux-2.4.29/fs/nls/nls_cp949.c
/usr/src/linux-2.4.29/fs/nls/nls_cp950.c
/usr/src/linux-2.4.29/drivers/scsi/qlogicfc_asm.c
/usr/src/linux-2.4.29/drivers/scsi/advansys.c
[email protected]:~#

Let's try some more examples of Linux find command:

Find a file named inetd.conf:

[email protected]:~# find / -name inetd.conf
/etc/inetd.conf
[email protected]:~#

Find a file named rpm:

[email protected]:~# find / -name rpm
/var/lib/rpm
/bin/rpm
/usr/lib/rpm
/usr/src/rpm
/usr/share/mc/extfs/rpm
/usr/include/rpm
[email protected]:~#

Search file using Linux locate command

The Linux locate command also a very powerful command for searching file in Linux system. It uses incremental encoding to compress its database to make searching faster. The locate command format is:

locate <option> search string

Here are some locate command examples to try:

[email protected]:~# locate inetd.conf
/etc/inetd.conf
[email protected]:~# locate whereis
/usr/bin/whereis
/usr/man/man1/whereis.1.gz
/usr/share/zsh/4.2.1/functions/_whereis
[email protected]:~#

The newer, slocate command, which stands for secure locate provides more secure way to search file than locate. For more information about locate command, visit Linux locate command tutorial.

That's all about how to search file in Linux command line terminal. Some command maybe not suitable to use in certain situation. Always try different commands as shown in the tutorial above.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.