basic Linux commands exercises terminal Fedora solutions cheat sheet

Basic Linux commands exercises — get comfortable in the terminal

Basic Linux commands exercises are where the terminal stops feeling foreign. You’ve seen the theory and practiced navigating Fedora. Now it’s time to solve challenges on your own, with wildcards, paths, basic permissions and a final challenge that combines everything.

These exercises are done directly in your Fedora terminal. Open it now and keep it open throughout the article.

As always: try to solve it yourself, check the hint if you’re stuck for more than 10 minutes, and compare with the solution at the end.

Before starting every exercise

pwd    # confirm where you are

Getting lost is the most common frustration. Make pwd a reflex.


Basic Linux commands exercises Basic Level

Exercise 1 — Wildcards and patterns

Wildcards let you work with multiple files at once using pattern matching. The most common are * (any characters) and ? (any single character).

Set up the exercise:

mkdir ~/wildcard_exercise
cd ~/wildcard_exercise
touch report_2023.txt report_2024.txt report_final.txt
touch data_january.csv data_february.csv data_march.csv
touch main.c utils.c header.h
touch notes.txt backup.txt readme.md
ls

Now solve these tasks using wildcards, don’t type each filename individually:

Task A: List only the .txt files

Task B: List only the .c files

Task C: List all files that start with report

Task D: List all files that contain data

Task E: Create a folder called csv_files and move all .csv files into it

Task F: Create a folder called c_source and copy all .c and .h files into it

Task G: Delete all files that start with backup

💡 Hints:

*.txt        # any filename ending in .txt
*.c          # any filename ending in .c
report*      # any filename starting with report
*data*       # any filename containing data
ls *.txt     # list all .txt files
mv *.csv folder/    # move all .csv files

Exercise 2 — Paths and navigation challenge

The goal: navigate and create files using both absolute and relative paths, without using the graphical file manager.

Set up:

mkdir -p ~/path_exercise/ProjectA/src
mkdir -p ~/path_exercise/ProjectA/docs
mkdir -p ~/path_exercise/ProjectB/src
mkdir -p ~/path_exercise/shared
cd ~/path_exercise

Solve these tasks:

Task A: From ~/path_exercise, create a file called main.c inside ProjectA/src using a relative path

Task B: From ProjectA/src, create a file called utils.c in ProjectB/src using a relative path (hint: ../../)

Task C: From anywhere on the system, copy main.c from ProjectA/src to shared/ using absolute paths

Task D: From ProjectB/src, list the contents of ProjectA/docs using a relative path

Task E: From ~/path_exercise/ProjectA/src, go to ~/path_exercise/shared using only cd with relative paths (no absolute paths, no cd ~)

💡 Hints:

# From ~/path_exercise
touch ProjectA/src/main.c          # relative path

# From ProjectA/src, to ProjectB/src
touch ../../ProjectB/src/utils.c   # go up twice, then down

# Absolute path always starts with /
cp /home/sergio/path_exercise/ProjectA/src/main.c /home/sergio/path_exercise/shared/

# Relative path from ProjectA/src to shared
ls ../../shared/

Basic Linux commands exercises Intermediate Level

A realistic scenario: you’ve just finished a semester and your files are completely disorganised. Your task is to organise them using only terminal commands.

Set up the mess:

mkdir ~/semester_mess
cd ~/semester_mess
touch IC2_lab1.c IC2_lab2.c IC2_lab3.c IC2_notes.txt
touch FP1_exercises.py FP1_notes.txt FP1_project.py
touch ME2_homework.R ME2_data.csv ME2_notes.txt
touch random_file.txt another_file.md backup_old.zip
ls

Your goal, create this organised structure using only terminal commands:

~/organised_semester/
├── IC2/
│   ├── labs/        ← .c files
│   └── notes/       ← .txt files
├── FP1/
│   ├── code/        ← .py files
│   └── notes/       ← .txt files
├── ME2/
│   ├── homework/    ← .R files
│   ├── data/        ← .csv files
│   └── notes/       ← .txt files
└── other/           ← everything else

💡 Hints:

  • Create the structure with mkdir -p in one block
  • Use wildcards to move files: mv IC2_*.c organised_semester/IC2/labs/
  • Use find to verify everything is in the right place: find organised_semester/ -name "*.c"
  • Check nothing is left behind: ls ~/semester_mess/

Basic Linux commands exercises Final Challenge

Exercise 4 — Basic file permissions

In Linux every file has permissions that control who can read, write and execute it. This is essential knowledge for IC2, especially when your compiled programs won’t run.

First, understand what you’re seeing. Run:

ls -l ~/GCID/IC2/Labs/Lab1_intro/

Output:

-rw-r--r--. 1 sergio sergio  0 Jun 7 10:23 main.c
-rwxr-xr-x. 1 sergio sergio 8432 Jun 7 10:45 main

