Lab 3.1 - Navigation and Creation in the File System
Objectives
By the end of this lab, you will be able to:
- Navigate the Linux file system
- Create files and directories
- Understand the FHS (Filesystem Hierarchy Standard) structure
- Use basic commands (cd, ls, mkdir, touch, etc.)
- Manipulate files and directories
Prerequisites
- Ubuntu 24.04 installed and functional
- Terminal open
- Normal user access (no need for root for most exercises)
Exercise 1: Basic Navigation
Step 1: Important Directories
# See the current directory
pwd
# Go to the home directory
cd ~
# or
cd $HOME
# View the content
ls
# View with details
ls -l
# View hidden files too
ls -la
Step 2: FHS Structure
# Go to the root
cd /
# View the structure
ls -la /
# Explore important directories
ls /bin # Essential binaries
ls /usr/bin # User binaries
ls /etc # Configuration files
ls /var # Variable data
ls /home # User directories
ls /tmp # Temporary files
Question: What are the main directories you see?
Step 3: Relative and Absolute Navigation
# Absolute path (from the root)
cd /home/$USER
# Relative path (from the current directory)
cd Documents # If Documents exists in the current directory
# Return to the previous directory
cd -
# Go up one level
cd ..
# Go up two levels
cd ../..
Exercise 2: Creating Directories
Step 1: Create Directories
# Create a directory
mkdir my-lab
# Create multiple directories
mkdir dir1 dir2 dir3
# Create a tree structure
mkdir -p project/src project/docs project/tests
# Verify
tree project
# If tree is not installed:
# sudo apt install tree
Step 2: Organized Structure
# Create a structure for a project
mkdir -p ~/projects/linux-lab/{docs,scripts,data,backup}
# View the structure
tree ~/projects/linux-lab
# Or with ls
ls -R ~/projects/linux-lab
Exercise 3: Creating Files
Step 1: Creation Methods
# Method 1: touch (empty file)
touch file1.txt
# Method 2: echo
echo "File content" > file2.txt
# Method 3: cat with heredoc
cat > file3.txt << EOF
Line 1
Line 2
Line 3
EOF
# Method 4: Editor
nano file4.txt
# Write text, Ctrl+O to save, Ctrl+X to exit
Step 2: Verify Files
# View created files
ls -lh
# View content
cat file2.txt
# Count lines
wc -l file3.txt
# View the first lines
head -2 file3.txt
# View the last lines
tail -2 file3.txt
Exercise 4: File Manipulation
Step 1: Copy and Move
# Create a test directory
mkdir test-files
cd test-files
# Create a file
echo "Test" > original.txt
# Copy
cp original.txt copy.txt
# Copy to another directory
cp original.txt ~/Documents/
# Copy a directory
cp -r ~/my-lab ~/my-lab-backup
# Move (rename)
mv original.txt renamed.txt
# Move to another directory
mv renamed.txt ~/Documents/
Step 2: Delete
# Delete a file
rm file1.txt
# Delete multiple files
rm file2.txt file3.txt
# Delete an empty directory
rmdir empty-dir
# Delete a directory with content
rm -r my-lab
# Delete with confirmation
rm -ri project/
Warning: rm -r deletes permanently! No trash bin.
Exercise 5: File Search
Step 1: find
# Find all .txt files in the current directory
find . -name "*.txt"
# Find throughout the system (can be long)
find / -name "file.txt" 2>/dev/null
# Find recently modified files
find ~ -mtime -1 # Last 24h
# Find files larger than 1MB
find ~ -size +1M
Step 2: locate (if installed)
# Install locate
sudo apt update
sudo apt install mlocate
# Update the database
sudo updatedb
# Search
locate file.txt
Exercise 6: Complete Practice
Step 1: Create a Project Structure
# Create the structure
mkdir -p ~/linux-project/{src/{scripts,config},docs,logs,backup}
# Create files
touch ~/linux-project/src/scripts/script1.sh
touch ~/linux-project/src/config/config.conf
echo "Initial log" > ~/linux-project/logs/app.log
# View the structure
tree ~/linux-project
Step 2: Organize Files
cd ~/linux-project
# Create test files
touch test1.txt test2.txt test3.txt
# Create a directory for tests
mkdir tests
# Move test files
mv test*.txt tests/
# Verify
ls -R
Exercise 7: Useful Commands
Step 1: File Information
# File type
file file.txt
file /bin/ls
# File size
du -h file.txt
# Directory size
du -sh ~/linux-project
# Disk space
df -h
Step 2: Symbolic Links
# Create a symbolic link
ln -s ~/linux-project/src/scripts/script1.sh ~/script1-link
# View the link
ls -l ~/script1-link
# Follow the link
readlink -f ~/script1-link
Verification
Checklist
Verify that you can:
- Navigate the file system (cd, pwd)
- View directory contents (ls with options)
- Create directories (mkdir, mkdir -p)
- Create files (touch, echo, cat, nano)
- Copy and move files (cp, mv)
- Delete files and directories (rm, rmdir)
- Search for files (find, locate)
- Understand the FHS structure
Reflection Questions
-
What is the difference between absolute and relative paths?
- Absolute: starts with
/(from the root) - Relative: from the current directory
- Absolute: starts with
-
Why use
mkdir -p?- Creates parent directories if necessary
- Avoids errors if the directory already exists
-
What is the difference between
rmandrmdir?rm: deletes files and directories (with -r)rmdir: deletes only empty directories
Next Steps
- Lab 3.2: Permissions Management
- Lab 3.3: Search and Links
- Chapter 3.2: Directory Structure
Ubuntu 24.04 Resources
- Ubuntu Documentation
- Filesystem Hierarchy Standard
- Commands:
man commandfor help
Lab created on: December 2024 - Tested on Ubuntu 24.04