Navigating the filesystem
pwd, ls, cd, absolute vs relative paths, hidden files and tab completion.
Everything you do on the command line happens somewhere: in a current working directory. In this lesson you'll learn to see where you are, look around, move anywhere in the tree, and write paths correctly.
Practice area#
Create a small directory tree to explore. It's safe to run in your home directory:
(You'll learn mkdir -p and touch properly in the next lesson.)
Where am I? pwd#
pwd (print working directory) shows the absolute path of where you are. Your prompt usually shows it too, with ~ standing for your home directory.
What's here? ls#
The most useful options:
Options combine, so ls -lah is one of the most-typed commands on Linux:
Reading one long line from left to right:
Permissions get their own lesson later. For a visual overview of a tree, install tree (sudo apt install tree):
Moving around: cd#
Tab completion is your best friend. Type
cd proand press Tab, and the shell completesprojects/. Press Tab twice to list all possibilities. It's faster and prevents typos.
Absolute vs relative paths#
Use absolute paths in scripts and config files, because a script can be run from anywhere, and relative paths interactively, because they're shorter.
. matters when running programs in the current directory. The shell doesn't look there by default, so you type ./script.sh, not script.sh.
Jumping between directories with pushd/popd#
When you're bouncing between two places, the directory stack helps:
dirs -v lists the stack.
Seeing more about where you are#
stat name shows full details: size, permissions, owner, inode and timestamps.
Common mistakes#
cdinto a file:cd notes/todo.txtgives "Not a directory".- Wrong case:
cd Projectsfails when the directory isprojects. - Spaces in names:
cd My Folderpasses two arguments. Usecd "My Folder",cd My\ Folderor Tab completion. - Forgetting where you are before running something destructive. Run
pwdfirst.
What's next#
You can look around. Next you'll create, copy, move, rename and delete files and directories, safely.
Check your understanding
Quick quiz
1.You're in
/home/ada/projects. Where doescd ../Downloadstake you?2.Which command shows hidden files, in long format, with human-readable sizes?
3.What does
cd -do?
Finished reading?
Mark this lesson complete to track your progress.