The type command in Linux displays information regarding Linux commands. It tells you the type of a Linux command. Type of command is indicative of how the system interprets the command.
Different types of command are :
We can also use type command to get the location and executable path of a command. In this tutorial, we will learn how to use the type command.
Finding the “type” of all the common commands
Let’s try running the type command on some of the popular commands we know.
1. ls command
Let’s try running the type command on ls.
type ls
Output:
ls is aliased to `ls --color=auto'
2. pwd command
Let’s try running the type on pwd command.
type pwd
Output:
pwd is a shell builtin
3. mkdir command
Let’s try running type on mkdir command.
type mkdir
Output :
mkdir is /usr/bin/mkdir
4. type command
Let’s try running the type on the type command.
type type
Output :
type is a shell builtin
5. if condition
Let’s try running the command on if conditions.
type if
Output:
if is a shell keyword
Reducing the type Command Output
To print just the type of the command in the output use the -t flag with type command.
The output can be one of the following :
- alias
- function
- builtin
- file
- keyword
Let’s use type with the -t flag for all the same commands we tried before
1. ls command
What type of command is the ls command in Linux?
type -t ls
Output :
alias
2. pwd command
Now, let’s understand what type of command is the pwd command in Linux?
type -t pwd
Output :
builtin
3. mkdir command
We create directories in our Linux system, but what type of command is the mkdir command anyway?
type -t mkdir
Output :
file
4. type command
Now time for some inception. What type of command is the “type command”?
type -t type
Output :
builtin
5. if condition
BASH scripting uses a lot of if conditions to make decisions. What kind of command is the if condition here?
type -t if
Output :
keyword
How to get the location of a command using type command?
Using -a flag with type command displays all the paths that contain the bin file of the command. Time to run the type -a command on all of the same commands and see what the outputs say.
1. ls command
What all locations can we find the ls command in our system? Let’s find out:
type -a ls
Output :
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
We can see that now we get locations of the command as well in the output.
2. pwd command
Time to check the locations for the pwd command now:
type -a pwd
Output :
pwd is a shell builtin
pwd is /usr/bin/pwd
pwd is /bin/pwd
3. mkdir command
Let’s try running the command on mkdir.
type -a mkdir
Output :
mkdir is /usr/bin/mkdir
mkdir is /bin/mkdir
How to find the absolute path of a command?
You can find the absolute path of any command by using -P flag along with type. Absolute paths are the paths that your system access when you run a particular command.
Let’s try it out on some commands.
1. ls command
Let’s try running the command on ls.
type -P ls
Output :
/usr/bin/ls
This is the absolute path for the executable file that is called every time we use the ls command.
2. pwd command
Let’s try running the command on pwd command.
type -P pwd
Output :
/usr/bin/pwd
3. mkdir command
Let’s try running the command on mkdir command.
type -P mkdir
Output :
/usr/bin/mkdir
Conclusion
That brings us to the end of this tutorial. The type command can be a useful tool to gain a better understanding of a new command or utility that you begin using. Obviously, you can always use the man pages for much deeper understanding. But if you’re looking for something quick and easy, using “type” is an obvious winner.
Hope you had a good read here and understood the command better!