Linux CLI HowTo 9 🐧 Commands type, which
1. How to Identify Command Types in Linux
Steps:
- Open terminal
- Run
type cdto check if cd is a shell builtin - Run
type lsto see if it’s an external command - Run
type mvto verify if it’s an external command - Compare results with
type cd mv lsfor multiple commands
Explanation: This helps distinguish between shell builtins (like cd), external commands (like ls), and aliases.
2. How to Find Command Locations Using type
Steps:
- Execute
type ls - Execute
type mv - Look at the output showing full paths like
/bin/ls - Compare with
which lsto see different information
Explanation: The type command shows where external commands are located and helps debug command resolution issues.
3. How to Locate All Executables with which -a
Steps:
- Run
which -a pythonto find all Python installations - Run
which -a javato locate multiple Java versions - Observe the different paths in output
Explanation: The -a option shows all matching executables, useful for finding multiple versions of software.
4. How to Debug PATH Issues Using which
Steps:
- Run
which lsto see default location - Run
which pythonto check Python installation - Compare with expected locations in your system
Explanation: Helps identify if commands are found in correct paths and diagnose PATH-related problems.
5. How to Identify Shell Builtins vs External Commands
Steps:
- Execute
type cd - Execute
type ls - Execute
type echo - Notice the difference in output format
Explanation: Shell builtins show as “cd is shell builtin” while external commands show full path locations.
6. How to Find Installation Paths of Commands
Steps:
- Run
which lsto find location - Run
which pythonfor Python installation - Run
which gccfor compiler location
Explanation: Useful for administrators to locate where programs are installed and verify correct versions.
7. How to Troubleshoot Command Not Found Errors
Steps:
- Run
type command_nameto see what type it is - Run
which command_nameto find if it exists - Check output to determine if it’s missing, not in PATH, or an alias
Explanation: Systematic approach to debugging why commands aren’t working.
8. How to List All Versions of a Command in PATH
Steps:
- Run
which -a python - Run
which -a node - Analyze the different paths shown
Explanation: Shows all available versions of a command, useful for managing multiple software versions.
9. How to Verify Command Availability Across Multiple Arguments
Steps:
- Run
which ls pythonto check both commands - Run
type cd mv lsto see all types at once - Observe the output format
Explanation: Shows how these commands can handle multiple arguments for batch checking.
10. How to Use type for Shell Script Debugging
Steps:
- Create a script with various command types
- Add
typecommands in script to debug - Run script and analyze output
Explanation: Essential for understanding how shell resolves commands in scripts and for debugging complex command resolution.
11. How to Compare type vs which Output
Steps:
- Run
type lsandwhich ls - Run
type cdandwhich cd - Compare the information provided
Explanation: Demonstrates the different information each command provides – type shows more details about command types.
12. How to Find Multiple Installations of Same Command
Steps:
- Run
which -a java - Run
which -a python3 - Check paths to identify different installations
Explanation: Helpful when you have multiple versions installed and need to determine which one is being used.
13. How to Check Command Type for Security Analysis
Steps:
- Run
type /usr/bin/lsto verify external command - Run
type cdto confirm shell builtin - Run
type sudoto check if it’s a special command
Explanation: Important for security audits and understanding what commands are actually executing.
14. How to Use type with Aliases
Steps:
- Create an alias:
alias ll='ls -la' - Run
type llto see the alias definition - Run
type lsto see original command
Explanation: Shows how aliases are resolved and helps understand shell command resolution.
15. How to Diagnose Command Resolution Issues
Steps:
- Run
type non_existent_command - Run
which non_existent_command - Check error messages to understand resolution failure
Explanation: Useful for learning how the shell resolves commands and troubleshooting when commands don’t work as expected.
16. How to Find Command Source for Learning Purposes
Steps:
- Run
type lessto see if it’s external or builtin - Run
type catto verify command type - Run
which catto find exact location
Explanation: Helps understand how different commands are implemented and where they’re located in the filesystem.
17. How to Check Multiple Command Types Simultaneously
Steps:
- Run
type ls mv cd grep - Observe how output shows different command types
- Note which are builtins vs external commands
Explanation: Efficient way to understand multiple command types at once for system administration tasks.
18. How to Use which for Program Version Detection
Steps:
- Run
which pythonto find default version - Run
which python3for specific version - Compare with actual program versions
Explanation: Useful for identifying which version of software will be executed when running commands.
19. How to Identify Command Type in Scripts
Steps:
- Create a script file
- Add
typestatements before commands - Execute script to see command resolution
Explanation: Essential for writing portable scripts that work across different Linux distributions.
20. How to Fix PATH Issues Using which Command
Steps:
- Run
which command_name - If not found, check if it’s in PATH
- Add missing directory to PATH using
export PATH=$PATH:/new/path
Explanation: Practical method for troubleshooting and fixing PATH-related command execution problems.