File System Navigation and Management
Now that you're comfortable with basic command line operations, let's explore how to navigate and manage files in Linux. This lesson builds directly on your command line knowledge and introduces essential tools for working with the file system.
Learning Goals:
- Navigate directories efficiently using absolute and relative paths
- Create, copy, move, and delete files and directories
- View and understand directory contents
- Use wildcards for file operations
- Find files and directories quickly
Understanding the Linux File System
The Linux file system is organized in a hierarchical tree structure, starting from the root directory (/). Here are some key directories you'll frequently encounter:
/- Root directory/home- User home directories/etc- System configuration files/var- Variable data (logs, databases)/tmp- Temporary files
Navigating Directories
Basic Navigation Commands
# See current directory
pwd
# List contents
ls
# Change to home directory
cd ~
# Change to root directory
cd /
# Go up one level
cd ..
# Return to previous directory
cd -
Absolute vs Relative Paths
# Absolute path (starts from root)
cd /home/username/Documents
# Relative path (starts from current location)
cd Documents
cd ../Downloads
# Combine relative paths
cd ../../var/log
Listing Directory Contents
The ls command has many useful options for viewing files:
# List with details (permissions, size, date)
ls -l
# List all files including hidden ones
ls -a
# List with human-readable sizes
ls -lh
# Sort by modification time (newest first)
ls -lt
# Reverse sort order
ls -ltr
Creating Files and Directories
Directory Operations
# Create single directory
mkdir projects
# Create nested directories
mkdir -p projects/linux-course/lesson4
# Create multiple directories at once
mkdir dir1 dir2 dir3
# Remove empty directory
rmdir old_directory
# Remove directory with contents (careful!)
rm -r directory_name
File Creation Methods
# Create empty file
touch newfile.txt
# Create file with content using echo
echo "Hello World" > greeting.txt
# Create multiple files
touch file1.txt file2.txt file3.txt
# Using text editors (we'll cover these in detail next lesson)
nano quicknote.txt
Use mkdir -p to create parent directories automatically. This prevents errors when creating nested directory structures.
Copying, Moving, and Renaming
Copy Operations
# Copy file
cp source.txt destination.txt
# Copy with new name
cp document.txt backup/document_backup.txt
# Copy directory recursively
cp -r old_directory new_directory
# Preserve file attributes
cp -p important_file.txt backup/
Move and Rename Operations
# Move file to different directory
mv file.txt /home/username/Documents/
# Rename file
mv oldname.txt newname.txt
# Move and rename simultaneously
mv current_name.txt /new/location/new_name.txt
# Move multiple files
mv file1.txt file2.txt destination_directory/
Removing Files and Directories
# Remove file
rm unwanted_file.txt
# Remove multiple files
rm file1.txt file2.txt file3.txt
# Remove directory and contents recursively
rm -r old_directory
# Force removal without confirmation
rm -f protected_file.txt
# Remove empty directories only
rmdir empty_dir
Be extremely careful with rm -rf. This command will permanently delete files and directories without confirmation. There's no recycle bin in the Linux command line!
Using Wildcards for File Operations
Wildcards help you work with multiple files matching patterns:
# List all .txt files
ls *.txt
# List files starting with 'doc'
ls doc*
# List files with single character before .txt
ls ?.txt
# List files with specific characters
ls [abc]*.txt
# Copy all .conf files
cp *.config /backup/
# Remove all temporary files
rm *.tmp
Finding Files and Directories
The find Command
# Find file by name in current directory
find . -name "target_file.txt"
# Find files by name (case insensitive)
find /home -iname "*.jpg"
# Find directories only
find /var -type d -name "log*"
# Find files modified in last 7 days
find . -type f -mtime -7
# Find files larger than 100MB
find / -type f -size +100M
Quick Search with locate
# Update file database (run as root)
sudo updatedb
# Search for file
locate apache2.conf
# Case insensitive search
locate -i "readme"
Common Pitfalls
- Spaces in filenames: Always quote filenames with spaces or use backslashes:
mv "my file.txt" new_name.txtormv my\ file.txt new_name.txt - Case sensitivity: Linux is case-sensitive -
File.txtandfile.txtare different files - Hidden files: Remember that files starting with
.are hidden and won't show up in basiclscommands - Permissions: You might not have permission to access certain directories - use
sudowhen appropriate - Overwriting files: Copy and move operations can silently overwrite existing files
Summary
You now have the essential skills for navigating and managing files in Linux. Remember:
- Use
cd,ls, andpwdfor navigation - Create with
mkdirandtouch, remove withrmandrmdir - Copy and move files with
cpandmv - Use wildcards (
*,?,[]) for batch operations - Find files efficiently with
findandlocate
Practice these commands regularly to build muscle memory - they form the foundation for almost everything you'll do in Linux.
Quiz
Show quiz
- What command would you use to create the directory structure
projects/2024/linuxincluding any missing parent directories? - How do you list all files (including hidden ones) in the current directory with detailed information?
- What's the difference between
rmandrmdircommands? - How would you find all
.conffiles in the/etcdirectory that were modified in the last 2 days? - What's the safest way to remove a directory named
temp_dataand all its contents?
Answers:
mkdir -p projects/2024/linuxls -larmremoves files,rmdironly removes empty directories (userm -rfor directories with content)find /etc -name "*.conf" -mtime -2- First check contents with
ls -la temp_data/, then userm -r temp_data