Finding an invoice with no mouse
A shop's web server holds thousands of files. A colleague asks you to find last month's invoices on it.
The server has no desktop and no mouse. You have one window and a blinking cursor.
Most servers work this way. You move round them by typing short commands.
By the end of this lesson
Note
I can work with files and folders from a shell.
Key words
| Word | What it means | Example |
|---|---|---|
| Shell | the program that reads your commands and runs them | |
| Path | the address of a file or folder | /home/student/projects |
| Working directory | the folder you are in now | |
| Redirect | send a command's answer into a file | >> |
| Pipe | hand one command's answer to the next | | |
| Mode | the letters that say who may read, write or run a file | rw-r--r-- |
The cells students fill in are left blank in this preview.
A path is the address of a file
The shell is always "in" one folder. This is the working directory. Commands act on it unless you say otherwise.
A path that starts with / is a full path. It works from anywhere.
A path without a / at the start is a relative path. The shell looks for it inside the working directory. Two dots, .., mean the folder above.
Worked example: where cd takes you
| You are in | You type | You end up in |
|---|---|---|
| /home/student | cd projects | /home/student/projects |
| /home/student/projects | cd app | /home/student/projects/app |
| /home/student/projects/app | cd .. | /home/student/projects |
| /home/student/projects | cd /var/log | /var/log |
| /var/log | cd projects | An error: no such folder |
The cells students fill in are left blank in this preview.
Where am I?
pwdShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
What is here?
lsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Go down two folders
cd projects/appShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Look inside
lsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Go home again
cdShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think cd projects works from any folder. It only looks inside the folder you are in now. Run pwd first, or type a full path that starts with /.
mkdir, cp and mv change things
mkdir makes a folder. cp copies a file, and the original stays where it was. mv moves a file. Give it a new name in the same folder and it renames the file.
rm removes a file. On a server there is no bin, so a removed file is gone.
Run the next four in order.
Make a folder
mkdir -p shopfilesShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Copy a file into it
cp projects/docs/notes.txt shopfiles/notes.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Rename the copy
mv shopfiles/notes.txt shopfiles/todo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Check the folder
ls shopfilesShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Redirects and pipes join commands
A command prints its answer on the screen. A redirect sends the answer into a file instead. > replaces the file. >> adds to the end.
A pipe, |, hands the answer to another command. ls | wc -l lists names, then counts the lines.
Add a line to the file
echo "Order 2043 shipped" >> shopfiles/todo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the file
cat shopfiles/todo.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Count the files in a folder
ls projects/app | wc -lShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think > and >> do the same job. > throws away what the file held and starts again. >> keeps it and adds to the end. Use >> unless you mean to start again.
Worked example: a mode as three digits
| Letters | Owner | Group | Others | Digits |
|---|---|---|---|---|
| rw-r--r-- | rw- = 4+2 = 6 | r-- = 4 | r-- = 4 | 644 |
| rwxr-xr-- | rwx = 4+2+1 = 7 | r-x = 4+1 = 5 | r-- = 4 | 754 |
| rwx------ | rwx = 7 | --- = 0 | --- = 0 | 700 |
The cells students fill in are left blank in this preview.
One group of letters as a number
# One group of three letters, worked out.
group = "r-x"
total = 0
if group[0] == "r":
total = total + 4
if group[1] == "w":
total = total + 2
if group[2] == "x":
total = total + 1
print(total)
# Change group to "rw-" and run again.
Going further
Type these yourself. Make a folder invoices inside shopfiles. Make three empty files in it with one command: touch takes several names.
Then count them with one pipe, and use > to write the count into shopfiles/count.txt. Say what cat shopfiles/count.txt will print before you run it.