This tutorial introduces you to the basics of the Linux command line.
After reading this tutorial, you should be comfortable...
This tutorials assumes that ...
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.
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
Here we'll cover some of the essential commands that you're likely to use again and again.
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.
Before reading further, open your terminal so that you can follow along by trying the commands yourself.
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.
You can create a directory using the mkdir
command:
mkdir ~/repos
That will create a directory called repos
within your home directory.
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
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
~
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
We can print text to the terminal using the echo
command:
echo "Howdy!"
Howdy!
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
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
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!
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).
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/
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
We can copy things use the cp
command.
Let's copy ~/repos/tutorials/OVERVIEW.md
to ~/repos/tutorials/OVERVIEW-copy.md
:
cp ~/repos/tutorials/OVERVIEW.md ~/repos/tutorials/OVERVIEW-copy.md
If you want to copy a directory, you need to use the -r
flag to recursively copy its contents.
You can delete data using the rm
command.
rm
skips the trash can. Things are obliterated.
Let's remove ~/repos/tutorials/OVERVIEW-copy.md
:
rm ~/repos/tutorials/OVERVIEW-copy.md
To remove a directory with rm
, you need to use the -r
option.
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
.
learning-the-shell.txt
in the ~/shell-practice/really/long/path
. ~/shell-practice/really/long
and rename the file to l-t-s.txt
. ~/shell-practice/really/long/path
directory.shutil
or subprocess
.↩