Linux which and whoami Command Tutorial for Beginners (with Examples)
There are certain Linux utilities that are aimed at performing a single task and hence offer very few or no command line options. Two such tools are which and whoami. In this tutorial, we will discuss these commands using some easy to understand examples.
But before we do that, it's worth mentioning that all examples in this article have been tested on an Ubuntu 16.04 LTS machine.
Linux which command
The which command in Linux lets users know about the absolute path of command line tools. Following is the syntax of this command:
which [-a] filename ...
And here's how the tool's man page describes it:
which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in a strictly POSIX-conformant shell. It does this bysearching the PATH for executable files matching the names of the arguments. It does not follow symbolic links.
Following are some Q&A-styled examples that should give you a good idea on how the 'which' command works.
Q1. How to use which command?
The simplest example of which could be something like this:
which ls
And the output on my system is:
/bin/ls
Of course, you can pass multiple command names as input. For example:
which ls echo rm mv
And the output on my system is:
/bin/ls
/bin/echo
/bin/rm
/bin/mv
Q2. How to make which list all paths?
Sometimes, a command line tool may be present at more than one location on the syste. So in that case, you can use the -a command line option to force 'which' to produce all paths in output.
which -a [command-name]
Linux whoami command
We've already discussed a command called who. It basically provides information about all users who are currently logged in. However, what if you are only interested in the user who is currently logged in? Well, for this, you can use the whoami command.
Following is the syntax of this command:
whoami [OPTION]...
And here's how the tool's man page describes it:
Print the user name associated with the current effective user ID. Same as id -un.
Following is how you can use the whoami command.
Q1. How whoami works?
The whoami command is probably the easiest Linux command to use. All you have to do is to run the command in the following way:
whoami
And you'll see the current user name is produced in the output. For example, the following output was produced on my system:
That's it. There aren't any specific command line options, other than of course the standard --help and --version options.
Conclusion
Both which and whoami are extremely simple commands when it comes to understanding and usage. You may not require them on daily basis, but you should be aware of them nonetheless. To access their man pages, head here and here.