Module 2
__________________________________
Basic File System Management
2.1 The Linux Filesystem Hierarchy Standard (FHS)
What is the FHS?
Filesystem Hierarchy Standard (FHS) defines the directory
• structure and directory contents in Linux distributions
• Provides a consistent layout across different Linux systems
• Ensures software and users can predict where to find files
Key Directory Structure in RHEL 9
/
├── bin -> usr/bin # Essential user command binaries
├── boot # Boot loader files, kernel
├── dev # Device files
├── etc # System configuration files
├── home # User home directories
├── lib -> usr/lib # Essential shared libraries
├── media # Mount point for removable media
├── mnt # Temporary mount points
├── opt # Optional application software packages
├── proc # Virtual filesystem for process information
├── root # Root user's home directory
├── run # Runtime variable data
├── sbin -> usr/sbin # System administration binaries
├── srv # Data for services provided by system
├── sys # Virtual filesystem for system information
├── tmp # Temporary files
├── usr # User utilities and applications
└── var # Variable data (logs, spools, etc.)
Critical Directories Explained
/ (Root Directory)
The top-level directory of the filesystem
Contains all other directories and files
/home
Contains personal directories for all regular users
Example: /home/john, /home/sarah
Each user has full control over their own home directory
/etc
System-wide configuration files
Examples:
/etc/passwd - User account information
/etc/group - Group information
/etc/hosts - Static hostname lookup table
Critical Directories Explained
/var
Variable data that changes during system operation
Important subdirectories:
/var/log - System and application logs
/var/spool - Queued files (print jobs, mail)
/var/tmp - Temporary files preserved between reboots
/tmp
Temporary files that may be deleted on reboot
All users can read and write files here
2.2 Paths: Absolute vs. Relative
Understanding Paths
A path specifies the location of a file or directory in the
filesystem
Two types: Absolute and Relative
Absolute Paths
Start from the root directory (/)
Always begin with a forward slash /
Completely specify the location regardless of current directory
2.2 Paths: Absolute vs. Relative
# Examples of absolute paths
/home/john/documents/[Link]
/etc/passwd
/usr/bin/ls
/var/log/messages
2.2 Paths: Absolute vs. Relative
Relative Paths
• Start from the current working directory
• Do NOT begin with a slash
• Use special symbols:
. - Current directory
.. - Parent directory
~ - Current user's home directory
2.2 Paths: Absolute vs. Relative
# Examples of relative paths
documents/[Link] # File in documents subdirectory
../shared/[Link] # File in sibling directory
./[Link] # File in current directory
~/downloads/[Link] # File in user's downloads directory
2.3 Navigating the Filesystem
Essential Navigation Commands
pwd - Print Working Directory
# Shows your current directory location
pwd
# Output: /home/john
cd - Change Directory
# Basic cd usage
cd /path/to/directory # Absolute path
cd relative/path # Relative path
cd # Go to home directory
cd ~ # Go to home directory
cd .. # Go up one level
cd - # Go to previous directory
2.3 Navigating the Filesystem
Essential Navigation Commands
ls - List Directory Contents
# Basic listing
ls
# List with details
ls -l
# List all files (including hidden)
ls -a
# List with details and all files
ls -la
# List human-readable sizes
ls -lh
# List sorted by modification time
ls -lt
# List reverse sorted by time
ls -ltr
2.3 Navigating the Filesystem
Essential Navigation Commands
Navigation Examples
# Starting in home directory: /home/john
pwd # /home/john
cd /etc # Absolute path
pwd # /etc
cd sysconfig/network # Relative path
pwd # /etc/sysconfig/network
cd # Back to home directory
pwd # /home/john
cd documents/work # Relative path
pwd # /home/john/documents/work
cd ../.. # Go up two levels
pwd # /home/john
cd - # Back to /home/john/documents/work
2.4 Managing Files and Directories
Creating Directories - mkdir
# Create single directory
mkdir directory_name
# Create multiple directories
mkdir dir1 dir2 dir3
# Create nested directories with parents
mkdir -p parent/child/grandchild
# Create directory with specific permissions
mkdir -m 755 shared_dir
2.4 Managing Files and Directories
Creating Files - touch
# Create empty file
touch [Link]
# Create multiple files
touch [Link] [Link] [Link]
# Update file timestamp (if file exists)
touch [Link]
2.4 Managing Files and Directories
Copying Files and Directories - cp
# Copy file to another location
cp [Link] [Link]
cp [Link] /path/to/destination/
# Copy multiple files to directory
cp [Link] [Link] /target/directory/
# Copy directory recursively
cp -r sourcedir/ destination/
# Preserve attributes (permissions, timestamps)
cp -p [Link] [Link]
# Interactive copy (prompt before overwrite)
cp -i [Link] [Link]
2.4 Managing Files and Directories
Moving and Renaming - mv
# Move file to different location
mv [Link] /new/location/
# Rename file
mv [Link] [Link]
# Move and rename simultaneously
mv [Link] /new/location/[Link]
# Move multiple files
mv [Link] [Link] target_directory/
# Interactive move
mv -i [Link] [Link]
2.4 Managing Files and Directories
Removing Files and Directories - rm
# Remove file
rm [Link]
# Remove multiple files
rm [Link] [Link] [Link]
# Remove directory recursively
rm -r directory_name
# Force remove without confirmation
rm -f [Link]
# Interactive removal
rm -i [Link]
# Remove empty directory
rmdir empty_directory
2.5 Using Wildcards
What are Wildcards?
• Special characters that represent patterns for matching filenames
• Also called "globbing patterns"
2.5 Using Wildcards
Common Wildcard Characters
* - Match Any Number of Characters
# Match all files
ls *
# Match all .txt files
ls *.txt
# Match files starting with "report"
ls report*
# Match files ending with "backup"
ls *backup
# Match files containing "temp"
ls *temp*
2.5 Using Wildcards
Common Wildcard Characters
? - Match Exactly One Character
# Match files with exactly 3 characters
ls ???
# Match files like [Link], [Link], but NOT [Link]
ls file?.txt
# Match files where second character is 'a'
ls ?a*
2.5 Using Wildcards
Common Wildcard Characters
[] - Match Character Set
# Match files starting with a, b, or c
ls [abc]*
# Match files starting with any digit
ls [0-9]*
# Match files starting with uppercase letters
ls [A-Z]*
# Match files starting with a-d or 0-9
ls [a-d0-9]*
2.5 Using Wildcards
Wildcard Examples in Practice
# List all configuration files
ls *.conf
# List all files with 4-character extensions
ls *.????
# Copy all text files to backup directory
cp *.txt backup/
# Remove all temporary files
rm *temp* *tmp*
# List files numbered 1-5
ls file[1-5].txt
Lab Exercise 2: Directory Structure Creation
# Create this directory structure in your home directory:
# home/
# └── username/
# ├── documents/
# │ ├── work/
# │ └── personal/
# ├── downloads/
# └── scripts/
cd ~
mkdir -p documents/work documents/personal downloads scripts
tree # If tree command is available, or use ls -R
Lab Exercise 3: File Management
# 1. Create test files
cd ~/documents/work
touch [Link] [Link] meeting_notes.doc
touch report_backup.bak temp_file.tmp
# 2. Practice copying
cp [Link] project1_copy.txt
cp *.txt ~/documents/personal/
# 3. Practice moving and renaming
mv meeting_notes.doc meeting_agenda.doc
mkdir ~/documents/archive
mv *backup* ~/documents/archive/
# 4. Practice wildcards
ls *.txt
ls *backup*
ls project?.*
Lab Exercise 4: Cleanup with Wildcards
# 1. Create various test files
cd ~/downloads
touch [Link] [Link] [Link] [Link] [Link] [Link]
touch [Link] [Link] [Link] [Link]
# 2. List files by type
ls *.txt
ls *.doc
ls *.jpg *.png
# 3. Organize files into directories
mkdir texts documents images temporary backups
mv *.txt texts/
mv *.doc documents/
mv *.jpg *.png images/
mv *.tmp temporary/
mv *.bak backups/
# 4. Verify organization
ls -R