Navigating the Filesystem
Learn how files and folders are organised on Linux and macOS, and how to move around confidently with pwd, ls and cd.
Files, folders and paths
A file stores data, such as a document, a picture or a script. A directory (also called a folder) holds files and other directories. The two words mean the same thing here, and this course uses both.
A path is the written address of a file or directory. It lists the directories you pass through, separated by /. For example, /home/alex/Documents/notes.txt is the file notes.txt, inside Documents, inside alex, inside home.
The filesystem is a tree
All files on Linux sit in one tree that starts at a single directory called root. Root is written as one slash: /. Every other directory branches off from it.
/
├── home
│ └── alex
│ ├── Documents
│ └── Downloads
├── etc
├── var
└── tmp
Directories you will meet again and again:
/home: where each user keeps personal files. Yours is/home/yourname./etc: system configuration files./var: files that change often, such as logs./tmp: temporary files, cleared regularly./binand/usr/bin: where many commands are stored.
Linux has no drive letters like C:. Extra disks and USB drives are attached to a place inside this one tree.
Where am I? pwd
The shell is always "in" one directory, called the current working directory. pwd (print working directory) shows which one:
pwd
It prints something like /home/alex. Commands that use relative paths, which you will meet below, start from this directory.
What is here? ls
ls lists the contents of the current directory:
ls
ls -l
ls -a
ls -lh
ls -la /etc
lslists the names.-luses the long format: permissions, owner, size and date for each item.-ashows hidden files too. Names that start with a dot, such as.bashrc, are hidden by default.-hshows sizes in a human-readable form such as4.2Kor1.5M. It works together with-l./etcis an argument. It tellslsto list that directory instead of the current one.
You can combine single-letter options: -la means -l and -a together.
Moving around with cd
cd means change directory. It changes your current working directory.
cd Documents
cd ..
cd /var/log
cd Documentsmoves intoDocuments, inside the current directory.cd ..moves up one level, to the parent directory.cd /var/logmoves straight to that exact location.
After each cd, run pwd to check where you ended up.
Special names: . .. ~ and -
.means the current directory...means the parent directory, the one above.~means your home directory.-(used withcd) means the previous directory you were in.
cd ~
cd
cd -
cd ~ and a plain cd both take you home. cd - jumps back to the last directory you were in and prints its path.
Absolute and relative paths
There are two ways to write a path.
- An absolute path starts with
/and gives the full location from the root. It means the same thing no matter where you are. Example:/home/alex/Documents. - A relative path does not start with
/. It is read starting from your current directory. Examples:Documents/notesor../Downloads.
If you are in /home/alex, these two commands do the same thing:
cd Documents
cd /home/alex/Documents
Use absolute paths in scripts and scheduled jobs, where you cannot be sure of the current directory. Use relative paths for quick work.
Case sensitivity
Linux is case sensitive. Documents, documents and DOCUMENTS are three different names. If cd says No such file or directory, check capital letters and spelling first. Tab completion helps here.
See the tree
If it is installed, tree draws the directory structure. Install it with sudo apt install tree on Debian-based systems.
tree -L 2
-L 2 limits the display to two levels deep.
Try it
Predict the output of each step before you press Enter. Predicting is the quickest way to learn this.
cd ~
pwd
ls -la
cd /etc
pwd
cd -
pwd