Articles & BlogBy 8 min read

For anyone who has never opened a terminal. By the end you will be able to move around a Linux system, read and search files, check on the machine, and look up the rest yourself.

The command line looks like a test you did not study for: a black window, a blinking cursor, no buttons. It is closer to texting a very literal assistant. You type one short line, it does that one thing, and it answers.

Servers are run this way, and many have no desktop at all. About 25 commands cover the bulk of the work. This article walks through them.

A beige DEC VT100 terminal with green text glowing on its dark screen and a separate black keyboard in front, a thick printed listing draped over the topA DEC VT100, from the days when the terminal was a machine of its own on the desk. Photo by Wolfgang Stief, CC0, via Wikimedia Commons.

What are a terminal and a shell?

A terminal used to be a machine like the one above: a screen and a keyboard wired to a computer somewhere else. Today it is a window that plays the same part.

The shell is the program running inside that window. It reads the line you type, runs it, and prints the result. The usual one is Bash, and it is the one this article uses.

The shell shows it is ready by printing a prompt. On Ubuntu and many others it looks like this:

sam@laptop:~$

Read it left to right. sam is the user you are logged in as. laptop is the name of the machine. ~ is the folder you are standing in, and ~ is shorthand for your home folder, /home/sam. The $ means you are a normal user. If it ever shows #, you are root, the administrator, and every command deserves a second look.

How is a command built?

Almost every command has the same three parts, separated by spaces.

A diagram of one command line split into four labeled boxes. First the prompt, sam@laptop:~$, which shows the user, machine and folder and which you do not type. Then the command, ls, which is what to run. Then the option, -l, which is how to run it. Then the argument, /var/log, which is what to act on. A note says spaces separate the parts and nothing happens until you press Enter.Command, option, argument: the pattern behind nearly every line you will type.

The command is the program to run. Options change how it behaves, and start with a dash: -l is a short option, --help is a long one. Short options can be stacked, so ls -l -a and ls -la mean the same. The argument is what to act on, usually a file or folder.

Linux is case sensitive. Notes.txt and notes.txt are two different files, and LS is not a command.

Arguments are usually paths, and there are two kinds. A path that starts with / is absolute: it is spelled out in full from the top of the system, like /var/log, and means the same thing wherever you are. A path that does not start with / is relative: it is read from the folder you are standing in. Three shortcuts save typing. A single dot . means "here", two dots .. mean "one level up", and ~ means your home folder.

Which commands do most of the work?

Here they are, grouped by job.

Moving around and filesWhat it does
pwdPrints the folder you are in
lsLists what is in a folder. ls -l adds details, ls -a shows hidden files.
cd folderMoves into a folder. cd .. goes up one level, plain cd goes home.
mkdir nameMakes a folder
touch nameMakes an empty file if none exists
cp from toCopies a file. Add -r to copy a folder.
mv from toMoves a file, or renames it
rm nameDeletes a file. There is no recycle bin.
Reading and findingWhat it does
cat filePrints a whole file to the screen
less fileOpens a file one page at a time. Space for the next page, /word to search, q to quit.
head filePrints the first 10 lines
tail filePrints the last 10 lines. tail -f keeps printing as the file grows, which is how you watch a log.
grep word filePrints only the lines that contain the word. -i ignores case, -r searches a whole folder.
find folder -name "*.log"Finds files by name, anywhere under a folder
System and helpWhat it does
ps auxLists every running program
topA live view of what is using the processor and memory. q quits.
df -hShows how full each disk is
du -sh folderShows how much space one folder takes
free -hShows memory in use and available
man commandOpens the manual for a command. q quits.
command --helpPrints a short summary of options
historyLists the commands you have typed
sudo commandRuns one command as the administrator

The -h on df, du and free means "human readable": sizes in megabytes and gigabytes.

The two that matter most are man and --help. Nobody memorizes options. People who look fast in a terminal are mostly people who look things up without embarrassment.

