Fixing a server at ten past nine
A website is slow. Somebody sends you the address of a server and says: look at the log. That server has no mouse and no menu, only a line to type on. The people who fix it fast are not cleverer. They know about thirty short commands by heart.
By the end of this lesson
Note
I can use the terminal to move around a Linux machine and make files.
Key words
- Terminal: a window where you type commands.
- Command: one instruction you type, then press Enter.
- Folder: a place that holds files and other folders.
- Path: the address of a file or folder, like
/var/log/test.log. - Home folder: the folder that is yours, written
~.
Starter: what you already know
Note
Answer in your head. 1. What is a folder for? 2. What does the end of a file name, such as .txt, tell you? 3. Why does a computer need a way to say where a file is? You will use the answers to the first and third in this lesson.
Before you start
Note
Press Start the lab on the bar at the top of this lesson. This lab is a whole Ubuntu machine of your own, so it takes a little longer to come up than a container. Each Run button types its command into the machine's terminal, one after another in the same session. If one says the machine is still starting, wait a few seconds and press it again. Where the lesson says Type this, the command never ends by itself or asks you something, so press Open terminal on the bar and type it yourself.
Where am I, and what is here?
cd with nothing after it goes to your home folder. pwd prints the folder you are in. ls -la lists everything in it, with the hidden names that start with a dot, and who owns each. cd projects/app moves into a folder, and cd - goes back to where you were. Short names help: ~ is home, . is here and .. is the folder above.
cdShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
pwdShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
ls -laShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
cd projects/appShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
cd -Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Three habits that save typing
Press Tab and the terminal finishes a name for you: cd pro and Tab gives cd projects/. Press the up arrow for the last command, and Ctrl+R then a few letters to search the older ones. Press Ctrl+C to stop a command that is stuck. clear wipes the screen. Try Tab and the up arrow in the terminal now.
Check
Tip
What does cd - do, and what does pwd print? Answer: cd - goes back to the folder you were in before, and pwd prints the folder you are in now.
Make a folder and a file, and move one
mkdir -p makes a folder, and the -p also makes any folder above it that is missing. touch makes an empty file. ls -R lists a folder and the folders inside it. Look at the answer: todo.txt is in work/notes.
mkdir -p work/notesShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
touch work/notes/todo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
ls -R workShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Write some words, then read them
echo prints words. The sign > sends them into a file instead of the screen, and it replaces what the file held. >> adds to the end instead. cat prints the whole file. Two lines come back, because the second echo added to the first.
echo "call the dentist" > work/notes/todo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
echo "email Sam" >> work/notes/todo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
cat work/notes/todo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Exit check
Tip
Answer without looking back. 1. Which command prints the folder you are in? 2. Which sign adds to a file instead of replacing it? 3. What does mkdir -p do that mkdir does not? Answers: pwd; >>; it also makes any missing folder above.
For teachers
Tip
This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. This lesson is worked in a VM lab: each student has an Ubuntu machine of their own, and a Run types into its terminal and shows what it printed. Key learning points: 1. pwd, ls and cd show where you are and move you. 2. mkdir, touch, echo and cat make and read files. Common mistake: using > when >> was meant, which replaces the file. Equipment: the lab and a browser. Tip: ask students to say what a command will print before they press Run. The lab has no check and marks nothing; set a hand-in of your own if you want one. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.