0
0
Linux CLIscripting~5 mins

which and whereis for commands in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to find where a program or command is located on your computer. The commands 'which' and 'whereis' help you find the exact location of these commands so you know where they run from.
When you want to know the full path of a command you just typed, to check which version runs.
When you have multiple versions of a program and want to find out which one is used by default.
When a command is not working and you want to verify if it is installed and where it is located.
When you want to find related files like source or manual pages for a command.
When you are writing scripts and want to use the full path of a command to avoid confusion.
Commands
This command shows the full path of the 'ls' command that will run when you type 'ls'. It helps confirm which executable is used.
Terminal
which ls
Expected OutputExpected
/bin/ls
This command shows locations of the binary, source, and manual files related to 'ls'. It gives more information than 'which'.
Terminal
whereis ls
Expected OutputExpected
ls: /bin/ls /usr/share/man/man1/ls.1.gz
Finds the full path of the 'python3' command to check which Python version runs by default.
Terminal
which python3
Expected OutputExpected
/usr/bin/python3
Shows locations of the binary, source, and manual files for 'python3'. Useful to find related files.
Terminal
whereis python3
Expected OutputExpected
python3: /usr/bin/python3 /usr/lib/python3 /usr/share/man/man1/python3.1.gz
Key Concept

If you remember nothing else, remember: 'which' shows the command path used, 'whereis' shows command and related files locations.

Common Mistakes
Using 'which' to find source or manual files of a command.
'which' only shows the executable path, not source or manual files.
Use 'whereis' to find source and manual files along with the executable.
Expecting 'whereis' to always find all related files.
'whereis' searches standard locations and may miss files in custom paths.
Use 'locate' or 'find' commands for a more thorough search if needed.
Summary
'which' shows the full path of the command that runs when you type it.
'whereis' shows locations of the command's binary, source, and manual files.
Use these commands to verify command locations and troubleshoot command issues.