By the end of this lesson you will be able to
- Name the purpose of /home, /etc, /var, /usr, /tmp and /bin and say which one holds your own files.
- Check free disk space with `df -h` and the size of a folder with `du -sh`.
- Read an environment variable with `echo` and explain how PATH helps the shell find commands.
- Write a short shell script in `~/practice`, make it executable and run it.
- Explain why you should read a script before running it and why you never pipe a download into a shell.
So far you have worked inside your own home folder. But a Linux computer has thousands of other files, and they all have a tidy place to live. Knowing the map tells you which folders are yours to use, which hold settings and which you should leave alone. This lesson also shows how the shell remembers small facts such as your user name, and how you can save a few commands in a file so they run again with one line. Everything you type here stays inside your practice folder.
One tree, starting at the root
On Windows you may know separate drives such as C: and D:. Linux has just one tree. Everything hangs from a single starting point written / and called the root of the File system (not to be confused with the Root user, who just happens to share the name). Other disks and USB sticks are attached to a folder somewhere in that tree. This agreed layout is called the filesystem hierarchy, and nearly every Linux system follows the same plan, so what you learn on Ubuntu helps on other versions too.
Think of a large library. The front desk, the staff rooms, the stockroom and the reading tables all have fixed places. A visitor can use the reading tables freely, but should not wander through the staff rooms moving things. The same idea applies here.
- /The root of the whole tree
- homeYour files: /home/amina
- aminaYour home folder (~)
- etcSystem settings as text files
- varChanging data, e.g. logs in /var/log
- usrInstalled programs and their files
- tmpTemporary files, may vanish
- binEssential commands (points to /usr/bin)
Read this figure as text
A folder tree with the root slash at the top and six branches: home, etc, var, usr, tmp and bin, each with a short note.
A tree with / at the top. Six folders sit directly below it. /home holds one folder per user, such as /home/amina, and this is where your own documents live. /etc holds system settings stored as text files. /var holds data that changes while the computer runs, such as logs in /var/log. /usr holds installed programs and their supporting files. /tmp holds temporary files that may be deleted at any time. /bin holds essential commands; on Ubuntu 24.04 it is a shortcut that points to /usr/bin.
Which folders are yours to use?
| Folder | What it holds | Your attitude |
|---|---|---|
/home | One folder per user. Yours is /home/amina, also written ~. | Yours. Work here. |
/etc | Settings for the system and its programs, as plain text files. | Look, do not edit. Changing them needs sudo. |
/var | Data that grows while the computer runs: logs, caches, waiting mail. | Look at logs. Do not delete things. |
/usr | Most installed programs, libraries and documentation. | Leave alone. The package manager looks after it. |
/tmp | Scratch space for temporary files. Often emptied at restart. | Fine for throw-away files. Never keep anything important here. |
/bin | Basic commands such as ls and cp. | Leave alone. |
How full is the disk?
A full disk causes odd problems: programs fail to save, updates stop and the computer slows down. Two read-only commands help. df -h shows the free space of each disk (df means disk free, -h means human readable, so sizes appear as 61G instead of a long number of bytes). du -sh ~/practice shows how much space one folder uses (du means disk usage, -s gives one total and -h is again human readable). Neither command changes anything.
- Marker 1: The main disk: 28% used, 162G free
- Marker 2: Folder size: 12 kilobytes
Read this figure as text
A simulated terminal with the output of df -h listing disks, sizes and use percentages, then du -sh showing a small folder size.
A simulated bash terminal. The command df -h prints a table with the columns Filesystem, Size, Used, Avail, Use percent and Mounted on. The line to look at is the one mounted on / : size 234G, used 61G, available 162G, 28 percent used (marker 1). Other lines such as tmpfs are small memory-based areas you can ignore. Next, du -sh ~/practice prints 12K followed by the folder name, meaning the practice folder uses 12 kilobytes (marker 2).
Your real output will have different names and numbers, and may include extra lines. Look for the line whose last column is /. When Use% there climbs above about 90%, it is time to delete things you own, such as old downloads, or to move them to external storage.
Environment variables: the shell's memory
An Environment variable is a named piece of text that the shell keeps and hands to the programs it starts. It is like a name badge: the program can look at it to learn who you are or where your home is. By tradition the names are written in capitals. To use a variable's value you put a dollar sign in front of its name. The shell replaces $USER with the text before running the command.
| Variable | Meaning | Example value |
|---|---|---|
$USER | Your user name | amina |
$HOME | Your home folder (the same place as ~) | /home/amina |
$SHELL | The shell you use | /bin/bash |
$PWD | The folder you are in now | /home/amina/practice |
$PATH | Folders searched for commands (see below) | /usr/local/bin:/usr/bin:/bin |
echo prints text, so echo $HOME prints the value of the variable. You can also make your own variable with NAME=value, with no spaces around the equals sign. The next command sets one, then prints it. It lasts only in this terminal window and disappears when you close it, so it is safe to experiment.
echo $USER
echo $HOME
greeting=Hello
echo $greeting, $USERamina
/home/amina
Hello, aminaPATH: how the shell finds a command
When you type ls, you do not write where the program is. The shell looks in each folder listed in the PATH variable, from left to right, and runs the first file with that name. The folders are separated by colons. If no folder has it, you get command not found.
/usr/local/binSearched 1stPrograms the owner added:ColonSeparator/usr/binSearched 2ndMost installed programs:ColonSeparator/binSearched 3rdEssential commandsRead this figure as text
The text /usr/local/bin:/usr/bin:/bin broken into three folders with colons between them and a note on search order.
The PATH value /usr/local/bin:/usr/bin:/bin has three folders separated by colons. The shell searches /usr/local/bin first, then /usr/bin, then /bin, and stops at the first match. /usr/local/bin is for programs added by the computer's owner. /usr/bin holds most installed programs. /bin holds the essential commands. Real Ubuntu values are longer, but they work the same way.
The command which ls prints where the shell found the program, for example /usr/bin/ls. Your own home folder is not in PATH, which is a safety feature: a stray file in a folder you happen to be in cannot pretend to be a normal command. This is why you must write ./hello.sh, where the leading ./ means 'the one in this folder', to run a script you have just made.
Your first shell script
A Shell script is a plain text file containing commands that the shell runs from top to bottom, as if you had typed them. Scripts save time and avoid typing mistakes, because the same steps run the same way each time. The first line, #!/bin/bash, is called the shebang: it tells the system which program should read the file. Lines starting with # are comments, notes for humans that the shell ignores.
First create the practice folder if you do not have one yet, move into it and open a simple editor called nano. mkdir -p makes the folder and does not complain if it already exists. nano hello.sh opens a new empty file called hello.sh.
mkdir -p ~/practice
cd ~/practice
nano hello.shType the following lines into nano. $(date +%A) runs the date command and inserts today's weekday name, and $(pwd) inserts the folder you are in. Quote marks keep each message together as one piece.
#!/bin/bash
# hello.sh: my first safe script. It only prints text.
name="$USER"
echo "Hello, $name!"
echo "Today is $(date +%A)."
echo "You are in: $(pwd)"In nano, save with Ctrl+O then press Enter to confirm the file name, and leave with Ctrl+X. Then try to run the script. It will fail first, on purpose: a new file is not allowed to run as a program until you say so. That is the permission system from the previous lesson at work. The command chmod u+x hello.sh adds the 'execute' permission for you, the owner, only.
Run, fix and run again
Step 1: Try to run it
Type
./hello.shand press Enter.You should see: You should see
bash: ./hello.sh: Permission denied. Nothing ran, and nothing is damaged.Step 2: Allow yourself to run it
Type
chmod u+x hello.sh, thenls -l hello.sh.You should see: No message from chmod. The listing now starts with
-rwxr--r--, showing anxfor the owner.Step 3: Run it again
Type
./hello.sh.You should see: Three lines appear: a greeting with your user name, today's weekday and your current folder.
- Marker 1: A new file is not allowed to run yet
- Marker 2: chmod u+x gives the owner permission to run it
- Marker 3: Now the three lines print
Read this figure as text
A simulated terminal where ./hello.sh gives Permission denied, chmod u+x is run, and the script then prints three lines.
A simulated bash terminal in the practice folder. The command ./hello.sh prints bash: ./hello.sh: Permission denied (marker 1). Then chmod u+x hello.sh prints nothing (marker 2). Then ls -l hello.sh shows the line -rwxr--r-- 1 amina amina 150 with the date and the name hello.sh. Finally ./hello.sh prints Hello, amina! then Today is Sunday. then You are in: /home/amina/practice (marker 3).
- Step 1: You type a namels or ./hello.sh
- Step 2: Has a slash?./ means 'this exact file'
- Step 3: Slash: run that fileNeeds execute permission
- Step 4: No slash: search PATHLeft to right, first match wins
- Step 5: Not found?command not found
Read this figure as text
A flow of five steps from typing a name, through checking for a slash, to searching PATH or running the file in this folder.
Step 1: you type a name and press Enter. Step 2: the shell checks whether the name contains a slash, as in ./hello.sh. Step 3a: if it does, the shell runs that exact file, provided it has execute permission. Step 3b: if it does not, the shell searches each folder in PATH from left to right. Step 4: if it finds a match, it runs it; otherwise it prints command not found.
Common mistakes
✖ Instead of: Running
hello.shinstead of./hello.shand concluding that the script is broken.✔ Do this: The current folder is not in PATH. Write
./hello.shto run the file in this folder.✖ Instead of: Putting spaces around the equals sign, as in
name = amina.✔ Do this: Write
name=aminawith no spaces. With spaces, the shell thinksnameis a command.✖ Instead of: Storing important documents in /tmp because it is easy to reach.
✔ Do this: Keep your work in your home folder. /tmp may be emptied at restart.
✖ Instead of: Running a script from the internet because it looks short.
✔ Do this: Read every line first. Short scripts can still delete files or download more code.
If something goes wrong
bash: ./hello.sh: Permission denied.- The file is not executable yet. Run
chmod u+x hello.shin the same folder, check withls -l hello.shfor an x in the owner part, and run it again. bash: ./hello.sh: No such file or directory.- You are probably in a different folder, or the name is misspelt. Run
pwdandls, thencd ~/practiceand try again. - Nano shows the text but my script prints nothing or strange symbols.
- Check that each line was typed exactly, including the quote marks and the
$signs. Usecat hello.shto read it andbash -n hello.shto check the structure. df -hshows many lines starting with tmpfs or loop.- That is normal. Ignore the small memory-based and snap entries. Look only at the line whose last column is
/.
Key terms
- Filesystem hierarchy
- The standard layout of folders on a Linux system, all branching from one root.
- Environment variable
- A named setting stored by the system that programs can look up.
- PATH variable
- A list of folders the computer searches when you type the name of a program in the terminal.
- Shell script
- A text file of commands that the shell runs in order, so you can repeat a job with one command.
- Home directory
- Your own personal folder on Linux, where your documents and settings live; written ~ in commands.
Practise it for real
Reading is not the same as being able to do it. Complete these on your own computer, then tick only what you have actually done. These ticks are your own honest record: nothing checks them for you.
Explore the map and check disk space
On your Ubuntu laptop open Terminal. Run ls / to see the top-level folders, then ls /home and ls /etc | head (the | head shows only the first ten lines). Do not open or change anything. Then run df -h and find the line whose last column is /. Finally run du -sh ~/practice if the folder exists. Write down how much free space your main disk has.
Write, fix and run hello.sh
Follow the steps in the lesson to create ~/practice/hello.sh in nano, see the Permission denied message, run chmod u+x hello.sh and run the script. Then run echo $PATH and which ls and compare them with the lesson.
Challenge: add a line and a variable of your own
Without copying a solution, change hello.sh so that it first stores your favourite fictional city in a variable called city and prints a line such as 'Amina would like to visit Mombasa.' using both $USER and $city. Before running it, use cat hello.sh and bash -n hello.sh to check it. Do not use rm or sudo.
Knowledge check
7 questions. You need 80% to pass. Mistakes here are part of learning, and you can retry as often as you like.
Summary
- Linux has one tree starting at /. Your files live under /home; settings, logs and programs live elsewhere and are not yours to change.
- `df -h` shows free disk space and `du -sh` shows a folder's size; both only read.
- Environment variables such as $USER, $HOME and $PATH are named facts the shell keeps. PATH is the list of folders searched for commands.
- A shell script is a text file of commands. It needs the execute permission and ./ in front when it is in the current folder.
- Read every script before running it, and never pipe a download into a shell.
Checklist: can you do these?
Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).
Related lessons
References and further reading
Official sources for details that can change between versions.
- Filesystem Hierarchy Standard (Linux Foundation)
- The Linux command line for beginners (Ubuntu)
- Bash Reference Manual (GNU Project)