The permission string -rw-r--r-- breaks down like this:

- rw- r-- r--
↑  ↑   ↑   ↑
│  │   │   └── other: can read only
│  │   └────── group: can read only
│   └────────── owner: can read and write
└────────────── type: - = file, d = directory

Each group of 3 characters: r = read, w = write, x = execute, - = no permission.

The chmod command changes permissions. It uses numbers:

r = 4    w = 2    x = 1
rwx = 7 (4+2+1)
rw- = 6 (4+2+0)
r-- = 4 (4+0+0)
--- = 0 (0+0+0)
chmod 755 file    # rwxr-xr-x (owner all, group read+exec, other read+exec)
chmod 644 file    # rw-r--r-- (owner read+write, group read, other read)
chmod 600 file    # rw------- (owner read+write only, nobody else)
chmod +x file     # add execute permission for everyone
chmod -x file     # remove execute permission

Now the challenge

Set up:

mkdir ~/permissions_exercise
cd ~/permissions_exercise
touch secret.txt public.txt script.sh program.c
gcc ~/GCID/IC2/Labs/Lab1_intro/main.c -o compiled_program 2>/dev/null || touch compiled_program
ls -l

Task A: Make secret.txt readable and writable only by you — no one else can read it
(Target permissions: rw------- = 600)

Task B: Make public.txt readable by everyone but only writable by you
(Target permissions: rw-r--r-- = 644)

Task C: Make script.sh executable by you and readable by everyone
(Target permissions: rwxr--r-- = 744)

Task D: Try to run compiled_program — it probably fails. Fix the permissions so you can execute it, then run it.

Task E: Check what happens when you try to read a file with no read permissions:

chmod 000 secret.txt    # remove all permissions
cat secret.txt          # try to read it
chmod 600 secret.txt    # restore your own permissions
cat secret.txt          # now it works

Task F: Use ls -l to verify each permission change worked correctly before moving to the next task.

💡 Hints:

chmod 600 secret.txt        # rw-------
chmod 644 public.txt        # rw-r--r--
chmod 744 script.sh         # rwxr--r--
chmod +x compiled_program   # add execute
./compiled_program          # run it (must be in same directory)

Commented solutions

Solution Exercise 1

# Setup
mkdir ~/wildcard_exercise && cd ~/wildcard_exercise
touch report_2023.txt report_2024.txt report_final.txt
touch data_january.csv data_february.csv data_march.csv
touch main.c utils.c header.h
touch notes.txt backup.txt readme.md

# Task A — list .txt files
ls *.txt

# Task B — list .c files
ls *.c

# Task C — files starting with report
ls report*

# Task D — files containing data
ls *data*

# Task E — move .csv files
mkdir csv_files
mv *.csv csv_files/
ls && ls csv_files/

# Task F — copy .c and .h files
mkdir c_source
cp *.c c_source/
cp *.h c_source/
# or in one command:
cp *.c *.h c_source/
ls c_source/

# Task G — delete backup files
rm backup*
ls

Solution Exercise 2

# Setup
mkdir -p ~/path_exercise/ProjectA/src
mkdir -p ~/path_exercise/ProjectA/docs
mkdir -p ~/path_exercise/ProjectB/src
mkdir -p ~/path_exercise/shared
cd ~/path_exercise

# Task A — from ~/path_exercise, relative path
touch ProjectA/src/main.c

# Task B — from ProjectA/src, relative path to ProjectB/src
cd ProjectA/src
touch ../../ProjectB/src/utils.c
ls ../../ProjectB/src/

# Task C — absolute paths (replace 'sergio' with your username)
cp /home/sergio/path_exercise/ProjectA/src/main.c /home/sergio/path_exercise/shared/
ls /home/sergio/path_exercise/shared/

# Task D — from ProjectB/src, list ProjectA/docs
cd ~/path_exercise/ProjectB/src
ls ../../ProjectA/docs/

# Task E — from ProjectA/src to shared using only relative paths
cd ~/path_exercise/ProjectA/src
cd ../../shared
pwd    # should show ~/path_exercise/shared

Solution Exercise 3

# Setup
mkdir ~/semester_mess && cd ~/semester_mess
touch IC2_lab1.c IC2_lab2.c IC2_lab3.c IC2_notes.txt
touch FP1_exercises.py FP1_notes.txt FP1_project.py
touch ME2_homework.R ME2_data.csv ME2_notes.txt
touch random_file.txt another_file.md backup_old.zip

# Create organised structure
mkdir -p ~/organised_semester/IC2/labs
mkdir -p ~/organised_semester/IC2/notes
mkdir -p ~/organised_semester/FP1/code
mkdir -p ~/organised_semester/FP1/notes
mkdir -p ~/organised_semester/ME2/homework
mkdir -p ~/organised_semester/ME2/data
mkdir -p ~/organised_semester/ME2/notes
mkdir -p ~/organised_semester/other

