The terminal for beginners
Connected to your server, all you see is one line, waiting. About fifteen commands are enough to find your way, read, write, tidy up, and get out of any jam.
- Course
- Tutorial 8 of 35
- Time
- 30 min to read
- Tested with
- Debian 13.7 (trixie), bash 5.2
What you will be able to do
- Know where you are and move between folders.
- Read a file, create one and edit it with nano.
- Copy, rename and delete without a slip.
- Get out of a jam with Ctrl+C, q or Ctrl+X.
Before you start: Your first SSH connection.
Contents
You're connected to your server. In front of you, one blinking line and not a single icon. How do you find your way without a mouse? With about fifteen commands, which you learn in thirty minutes and keep for life. A command is a short instruction, typed in the terminal and sent with Enter. You'll watch each one work on your server, in a practice folder that we'll delete at the end. The connection itself is in Your first SSH connection.
1The three emergency exits
Before the first command, learn how to escape: it's easier to learn when you know the way out. Sooner or later you'll be stuck in front of a screen that stops responding, and the cause is always one of these three:
| What you see | The key | Effect |
|---|---|---|
| A command is running and won't give the prompt back | Ctrl + C | stops it |
Full-screen text (less, man) | the q key | closes it |
The nano editor | Ctrl + X | quits it, after offering to save |
Let's try Ctrl + C right now. The sleep command waits for the number of seconds you give it and does nothing else, which makes it a perfect victim. Type it, let it run for a second, then press Ctrl and C together.
sleep 100^CThe ^C is the trace of your keypress, and the prompt comes straight back. The prompt is the line that waits for you to type, for example root@mon-serveur:~#. You can now stop a command that drags on forever, which is very reassuring. What is left is knowing where you are.
2Where are you: pwd and ls
The pwd command prints the place where you are, like the red dot on a map. Its name comes from print working directory. A folder is a container that holds files and other folders. Its address is called a path, and its parts are separated by slashes.
pwd/root/root is the home folder of root (the administrator account), and your starting point at every login. To see what it contains, use ls (short for "list").
lsOn a new machine nothing shows up: the folder is empty, or holds only hidden files. A hidden file has a name that starts with a dot, and ls doesn't show it. To see them, add the option -a ("all"), a setting placed after the command: ls -a. Now let's see how to leave this folder.
3Move around with cd
The cd command ("change directory") takes you into another folder, whose path you give. Try it with /etc, the folder where Linux keeps its settings. A path that starts with a slash begins at the very top of the system (the root, written /). So it works from anywhere.
cd /etcroot@mon-serveur:/etc#The command prints nothing, but the prompt changes. Where the ~ used to stand for your home folder, it now shows /etc. Read the prompt: it always tells you where you are. Two shortcuts get used all the time:
cd ..goes up one folder. The two dots stand for the parent folder.cdalone, with nothing after it, takes you back to your home folder.
Try the second one, then check with pwd.
cdroot@mon-serveur:~#You're back home, and the prompt shows the ~ again. You can move around, so let's read what the files contain.
4Read: cat and less
The cat command prints the content of a file, and less shows it one page at a time when it's long. For a short file, cat is enough. The file /etc/hostname holds the machine's name, so let's read it.
cat /etc/hostnamemon-serveurFor a long file, cat would dump hundreds of lines at once. Use less instead, which opens the file full screen. Try it on /etc/login.defs, a settings file over a hundred lines long.
less /etc/login.defsFour keys are enough inside less:
- the space bar moves forward one page, and b moves back one page;
/followed by a word, then Enter, searches for that word (try/LOGIN_RETRIES);- q closes the file and gives you the prompt back.
If you get stuck in less, press q, as in the table at the start. cat has a trap of its own: with no file name, it reads what you type on the keyboard.
catcoucou
coucou
^CThe first coucou is what you typed, and the second is cat repeating it back. Nothing is broken: it was waiting for some text. Ctrl + C stops it, as at the start. Ctrl + D does too, because it means "I'm done typing". Reading is only half the job, so let's write a file.
5Create: mkdir and nano
The mkdir command ("make directory") creates a folder, and nano is a small text editor that opens inside the terminal. First create a practice folder, then step into it.
mkdir exercicecd exerciceroot@mon-serveur:~/exercice#Now open a new file called bonjour.txt. If that name doesn't exist yet, nano creates the file when you save.
nano bonjour.txtThe screen empties, with a reminder of the shortcuts at the bottom. The ^ sign stands for the Ctrl key there: ^O means Ctrl + O. Just write Bonjour depuis mon-serveur as you would in a word processor, then do this:
- Press Ctrl + O to save. At the bottom,
nanoshowsWrite to File: bonjour.txt. - Press Enter to keep that name. It confirms with
[ Wrote 1 line ]. - Press Ctrl + X to quit.
If you quit without saving, nano asks Save modified buffer? ("save your changes?"). The Y key says yes, then Enter confirms the name, and N discards the text. Check that the file exists, with cat.
cat bonjour.txtBonjour depuis mon-serveurYour first file is written, and it is yours. Let's make copies of it.
6Copy and rename: cp and mv
The cp command ("copy") copies a file: give the original first, then the name of the copy. The mv command ("move") changes the name or the place of a file.
cp bonjour.txt copie.txtNothing shows when all goes well. That's the rule of the terminal: silence means it worked. Let's look at the folder.
lsbonjour.txt copie.txtNow rename the copy. For mv, renaming and moving are the same thing: you give the old name, then the new one.
mv copie.txt salut.txtlsbonjour.txt salut.txtThe copy is now called salut.txt. For more detail, add the option -l ("long"), which also shows each file's size and date.
ls -ltotal 8
-rw-r--r-- 1 root root 27 Nov 4 22:15 bonjour.txt
-rw-r--r-- 1 root root 27 Nov 4 22:15 salut.txtThe number before the date is the size in bytes (here one byte is one character): 27 characters, as in "Bonjour depuis mon-serveur". Your dates will be your own. You now have one file too many, and it's time for the most dangerous command.
7Delete with care: rm
The rm command ("remove") deletes a file for good. There is no trash can in the terminal: what's erased doesn't come back. One habit protects you before every rm: run ls first to see what you're about to erase.
rm salut.txtlsbonjour.txtTo make the terminal ask for confirmation before each deletion, add the option -i ("interactive"). Answer n to cancel. It's a good setting to keep until you feel at ease.
rm -i bonjour.txtrm: remove regular file 'bonjour.txt'? nThe file stayed in place; check with ls. A folder can't be deleted with a plain rm: the terminal refuses.
cd ..rm exercicerm: cannot remove 'exercice': Is a directoryYou just saw the terminal say no, and that's a feature. Next, learn to ask it questions.
8Get help: --help and man
Every command explains itself: add --help after it. The text is often long, so keep only the beginning. The | sign (the vertical bar) sends one command's output to the next. Here, head -3 ("head") prints only the first three lines of that text.
ls --help | head -3Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.For the full manual, run man (short for "manual") followed by the command name. It opens full screen like less: same keys, and q to leave.
man lsThese pages are dense, but they say everything. Search for an option with / as in less. By now you have typed a lot of commands, and the terminal kept track of them.
9Go faster: history and Tab
The terminal remembers the commands you typed, and two tricks save you from typing them again. The first is the up arrow: it brings back the previous command, then older ones if you keep pressing. Enter runs it again, after any fix you make. The history command shows the list, with a number on the left. Add a number to see only the last lines.
history 4 15 rm -i bonjour.txt
16 cd ..
17 rm exercice
18 history 4The last line is the history command itself. Your numbers will differ, and so will your commands.
The second trick is the Tab key (left of the letter Q, above Caps Lock): it completes a name for you. Type cd exe then press Tab, and the terminal writes cd exercice/ by itself. If there are several possibilities, a second Tab lists them. Tab prevents typos, and it's the key regulars press most. Let's use it to tidy up.
10Tidy up
One thing is left: the practice folder. With Tab, type ls exe then Tab, and check what it holds before erasing it. That's the habit from step 7.
ls exercicebonjour.txtIt holds only your test file. So you can delete the folder with the -r option from the warning.
rm -r exercicelsThe folder is gone and ls shows nothing: you are back to the starting state. Now for the two messages you're sure to meet.
If it fails
Two messages come back almost every session: a name that can\'t be found and a missing command. The first appears as soon as one letter is wrong, a capital for example. Linux tells Exercice and exercice apart.
cat absent.txtcat: absent.txt: No such file or directoryThe second message shows up when a program is missing on your machine, for example nano, less or man on a very minimal install.
nano essai.txtbash: nano: command not foundThe last trap is a terminal that seems frozen while it's waiting for more. Think of the three emergency exits from the start: Ctrl + C, q and Ctrl + X. With them, you always land back on the prompt.
What comes next
You can now find your way, read, write, copy, delete and escape a jam. Yet all this work happened as root, the account with no safety net, where a typo can cost you. The next article, Users, sudo and file permissions, creates an ordinary account for you and explains sudo. It comes now because you have to know how to type commands before you limit what they can do.