Day 3 Linux: which Vs whereis Vs locate Vs find

Day 3 Linux: which Vs whereis Vs locate Vs find

Zero To Hero Linux Series

Use "which" Command

The "which" command is a simple but effective way to locate a binary command's location.

It will tell you full path of command you are looking for.

For example, if you're looking for location of "ls" command

Simply open up your terminal and type "which ls"

which ls

This will output location of "ls" command, which is usually located in /usr/bin directory.

Use "whereis" Command

The "whereis" command is similar to "which" command, but it also provides additional information, such as location of binary, source files, and man page. To use "whereis" command, simply open up your terminal and type − whereis

For example, if you're looking for location of "gcc" command, type −

whereis gcc

This will output location of "gcc" command, as well as location of source files and man page.

Use "locate" Command

The "locate" command is a powerful tool for finding files on your system. It works by indexing your file system and creating a database of all files on your computer.

For example, if you're looking for location of "grep" command,

locate grep

This will output a list of all files on your computer that contain word "grep" in their name or path. You can then narrow down your search by looking for files that are located in /bin/ directory.

Use "find" Command

The "find" command is another powerful tool for finding files on your system. It works by searching for files that match a certain set of criteria.

To use "find" command, simply open up your terminal and type −

find / -name < >

For example, if you're looking for location of "curl" command

find / -name curl

Find is perhaps the most versatile and robust of these commands. It allows users to search for files and directories based on various criteria such as name, type, size, and permissions. Unlike locate, find searches the file system in real-time, which might be slower but provides up-to-date results.

Example:

find /home/srinu -name "*.txt"

Output:

/home/srinu/example.txt
/home/srinu/documents/sample.txt

This will search your entire file system for files that are named "curl". It may take a few minutes to complete, so be patient. Once search is complete, you can narrow down your search by looking for files that are located in /bin/ directory.

Use "dpkg" Command

The "dpkg" command is a package manager for Debian-based systems. It allows you to search for packages that contain a specific binary command.

To use "dpkg" command, simply open up your terminal and type

dpkg -S <>

For example, if you're looking for package that contains "tar" command,

dpkg -S tar

This will output package that contains "tar" command, as well as location of package on your file system.

Command Completion Using TAB Key

Use Tab Completion Most shells (such as Bash) have tab completion built-in. This means that you can start typing a command and then press "Tab" key to see a list of available options. This can be a quick way to find a command that you know first few letters of, without having to type out full command.

It's worth noting that location of binary commands may vary depending on your operating system and version of software you are using. While commands we've listed here are common and widely available, it's always a good idea to check documentation for your specific system if you are having trouble locating a particular command.

In addition to these methods, there are other ways to find information about binary commands on your file system. For example, you can use "man" command to read manual pages for a command, which provides detailed information about how to use it. You can also use "info" command to access additional documentation about a command.

Conclusion:

In the realm of Linux file system navigation, having a firm grasp of tools like which, whereis, locate, and find can significantly enhance productivity and efficiency. Each command serves distinct purposes, catering to different search requirements and scenarios. By understanding their capabilities and nuances, users can wield these tools adeptly to navigate the Linux environment with ease.