Beginners Linux Guide: Navigating the Command Line

Matt Kmety
The KickStarter
Published in
8 min readJul 28, 2020

An introduction to the command line and 5 commands that will help you to become more comfortable using the command line in Linux.

Being able to navigate the command line is essential to fully utilize the power of any operating system. While most popular Linux distributions come with the option of installing a graphical user interface (GUI), the command line allows you to be more efficient and leverage tools not available through the GUI.

In this article, we will discuss 5 basic Linux commands that will introduce you to the command line, explain their use, and give several examples and variations of each.

The command line may seem intimidating if you’ve never used it before. Learning and understanding these commands will help build the foundation required to master the Linux command line.

Requirements

A system running Linux (any distribution is fine but we will be running a fresh install of Ubuntu Desktop 20.04)

Accessing the Command Line

This step will vary depending on your Linux distribution, how you are interacting with your Linux system, and whether or not you have a GUI installed.

Since we are running Ubuntu Desktop 20.04 I have a few options to get to the command line.

First, we can right-click the desktop and select Open in Terminal. A terminal window will display on your desktop with a prompt to enter commands.

Note: For the sake of simplicity, we will use the terms command line, terminal, and console interchangeably and to represent the shell. Yes, there are differences. They are beyond the scope of this article. Feel free to research each for a better understanding.

Second, you can connect remotely to your Linux system using a terminal emulator such as PuTTY. This can be done from any remote system and will display a terminal window similar to the first option.

Note: This option is more advanced than the first option since you are connecting remotely.

PuTTY

Understanding the Command Line

You should now have your terminal window opened. Before we start jumping into commands it’s important to understand what is being displayed on the screen.

There are a few key pieces of information that can be noted from the information being displayed in your terminal window. Let’s break down what is being displayed in our prompt.

mrkmety@ubuntu-desktop:~/Desktop$ 

mrkmety: The current logged in user
ubuntu-desktop: The hostname of the system
~/Desktop: The current working directory
$: Type of user logged in. $ means you are a normal user. # means you are the root user.

Note: ~ is shorthand for your home directory. In this case, my home directory is /home/mrkmety.

We can see that we are logged in as mrkmety on a host named ubuntu-desktop. Our current working directory is ~/Desktop and we are a normal user.

Let’s move on to some of the basic commands available.

pwd

The pwd command stands for print working directory. This command will display the full name of your current working directory.

Run the following command to print your current working directory.

mrkmety@ubuntu-desktop:~/Desktop$ pwd
/home/mrkmety/Desktop

The output displayed shows /home/mrkmety/Desktop.

This command is useful if the current working directory isn’t displayed in your prompt.

ls

The ls command lists the contents of the directory. By default, running the ls command will display the contents of your current working directory.

There are additional options we can add to the end of commands to change their behavior. We will touch on a few of these below.

Run the following command to print the contents of your current working directory.

mrkmety@ubuntu-desktop:~/Desktop$ ls
directory1 directory2
file1 file2

The output displays the files and directories in the current working directory. The Ubuntu terminal will display directories as a different color to differentiate them from files.

Note: Not all Linux distributions and terminal emulators will change colors to differentiate directories and files.

The ls command allows for options to be appended to the command that will display more information.

Run the following command to print the contents of another directory.

mrkmety@ubuntu-desktop:~/Desktop$ ls /var
backups cache crash lib local lock log mail ...

You can see that our prompt is still showing our current working directory of ~/Desktop, but we can add any directory to the end of the ls command to display the contents.

Run the following command to display additional information on the files and directories.

mrkmety@ubuntu-desktop:~/Desktop$ ls -l
total 8
drwxrwxr-x 2 mrkmety mrkmety 4096 Jul 26 15:16 directory1
drwxrwxr-x 2 mrkmety mrkmety 4096 Jul 26 15:16 directory2
-rw-rw-r-- 2 mrkmety mrkmety 4096 Jul 26 15:16 file1
-rw-rw-r-- 2 mrkmety mrkmety 4096 Jul 26 15:16 file2

The output of this command shows much more useful information such as:

  • Permissions
  • Owner
  • Group Owner
  • Date/Timestamp
  • Filename

You’ll notice that the directories are prefixed with a “d” to show that they are directories and not files. Files show a dash instead of a “d” to indicate they are files.

The rest of the command output is out-of-scope for this article but is helpful to know while learning Linux.

Run the following command to display hidden files in the current working directory.

The last variation of ls that we’ll cover is for displaying hidden files. In Linux, files prefixed with a period don’t show up using the base ls command.

