🗂️ File System Management

Master the essential commands for managing files and directories

💻 Practice While You Learn!

Click the green 💻 Terminal button in the bottom-right corner to open an interactive terminal. You can practice all the commands on this page in real-time!

  • The terminal stays open as you navigate between pages
  • Try commands immediately after reading about them
  • Use ↑/↓ arrows for command history, Tab for auto-complete

Working with Files and Directories

The file system is the foundation of Linux. Understanding how to create, move, copy, and delete files is essential for any Linux user.

5. Creating Directories (mkdir📁 mkdir - Make DirectoryCreates new directories for organizing your files. Essential for building project structures and keeping your system organized.Basic Usage$ mkdir my_folder▸ Creates a new directory named 'my_folder'Create Parent Directories$ mkdir -p path/to/nested/dir▸ Creates all parent directories automaticallyMultiple Directories$ mkdir docs tests src▸ Creates three directories at once)

The mkdir command creates directories to organize your files.

Command: mkdir

Try it in the interactive terminal:

mkdir my_folder

Expected Output:

Creates a new directory named 'my_folder'

Advanced: Create Nested Directories

Use the -p flag to create parent directories automatically:

mkdir -p projects/2024/my_app
ls -R projects

Expected Output:

projects/
projects/2024:
my_app/

projects/2024/my_app:

6. Creating Empty Files (touch📄 touch - Create Empty File or Update TimestampCreates a new empty file or updates the timestamp of an existing file. Quick way to create placeholder files.Create New File$ touch newfile.txt▸ Creates an empty fileUpdate Timestamp$ touch existingfile.txt▸ Updates last modified timeMultiple Files$ touch file1.txt file2.txt file3.txt▸ Creates three empty files)

The touch command creates empty files or updates file timestamps.

Command: touch

touch newfile.txt
ls

Expected Output:

newfile.txt

Use Cases

Creating Multiple Files

touch file1.txt file2.txt file3.txt
ls

Expected Output:

file1.txt  file2.txt  file3.txt

7. Removing Files and Directories (rm🗑️ rm - Remove Files or DirectoriesDeletes files or directories permanently. Be careful - there's no undo!Remove File$ rm file.txt▸ Deletes the file permanentlyRemove Directory$ rm -r folder/▸ Recursively deletes directory and contentsForce Remove$ rm -rf temp/▸ Force remove without prompts (use carefully!))

The rm command deletes files and directories. Be careful - there's no undo!

Removing a File

touch oldfile.txt
ls
rm oldfile.txt
ls

Expected Output:

(oldfile.txt is no longer listed)

Removing Directories Recursively (rm -r)

mkdir test_dir
touch test_dir/file1.txt
rm -r test_dir
ls

Expected Output:

test_dir is completely removed

Force Removal (rm -f)

Use -f to force removal without prompts:

touch unwanted.txt
rm -f unwanted.txt
ls

Expected Output:

(unwanted.txt removed without confirmation)

8. Safely Removing Empty Directories (rmdir📁 rmdir - Remove Empty DirectorySafely removes empty directories only. Won't delete if directory contains files (safer than rm -r).Remove Empty Directory$ rmdir empty_folder▸ Removes the directory if emptySafety Feature$ rmdir folder_with_files▸ Error: directory not empty (safe!))

The rmdir command only removes empty directories - a safer alternative to rm -r.

Command: rmdir

mkdir empty_folder
rmdir empty_folder

Expected Output:

Directory removed safely

Safety Feature

mkdir folder
touch folder/file.txt
rmdir folder

Expected Output:

rmdir: failed to remove 'folder': Directory not empty

This error prevents accidental data loss!

9. Moving and Renaming (mv↔️ mv - Move or Rename FilesMoves files to a new location or renames them. Can also move directories.Rename File$ mv oldname.txt newname.txt▸ Renames the fileMove File$ mv file.txt /path/to/destination/▸ Moves file to new locationMove with Wildcards$ mv *.txt documents/▸ Moves all .txt files to documents folder)

The mv command moves files to a new location or renames them.

Renaming Files

touch oldname.txt
mv oldname.txt newname.txt
ls

Expected Output:

newname.txt

Moving Files

touch newname.txt
mkdir documents
mv newname.txt documents/
ls documents/

Expected Output:

newname.txt

Using Wildcards

touch report1.txt report2.txt code.js
mkdir backups
mv *.txt backups/
ls backups/

Expected Output:

report1.txt  report2.txt

10. Copying Files and Directories (cp📋 cp - Copy Files or DirectoriesCreates a copy of files or directories, leaving the original unchanged.Copy File$ cp source.txt destination.txt▸ Creates a copyCopy Directory$ cp -r folder/ backup_folder/▸ Recursively copies entire directoryCopy to Different Location$ cp file.txt /path/to/backup/▸ Copies file to backup directory)

The cp command creates copies of files or directories.

Copying Files

touch source.txt
cp source.txt backup.txt
ls

Expected Output:

source.txt  backup.txt

Copying Directories (cp -r)

Use the -r flag to copy entire directories:

mkdir original
touch original/file.txt
cp -r original/ copy_of_original/
ls

Expected Output:

original/  copy_of_original/

Understanding File Paths

📍 Absolute vs Relative Paths

Absolute Paths: Start from root (/)

/home/user/documents/file.txt

Relative Paths: Start from current directory

documents/file.txt         # In current directory
../other_folder/file.txt   # Go up one level, then down

Special Path Symbols:

  • . - Current directory
  • .. - Parent directory (one level up)
  • ~ - Home directory
  • / - Root directory (top of filesystem)

File System Concepts

Hidden Files: Files starting with . are hidden

ls           # Won't show hidden files
ls -a        # Shows ALL files, including hidden

File Extensions: Optional in Linux (not like Windows)

Linux uses file permissions and content to determine file types, not extensions.

Case Sensitivity: Linux is case-sensitive!

File.txt ≠ file.txt ≠ FILE.TXT

Wildcards: Match multiple files

  • * - Matches any characters
  • ? - Matches single character
  • [abc] - Matches one of a, b, or c

Practice Exercises

🎯 Try These Challenges!

Exercise 1: Build a Project Structure

mkdir -p my_project/src my_project/docs my_project/tests
ls -R my_project

Goal: Create organized project directories

Exercise 2: Organize Files

touch report1.txt report2.txt code.js notes.md
mkdir documents code
mv *.txt documents/
mv *.js code/
ls

Goal: Use wildcards to organize files by type

Exercise 3: Clean Up

mkdir temp_work
touch temp_work/test1.txt temp_work/test2.txt
rm -r temp_work
ls

Goal: Practice safe deletion with rm -r

Exercise 4: Copy Directory Structure

mkdir original
touch original/file.txt
cp -r original/ backup/
ls -R

Goal: Understand recursive copying

⚠️ Safety with rm

Best Practices:

  • Always use ls before rm to verify files
  • Use rmdir for empty directories when possible (safer)
  • Double-check paths - there's no "undo"!
  • Avoid rm -rf / (extremely dangerous)
  • Test with non-important files first

⚠️ Overwrite Warning

If you move a file to a location where a file with the same name already exists, mv will overwrite it without asking!

What's Next?

Now that you can manage files and directories, you're ready to learn about:

  • File Permissions: Control who can read, write, and execute
  • User Management: Understand users, groups, and ownership
  • System Administration: Process management and monitoring

Continue to: User & Permission Management →

EN
AR
EN
AR