Essential Linux Commands: ls, pwd, mkdir, echo, whoami, and cd
Learn the essential Linux terminal commands ls, pwd, mkdir, echo, whoami, and cd for inspecting directories, identifying users, creating folders, and navigating paths.
Linux commands are instructions entered at a terminal prompt. A terminal is a text-based interface for entering commands, while a shell is the command interpreter that reads and runs them.
After opening a terminal, type a command and press Enter. A command usually consists of a command name followed by optional arguments. An argument supplies additional input, such as a directory name or path.
command [optional argument]
The shell always has a current working directory. This is the directory the shell is currently using as its location. Commands such as ls and mkdir use this directory when you do not provide another path.
Paths, directories, and locations
A directory is a filesystem container that can hold files and other directories. A path identifies a location in the filesystem.
An absolute path begins at the filesystem root, written as /. A relative path is interpreted from the current working directory.
| Path type | Example | Meaning | Typical command use |
|---|---|---|---|
| Relative path | my_folder | A directory named my_folder below the current working directory | cd my_folder or ls my_folder |
| Absolute path | /tmp | The tmp directory directly under the filesystem root | cd /tmp or ls /tmp |
Using ls to list directory contents
ls displays the contents of a directory. With no argument, it lists the contents of the current working directory.
ls
You can provide a directory path to inspect another location without changing the shell's current directory.
ls /tmp
The output may contain directory names as well as names of other entries, such as files. A name shown by ls is not necessarily a directory; use the path and command context to determine what you want to inspect.
Using pwd to display the current directory
pwd prints the full path of the current working directory. The name means “print working directory.”
pwd
For example, the output might be:
/home/alex
This tells you exactly where relative paths will be interpreted. Use pwd before creating a directory or changing location when you are unsure where the shell is.
Creating directories with mkdir
mkdir creates a directory. A simple directory name creates the new directory inside the current working directory.
mkdir my_folder
Verify that it was created by listing the current directory:
ls
You can also provide a path. This creates the directory at that location when the parent directory already exists and you have permission to write there.
mkdir /tmp/my_folder
In this example, /tmp is the parent location and my_folder is the directory being created. Check the result with:
ls /tmp
Using echo to print text
echo writes supplied text to the terminal output.
echo Hello
When text contains spaces, place it inside quotes so the shell treats it as one intended message.
echo "Hello, Linux"
This lesson uses echo for terminal output. It is not necessary to redirect the output to a file.
Using whoami to identify the active user
whoami displays the username of the user running the current shell session. A username is the account name associated with that user.
whoami
The current user and the current directory are different pieces of information. whoami answers “Which account am I using?” while pwd answers “Where is the shell located?”
Changing directories with cd
cd changes the shell's current working directory. The target directory must exist and be accessible to the current user.
Using a relative path
If my_folder is inside the current working directory, change into it with:
cd my_folder
Confirm the new location:
pwd
If the starting location was /home/alex, the result would be:
/home/alex/my_folder
Using an absolute path
An absolute path starts at the filesystem root, so it does not depend on the current directory.
cd /tmppwd
The second command should show:
/tmp
Essential Linux command reference
| Command | Purpose | Example | What it affects or displays |
|---|---|---|---|
ls | List directory contents | ls /tmp | Displays entries in the chosen directory; without a path, uses the current working directory |
pwd | Print the current directory path | pwd | Displays the full path of the shell's current working directory |
mkdir | Create a directory | mkdir my_folder | Creates a directory in the current location or at a supplied path |
echo | Display text | echo "Hello, Linux" | Prints the supplied message in the terminal |
whoami | Display the active username | whoami | Displays the account running the current shell |
cd | Change directories | cd /tmp | Changes the shell's current working directory |
A basic command workflow
The following sequence checks your location, inspects it, creates a directory, enters it, and identifies the active account.
pwd
ls
mkdir my_folder
ls
cd my_folder
pwd
echo "Hello, Linux"
whoami
What each step does:
pwdshows the starting location.lslists the contents of that location.mkdir my_foldercreates a directory in the current location.- The second
lsconfirms thatmy_folderappears. cd my_folderenters the new directory using a relative path.- The second
pwdconfirms the directory change. echoprints a status message.whoamidisplays the username running the shell.
Troubleshooting common problems
cd reports that a directory does not exist
The directory name or path may be incorrect, or the directory may not have been created. Run pwd to confirm your current location and ls to inspect available names. Then use the correct path or create the directory with mkdir.
mkdir cannot create a directory
An entry with that name may already exist, the parent path may be missing, or you may not have permission to write there. Use ls to inspect the target location, verify the parent path, choose a different name if needed, and use a location accessible to your account.
ls shows an unexpected directory
ls without an argument uses the current working directory. If the shell is somewhere unexpected, run pwd, then use cd to move to the intended location or provide the intended path directly to ls.
echo displays words differently than expected
Text containing spaces may have been entered without quotes, or shell-special characters may have been interpreted. Enclose the intended message in quotes:
echo "Hello, Linux"
whoami shows an unexpected username
The terminal session may be running under a different account than you expected. Treat the output of whoami as the active shell identity and check which account was used to start the session.
Exam-relevant notes
pwddisplays the full path of the current working directory.lsdisplays directory contents;ls /tmpinspects/tmpwithout changing location.mkdir my_foldercreates the directory in the current working directory.cdchanges the shell's current working directory, whilepwdverifies it.whoamiidentifies the active username, not the current directory.- Quote text containing spaces when using
echo. - A relative path depends on the current working directory; an absolute path begins at
/.
For related study, see Linux fundamentals, Bourne Again Shell (Bash), and showing the full path of shell commands.