mrkmety@ubuntu-desktop:~/Desktop$ ls -al
total 8
drwxrwxr-x 2 mrkmety mrkmety 4096 Jul 26 15:16 directory1
drwxrwxr-x 2 mrkmety mrkmety 4096 Jul 26 15:16 directory2
-rw-rw-r-- 2 mrkmety mrkmety 4096 Jul 26 15:16 file1
-rw-rw-r-- 2 mrkmety mrkmety 4096 Jul 26 15:16 file2
-rw-rw-r-- 2 mrkmety mrkmety 4096 Jul 26 15:16 .hidden1
-rw-rw-r-- 2 mrkmety mrkmety 4096 Jul 26 15:16 .hidden2
drwxrwxr-x 2 mrkmety mrkmety 4096 Jul 26 15:16 .hidden_dir1
drwxrwxr-x 2 mrkmety mrkmety 4096 Jul 26 15:16 .hidden_dir2

Note: ls -a works as well, but I like to display the long listing to have more information displayed in the output.

The output now shows files that are prefixed with a period that were previously hidden without the -a option on the command.

cd

Up until this point, we haven’t changed our current working directory. The cd command allows you to change your current working directory.

You simply type cd followed by the name of the directory you’d like to change into.

Run the following command to change into the /opt directory.

mrkmety@ubuntu-desktop:~/Desktop$ cd /opt
mrkmety@ubuntu-desktop:/opt$
mrkmety@ubuntu-desktop:/opt$ pwd
/opt

No output is displayed after executing the command, but you’ll notice our prompt updated to show us moving from ~/Desktop to /opt.

We can also run the pwd command to confirm our current working directory is /opt.

sudo

The sudo command allows you to execute a command as another user. Most notably, it allows you to execute a command as the root user. This is comparable to running a command as admin in a Windows environment. You’re temporarily elevating your privileges instead of running every command as root.

Note: It is best practice to leverage sudo and not log in as root.

Your user account must be allowed to run the sudo command. If you are running Ubuntu desktop, the account you created will be part of the sudo group. You can verify this by executing the groups command and looking for the sudo group on your account.

mrkmety@ubuntu-desktop:~/Desktop$ groups
mrkmety adm cdrom sudo dip plugdev lpadmin lxd sambashare

The account mrkmety is part of the sudo group allowing me to run commands with elevated privileges.

First, we’ll try to list the contents of the /root directory as a normal user using ls.

mrkmety@ubuntu-desktop:~/Desktop$ ls -l /root
ls: cannot open directory '/root': Permission denied

We get a permission denied message when trying to ls /root as a normal user.

Let’s run the same command with sudo.

Note: You will be prompted to enter your password when running commands using sudo.

mrkmety@ubuntu-desktop:~/Desktop$ sudo ls -l /root
[sudo] password for mrkmety:
total 0
-rw-r--r-- 1 root root 0 Jul 27 21:06 root_test_file.txt

The sudo command has given us temporary elevated privileges to list the contents of the /root directory. Certain actions can only be done with elevated privileges.

cat

The cat command allows you to concatenate files and print them to standard output. Essentially, you can read the contents of one file (or more) and have the contents printed to your terminal. This is a useful and quick way to see what’s in a file without opening a text editor.

Run the following command to see the contents of the /etc/fstab file.

mrkmety@ubuntu-desktop:~/Desktop$ cat /etc/fstab
# /etc/fstab: static file system information.
#...
#...
/swapfile none swap sw 0 0

The contents are displayed in the terminal window.

Note: Files that have large amounts of text can be managed easier by using commands such as less and more.

man

One of the most important commands available in Linux is the man command. The man command allows you to see man pages or manual pages.

These pages give detailed information on commands such as syntax, flags, options, and examples. If you need help with how a command works or the available options the man page should be your first stop.

Run the following command to see the man page for cat.

mrkmety@ubuntu-desktop:~/Desktop$ man cat

You should see an output in your terminal similar to the screenshot below.

Honorable Mentions

There are an endless amount of useful Linux commands. We haven’t even begun to scratch the surface on all of the basic commands you will use when starting to learn and use Linux.

Feel free to use the man command to check out these other Linux commands that will be useful when learning Linux

  • ps
  • top
  • grep
  • su
  • apt (if on Ubuntu or another Debian-based distro)
  • yum (found on Red Hat and CentOS distros)

Wrapping Up

Learning how to navigate the command line allows you to work more efficiently and puts more tools at your disposal. Employers see this as a valuable asset as well.

Over the years I’ve learned to go to the command line first to try and do everything I need to do before turning to the GUI. However, there are times when the GUI is your only option.

Continue using the command line, learning new commands, and you’ll become proficient in no time. Once you have a solid foundation you will be able to move onto more advanced training such as shell scripting.

If you have any questions feel free to Tweet or PM me @mrkmety

--

--

Matt Kmety
The KickStarter

Cybersecurity Enthusiast | Cloud Security & Information Protection @ Boeing | Trying to pass on knowledge to others | www.thecyberblog.com