Day 3 — The terminal describes the same folders
Lecture slides · Quiz · Weekly revision
Today’s question
Can you move around your folders by typing instead of clicking? The computers that hold large biological data, such as a sequencing facility’s server, usually have no desktop: you log in and type commands. Today you practise those commands on your own laptop, in a small workspace you create yourself. Tomorrow the same workspace will hold files from a laboratory exercise.
Save today: the introcomputing-shell workspace and navigation-notes.txt inside it.
Opening a terminal
Windows students open Ubuntu; Mac students open Terminal. The Windows/Mac setup page shows how. A shell is the program that reads your commands and runs them, and the terminal is the window you type them into. When it is ready, it shows a prompt, for example:
anna@laptop:~$
Your prompt will look different, and on a Mac it may end in %. Never type the prompt itself: type only the command after it and press Enter.
On Windows, Ubuntu has its own home folder, separate from your Windows Documents folder. This week we work in that Ubuntu home folder on purpose; the R project stays in Documents.
Where am I?
Use pwd (print working directory) to find out which folder the terminal is in, and ls (list) to find out what is in it:
pwd
lspwd prints something like /home/anna (Ubuntu) or /Users/anna (Mac): your home folder. ls lists its contents, which will differ from your neighbour’s.
Now make a workspace for this week and move into it:
cd
mkdir -p introcomputing-shell/practice
cd introcomputing-shell
pwd
lscd (change directory) on its own takes you home. mkdir (make directory) creates folders; the option -p lets it create introcomputing-shell and practice in one go, and it does not complain if they already exist. The last pwd should end in /introcomputing-shell, and ls should show practice.
The same folder in the file manager
To find out whether this is really the same folder you would click through, open it in the file manager. In Ubuntu on Windows type explorer.exe ., and on a Mac type open .. The . means “the folder I am in”. A window opens showing the practice folder: one folder, two ways of looking at it. Keep the window open.
Moving between folders
Make two folders for plant data and move down into one and back up again:
cd practice
mkdir seedlings
mkdir observations
ls
cd seedlings
pwd
cd ..
pwdls shows the two new folders. .. means the folder one level up, so the two pwd lines end in /practice/seedlings and then /practice. The workspace now looks like this, which you can check in the file manager:
introcomputing-shell/
└── practice/
├── seedlings/
└── observations/
Now click into seedlings in the file manager and run pwd in the terminal. The terminal is still in practice: each window keeps its own location.
A command can also take an option that changes what it does. Use ls -l in practice to find out when each folder was last changed; the folder names are in the last column. Then use cd observations and ls to find out what is in the observations folder. ls prints nothing at all, because the folder is empty. Many commands work silently like this; a real problem prints a message.
When a command fails
Go back to practice with cd .. and try cd Observations, with a capital O. On Ubuntu this fails, because Ubuntu treats Observations and observations as different names. Many Macs accept it. Always type the real spelling, so that your commands work on every computer.
Then try a folder that does not exist:
cd no-such-folder
pwdThe shell says there is no such file or directory, and pwd shows you are still where you were. A failed cd never moves you and never creates anything. When in doubt, ask “where am I, and what is here?” with pwd and ls.
Run cd ~/introcomputing-shell, then pwd. ~ is short for your home folder, so this works from anywhere on this computer.
Names with spaces, and keys that save typing
Inside practice, create a folder day03, move into it, and create two folders in it: readings and field notes. Type the second one with quotes:
mkdir day03
cd day03
mkdir readings
mkdir "field notes"
lsls shows two folders. Without the quotes, mkdir field notes would make two folders called field and notes, because the shell splits what you type at spaces. Quoting works, but names without spaces, such as field-notes, are easier to type.
Four keys make the terminal much less tiring:
- Tab completes a name. Type
cd fieand press Tab: the shell fills infield\ notes/, where the backslash keeps the space inside the name, just as quotes do. Tab only completes names that exist, so it also prevents typos. - Up arrow brings back your previous command. Read it before pressing Enter.
- Ctrl+C stops a running command or clears a half-typed one. It does not copy. To copy and paste in the terminal use Ctrl+Shift+C / Ctrl+Shift+V in Ubuntu and Command+C / Command+V on a Mac.
Two terminal windows
Open a second terminal window and use pwd to find out where it starts. Then, in the first window, cd into readings and run pwd in both windows. The first ends in /day03/readings; the second is still in your home folder. Each shell has its own current folder, and moving one does not move the other.
Return the first window to the workspace with cd ~/introcomputing-shell, and reach day03 again with cd practice/day03. The first route works from anywhere, because it starts at your home folder. The second only works from introcomputing-shell: try it from inside readings and you get an error.
A route between neighbouring folders
Inside your workspace, make a small project tree:
route-challenge/
├── inputs/
└── results/
└── notes/
Create it with mkdir -p route-challenge/inputs route-challenge/results/notes and check it with ls in each folder.
Use cd to find a route from notes to its neighbour inputs, and check where you end up with pwd. The route is cd ../../inputs: up once to results, up again to route-challenge, then into inputs.
In the file manager, save a plain-text file navigation-transfer.txt inside route-challenge. Write down three routes, each with the folder it starts from: route-challenge to notes, notes to inputs, and inputs back to introcomputing-shell. Use .. in at least one and ~ in at least one. Then open a new terminal window and follow each route from its starting folder. If a route fails, the starting folder in your note is usually wrong. Correct the note.
- From
route-challenge:cd results/notes - From
notes:cd ../../inputs - From
inputs:cd ..thencd .., orcd ~/introcomputing-shellfrom anywhere
Check your understanding
cd? What stays unchanged?
Only the terminal’s current folder changes. No file or folder moves, and other windows stay where they were. pwd shows the new location.
ls work without printing any names?
The folder is empty. An error would print a message.
pwd, ls, mkdir readings, cd readings creates something?
Only mkdir readings. pwd and ls look, and cd moves you.
Save today: in introcomputing-shell, a plain-text note navigation-notes.txt with the full path printed by pwd, the command that brings you back from home, and three commands with what they do. You can write it in the file manager’s text editor.