T6 Linux and Bash · Lesson 2
The file system
Finding your way around a Linux machine: one tree of folders from one root, absolute and relative paths, moving with cd, reading what ls -l tells you, what the main folders hold, and the rules for names.
- picture the file system as one tree that starts at the root
- write absolute and relative paths, and use . and ..
- move around with cd, and use ~ and cd -
- read every column of ls -l, and sort a listing
- say what /home, /etc, /usr/bin, /tmp and the other main folders hold
- choose safe file names, and handle spaces and hidden files
The back cover
Every file on a computer lives somewhere, and finding it means knowing the map. On Linux the map is one tree of folders, and a few short commands are enough to walk it, look around and say exactly where anything is.
These pages show the one tree that starts at the root, and paths that are absolute or relative. They show how to move with cd, and how to read the long listing of ls. They show what the main folders hold, and the rules for naming files, spaces and hidden files included.
Before you start
The story: Corin is somewhere near here
Corin Vale went down ahead of you to fetch a crate, and sent word up by the tube: he is by the third pump, past the second left, near the big door. Warden Rao reads the message twice. Every direction in it starts from where Corin happened to be standing, and nobody knows where that was.
An hour later he sends another: he has gone back the way he came, and everything looks different.
"Every tunnel has one true name," says the warden, "and it starts at the root of the city. Ask the tube where you stand. Then you can tell anyone where you are, and they can find you."
The last lesson taught you to speak to the shell. Now you need to know where things are. Every command that touches a file needs a place, and a wrong place is the most common reason a command fails. This lesson gives you the map, and the words for moving around on it.
It builds on these ideas. Each one has a tiny example, so you can check that you remember it.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| Where am I? | pwd | Now you will learn to read its answer. |
| What is here? | ls | It tells you far more with -l. |
| Hidden names. | ls -a shows .secret | The dot rule, in the names section. |
| The odd names . and .. | ls -a shows them | They are explained at last. |
| A builtin. | cd is part of the shell | It is the command for moving. |
| Quotes. | "my notes.txt" | Names with spaces need them. |
| Tab. | It finishes a name | It writes difficult names for you. |
| The usual error. | No such file or directory | Most often, a wrong path. |
| Links in HTML. | href="../style.css" | The same .. means the same thing. |
| An address on a site. | href="/images/logo.png" | A path from the top, as here. |
By the end of this lesson you will be able to:
- picture the file system as one tree that starts at the root;
- write absolute and relative paths, and use . and ..;
- move around with cd, and use ~ and cd -;
- read every column of ls -l, and sort a listing;
- say what /home, /etc, /usr/bin, /tmp and the other main folders hold;
- choose safe file names, and handle spaces and hidden files.
How to follow this lesson
Each block with a Run button starts afresh in the same small folder, so you cannot break anything. Press Run, then Edit, and change a path to see what happens. A block forgets what the block before it did, including any cd.
In one line
Every file has a place, and this lesson teaches you to name that place and to walk to it.
One tree from one root
At a glance
A family tree starts with one ancestor and branches downward. A Linux machine keeps all its files the same way: one tree, with one starting point at the top.
Every file lives in one tree of folders that starts at the root, written as a single /. A folder inside another is its child, and the folder above is its parent. Folder and directory are two names for the same thing. Even a second disk is not a separate tree: it is attached at a folder somewhere inside the one tree.
Take a guess: Where does a second disk go, when Linux has only one tree?
Picture a postal address, written from the largest place to the smallest: India, then Maharashtra, then Pune, then MG Road, then house 14. Every house has one such address. Two houses in different towns can both be number 14 with no confusion, because the rest of the address differs.
Files are kept the same way. Every file sits in a folder, that folder sits in another folder, and so it goes up to one folder at the very top. Here is the folder that this lesson's blocks use, drawn by a command called tree. The dot at the top means the folder you are in:
In the shop folder drawn at the top of this lesson
tree
.
├── logs
│ └── monday.txt
├── notes.txt
└── pumps
├── north
│ └── gauge.txt
└── south
5 directories, 3 files
Read it from the left. logs, notes.txt and pumps sit directly in this folder. The lines show that monday.txt is inside logs, and that north and south are inside pumps. gauge.txt is one level deeper still. The last line counts what it found, and tree counts the starting folder too. Plain tree does not show the hidden .secret; tree -a would.
tree may need installing
tree is a separate small program, and many systems do not have it until you add it. On Ubuntu that is sudo apt install tree. Without it, ls -R and find show the same things in a plainer form.
Folder and directory mean the same thing. Commands and error messages say directory, and people say both, so this school uses folder in its sentences and leaves the commands as they are.
The way a computer organises its files: one tree of folders, with every file somewhere inside it.
The folder at the very top of the tree, written as a single slash, /. Every file on the machine is somewhere below it. It is not the same as the root user, who is the machine's all-powerful account.
Looking at the top
You can list the root like any other folder, by naming it:
ls /
bin dev etc home lib lib64 proc tmp usr
That is the small world of this page. A full machine has a few more. This is the root of the laptop this lesson was written on:
On a real Ubuntu machine
asha@tower:~/shop$ ls /
bin cdrom etc lib lost+found mnt proc run snap swap.img tmp var
boot dev home lib64 media opt root sbin srv sys usr
asha@tower:~/shop$
A later section says what the main ones hold. For now, notice that there is one tree, and everything hangs from /. Windows keeps a separate tree for each drive, C: and D: and so on. Linux does not. A second disk or a USB stick appears as a folder somewhere inside the one tree, often under /media or /mnt.
Words for the family
Folders are spoken of as a family. The folder that holds another is its parent, and a folder inside it is its child. So pumps is the parent of north, and north is a child of pumps. Every folder has exactly one parent, and the root is the only one that has none.
| Word | Means | Here |
|---|---|---|
| root | the top of the tree | / |
| parent | the folder one level up | pumps, for north |
| child | a folder one level down | north, for pumps |
| current folder | where you are now | what pwd prints |
| home folder | your own folder | /home/asha |
Worked example 1
Warden Rao asks where the gauge readings are kept, and how many folders pumps holds. Do not guess from memory: ask the machine.
What we have: the shop folder, and the two questions
What we need: the place of gauge.txt, and the count of folders in pumps
The plan: list pumps, then let find search the tree for the name
In the shop folder drawn at the top of this lesson
ls pumps
find . -name gauge.txt
north south
./pumps/north/gauge.txt
- ls can look inside any folder you name, without going there. find walks down the tree from the folder you give it, here the dot, and prints each match.
gauge.txt is at ./pumps/north/gauge.txt, and pumps holds 2 folders.
Check: the drawing at the top of this section shows the same two facts.
A path that fails: counting in ls -a pumps. It also lists . and .., which are not folders of your own: they belong to every folder, as the next section shows.
When you are stuck: you cannot picture where something is
Run pwd to see where you stand.
Draw it: run tree, or ls -R if there is no tree.
Search for it: find . -name something walks down from where you are.
Remember that you can name a folder without going there: ls pumps/north.
Your turn
ls logs
ls pumps/north
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
monday.txt
gauge.txt
- ls lists the folder you name.
- Each folder holds one file.
It prints monday.txt, then gauge.txt.
What is the parent of the folder pumps, in the tree above?Tap to reveal
- The dot at the top is the folder that holds pumps.
- That folder is /home/asha/shop.
/home/asha/shop, the shop folder.
A folder has a parent. Which folder is the one exception?Tap to reveal
- Every folder sits inside another, up to the top.
The root, /.
Did you know?
A tree since 1974
The one tree is as old as Unix. In The UNIX Time-Sharing System, published in July 1974, Dennis Ritchie and Ken Thompson wrote that the system keeps several directories for its own use, and that one of these is the root directory. A name that begins with a slash, they explained, starts its search at the root, just as /home does today.
Why it fits here: It shows how old this topic's tree is: a root directory, with paths that begin at a slash, was already there in 1974.
Source: Dennis M. Ritchie and Ken Thompson: The UNIX Time-Sharing System
One big tree, many disks
A second disk does not start a second tree. The mount manual explains that all files in a Unix system are arranged in one big tree rooted at /, though they can be spread over several devices. The mount command attaches the files of a device to a folder in that tree, and the hier manual names /media as the place for removable media such as USB sticks.
Why it fits here: It answers this topic's question: a second disk is attached at a folder inside the one tree, often under /media.
Sources: Linux man-pages: mount(8); Linux man-pages: hier(7)
In one line
All files live in one tree of folders, which starts at the root, written /. A folder inside another is its child, and the folder above is its parent. A folder and a directory are the same thing.
Paths: absolute and relative
At a glance
Directions can be given from a fixed point, such as the station, or from wherever you are standing now. Paths come in the same two kinds.
An absolute path starts with / and means the same place from anywhere. A relative path starts from the folder you are in, so it changes meaning when you move. In any path, . means here and .. means up one level. So docs/notes.txt and ./docs/notes.txt name the same file, and realpath prints the full absolute name of any path.
Take a guess: Are . and .. real entries in every folder, or just shortcuts that the shell understands?
A path is the name of a place in the tree: the folders you pass through, with a slash between them, and the last name at the end. /home/asha/shop/notes.txt says: start at the root, go into home, then asha, then shop, and there find notes.txt.
There are two ways to give directions, and you use both every day. Suppose a friend asks for your shop. You can give the full address, which works from anywhere: 14, MG Road, Pune. Or you can say, second left from where you are standing, which is shorter, but only works if you know where they stand. Corin's message from the tunnels had the second kind, with no starting point.
A path that starts with /. It is read from the root, so it means the same place wherever you are.
A path that does not start with /. It is read from the folder you are in, so it means a different place in a different folder.
So these two lines read the same file. The first works from anywhere, and the second works only because this block starts in the shop folder:
In the shop folder drawn at the top of this lesson
cat /home/asha/shop/pumps/north/gauge.txt
cat pumps/north/gauge.txt
pressure 12
pressure 12
The two special names
Remember the . and .. that ls -a always shows? Now they make sense. Every folder has both of them:
| Name | Means | Read it as |
|---|---|---|
. | this folder | here |
.. | the parent of this folder | up one level |
They can go anywhere in a path, and each .. climbs one level. This block goes into north, then reaches around the tree without moving again:
In the shop folder drawn at the top of this lesson
cd pumps/north
pwd
ls ..
ls ../..
cat ../../notes.txt
/home/asha/shop/pumps/north
north south
logs notes.txt pumps
pump one
pump two
valve
From north, .. is pumps, so ls .. lists pumps. Then ../.. is two levels up, the shop folder. And ../../notes.txt means up, up, then into notes.txt. You can even go up and across: from north, the folder south is ../south.
The same dots appeared in your HTML links. In href="../style.css", the .. means the parent folder, for the same reason. An address that starts with a slash, as in href="/images/logo.png", starts from the top of the site, as an absolute path starts from the root.
Asking for the full name
realpath turns any path into its absolute form, working out the dots for you:
In the shop folder drawn at the top of this lesson
cd pumps/north
realpath .
realpath ..
realpath ../../logs/monday.txt
/home/asha/shop/pumps/north
/home/asha/shop/pumps
/home/asha/shop/logs/monday.txt
| Path | Kind | Starts from |
|---|---|---|
/home/asha/shop/logs | absolute | the root |
logs | relative | the current folder |
./logs | relative | the current folder, said aloud |
../logs | relative | the parent |
~/shop/logs | absolute, once ~ is filled in | your home folder |
Which should you use? By hand, relative paths are shorter. In a script or a setting that may run from any folder, use absolute paths, so that its meaning cannot change.
Worked example 2
Corin is in /home/asha/shop/pumps/north. He must read logs/monday.txt, and he asks you for the way. Give him the absolute path and the relative path, and check them both.
What we have: where he stands, and where the file is
What we need: two paths to one file
The plan: the absolute path is the full name from the root. For the relative one, climb until the two paths share a folder, then go down
- He stands in north. Up one is pumps, and up two is shop, which holds logs. So he climbs twice, then goes down into logs.
In the shop folder drawn at the top of this lesson
cd pumps/north
cat /home/asha/shop/logs/monday.txt
cat ../../logs/monday.txt
Monday: all clear
Monday: all clear
Absolute: /home/asha/shop/logs/monday.txt. Relative: ../../logs/monday.txt.
Check: both print the same line, and realpath ../../logs/monday.txt gives the absolute one.
A path that fails: writing ../logs/monday.txt. One .. reaches only pumps, which has no logs in it, so cat says No such file or directory.
When you are stuck: a path gives No such file or directory
Run pwd: a relative path depends on it.
Check whether the path should start with a slash. /logs and logs are different places.
Walk it one step at a time: ls, then ls pumps, then ls pumps/north.
Let Tab write it: if Tab will not finish a part, that part is wrong.
Your turn
cd pumps/south
realpath ../north
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
/home/asha/shop/pumps/north
- cd moves into south.
- .. is pumps, and ../north is north, beside it.
It prints /home/asha/shop/pumps/north.
Is pumps/north absolute or relative? And /pumps/north?Tap to reveal
- An absolute path starts with a slash.
- The second one starts at the root, and there is no pumps there.
The first is relative; the second is absolute, and wrong here.
You are in /home/asha/shop/logs. Write the relative path to gauge.txt.Tap to reveal
- Up one reaches shop.
- Then go down through pumps and north.
../pumps/north/gauge.txt.
Did you know?
Two entries in every folder
The . and .. of a path are real entries in every folder. The 1974 paper on Unix says that each directory always has at least two entries: . names the directory itself, so a program can read the current directory without knowing its full path, and .. refers to its parent. The shell does not invent them; the file system holds them.
Why it fits here: It answers this topic's question: . and .. are entries that every folder really carries.
Source: Dennis M. Ritchie and Ken Thompson: The UNIX Time-Sharing System
4096 characters at most
A path cannot be endless. Linux's own source code sets PATH_MAX to 4096, described there as the number of characters in a path name, including the final null character. The path_resolution manual adds that a path that is too long gives the error ENAMETOOLONG, Filename too long. Deep folders with long names can reach that limit.
Why it fits here: It gives this topic's paths a limit: on Linux, a whole path may be at most 4096 characters.
Sources: Linux source code: include/uapi/linux/limits.h; Linux man-pages: path_resolution(7)
In one line
An absolute path starts with / and means the same place from anywhere. A relative path starts from where you are. In any path, . means here and .. means up one level, and realpath gives the full name.
Moving around with cd
At a glance
A visitor in a big building keeps track of the floor they are on. The shell keeps track of its current folder in the same way.
The command cd moves the shell's current folder, and pwd prints where it is. The line cd .. goes up one level, cd alone goes home, ~ stands for your home folder, and cd - goes back to the folder you were in before. A cd that fails leaves you exactly where you were.
Take a guess: Why can cd not be an ordinary program, like ls?
Your shell always has a current folder, which is where it stands. It is the place that relative paths start from, and the folder that ls lists when you name no other. cd, short for change directory, moves it:
In the shop folder drawn at the top of this lesson
pwd
cd pumps
pwd
cd north
pwd
/home/asha/shop
/home/asha/shop/pumps
/home/asha/shop/pumps/north
cd says nothing when it works. That is the rule from the last lesson: no news is good news. Check with pwd whenever you are unsure. You can also take several steps at once, as in cd pumps/north, because cd takes any path, absolute or relative.
Going up
cd .. moves to the parent. The space matters. Going up from the top is not an error: the root is its own parent, so you stay where you are.
cd ..
pwd
cd ..
pwd
cd ..
pwd
cd ..
pwd
/home/asha
/home
/
/
Going home
Every user has a home folder, their own place on the machine. Here it is /home/asha. On a Linux machine it is usually /home/ followed by your user name. The shell gives you three short ways to reach it, and one for going back:
| You type | It does | Read it as |
|---|---|---|
cd | go to your home folder | cd with nothing means home |
cd ~ | the same | ~ means your home folder |
cd ~/shop | go to shop, inside your home | a path from home |
cd - | go back to the last folder, and print it | the back button |
cd
pwd
cd shop
pwd
cd /tmp
cd -
pwd
/home/asha
/home/asha/shop
/home/asha/shop
/home/asha/shop
The first two lines go home and print it. Then cd moves down into shop. cd moves to /tmp, and cd - comes back, printing where it landed, which is why that path appears twice at the end.
The ~ sign is called a tilde. The shell swaps it for your home folder before the command even runs, so it works with any command. Inside quotes it stays a plain character, which is a trap you will meet again in the mistakes section:
echo ~
echo ~/shop
echo "~"
/home/asha
/home/asha/shop
~
When cd fails
If the folder is not there, cd says so and you stay where you were. That is worth remembering, because the next command then runs in a place you did not expect:
cd nowhere
pwd
bash: line 1: cd: nowhere: No such file or directory
/home/asha/shop
You do not need to move in order to look at something. It is often neater to stay put, and name the path:
In the shop folder drawn at the top of this lesson
ls pumps/north
cat logs/monday.txt
pwd
gauge.txt
Monday: all clear
/home/asha/shop
Worked example 3
Start in the shop folder. Walk to north, then straight to logs without passing through shop by hand, then back to north in one short command, and finally home.
What we have: the tree, and where you start
What we need: four moves, each checked
The plan: use a relative path to go down, climb with .. to go across, cd - to go back, and cd alone to go home
In the shop folder drawn at the top of this lesson
cd pumps/north
pwd
cd ../../logs
pwd
cd -
cd
pwd
/home/asha/shop/pumps/north
/home/asha/shop/logs
/home/asha/shop/pumps/north
/home/asha
- cd - printed north by itself, so that line needed no pwd. cd with no argument went home.
You end in /home/asha, after visiting north, logs and north again.
Check: each printed line is the place that move should have reached.
A path that fails: typing cd logs from north. There is no logs inside north, because a relative path starts from where you stand, not from where the folder is in your head.
When you are stuck: cd fails, or you end up somewhere unexpected
Run pwd and ls: where are you, and what is here?
Use Tab to finish folder names, so that spelling cannot go wrong.
Lost entirely? cd with nothing takes you home.
Went one step too far? cd - takes you back.
Your turn
cd pumps/north
cd ../..
pwd
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
/home/asha/shop
- The first line goes down two levels.
- The second climbs two levels.
It prints /home/asha/shop.
What does cd do with no argument, and what does cd - do?Tap to reveal
- With nothing, it goes to your home folder.
- With a dash, it goes back to the last folder.
cd goes home; cd - goes back.
You run cd / and then cd ... Where are you?Tap to reveal
- The root has no parent above it.
- So .. at the root is the root.
Still at /.
Did you know?
Why cd is built in
cd cannot be an ordinary program. The bash manual explains that a command other than a builtin runs in a separate execution environment, with its own copy of the current working directory, so a program that changed its folder would not move the shell. The manual adds that builtins exist for exactly such jobs, impossible or inconvenient for separate programs. So type cd answers: cd is a shell builtin.
Why it fits here: It answers this topic's question: only the shell can change its own folder, so cd must be built into it.
Source: GNU Bash Reference Manual: Command Execution Environment
cd - remembers
cd - works because the shell remembers the last folder. The bash manual says that a directory of - is converted to $OLDPWD before the change is attempted, and OLDPWD holds the folder you were in before the last cd. So cd - twice in a row brings you back to where you started, like a switch between two rooms.
Why it fits here: It explains this topic's cd -: the shell keeps the last folder in OLDPWD, and cd - swaps back to it.
In one line
cd moves your shell's current folder, and pwd says where it is. cd .. goes up, cd alone goes home, ~ stands for your home folder, and cd - goes back. A failed cd leaves you where you were.
Reading what ls -l tells you
At a glance
The register at a shop gate lists each item with its kind, owner, size and the time it came in. The long form of ls is that register for a folder.
Each line of ls -l shows an item's kind, permissions, links, owner, group, size, date and name. The option -h makes sizes readable, -S sorts by size, -t by time, and -r reverses the order. And -d lists a folder itself, -F marks each kind with a sign, and -R goes all the way down.
Take a guess: Why do some lines of ls -l show a year where others show a time?
Plain ls gives names. The option -l gives the long form, one line for each name, and every part of a line means something. Think of it as the register at the shop gate: the name of each item, its kind, who owns it, how big it is, and when it last changed.
In the shop folder drawn at the top of this lesson
ls -l
total 12
drwxrwxr-x 2 asha asha 4096 Jan 1 10:00 logs
-rw-rw-r-- 1 asha asha 24 Jan 1 10:00 notes.txt
drwxrwxr-x 4 asha asha 4096 Jan 1 10:00 pumps
Take the notes.txt line one piece at a time, from the left:
| Piece | Here | It means |
|---|---|---|
| kind | - | the first letter: - is a file, d is a folder, l is a link |
| permissions | rw-rw-r-- | who may do what; lesson 05 of this track explains it |
| links | 1 | how many names point here; rarely needed for now |
| owner | asha | the user who owns it |
| group | asha | the group that owns it |
| size | 24 | its size in bytes, one byte for each plain letter |
| date | Jan 1 10:00 | when its contents last changed |
| name | notes.txt | the name |
The size is real. notes.txt holds pump one, pump two and valve, which is 8 letters and spaces, then 8, then 5, with one line ending after each: 21 + 3 = 24 bytes. wc -c, which counts bytes, agrees:
In the shop folder drawn at the top of this lesson
wc -c notes.txt
24 notes.txt
The first line, total 12, is the disk space that the listed items take, counted in units of 1024 bytes. The 4096 next to a folder is the size of the folder's own list of names, not of what is inside it. To measure a folder with everything in it, use du, short for disk usage. The small shell does not have it, and the option -s means give the sum, and -h means use easy units:
Terminal
asha@tower:~/shop$ du -sh pumps
16K pumps
Sizes people can read
A plain byte count becomes hard to read when it grows. -h means human-readable, and it uses K, M and G, each 1024 times the one before:
seq prints numbers; > puts them in a file
seq 1 100000 > numbers.txt
ls -l numbers.txt
ls -lh numbers.txt
-rw-rw-r-- 1 asha asha 588895 Jan 1 10:00 numbers.txt
-rw-rw-r-- 1 asha asha 576K Jan 1 10:00 numbers.txt
Sorting a listing
ls sorts by name unless you say otherwise. These options change the order, and they combine with the others:
| Option | Sorts by | A way to remember |
|---|---|---|
-S | size, largest first | S for size |
-t | time, newest first | t for time |
-r | reverses whatever order it was | r for reverse |
In a folder with three files, dated February, March and April
ls -t
ls -tr
april.txt march.txt february.txt
february.txt march.txt april.txt
In a folder with an empty, a small and a big file
ls -S
ls -Sr
big.txt small.txt empty.txt
empty.txt small.txt big.txt
A favourite of working programmers is ls -ltr: the long form, oldest first, so that the newest file sits just above your next prompt.
Three more worth knowing
| Option | It does | When you need it |
|---|---|---|
-d | lists the folder itself, not what is inside it | to see a folder's own line |
-F | adds a mark: / to a folder, * to a program | to tell them apart at a glance |
-R | goes down into every folder | to see everything below |
In the shop folder drawn at the top of this lesson
ls -F
ls -ld pumps
ls -R pumps
logs/ notes.txt pumps/
drwxrwxr-x 4 asha asha 4096 Jan 1 10:00 pumps
pumps:
north south
pumps/north:
gauge.txt
pumps/south:
Worked example 4
Warden Rao hands you a folder of logs and asks three things: which file is the largest, how big it is in a form she can read, and whether anything in there is a folder.
What we have: a folder with three log files and one folder
What we need: the largest file, its size, and any folders
The plan: sort by size, read the long form with -h, and let the first letter of each line, or -F, show the folders
In a folder of logs
ls -S
ls -lhS
ls -F
old pump.log gate.log empty.log
total 12K
drwxrwxr-x 2 asha asha 4.0K Jan 1 10:00 old
-rw-rw-r-- 1 asha asha 3.9K Jan 1 10:00 pump.log
-rw-rw-r-- 1 asha asha 21 Jan 1 10:00 gate.log
-rw-rw-r-- 1 asha asha 0 Jan 1 10:00 empty.log
empty.log gate.log old/ pump.log
- The folder old comes first because its own list counts 4096 bytes, but it is not a log. A line that starts with d is a folder.
The largest file is pump.log, at 3.9K, and old is a folder.
Check: the size of a folder is its list of names, so old does not hold 4.0K of logs.
A path that fails: taking the first name of ls -S as the answer. That is old, a folder. Always check the kind letter at the start of the line.
When you are stuck: you are not sure what a line of ls -l says
Read it from the left: kind, permissions, links, owner, group, size, date, name.
The first letter tells you the kind: - for a file, d for a folder.
If sizes are long, add -h. A folder's 4096 is not its contents.
If a file seems to be missing, add -a: its name may start with a dot.
Your turn
ls -l notes.txt | wc -l
ls -ld pumps | wc -l
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
1
1
- A file named alone gives one line, with no total.
- -d gives the folder's own line, so one line again.
It prints 1 twice.
A line of ls -l starts with d. What is it?Tap to reveal
- The first letter gives the kind.
A folder.
Which command lists a folder with the newest item last, in the long form?Tap to reveal
- -l is the long form.
- -t sorts by time, newest first.
- -r reverses that.
ls -ltr.
Did you know?
A year for old files
ls shows a time for recent files and a year for older ones. The GNU manual explains that timestamps use a date and time, like Mar 30 23:45, for recent files, and a date with the year, like Mar 30 2020, for the rest. A timestamp counts as recent if it is less than six months old and not in the future.
Why it fits here: It answers this topic's question: ls -l shows the year instead of the time for anything older than six months.
Which K?
With -h, ls counts in powers of 1024. The GNU manual says that there K stands for 1024, not 1000, and M for 10,48,576 bytes. Those who prefer powers of 1000 can use the --si option instead. So one file can show slightly different sizes under the two options, and both are right.
Why it fits here: It explains the sizes of this topic's ls -h: a K there is 1024 bytes, not 1000.
In one line
The long form shows each item's kind, permissions, links, owner, group, size, date and name. The option -h makes sizes readable. Then -S sorts by size, -t by time, and -r reverses. Finally, -d lists a folder itself, -F marks kinds, and -R goes all the way down.
What the main folders hold
At a glance
Every railway station keeps its booking office, its waiting room and its platforms in places travellers expect. Linux keeps its folders in expected places in the same way.
Home folders sit in /home, settings in /etc, programs in /usr/bin, changing data such as logs in /var, and short-lived files in /tmp. The folders /dev and /proc show devices and live facts about the system as if they were files, so ordinary tools can read them. The same places hold on almost every Linux machine.
Take a guess: Why is the folder for programs called /usr, when it does not hold users?
A railway station puts the ticket office, the waiting room and the parcels office in places you can guess, even in a town you have never visited. Linux does the same. Nearly every distribution keeps the same kinds of file in the same folders, so what you learn on one machine holds on the next.
| Folder | It holds | A way to remember |
|---|---|---|
/ | everything: the root of the tree | the top |
/home | each person's own folder | your home |
/root | the home folder of the root user | root's own, not the root of the tree |
/etc | settings for this machine | the machine's rule book |
/bin, /usr/bin | programs that anyone can run | bin for binary, a built program |
/sbin | programs for running the system itself | s for system |
/usr | programs and shared files of the system | read, not written, in daily use |
/lib | shared code that programs need | a library |
/var | data that keeps changing, such as logs | var for variable |
/tmp | temporary files, which may vanish | never keep anything here |
/dev | devices, shown as files | dev for device |
/proc | live facts about programs and the kernel | proc for process |
/media, /mnt | where USB sticks and other disks appear | mount points |
/opt | add-on software | optional |
/boot | what the machine needs in order to start | starting up is booting |
You do not need all of these today. Four cover most of a beginner's work: your home for your files, /etc for settings, /usr/bin for programs, and /var/log for logs. The small shell has only some of these folders, and each block below reads only what is here.
The name of a machine, for example, is a setting, so it lives in /etc. Your programs are in /usr/bin, and the command which finds the file behind a command name:
cat /etc/hostname
which ls
echo $HOME
ls /home
tower
/usr/bin/ls
/home/asha
asha
These are the plain text files that much of Linux runs on. There is no hidden settings panel behind them. To change the machine's name, a person with the right to do so changes that one file.
Files that are not really files
Two folders hold surprises. /dev holds devices, shown as files: a disk, a keyboard, a terminal. /proc is filled in by the kernel on the spot, with live facts about what is running. Those files are not stored on any disk. Linux shows them as files so that the same small tools can read them.
That idea, of treating nearly everything as a file, is why cat and ls reach so far. You will meet /dev/null, the file that throws away whatever is written to it, in the pipes lesson.
Keep your own files at home
Your own work belongs under your home folder. Files in /tmp may be removed without warning, and the system's folders are not yours to fill. Most of them will refuse you anyway, with Permission denied, as lesson 05 explains.
Worked example 5
A new helper asks where to look for four things: the program behind the wc command, the settings of the machine, Asha's own work, and a place for a file needed for five minutes. Answer with a path for each, and check the first and third.
What we have: four needs, and the table of folders
What we need: a folder for each, with two checked by commands
The plan: match each need to a folder's job, then ask which and ls
- The program is found by which.
- Settings live in /etc.
- Asha's work is under /home/asha.
- Files for a few minutes go in /tmp.
which wc
ls /home/asha
/usr/bin/wc
shop
/usr/bin/wc, /etc, /home/asha and /tmp.
Check: which printed the program's path, and ls found the shop folder inside Asha's home.
A path that fails: saving the five-minute file in /etc because it is near the top of the tree. Only the root user may write there, so you would get Permission denied, and it is the wrong place in any case.
When you are stuck: you cannot find where something lives
For a command, ask which name or type name.
For a setting, look in /etc, often in a file or folder named after the program.
For a log, look in /var/log.
For your own lost file, try find ~ -name 'part*' from your home.
Your turn
cat /etc/hostname
ls /home/asha
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
tower
shop
- /etc/hostname holds the machine's name.
- Asha's home holds the shop folder.
It prints tower, then shop.
Which folder would you search for a program's settings, and which for its log?Tap to reveal
- Settings for the machine are in /etc.
- Changing data such as logs are under /var.
/etc for settings, and /var/log for logs.
What is the difference between / and /root?Tap to reveal
- / is the root of the tree.
- /root is one folder inside it, the home of the root user.
/ is the top of the tree; /root is the root user's home.
Did you know?
Why /usr
The name /usr comes from a disk that filled up. Rob Landley tells the story: around 1971 Unix ran on a PDP-11 with two RK05 disk packs of 1.5 megabytes each. When the system grew too big for the first, it leaked into the second, which held the users' home folders, and that is why the second disk was mounted at /usr.
Why it fits here: It answers this topic's question: /usr was once the users' disk, and programs moved there when the first disk was full.
Source: Rob Landley: Understanding the bin, sbin, usr/bin, usr/sbin split (busybox mailing list)
/tmp may vanish
Linux's own description of its folders warns about one of them. The hier manual says that /tmp contains temporary files which may be deleted with no notice, such as by a regular job or at system boot up. So /tmp suits files needed for a few minutes, and never anything worth keeping.
Why it fits here: It sharpens this topic's /tmp: files there can disappear at any time, even at the next start.
Source: Linux man-pages: hier(7)
In one line
Linux keeps the same things in the same places: /home for people, /etc for settings, /usr/bin for programs, /var for changing data such as logs, /tmp for short-lived files. The folders /dev and /proc show devices and live facts as if they were files.
Names, spaces and hidden files
At a glance
A name tag is useless if the letters are misread. File names on Linux are read exactly, letter by letter.
Capitals count, so Notes and notes are two files. A space in a name needs quotes or a backslash, and Tab writes them for you. A name that starts with a dot is hidden from plain ls, an extension such as .txt is only a habit, and -- ends the options. Prefer small letters and hyphens. Such names work everywhere without quotes.
Take a guess: How did hidden files begin: by design, or by accident?
A file name can hold nearly anything, which does not mean that it should. Names are where small slips cost you the most time, so the rules are worth learning once.
Capitals count
Linux treats capitals and small letters as different, so these are two files, side by side. On Windows and on a Mac they would usually be one:
touch notes.txt Notes.txt
ls
Notes.txt notes.txt
touch makes an empty file if none exists. Capitals sort first here, because ls is sorting by each character's code. On other systems the order can differ.
Spaces need care
The shell splits a line at spaces, so a name with a space must be protected. There are two ways: quote it, or put a backslash before the space. A backslash, \, tells the shell to take the next character as a plain one.
touch "my notes.txt"
cat "my notes.txt"
cat my\ notes.txt
ls
'my notes.txt'
Both cat lines read the same empty file. Look at what ls printed: it put the name in single quotes. Those quotes are not part of the name. ls adds them on a terminal to show you where a name with a space begins and ends.
The easy way is to let Tab do it. With a file named my notes.txt, type cat my and press Tab:
Typed: cat my, then Tab, then Enter
asha@tower:~/shop$ cat my\ notes.txt
asha@tower:~/shop$
Better still, avoid spaces. Most developers write names with small letters, and use a hyphen or an underscore between words: pump-report.txt, or pump_report.txt. Then no quotes are ever needed.
Hidden files
A name that starts with a dot is hidden from plain ls. Nothing protects it: it is only left out of the list, to keep things tidy. Programs keep their settings in such files in your home folder, .bashrc for example. -a shows everything, and -A shows everything except . and ..:
touch .secret shown.txt
ls
ls -A
ls -a
shown.txt
.secret shown.txt
. .. .secret shown.txt
A folder can be hidden too, and it works normally once you name it. Git keeps all its data in a hidden folder named .git, which you will meet in the next step.
The ending is only a habit
On Linux, the end of a name such as .txt or .html is called an extension, and it is only a habit. Nothing enforces it. A file named photo.txt can hold a picture, and a program named backup needs no ending at all. Programs look at what is inside. People use extensions because they help other people, and editors, to guess.
A dot in the middle of a name is an ordinary letter, so report.txt.bak is one name. Only a dot at the very start hides a file:
touch report.txt.bak .hidden.txt
ls
ls -a
report.txt.bak
. .. .hidden.txt report.txt.bak
What a name can hold
| In a name | What happens | Advice |
|---|---|---|
| a slash, / | not allowed: it separates folders | never |
| a space | works, and needs quotes every time | use a hyphen |
* ? $ & ; ( ) | work, and mean something else to the shell | avoid |
| a dash at the start | looks like an option | avoid |
| a dot at the start | hides the file | only when you mean it |
| capitals | work, and must then be typed exactly | stay with small letters |
| Hindi or other letters | work in names here | use them for your own files if you like |
A name that starts with a dash is the classic trap. The command takes it for an option. -- means no more options follow, so everything after it is a name:
touch -- -odd.txt
ls
ls -- -odd.txt
ls ./-odd.txt
-odd.txt
-odd.txt
./-odd.txt
The last line shows a second way out: a path that starts with ./ does not start with a dash.
Worked example 6
A helper saved a report as Pump Report (final).txt, in a folder. Warden Rao wants to read it with cat. Show two ways to name it on the command line, then say what name it should have had.
What we have: a name with capitals, spaces and brackets
What we need: two working commands, and a better name
The plan: quote the whole name, or put a backslash before each special character. Then rename in your head: small letters, hyphens, no brackets
In a folder with that file
cat "Pump Report (final).txt"
cat Pump\ Report\ \(final\).txt
All pumps tested
All pumps tested
- The quotes protect everything at once. The backslashes protect each character alone, and the brackets need them as much as the spaces do. Pressing Tab would write this second form for you.
Quotes are the easy way, and a better name is pump-report-final.txt.
Check: both lines print the report, and the better name needs no protection at all.
A path that fails: writing cat Pump Report (final).txt. The shell stops at the bracket, which means something else to it, and reports a syntax error before cat ever runs.
When you are stuck: a name will not work
Press Tab, and let bash write the name.
Put the whole name in quotes.
If the name starts with a dash, write ./ in front of it, or use -- before it.
If ls shows quotes round a name, the name has a space or an odd character in it. The quotes are not part of the name.
Your turn
touch a.txt A.txt "a b.txt"
ls | wc -l
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
3
- Capitals make A.txt a different file from a.txt.
- The quotes make a b.txt one name.
- So there are three files.
It prints 3.
Why is pump-report.txt a better name than Pump Report.txt?Tap to reveal
- It needs no quotes.
- You cannot get its capitals wrong.
It needs no quotes, and has no capitals to get wrong.
Is a file named .env hidden? And notes.old.txt?Tap to reveal
- Only a dot at the very start hides a file.
.env is hidden; notes.old.txt is not.
Did you know?
65 safe characters
Some characters are safe in a file name everywhere. POSIX defines a portable filename character set: the letters A to Z and a to z, the digits 0 to 9, and the full stop, the underscore and the hyphen, 65 in all. It also advises against a hyphen as the first character, since such names may cause problems when passed as command line arguments.
Why it fits here: It backs this topic's advice on names: small letters, digits and hyphens work everywhere, as long as a name does not begin with a hyphen.
In one line
Names are exact, so capitals count. A space needs quotes or a backslash, and Tab writes them for you. A name that starts with a dot is hidden, an extension is only a habit, and -- ends the options. Prefer small letters and hyphens.
Where it is used
You will use these ideas every day. Here is where each one does its work.
| What you learned | Where it is used | A real example |
|---|---|---|
| absolute paths | scripts and server settings | a web server told where its site lives |
| relative paths | projects that move between machines | ../src/index.js in an import |
| the home folder | your own settings | ~/.gitconfig |
| hidden names | project settings | .git, .env, .gitignore |
ls -ltr | finding what just changed | the newest log file on a server |
/etc | machine settings | a server's configuration |
/var/log | finding out what went wrong | a web server's error log |
| safe names | anything shared | file names in a web address |
Mistakes and debugging
These are the slips that people meet most often with paths. Every output below is the real one, from the code shown.
1. A slash too many
In the shop folder drawn at the top of this lesson
cd /pumps
bash: line 1: cd: /pumps: No such file or directory
A leading slash starts at the root, and the root has no pumps. The fix: cd pumps.
2. Going where you already are
In the shop folder drawn at the top of this lesson
cd pumps
cd pumps
bash: line 2: cd: pumps: No such file or directory
After the first line you are inside pumps, and pumps holds no pumps. The fix: check with pwd; a relative path starts from where you are now.
3. No space after cd
cd..
bash: line 1: cd..: command not found
With no space it is one word, and no command has that name. The fix: cd ...
4. A Windows backslash
In the shop folder drawn at the top of this lesson
cd pumps\north
bash: line 1: cd: pumpsnorth: No such file or directory
On Linux, a backslash protects the next character, and the shell then removes it. The fix: cd pumps/north, with a forward slash.
5. A folder name with a space
mkdir "pump house"
cd pump house
bash: line 2: cd: too many arguments
cd received two words. The fix: cd "pump house", or Tab.
6. A tilde inside quotes
cd "~/shop"
bash: line 1: cd: ~/shop: No such file or directory
Inside quotes, ~ is a plain character, so cd looks for a folder named ~. The fix: leave the ~ outside the quotes, as in cd ~/shop, or use "$HOME/shop".
7. Forgetting that files can be hidden
In a folder that holds only .env
ls
ls -A
.env
Plain ls printed nothing, so the folder looked empty. The fix: add -a or -A before you believe an empty listing.
The debugging corner: pwd, ls, Tab
Nearly every path problem falls to three questions. Where am I? Ask pwd. What is here? Ask ls -a. Is the name right? Press Tab and watch whether it finishes.
Quick card
The whole lesson on one card. Come back to it whenever you need a reminder.
The lesson in code
pwd # where am I?
ls -F # what is here, with kinds marked
ls -l notes.txt # the long form
ls -lhS logs # by size, in easy units
ls -A # hidden names too
ls pumps/north # look without moving
cat /home/asha/shop/notes.txt | wc -l # an absolute path
cd pumps/north # go down
realpath ../../logs # the full name of a relative path
cd .. # up one level
cd - # back again
cd # home
pwd
/home/asha/shop
logs/ notes.txt pumps/
-rw-rw-r-- 1 asha asha 24 Jan 1 10:00 notes.txt
total 4.0K
-rw-rw-r-- 1 asha asha 18 Jan 1 10:00 monday.txt
.secret logs notes.txt pumps
gauge.txt
3
/home/asha/shop/logs
/home/asha/shop/pumps/north
/home/asha
| When you see | It means | Look at |
|---|---|---|
No such file or directory | that path does not lead anywhere | pwd; a stray / at the start |
Not a directory | a file where a folder should be | the path, part by part |
too many arguments | a space split the name | quotes |
command not found after cd.. | no space | cd .. |
| an empty ls | the folder may hold hidden names | ls -a |
| quotes round a name in ls | the name has a space or an odd character | they are not part of the name |
| a size of 4096 on a folder | the size of its list of names | du -sh |
Permission denied | not yours to touch | lesson 05 of this track |
The story: One true name
You send Corin the one question that matters, and he asks the tube where he stands. Back comes a full path, from the root down. You read the map as a tree, every tunnel hanging from one trunk. From your own place you work out the way, up two levels and down three, and you walk it without one wrong turn.
You find Corin sitting on his crate, in a tunnel that has a perfectly good name.
He grins. "I was never lost," he says. Warden Rao writes his position in the book, in full. "He will be again," she says.
Reagents gathered: file system, root, absolute path, relative path, hidden file, home folder.
Sources
- GNU Bash Reference Manual (GNU Free Documentation License 1.3 or later (the manual's copying notice, read 2026-11-06); facts and ideas only)
- GNU Coreutils manual (GNU Free Documentation License 1.3 or later (the manual's copying notice, read 2026-11-06); facts and ideas only)