Tip

Press Tab and the shell finishes the name you started typing. Press it twice and it shows every match. Use it all the time: it is faster, and a name that will not complete is a name you have misspelled. The up arrow brings back earlier commands, and Ctrl+R searches them. Ctrl+C stops a command that is stuck.

Pipes and redirection

Each command does one small thing. The power comes from joining them.

A pipe, written |, takes what one command prints and feeds it to the next as input.

A diagram of the command line ls /etc | grep conf | wc -l as three boxes joined by arrows. The first box, ls /etc, lists every name in /etc. A pipe carries those many names to the second box, grep conf, which keeps the lines that contain conf. A second pipe carries the fewer names to the third box, wc -l, which counts the lines it was given. An arrow from the last box leads to the result, one number. A note says what one command prints the next one reads, and only the last result reaches your screen.Three small commands, two pipes, one answer.

Redirection sends output to a file in place of the screen. > writes a new file, and replaces one that is already there. >> adds to the end.

ps aux | grep ssh

This lists every running program, then keeps only the lines that mention ssh.

df -h > disk-report.txt

This saves the disk report to a file. Nothing appears on screen.

A first session to type along with

Open a terminal and type each line, pressing Enter after each. Your name and dates will differ from mine.

pwd

Shows where you are. You start at home.

/home/sam
mkdir practice
cd practice

Makes a folder named practice and steps into it. Neither prints anything. On the command line, silence means it worked.

echo "first line" > notes.txt
echo "second line" >> notes.txt

echo prints its argument. The first line sends that into a new file, the second adds to it.

cat notes.txt

Prints the file.

first line
second line
ls -l

Lists the folder with details.

total 4
-rw-r--r-- 1 sam sam 23 Nov 11 19:02 notes.txt
cp notes.txt copy.txt
grep second *.txt

Copies the file, then searches every .txt file for the word "second". The * stands for "anything".

copy.txt:second line
notes.txt:second line
ls | wc -l

Counts the entries in the folder by piping the list into a line counter.

2
cd ..
rm -r practice

Steps back out and deletes the practice folder and everything in it. You are back where you started.

When a line fails, the shell says why, and three messages cover most cases. "command not found" means you misspelled the command or it is not installed. "No such file or directory" means the path is wrong, so check it with ls. "Permission denied" means the file exists but your user may not touch it. Read the message before you try again. It is nearly always telling you the truth.

Two ways beginners get hurt

The first is rm. It does not ask and it does not keep a copy. With -r it deletes folders and everything below them. With -f it never prompts, even for protected files. Put together as rm -rf, one typo in the path is enough. rm -rf ~/practice removes a practice folder. rm -rf ~ /practice, with one stray space, removes your whole home folder first.

Before any rm -r, run ls on the same path and look at what comes back. While you are learning, rm -i asks before each deletion.

The second is sudo. It runs a command as root, the account that may change anything on the system. It asks for your own password, and remembers it for a few minutes. That is fine for installing software. It is a problem when you put sudo in front of something you do not understand.

Watch out

Do not paste commands from a website, a video description or a chat message unless you can say what each part does. A single line can download a script and run it as root, and a web page can hide text that only shows up once pasted. Type it out, or paste into a text editor first and read it. If a guide says to pipe a download straight into sudo bash, look for a different guide.

What to remember

  • The terminal is the window, the shell is the program in it, and the prompt tells you who and where you are.
  • Command, options, argument, separated by spaces.
  • pwd, ls and cd tell you where you are and move you around.
  • cat, less, grep and tail -f cover most reading and searching.
  • A pipe feeds one command's output into the next. > replaces a file, >> adds to it.
  • man and --help answer most questions faster than a search engine.
  • ls before you rm -r. Understand a line before you sudo it.

Run the practice session twice, then try man ls and read the first screen. When ls -l starts to look familiar, the letters at the start of each line are the next thing to learn, and Linux Files, Users and Permissions explains them.