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:
root@slackware:~# whereis inetd.conf
inetd: /usr/sbin/inetd /etc/inetd.conf
/usr/man/man8/inetd.8.gz
/usr/share/man/man8/inetd.8.gz
root@slackware:~# |
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:
root@slackware:~# whereis -b nmap
nmap: /usr/bin/nmap /usr/share/nmap
root@slackware:~# |
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:
root@slackware:~# whereis -m whereis
whereis: /usr/man/man1/whereis.1.gz |
Use Linux whereis command with -s option to look for a source file:
root@slackware:~# whereis -s kernel
kernel: /usr/src/linux-2.4.29/kernel
/usr/src/linux/kernel
root@slackware:~# |
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:
root@slackware:~# which ls
/usr/bin/ls
root@slackware:~# which -a ls
/usr/bin/ls
/bin/ls
root@slackware:~# which -a whereis
/usr/bin/whereis
root@slackware:~# |
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
root@slackware:~# find /usr/src -name '*.c' -size +500k -print |
Let's try some more examples of Linux find command:
Find a file named inetd.conf:
root@slackware:~# find / -name inetd.conf
/etc/inetd.conf
root@slackware:~# |
Find a file named rpm:
root@slackware:~# find / -name rpm
/var/lib/rpm
/bin/rpm
/usr/lib/rpm
/usr/src/rpm
/usr/share/mc/extfs/rpm
/usr/include/rpm
root@slackware:~# |
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:
root@slackware:~# locate inetd.conf
/etc/inetd.conf
root@slackware:~# locate whereis
/usr/bin/whereis
/usr/man/man1/whereis.1.gz
/usr/share/zsh/4.2.1/functions/_whereis
root@slackware:~# |
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.
