Objectives & prerequisites
- Know and use the command that prints the user's current directory.
- Know and use the command that prints the current user's username.
None.
Introduction
Depending on how your shell prompt is configured, you might only see the last part of your current directory. This can make it hard to understand where you are exactly. Or rather: in which directory.
And when you switch to another user account, as you must do to run commands that require administrative privileges, it's sometimes unclear which user account you are currently using.
Here are two commands to help you out.
Asking for the current directory
The pwd command prints the name and full path of the working directory (current directory).
Example
sarah@laptop ~$ pwd
/home/sarahWhy would you use this command, if your working directory is right there in the prompt?
Default prompts sometimes only show the last part of your working directory to save screen space. If you're writing a shell script that can be executed in any directory, the pwd command will tell you from which directory the script is being executed.
Activity
Open a terminal and execute the pwd command to see the full path to your current directory.
Asking for the current username
The whoami command - literally who am I - prints the username of the current user.
Example
sarah@laptop ~$ whoami
sarah
Activity
Open a terminal and execute the whoami command to see the full path to your current directory.
Demo
Summary
- Use
pwd(print working directory) to print the full path to the current directory. - Use
whoamito print the current user's username.