File and Directory Commands¶
Files and directories are the building blocks of every Linux system. Whether you're managing configuration files, application code, logs, or backups, mastering file and directory commands is an essential Linux skill.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Create files and directories
- Copy files and folders
- Move and rename files
- Delete files safely
- Understand file overwrite behavior
- Work efficiently with multiple files
- Apply file management best practices
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Understanding the Shell
- Bash Basics
- Navigating the Filesystem
Why Learn File Management?¶
Everything in Linux is stored as a file.
Examples include:
- Configuration files
- Application code
- Log files
- Shell scripts
- Databases
- Docker Compose files
- Kubernetes YAML manifests
As a Linux administrator, you'll manage thousands of files every day.
Creating Directories¶
Create a new directory.
Verify:
Output:
Create Multiple Directories¶
Create Nested Directories¶
Use the -p option.
Without -p, Linux reports an error if parent directories don't exist.
Creating Files¶
Create an empty file.
Verify:
Output:
Create Multiple Files¶
Display File Details¶
Example:
Copy Files¶
Syntax:
Example:
Verify:
Copy Files to Another Directory¶
Copy Multiple Files¶
Copy Directories¶
Use:
The -r option copies directories recursively.
Move Files¶
Syntax:
Example:
Rename Files¶
Moving and renaming use the same command.
Rename Directories¶
Delete Files¶
Use:
The file is permanently deleted.
Delete Multiple Files¶
Delete Directories¶
Empty directory:
Delete Non-Empty Directories¶
Force Delete¶
Warning
rm -rf permanently deletes files and directories without asking for confirmation.
Never use it unless you understand its impact.
Interactive Delete¶
Prompt before deleting.
Output:
Confirm Before Overwriting¶
If the destination exists:
Verbose Output¶
Display each operation.
Copy:
Move:
Delete:
Wildcards¶
Delete all text files.
Copy all YAML files.
Move all log files.
Command Summary¶
| Command | Purpose |
|---|---|
| mkdir | Create directory |
| mkdir -p | Create nested directories |
| touch | Create empty file |
| cp | Copy files |
| cp -r | Copy directories |
| mv | Move or rename |
| rm | Delete file |
| rm -r | Delete directory |
| rm -rf | Force delete |
| rmdir | Delete empty directory |
Visual Workflow¶
Real Production Example¶
Imagine you're deploying an NGINX configuration.
Create backup:
Edit configuration.
Verify.
Restart service.
If something goes wrong:
Restart NGINX.
This simple backup strategy prevents downtime.
Production Perspective¶
Linux engineers constantly perform file operations.
Examples:
Copy configuration:
Move application:
Delete old logs:
Archive data:
These commands are used daily by:
- Linux Administrators
- Cloud Engineers
- DevOps Engineers
- Platform Engineers
- SREs
Hands-on Lab¶
Task 1¶
Create a lab directory.
Task 2¶
Navigate into it.
Task 3¶
Create three files.
Task 4¶
Create directories.
Task 5¶
Copy notes.txt.
Task 6¶
Rename app.py.
Task 7¶
Move config.yaml.
Task 8¶
Delete notes.txt.
Task 9¶
Display everything.
Mini Challenge¶
Create the following structure.
Requirements:
- Create directories
- Create files
- Copy config.yaml to backup
- Rename app.py to server.py
- Delete an unused file
Best Practices¶
- Create backups before modifying important files.
- Use
cp -ito prevent accidental overwrites. - Verify your current directory before deleting files.
- Avoid using
rm -rfunless absolutely necessary. - Use descriptive file names.
Common Mistakes¶
❌ Running:
✅ Use:
This is one of the most dangerous commands in Linux.
Never execute commands you don't fully understand.
❌ Forgetting -r while copying directories.
✅ Use:
Produces an error.
Correct:
❌ Using rmdir on non-empty directories.
✅ Use:
instead.
Interview Questions¶
Beginner¶
- Which command creates a directory?
- Which command creates an empty file?
- Difference between
cpandmv? - What does
touchdo? - Which command deletes a directory?
Intermediate¶
- Difference between
rmandrmdir? - What does
mkdir -pdo? - Explain recursive copy.
- Why should you use
cp -i?
Architect Level¶
- Why should configuration files always be backed up?
- How would you safely replace production configuration files?
- What precautions should engineers take before using
rm -rf?
Summary¶
In this lesson, you learned:
- Creating files
- Creating directories
- Copying files
- Moving files
- Renaming files
- Deleting files
- Recursive operations
- Safe file management practices
These commands are fundamental to Linux administration and are used daily in production environments.
Key Takeaways¶
mkdircreates directories.touchcreates empty files.cpcopies files and directories.mvmoves and renames files.rmpermanently deletes files.rm -rfis powerful and should be used with extreme caution.- Always back up important files before modifying them.
What's Next?¶
In the next lesson, you'll learn:
catlessmoreheadtailnltac- Viewing large log files efficiently
- Real-world log analysis