Overview

This tutorial introduces you to the basics of the Linux command line.

Outcomes

After reading this tutorial, you should be comfortable...

  • navigating your filesystem from the command line
  • creating directories from the command line
  • moving directories and files
  • copying directories and files
  • removing files
  • inspecting the value of environment variables
  • listing files in a directory
  • navigating documentation

Prerequisites

This tutorials assumes that ...

Background

What is the command line interface? What is a shell?

A command line interface (CLI) is a way of interacting with a program using only text-based instructions (no GUI). Here we're focusing on the command line interace accessed through your terminal that talks to the operating system. In our case, the program we're interacting with is a shell (ex. bash) which is an intepreter1 for a scripting language that supports statements such as for loops, conditionals, etc. and allows a user to invoke other languages and execute commands2.

Why should I learn this? I already know how to program in x

There are a number of tasks that are much shorter (fewer lines of code) and faster (in terms of execution) to write as a shell script than in a language such as Python3. It's a matter of using the right tool for the job. You can whack at nails with a wrench, but it's usually more efficient to use a hammer.

While it might seem esoteric, Shell scripting is ...

[...] an elegant weapon for a more civilized age

Fair use

Linux command line basics

Here we'll cover some of the essential commands that you're likely to use again and again.

commands and options

Most commands can be modified with one or more options or flags. These are usually denoted with a) --<word> (ex. --version to display the installed version of some command) or b) a single - and a letter (ex. -r) which serves as a mnemonic for its purpose. For example, -r typically refers to recurse or a recursive operation. Often, you will find it useful to apply more than one option at a time.

Try it out

Before reading further, open your terminal so that you can follow along by trying the commands yourself.

Listing files and directories

You can list all files and directories at some location using the ls command:

ls Downloads
important-doc.pdf data.csv readme-old.md

ls by itself will list all files and directories in the current directory.

Let's list details (-l), reverse sort by time modified (-tr), and include file size in a human readable form (-h):

ls -ltrh Downloads
rw-rw-r-- 1 hahnpowell hahnpowell  3G Feb 19  2020 data.csv
rw-rw-r-- 1 hahnpowell hahnpowell  100K Jan 16  2019 important-doc.pdf
rw-rw-r-- 1 hahnpowell hahnpowell  4.8K November 30  2015 readme-old.md

As written, the command above won't reveal hidden files (i.e., files with a . prefix) such as config files. We need the -a flag for that:

ls -a ~/
.bashrc

NOTE: ls by itself will list all files and directories in the current directory.

Create a directory

You can create a directory using the mkdir command:

mkdir ~/repos

That will create a directory called repos within your home directory.

Create a series of directories all at once

What if you wanted to create series of nested directories (for example, repos/tutorials/shell-basics)? All we need is the -p option:

mkdir -p ~/repos/tutorials/shell-basics

Changing directories

We can move to a new directory using the cd command:

cd /var/log

If you keep wandering around the file system, you may get lost. The two commands that follow can help you get back to familiar territory...

pwd

The pwd stands for print working directory. Invoking it will print the absolute path to the current directory:

pwd
/var/log

Tilde ~

pwd can help you get your bearings, but what about returning home? ~ is shorthand for the current user's home directory:

cd ~/
/home/hahnpowell

NOTE: Unless your username happens to be hahnpowell, your output will be different

Printing text

We can print text to the terminal using the echo command:

echo "Howdy!"
Howdy!

Using environment variables

We can define variables and assign values in the shell. Let's imagine we have a long path (ex. ~/repos/tutorials/shell-basics) that we need to use again and again. Let's assign it to the variable LONG_PATH:

LONG_PATH=~/repos/tutorials/shell-basics

if we want to print the value of that variable, we can use echo and prefix a $ to the name of the variable:

echo $LONG_PATH
/home/hahnpowell/repos/tutorials/shell-basics

Did you notice that the shell expanded 4 ~ to /home/hahnpowell?

This is a very powerful feature of the shell. It means we can store arbitrary commands in variables and execute them just by invoking the value of the variable.

Applying this idea, we can move to that directory:

cd $LONG_PATH
/home/hahnpowell/repos/tutorials/shell-basics

Creating files

Empty files

What if we wanted to create an empty file called README.md in our home directory (maybe as a reminder so that we can later fill it out using using our favorite text editor)? In such cases, we can use the touch command:

touch ~/README.md

Initializing files with text

We won't introduce CLI text editors like vim in this tutorial. Even so, we can initialize files with some content:

echo "I love the shell" > $LONG_PATH/README.md
  • > means take the output of the thing on the left and write to the thing on the right overwriting whatever is there.

  • >> is used to append.

Did you notice we combined a variable with another string to create a file path ($LONG_PATH/README.md)? The shell really is useful!

Viewing the contents of files

If we want to see the contents of a file all at once, we can use cat:

cat $LONG_PATH/README.md
I love the shell

If it's a large file, we might want to use less:

less $LONG_PATH/README.md

You can use the arrow keys to move up and down. type q to exit. You can search for string by typing / followed by the search term. Move forward a match using n (for next) and backward using p (for previous).

Moving data

We can move files using the mv command:

Let's move ~/repos/tutorials/shell-basics/README.md to ~/repos/tutorials:

mv ~/repos/tutorials/shell-basics/README.md ~/repos/tutorials/

Renaming files

mv can also be used to rename files. Let's rename ~/repos/tutorials/README.md to ~/repos/tutorials/OVERVIEW.md:

mv ~/repos/tutorials/README.md ~/repos/tutorials/OVERVIEW.md

Copying data

We can copy things use the cp command.

Files

Let's copy ~/repos/tutorials/OVERVIEW.md to ~/repos/tutorials/OVERVIEW-copy.md:

cp ~/repos/tutorials/OVERVIEW.md ~/repos/tutorials/OVERVIEW-copy.md

Directories

If you want to copy a directory, you need to use the -r flag to recursively copy its contents.

Deleting data

You can delete data using the rm command.

🔥 Be very careful with this command. Deleting with rm skips the trash can. Things are obliterated.

Files

Let's remove ~/repos/tutorials/OVERVIEW-copy.md:

rm ~/repos/tutorials/OVERVIEW-copy.md

Directories

To remove a directory with rm, you need to use the -r option.

Viewing documentation (man)

There are many options to each command. It's easy to forget what they all do. If you're not sure what a command does or what options are available, you can view its manual page using the man command:

man mkdir

The navigation controls are the same as those for less.

Next steps

  • Practice what you've learned
    1. create a file called learning-the-shell.txt in the ~/shell-practice/really/long/path.
    2. Next, move it to ~/shell-practice/really/long and rename the file to l-t-s.txt.
    3. Finally, delete the ~/shell-practice/really/long/path directory.

  1. A program that is able to execute instructions on-the-fly without compiling. See https://en.wikipedia.org/wiki/Interpreter_(computing).
  2. https://en.wikipedia.org/wiki/List_of_Unix_commands
  3. Compare the succintness of the techniques covered in this tutorial with the amount of code needed when using Python's shutil or subprocess.
  4. See https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion
cd ~/Creative Commons License