Linux Command Line Interface
The Linux command-line interface (CLI) is one of the primary ways that Linux administrators and ethical hackers use Linux. This chapter will walk you through a few commands that you can run in the command-line interface.
Learning Objectives
You should be able to:
- Launch a terminal
- Use the
pwd,cd,whoamiandlscommands - Run basic commands with options
Video Walkthrough
Use this video to follow along with the steps in this lab.
Basic Linux Commands
Most people learn best by doing. This section will walk you through the basic use of several commands. Pay attention to the commands that you type. It is more important to understand why the commands are important and when they would be useful. In a sense, different Linux commands are like different ingredients when cooking: the more commands you know, the more "recipes" you'll be able to create and the more problems you'll be able to solve.
Use the Terminal
- Log into your Linux computer.
- If using the cloud, you probably went right into the terminal.
- If you were using a desktop Linux distribution, you might have a graphical user interface and need to launch the terminal application.
The terminal shows key information in the prompt. For example, you might see:
user@host$
This indicates that the account using the terminal is named user, and the name of the computer is host. The $ indicates that a regular user is using the terminal. A prompt showing # would indicate that the terminal is being used with root (i.e., administrative) permissions.
Note
Your prompt may look slightly different than user@host$ as you complete the exercises. That's okay. Also, sometimes the output will be explained but not shown. This is intentional. You should run the commands and evaluate the output rather than reading this text and figuring that everything makes sense, so there's no reason to run it.
Print Working Directory Command: pwd
One of the hardest things for new Linux terminal users to understand is where they are in the filesystem. Files are organized in directories. Often, we run commands to load or create files. We must know where the commands will look for files. The pwd command helps confirm we are in the right place to create to load or create a file. The pwd command prints the name of the working directory.
Run pwd in a terminal by typing the command and pressing enter.
user@host$ pwd
- You should see output similar to the following. (The exact output may change depending on the Linux distribution and account used.)
user@host$ pwd
/home/ec2-user
- Note the location of your home folder. In Linux, users' files and folders are organized in the
/homefolder. If there were multiple user accounts on this computer, there would be multiple directories inside the/homedirectory. - Run
PWDin the terminal.
user@host$ PWD
PWD: command not found
- Notice the error message: command not found. In Linux, all commands and file names are case-sensitive. This behavior is very different in Windows. In Windows, the case of commands and file names are ignored.
Change Directory Command: cd
The cd command is used to change your working directory. This command lets you move around the directories on your Linux computer.
- Run:
user@host$ pwd
-
This will just check your current working directory.
-
Run:
user@host$ cd /etc
user@host$ pwd
/etc
- Your current working directory should be
/etc. - Using
cdfollowed by a forward slash (/) changes directory to directory relative to theroot. The root of the file system is/. This differs from Windows which organizes files by drive letters, such asC:\. - Run:
user@host$ cd /
user@host$ pwd
/
- You should see
/as your current working directory. - To quickly return to your home folder, run:
user@host$ cd ~
- The tilde is a special character that references the home directory.
- Print your working directory with
pwd. It should be your home directory. - Run:
user@host$ cd ..
-
The two periods tell the
cdcommand to change directories to the parent directory. -
Print your working directory again (
pwd). It should be/home. - Run:
user@host$ cd /
user@host$ cd etc
Because the etc directory exists in the root, changing directories from the root to etc just requires you to enter cd etc.
Absolute and Relative Paths
Entering directory paths that start with / or ~ are absolute paths because they will take you to the same place no matter your present working directory.
- In the terminal, run:
user@host$ cd ~
- Because
~refers to the home directory, it will always take you to your home directory. - Change to another directory with an absolute path.
user@host$ cd /etc
- It would not matter if your previous working directory was
/,/home/bob,/var, or any other directory. Because the path started with/, you will be taken directory to/etc. - Relative paths include
.,.., files in the current directory, and subdirectories of the current directory. - The period (
.) refers to the current directory. Run the following command to check your directory, change the current directory (.), and check your directory again.
user@host$ pwd
user@host$ cd .
user@host$ pwd
- The current working directory should not have changed. The command
cd .is not terribly useful itself, but it is important to remember that the period refers to the current directory. At some point, you will run programs with commands like./runwhich require you to refer to the current directory using a period. - Two periods in a row (
..) refer to theparentdirectory. Run the following commands to observe what happens when changing directories usingcd ...
user@host$ cd ..
user@host$ pwd
- You should now be in the root (
/) directory. - Run
lsto see the directory's contents.
user@host$ ls
- Notice that the
homedirectory is inside/. To navigate to thehtmlfolder, you can use a relative path withcd.
user@host$ cd home
- This command uses a
relative referenceto the/folder because it can be found relative to your present working directory. - Run
lsto see the files inside of/home.
user@host$ ls
- You will see all of the users' home directories.
The distinction between absolute and relative paths is important in many computing contexts, including Windows, website URLs, and importing modules when developing code, just to name a few.
User Command: whoami
The whoami command tells you the account that you are currently logged in as. It might seem obvious right now since you have only logged in with the one user account, but there are times when you may need to log into systems with multiple accounts.
- Run:
user@host$ whoami
You should see your current account. This could be ec2-user, ubuntu, kali, or something else depending on the Linux distribution.
List Command: ls
The ls command is used to list files and directories. This is such a common command that even a simple 4-letter word had to be abbreviated to save Linux admins from getting carpal tunnel syndrome, apparently.
- Run the following command to navigate to your home folder.
user@host$ cd ~
- Run:
user@host$ ls
- You will see the list of files and directories in your home folder. It may be fairly empty if this system is new, or if you have not created any files or directories.
- By default, filenames that start with a period are hidden. But you can view them with the
-aoptions.
user@host$ ls -a
- To list files and directories vertically (the long view), use
-l. Combining that with the-aoption gives usls -al.
user@host$ ls -al
The -l view gives extra information, such as file permissions, file size, and when the file was last modified.
Challenge
Adapt the commands in the previous section to complete the following tasks.
- Navigate to
/var. - Print your working directory.
- List the files and directories in a wide format.
- Navigate to the
logdirectory that exists in the /var directory. - Print your working directory.
- Print the name of the currently logged-in user.
- List the files and directories in a wide format.
- Navigate up one directory.
- Print your working directory.
- Return to your home folder.
- Print your working directory.
Reflection
- Have terminals gone the way of the dinosaur?
- What advantages does a terminal have over a graphical interface?
Key Terms
- Absolute Path: A file or directory path that specifies the complete location in the file system, starting from the root directory. It provides the full hierarchy of directories needed to locate a file or directory, regardless of the current working directory. For example,
/home/user/documents/file.txtis an absolute path. - Relative Path: A file or directory path that specifies the location relative to the current working directory. It does not start from the root directory but from the current position in the file system. For example, if the current working directory is
/home/user, the relative pathdocuments/file.txtrefers to/home/user/documents/file.txt. - Working Directory: The current directory in which a user or a process is operating. It serves as the reference point for relative paths. Commands and operations performed in the terminal or command line interface are executed relative to the working directory unless an absolute path is specified.