# Move files
mv IC2_*.c ~/organised_semester/IC2/labs/
mv IC2_*.txt ~/organised_semester/IC2/notes/
mv FP1_*.py ~/organised_semester/FP1/code/
mv FP1_*.txt ~/organised_semester/FP1/notes/
mv ME2_*.R ~/organised_semester/ME2/homework/
mv ME2_*.csv ~/organised_semester/ME2/data/
mv ME2_*.txt ~/organised_semester/ME2/notes/
mv * ~/organised_semester/other/ 2>/dev/null; true

# Verify
find ~/organised_semester/ -name "*.c"
find ~/organised_semester/ -name "*.py"
ls ~/semester_mess/    # should be empty or nearly empty

Solution Exercise 4

# Setup
mkdir ~/permissions_exercise && cd ~/permissions_exercise
touch secret.txt public.txt script.sh compiled_program

# Task A — rw------- (600)
chmod 600 secret.txt
ls -l secret.txt
# -rw------- 1 sergio sergio 0 Jun 7 secret.txt

# Task B — rw-r--r-- (644)
chmod 644 public.txt
ls -l public.txt
# -rw-r--r-- 1 sergio sergio 0 Jun 7 public.txt

# Task C — rwxr--r-- (744)
chmod 744 script.sh
ls -l script.sh
# -rwxr--r-- 1 sergio sergio 0 Jun 7 script.sh

# Task D — add execute permission and run
chmod +x compiled_program
ls -l compiled_program
./compiled_program    # runs it (empty file will do nothing or give error)

# Task E — remove all permissions and restore
chmod 000 secret.txt
cat secret.txt    # Permission denied
chmod 600 secret.txt
cat secret.txt    # works now (empty file)

# Task F — verify all permissions
ls -l

Cheat sheet — Linux commands and permissions

# ============================================
# CHEAT SHEET — Basic Linux Commands
# Sergio Learns · sergiolearns.com
# ============================================

# NAVIGATION
pwd                    # where am I?
ls                     # list current directory
ls -la                 # detailed + hidden files
ls -lh                 # with human-readable sizes
cd folder              # enter folder
cd ..                  # go up one level
cd ~                   # go home from anywhere
cd -                   # go back to previous location

# CREATE
mkdir folder           # create directory
mkdir -p a/b/c         # create full path at once
touch file.c           # create empty file
echo "text" > file     # create file with content (overwrites)
echo "text" >> file    # append to file

# VIEW
cat file               # show file contents
less file              # scroll through file (q to quit)
head -n 20 file        # show first 20 lines
tail -n 20 file        # show last 20 lines

# COPY AND MOVE
cp file copy           # copy file
cp -r folder/ backup/  # copy directory recursively
mv file newname        # rename
mv file folder/        # move to directory

# DELETE — no undo
rm file                # delete file
rm -i file             # delete with confirmation
rm -r folder/          # delete directory
rm -ri folder/         # delete with confirmation

# WILDCARDS
*.txt                  # any .txt file
*.c                    # any .c file
report*                # files starting with report
*data*                 # files containing data
file?.txt              # file1.txt, fileA.txt (one char)

# SEARCH
find folder/ -name "*.c"      # find .c files
find . -name "main*"          # find files starting with main
find ~ -name "*.py" -type f   # find .py files (files only)

# DISK AND MEMORY
df -h                  # disk usage
du -sh folder/         # folder size
free -h                # RAM usage

# PERMISSIONS
ls -l                  # show permissions
chmod 755 file         # rwxr-xr-x
chmod 644 file         # rw-r--r--
chmod 600 file         # rw------- (private)
chmod +x file          # add execute permission
chmod -x file          # remove execute permission

# PERMISSION NUMBERS
r=4  w=2  x=1
7 = rwx    6 = rw-    5 = r-x    4 = r--    0 = ---
chmod XYZ: X=owner, Y=group, Z=other

# OUTPUT FILTERING
command | head -20     # first 20 lines
command | tail -20     # last 20 lines
command | grep word    # lines containing word
command | wc -l        # count lines

# USEFUL SHORTCUTS
Tab          → autocomplete
↑ ↓          → command history
Ctrl+C       → cancel running command
Ctrl+L       → clear screen
!!           → repeat last command

# GOLDEN RULES
# 1. pwd before rm — know where you are
# 2. ls before rm — know what you're deleting
# 3. rm -i or rm -ri for safety
# 4. No spaces in filenames — use _ or -
# 5. Linux is case sensitive
# 6. . = current directory, .. = parent directory
# 7. ~ = home directory, / = root

Similar Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *