Basic Linux commands — practice exploring Fedora, building your IC2 folder and escaping a maze of folders (and the structure I created in the wrong place)
In the week of the first IC2 hand-in I wanted to tidy up my labs. I opened the terminal, chained a few cd .. to “go back up”, typed mkdir -p IC2/Practices/P1 and started copying files. An hour later, the lecturer asked me why my shared folder was empty. I had created the whole structure inside Documentos/IC2, because I miscounted the levels on the way up. Since then I have a rule: before creating or deleting anything, pwd. That rule shows up in all three programs today.
This practice puts into action everything you saw in the basic Linux commands theory. There are three programs (terminal sessions) of medium level: exploring the Fedora file system, building and filling your IC2 folder, and a mini paths challenge with seven steps. They use data that looks like the real thing: folders named after real labs, files that get copied, renamed and deleted, and errors you’ll meet in the lab.
Table of Contents
Before you start
It helps to be clear about:
- What
pwd,lsandcddo, and the shortcuts..,~and-. - The difference between an absolute path (starts with
/) and a relative one. - That
cpandmvbehave differently depending on whether the destination is an existing folder or a new name. - That
rmhas no recycle bin.
If any of these sounds shaky, review the theory before going on.
Open the Fedora terminal and type every command by hand instead of copying and pasting. The outputs here are real, but on your computer you’ll see your own user (instead of sergio) and your date. My Fedora is in Spanish, so its default folders are Descargas, Documentos and Escritorio (Downloads, Documents and Desktop on an English system). ls will show names in columns; here they appear one per line because the output was saved to a file.
Program 1: explore the Fedora file system
Before creating anything it’s worth knowing where you are and what’s around you. This program walks through your home folder, looks at the important system folders without touching them, finds out where commands live and practises the navigation shortcuts. What makes it tricky: some folders aren’t what they seem (one is a link) and one shortcut, cd -, prints something even though you didn’t ask.
The idea
Four steps, from closest to farthest: your home (pwd, ls -a), the system folders seen from outside (ls -ld), commands as files (which) and, finally, moving around them with the shortcuts. The key is ls -ld: the -d asks ls to talk about the folder and not about what’s inside it. That way you see six system folders in six lines, instead of hundreds of files.
Before you start, think about what you expect to see. Who do you think owns /etc? And your home folder? Then compare with the output.
The code
# 1. Where do I start? In my home folder pwd # hidden files are your user settings ls -a # 2. The important system folders, without going in (-d: the folder, not its contents) ls -ld /bin /etc /home /tmp /usr /var # 3. Commands are files: which tells you where each one lives which ls cp mv # 4. Navigation shortcuts: go, jump straight back and undo the last cd cd /etc pwd cd ~ pwd cd - cd .. pwd
Output
$ pwd /home/sergio $ ls -a . .. .bash_history .bash_logout .bashrc .profile Descargas Documentos Escritorio $ ls -ld /bin /etc /home /tmp /usr /var lrwxrwxrwx 1 root root 7 Apr 22 2024 /bin -> usr/bin drwxr-xr-x 88 root root 4096 Oct 25 23:59 /etc drwxr-xr-x 7 root root 4096 Oct 25 23:58 /home drwxrwxrwt 10 root root 4096 Oct 25 23:59 /tmp drwxr-xr-x 12 root root 4096 Sep 30 14:05 /usr drwxr-xr-x 11 root root 4096 Sep 30 14:12 /var $ which ls cp mv /usr/bin/ls /usr/bin/cp /usr/bin/mv $ cd /etc $ pwd /etc $ cd ~ $ pwd /home/sergio $ cd - /etc $ cd .. $ pwd /
Let’s check it. pwd says /home/sergio: when you open the terminal you’re in your home folder. ls -a shows the hidden settings files (.bashrc, .profile…) and the three usual folders: Descargas, Documentos and Escritorio.
The /bin line starts with l and ends with -> usr/bin: it isn’t a folder, it’s a link pointing to /usr/bin. That’s why which says ls lives in /usr/bin/ls: it’s the same place with two names. Fedora works exactly the same way. The other lines start with d (folders) and belong to root, the administrator.
In the shortcuts part, cd - prints /etc without being asked: it’s its way of telling you where it went back to. And the last cd .. from /etc leaves you in /, the root, because /etc hangs directly from it.
The number after the permissions of /etc (88) is its link count: for a folder, it comes from its subfolders. On your Fedora it’ll be a different number; don’t worry about it.
This diagram sums up what each of the six folders you just listed holds:
Questions you might ask
Why ls -ld and not ls -l? Because ls -l /etc would list the hundreds of files inside /etc. With -d you get a single line with the information about the folder itself, which is what we want to compare.
Can I break something by exploring? No. ls, cd, pwd and which only look. Besides, the system folders belong to root: even if you tried to delete something in /etc, Linux would say “Permission denied”. You’ll see it in Permissions.
Why does /tmp have a t at the end of its permissions (drwxrwxrwt)? It’s a special permission that lets everyone create files in /tmp but only delete their own. It’s a preview of the permissions topic.
What’s the difference between cd ~ and cd? None: both take you to your home folder. ~ is useful inside paths, like cd ~/IC2.
What is .bash_history for? It stores the commands you type in the terminal, and it’s what you walk through with the ↑ and ↓ arrows. So if you make a mistake in a long command, you don’t need to type it again: arrow up, fix it and press Enter. And .bashrc is bash’s configuration: that’s where your terminal’s aliases and colours are kept (don’t touch it yet).
What happens if I type cd - twice? You go back and forth between the last two folders. It’s very handy when you’re working with two folders at once (for example, your lab and the hand-ins one).
Believing /bin and /usr/bin are two different places and looking for a program in the “wrong” one. The l and the -> arrow in ls -l tell you when something is a link.
Try it yourself
- List the folders
/rootand/bootwithls -ld. Who owns them? Can you get into/rootwithcd? - Type
type cd ls. What difference do you see between the two? (Hint:cdcan’t be a separate program, because it has to change the folder of bash itself.)
Program 2: copy and organise your lab files
This is the real IC2 situation: you need a tidy folder structure and files that get created, copied to another lab, renamed, filled and deleted. What makes it tricky: five different commands (mkdir, touch, cp, mv, rm) in the right order, and the detail of > versus >>, which overwrites or appends.
The idea
First the skeleton, then the content. A single mkdir -p command creates all six folders at once. Then we work inside P1_introduction with short relative paths (../P2_commands/) instead of long absolute ones, because we know exactly where we are. And we finish by deleting with rm -i, which asks first.
Before you start, draw the tree you want on paper. If every folder has its place in the drawing, the mkdir -p command writes itself.
The code
# 1. The IC2 structure in one go (-p creates whatever is missing)
mkdir -p IC2/Practices/P1_introduction IC2/Practices/P2_commands IC2/Class_notes IC2/Hand_ins
ls -l IC2
ls -R IC2
# 2. Test files in lab 1
cd IC2/Practices/P1_introduction
touch hello.c exercise1.c exercise2.c
ls -l
# 3. Copy to lab 2 and rename
cp exercise1.c ../P2_commands/
ls ../P2_commands
mv exercise2.c practice_final.c
ls
# 4. Content: > creates or overwrites, >> appends to the end
echo '#include <stdio.h>' > hello.c
echo 'int main(void) { return 0; }' >> hello.c
cat hello.c
ls -l hello.c
# 5. Delete with a safety net: rm -i asks and I answer y
rm -i practice_final.c
ls
Output
$ mkdir -p IC2/Practices/P1_introduction IC2/Practices/P2_commands IC2/Class_notes IC2/Hand_ins
$ ls -l IC2
total 12
drwxr-xr-x 2 sergio sergio 4096 Oct 25 23:59 Class_notes
drwxr-xr-x 2 sergio sergio 4096 Oct 25 23:59 Hand_ins
drwxr-xr-x 4 sergio sergio 4096 Oct 25 23:59 Practices
$ ls -R IC2
IC2:
Class_notes
Hand_ins
Practices
IC2/Class_notes:
IC2/Hand_ins:
IC2/Practices:
P1_introduction
P2_commands
IC2/Practices/P1_introduction:
IC2/Practices/P2_commands:
$ cd IC2/Practices/P1_introduction
$ touch hello.c exercise1.c exercise2.c
$ ls -l
total 0
-rw-r--r-- 1 sergio sergio 0 Oct 25 23:59 exercise1.c
-rw-r--r-- 1 sergio sergio 0 Oct 25 23:59 exercise2.c
-rw-r--r-- 1 sergio sergio 0 Oct 25 23:59 hello.c
$ cp exercise1.c ../P2_commands/
$ ls ../P2_commands
exercise1.c
$ mv exercise2.c practice_final.c
$ ls
exercise1.c
hello.c
practice_final.c
$ echo '#include <stdio.h>' > hello.c
$ echo 'int main(void) { return 0; }' >> hello.c
$ cat hello.c
#include <stdio.h>
int main(void) { return 0; }
$ ls -l hello.c
-rw-r--r-- 1 sergio sergio 48 Oct 25 23:59 hello.c
$ rm -i practice_final.c
rm: remove regular empty file 'practice_final.c'? y
$ ls
exercise1.c
hello.c
Let’s check what matters:
ls -l IC2shows three folders (dat the start).Practiceshas4links and the others2: each folder counts its own name, the.inside it and one..per subfolder.Practiceshas two subfolders, so 2 + 2 = 4.ls -R IC2walks the whole tree:Class_notesandHand_insare empty andPracticeshasP1_introductionandP2_commands.- After
touch, the three files weigh0bytes. cpleavesexercise1.cin both places andmvswapsexercise2.cforpractice_final.c: it no longer exists under the old name.hello.cends up taking48bytes. Check it:#include <stdio.h>is 18 characters plus the newline (19), andint main(void) { return 0; }is 28 plus the newline (29). 19 + 29 = 48.rm -iasks, I answeryand the lastlsconfirms onlyexercise1.candhello.cremain.
This is your folder when you finish:
Questions you might ask
Why mkdir -p with four paths and not four commands? Because mkdir accepts several paths in the same command and -p creates whatever is missing in each. The four separate commands from the theory do the same; this is the quick version.
Why does ../P2_commands/ end with a slash? It isn’t required, but it makes it clear the destination is a folder. If the folder didn’t exist, cp would give an error instead of quietly creating a file called P2_commands.
What would happen if I used > on the second line of hello.c? It would overwrite the first one: the file would be left with only int main(void) { return 0; } and would take 29 bytes. > creates or empties the file; >> appends to the end.
Why does practice_final.c say “regular empty file”? Because rm -i describes what it’s about to delete: it’s a normal (regular) file and it’s empty (0 bytes). If it had content, it would just say “regular file”.
And if exercise1.c already existed in P2_commands? cp would overwrite it without asking. In a real lab, where the P2 version might contain your changes, use cp -i: it asks before overwriting and you answer n if you’re not sure.
Does mv make a copy and then delete? Within the same disk, no: it only changes the name on the folder’s “index card”, which is why it’s instant even if the file is huge.
Working with relative paths without knowing where you are. cp exercise1.c ../P2_commands/ only works from P1_introduction. If you’re not sure, pwd first.
When you finish organising, ls -R IC2 from your home folder shows you the whole tree at a glance. It’s the best check before a hand-in.
Try it yourself
- Add a third line to
hello.cwithecho '// end' >> hello.cand predict the new size before checking it withls -l. - Copy the whole
P1_introductionfolder intoHand_ins(you’ll need-r) and check the result withls -R.
Program 3: the mini navigation challenge
The final challenge: seven operations in order, starting from your home folder. Create a three-level structure, place a file, copy it with another name, go down and come back up, list without moving, delete an empty folder and get the absolute path of a file without going into its folder. Time yourself: the goal is under three minutes.
The idea
Almost everything is done without moving, typing relative paths from ~. There’s only one trip: down to level3 and back up with cd ../.. (two jumps). For the absolute path we use realpath, which turns any relative path into an absolute one.
Before you start, count the levels. From ~/challenge/level3, how many .. do you need to get back to ~?
The code
# 1. challenge with three subfolders mkdir -p challenge/level1 challenge/level2 challenge/level3 # 2. secret.txt inside level2 touch challenge/level2/secret.txt # 3. copy to level3 with another name cp challenge/level2/secret.txt challenge/level3/secret_copy.txt # 4. go into level3 and climb two levels with a single command cd challenge/level3 cd ../.. pwd # 5. list level2 and level3 without moving ls challenge/level2 challenge/level3 # 6. delete level1, which is empty rmdir challenge/level1 ls challenge # 7. absolute path of the copy without entering its folder realpath challenge/level3/secret_copy.txt
Output
$ mkdir -p challenge/level1 challenge/level2 challenge/level3 $ touch challenge/level2/secret.txt $ cp challenge/level2/secret.txt challenge/level3/secret_copy.txt $ cd challenge/level3 $ cd ../.. $ pwd /home/sergio $ ls challenge/level2 challenge/level3 challenge/level2: secret.txt challenge/level3: secret_copy.txt $ rmdir challenge/level1 $ ls challenge level2 level3 $ realpath challenge/level3/secret_copy.txt /home/sergio/challenge/level3/secret_copy.txt
After cd ../.., pwd says /home/sergio: the first .. goes up from level3 to challenge and the second from challenge to your home folder. Two jumps, as in the diagram.
ls challenge/level2 challenge/level3 lists two folders in a single command: when you give it several, ls prints each one’s name before its contents. rmdir challenge/level1 works first time because level1 is empty, and ls challenge confirms only level2 and level3 remain. Finally, realpath translates challenge/level3/secret_copy.txt into its full path without you having to go into any folder.
Questions you might ask
Why not cd .. twice? That works too, but cd ../.. does it in a single command and the challenge asks for “one command”.
Why rmdir and not rm -r for level1? Because the task says it’s empty. If you’d got it wrong and it had something inside, rmdir would warn you; rm -r would silently delete everything.
Why doesn’t ls challenge show secret.txt? Because ls only lists one level: inside challenge there are folders, and what they contain isn’t shown until you go in or use ls -R challenge. It’s the same reason we used ls -R in program 2 to check the whole IC2 structure at once.
Does realpath check that the file exists? Yes: if the path doesn’t exist, it gives an error. It’s a good way to verify a path before using it in another command.
What if I don’t have realpath? It comes installed in Fedora. The classic alternative is echo $HOME/challenge/level3/secret_copy.txt, but that one checks nothing: it prints the path even if the file doesn’t exist.
Typing cd /challenge/level3 with a leading slash. Bash would look for challenge in the system root and say “No such file or directory”. The folder is in your home: cd challenge/level3 or cd ~/challenge/level3.
Try it yourself
- Redo the challenge without using
cdat any point: which command changes? - Add a step 8: move
secret_copy.txttolevel2and check withls -R challengethatlevel3is now empty. Then remove it withrmdir.
Common mistakes in this practice
- Creating the structure in the wrong place. One
cd ..too many and yourIC2folder ends up insideDocumentos. Fix:pwdbefore anymkdir. - Copying a folder without
-r.cpwarns with “omitting directory” and copies nothing. - Using
>instead of>>.>empties the file before writing. - Mixing up
/and././challengeis the system root;./challengeis “here”. - Typing
ic2in lowercase. Linux is case-sensitive:IC2andic2are different folders. - Deleting with
rm -rwithout looking. Firstls, thenrm -iorrmdir.
Try it in your terminal
This short session sums up the most important idea of the practice: a relative path depends on where you are, and realpath turns it into an absolute one.
mkdir -p IC2/Practices/P1 cd IC2/Practices/P1 pwd cd ../../.. pwd realpath IC2/Practices/P1
What you should see:
$ mkdir -p IC2/Practices/P1 $ cd IC2/Practices/P1 $ pwd /home/sergio/IC2/Practices/P1 $ cd ../../.. $ pwd /home/sergio $ realpath IC2/Practices/P1 /home/sergio/IC2/Practices/P1
What to watch:
mkdir -pcreates three levels at once.- Inside
P1,pwdshows the whole absolute path. cd ../../..is three jumps:P1→Practices→IC2→ your home folder.realpath IC2/Practices/P1gives the same path aspwdinsideP1: proof that the relative and the absolute paths point to the same place.
Summary
pwdbefore creating, copying or deleting: it stops you working in the wrong folder.ls -ldtalks about the folder;ls -labout what’s inside it.mkdir -pbuilds a whole structure with one command.cpduplicates,mvrenames or moves, and both depend on whether the destination is an existing folder.>overwrites and>>appends; the size inls -llets you check it.cd ../..goes up two levels andrealpathgives you the absolute path of anything.
Checklist: have you practised it?
- Have you run the three programs and checked the outputs?
- Have you tried the “Try it yourself” tasks without looking at the theory?
- Can you explain why
/binshows up with anlinls -ld? - Can you predict the size of a file after an
echo ... >>? - Do you know how many
..you need to get back home from anywhere in your IC2 structure? - Do you use
rm -iorrmdirinstead ofrm -rwhen you’re not sure?
If you answered yes to almost everything, you’re ready for the basic Linux commands exercises, where I no longer give you the commands. See you there!
Next step: exercisesBasic Linux commands — exercises with paths, wildcards, an IC2 hand-in and a folder in chaos (and the space that wiped out my .c files)Did this article help you? Here is how you can support SergioLearns →




What is an operating system — theory with your laptop on a study afternoon, a write() call and a CPU that never rests (and the three programs that weren’t running at the same time)