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.
📋 Table of Contents
🎯 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 |
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
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}
🔗 Working with Links
Hard Links
A hard link is an additional directory entry for an existing file. Both names point to the same data on disk.
# Create hard link
ln /path/to/original /path/to/link
# Example
ln /tmp/original.txt /home/student/link.txt
Hard Link Characteristics
- Cannot span different file systems
- Cannot link to directories
- Deleting one link doesn't affect others
- All links have equal status
- Same inode number
Symbolic (Soft) Links
A symbolic link is a special file that points to another file or directory by name.
# Create symbolic link
ln -s /path/to/original /path/to/link
# Example
ln -s /usr/share/doc ~/Documents/shared-docs
# Link to directory
ln -s /var/log ~/logs
Symbolic Link Characteristics
- Can span different file systems
- Can link to directories
- Breaks if target is deleted
- Different inode number
- Shows as l type in ls -l
Procedure: Creating and Verifying Links
- Create original file:
echo "Original content" > original.txt - Create hard link:
ln original.txt hardlink.txt - Create symbolic link:
ln -s original.txt symlink.txt - Verify with ls -li:
ls -li *.txt - Test by modifying original:
echo "Modified" >> original.txt cat hardlink.txt cat symlink.txt
Use ls -i to see inode numbers. Hard links to the same file share the same inode.
📝 Practice Questions
Question 1: Which command creates parent directories as needed?
The -p option creates parent directories as needed without errors.
Question 2: What is the difference between hard and symbolic links?
Hard links point to the same inode and cannot link to directories or span file systems.
Question 3: Which wildcard matches exactly one character?
The ? wildcard matches exactly one character, while * matches zero or more characters.
Question 4: What does 'cp -r' do?
The -r option enables recursive copying, necessary for copying directories.
Question 5: Which directory contains system configuration files?
The /etc directory contains system and application configuration files.