Lesson 2 of 14 · 11 min
Finding Your Way Around: Paths and Navigation
In Linux, files are kept in folders called directories, nested inside one another like an upside-down tree. Whenever the terminal (the text window where you type commands) is open, you are always sitting inside one of these directories, known as your working directory. Three short commands help you find your way: pwd shows where you are, ls lists what is there, and cd moves you somewhere new.
Seeing Where You Are
pwd/home/rosa
The result /home/rosa is a path, which is just a written address for a place in the tree. Because it starts with a slash, it is an absolute path: it is spelled out all the way from the root directory, the single / at the very top of the tree. This particular folder is rosa's home directory, and the shortcut ~ (the tilde character) always points to it, wherever you happen to be standing.
Listing With ls
The command ls (short for list) shows the contents of a directory. On its own it prints just the names, but options called flags make it far more useful: -l gives a long view with sizes, dates and permissions, -a also shows hidden files whose names begin with a dot, and -h prints sizes in friendly units like K and M instead of raw bytes. You can stack flags together, so ls -lah does all three at once.
ls
ls -lahnotes.txt rnaseq
total 24K
drwxr-xr-x 3 rosa rosa 4.0K Jul 13 09:14 .
drwxr-xr-x 4 root root 4.0K Jul 1 08:00 ..
-rw------- 1 rosa rosa 1.5K Jul 13 09:10 .bash_history
-rw-r--r-- 1 rosa rosa 220 Jul 1 08:00 .bashrc
-rw-r--r-- 1 rosa rosa 480 Jul 10 11:05 notes.txt
drwxr-xr-x 2 rosa rosa 4.0K Jul 12 16:30 rnaseq
The command cd (change directory) moves you from one directory to another. You can give it an absolute path that starts at the root, like /home/rosa, or a relative path that starts from where you already are, like rnaseq. A few shortcuts save typing: . means the current directory, .. means the parent one level up, / is the root, and ~ is your home.
cd rnaseq # relative path: move into rnaseq inside the current folder
pwd # prints /home/rosa/rnaseq, confirming the move
cd .. # .. is the parent directory, so you go back up to /home/rosa
cd /home/rosa/rnaseq # an absolute path (starts at /) works from anywhere
cd ~ # ~ is a shortcut for your home directory, /home/rosa
cd # cd with no path also sends you home
# "." means the current directory; you will use it later as ./script.sh to run a script here
# Tab completion: type "cd rn" and press the Tab key; the shell finishes it to "rnaseq/",
# which saves typing and prevents spelling mistakes in long file names.Try it yourself: Open a terminal and run pwd to see your home directory. Run ls -lah to reveal every file, including the hidden ones that start with a dot. Then run cd .. to move up one level, run pwd again to confirm you moved, and finally run cd with nothing after it to jump straight back home. As you type folder names, press Tab to let the shell complete them for you.