Chapter 2 ⏱️ 60 min read 📚 Beginner

Manage Files from the Command Line

Master essential file system operations including navigation, file creation, copying, moving, and deletion. Learn to efficiently work with files and directories using powerful command-line tools.

🎯 Introduction

File management is a core skill for Linux system administrators. Understanding how to navigate the file system, create and modify files, and organize data efficiently is essential for daily system administration tasks.

Key skills you'll learn:

  • Understanding the Linux file system hierarchy
  • Navigating directories with cd, pwd, and ls
  • Creating, copying, moving, and deleting files and directories
  • Using wildcards for efficient file operations
  • Creating and managing symbolic and hard links

🗂️ Linux File System Hierarchy

Linux follows the Filesystem Hierarchy Standard (FHS), which defines the directory structure and directory contents in Unix-like operating systems.

Important Directories

Directory Purpose
/ Root directory (top of hierarchy)
/home User home directories
/root Root user's home directory
/etc Configuration files
/var Variable data (logs, databases)
/usr User programs and utilities
/tmp Temporary files
/dev Device files
📘 Note

Absolute paths start with / (e.g., /home/student). Relative paths don't start with / and are relative to your current directory.

📝 Manage Files and Directories

Creating Files and Directories

touch - Create Empty File

# Create a new file
touch newfile.txt

# Create multiple files
touch file1.txt file2.txt file3.txt

# Update timestamp of existing file
touch existingfile.txt

mkdir - Make Directory

# Create directory
mkdir mydir

# Create multiple directories
mkdir dir1 dir2 dir3

# Create parent directories as needed
mkdir -p parent/child/grandchild

# Set permissions while creating
mkdir -m 755 securedir

Copying Files and Directories

cp - Copy

# Copy file
cp source.txt destination.txt

# Copy to directory
cp file.txt /tmp/

# Copy multiple files
cp file1.txt file2.txt /tmp/

# Copy directory recursively
cp -r sourcedir/ destdir/

# Preserve attributes
cp -p file.txt backup.txt

# Interactive (prompt before overwrite)
cp -i file.txt existing.txt

# Verbose output
cp -v file.txt /tmp/

Moving and Renaming

mv - Move/Rename

# Rename file
mv oldname.txt newname.txt

# Move file to directory
mv file.txt /tmp/

# Move multiple files
mv file1.txt file2.txt /tmp/

# Move directory
mv olddir/ newdir/

# Interactive mode
mv -i file.txt /tmp/

# No overwrite
mv -n file.txt /tmp/

Deleting Files and Directories

rm - Remove

# Delete file
rm file.txt

# Delete multiple files
rm file1.txt file2.txt

# Interactive deletion
rm -i file.txt

# Force deletion
rm -f file.txt

# Delete directory and contents
rm -r directory/

# Verbose output
rm -v file.txt
⚠️ Warning

Be extremely careful with rm -rf. There is no trash/recycle bin in Linux. Deleted files cannot be easily recovered!

rmdir - Remove Empty Directory

# Delete empty directory
rmdir emptydir/

# Delete parent directories if empty
rmdir -p parent/child/

Wildcards and Pattern Matching

Pattern Matches Example
* Any characters ls *.txt
? Single character ls file?.txt
[abc] Any char in brackets ls file[123].txt
[!abc] Not in brackets ls file[!0-9].txt
{a,b} Brace expansion cp file.{txt,bak}

Wildcard Examples

# List all .txt files
ls *.txt

# Copy all .conf files
cp /etc/*.conf ~/backup/

# Delete all files starting with 'temp'
rm temp*

# List files with single character extension
ls *.?

# Create backup copies
cp file.txt{,.bak}

📝 Practice Questions

Question 1: Which command creates parent directories as needed?

  • A) mkdir -r parent/child
  • B) mkdir -p parent/child
  • C) mkdir -a parent/child
  • D) mkdir parent/child
Answer: B) mkdir -p parent/child
The -p option creates parent directories as needed without errors.

Question 2: What is the difference between hard and symbolic links?

  • A) Hard links can span file systems
  • B) Symbolic links share the same inode
  • C) Hard links cannot link to directories
  • D) Symbolic links are faster
Answer: C) Hard links cannot link to directories
Hard links point to the same inode and cannot link to directories or span file systems.

Question 3: Which wildcard matches exactly one character?

  • A) *
  • B) ?
  • C) []
  • D) {}
Answer: B) ?
The ? wildcard matches exactly one character, while * matches zero or more characters.

Question 4: What does 'cp -r' do?

  • A) Copy in reverse order
  • B) Copy recursively (including subdirectories)
  • C) Copy and remove source
  • D) Copy with root permissions
Answer: B) Copy recursively (including subdirectories)
The -r option enables recursive copying, necessary for copying directories.

Question 5: Which directory contains system configuration files?

  • A) /var
  • B) /usr
  • C) /etc
  • D) /opt
Answer: C) /etc
The /etc directory contains system and application configuration files.