Skip to main content

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

Basic Navigation Commands

Basic directory navigation
# 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

Path examples
# 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:

Advanced ls usage
# 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

Working with directories
# 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

Creating files
# 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
tip

Use mkdir -p to create parent directories automatically. This prevents errors when creating nested directory structures.

Copying, Moving, and Renaming

Copy Operations

Copying files and directories
# 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

Moving and renaming
# 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

Removal operations
# 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
warning

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:

Wildcard examples
# 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

Finding files
# 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

Using locate for fast searches
# 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.txt or mv my\ file.txt new_name.txt
  • Case sensitivity: Linux is case-sensitive - File.txt and file.txt are different files
  • Hidden files: Remember that files starting with . are hidden and won't show up in basic ls commands
  • Permissions: You might not have permission to access certain directories - use sudo when 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, and pwd for navigation
  • Create with mkdir and touch, remove with rm and rmdir
  • Copy and move files with cp and mv
  • Use wildcards (*, ?, []) for batch operations
  • Find files efficiently with find and locate

Practice these commands regularly to build muscle memory - they form the foundation for almost everything you'll do in Linux.

Quiz

Show quiz
  1. What command would you use to create the directory structure projects/2024/linux including any missing parent directories?
  2. How do you list all files (including hidden ones) in the current directory with detailed information?
  3. What's the difference between rm and rmdir commands?
  4. How would you find all .conf files in the /etc directory that were modified in the last 2 days?
  5. What's the safest way to remove a directory named temp_data and all its contents?

Answers:

  1. mkdir -p projects/2024/linux
  2. ls -la
  3. rm removes files, rmdir only removes empty directories (use rm -r for directories with content)
  4. find /etc -name "*.conf" -mtime -2
  5. First check contents with ls -la temp_data/, then use rm -r temp_data