Bird
Raised Fist0
Linux CLIscripting~10 mins

rm -r (remove directories) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - rm -r (remove directories)
Start: rm -r command
↓
Check if target exists
Yes↓
Is target a directory?
No→Remove file
Yes↓
Recursively remove contents
↓
Remove directory itself
↓
End
The rm -r command checks if the target exists, then if it's a directory it removes all contents recursively before deleting the directory itself.
Execution Sample
Linux CLI
rm -r myfolder
# removes 'myfolder' and all inside files and folders
This command deletes the folder named 'myfolder' and everything inside it.
Execution Table
StepActionTargetResultNotes
1Check if 'myfolder' existsmyfolderExistsProceed to next step
2Check if 'myfolder' is directorymyfolderYesStart recursive removal
3Remove file 'file1.txt' inside 'myfolder'myfolder/file1.txtDeletedFile removed
4Remove subdirectory 'subdir' inside 'myfolder'myfolder/subdirDirectory foundRecurse into subdir
5Remove file 'file2.txt' inside 'subdir'myfolder/subdir/file2.txtDeletedFile removed
6Remove directory 'subdir'myfolder/subdirDeletedSubdirectory removed
7Remove directory 'myfolder'myfolderDeletedTop directory removed
8Check if 'myfolder' existsmyfolderDoes not existRemoval complete
💡 'myfolder' and all contents removed, command ends
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 7Final
myfolder/file1.txtExistsDeletedDeletedDeletedDeleted
myfolder/subdir/file2.txtExistsExistsDeletedDeletedDeleted
myfolder/subdirExistsExistsDeletedDeletedDeleted
myfolderExistsExistsExistsDeletedDeleted
Key Moments - 2 Insights
Why does rm -r remove files inside directories before deleting the directory itself?
Because a directory must be empty before it can be deleted. The execution_table rows 3 to 6 show files and subdirectories removed first, then the directory itself is deleted at step 7.
What happens if the target is not a directory?
The command deletes the file directly without recursion. The flow in concept_flow shows the branch 'Is target a directory?--No--> Remove file'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the status of 'myfolder/subdir' after step 5?
ADeleted
BDoes not exist
CDirectory found
DFile
💡 Hint
Check the 'Result' column at step 5 in execution_table
At which step does the top directory 'myfolder' get deleted?
AStep 6
BStep 7
CStep 3
DStep 8
💡 Hint
Look for the action 'Remove directory 'myfolder'' in execution_table
If 'myfolder' contained no files or subdirectories, how would the execution_table change?
ASteps 3 to 6 would be skipped
BStep 7 would be skipped
CStep 1 would be skipped
DStep 8 would be skipped
💡 Hint
Empty directory means no contents to remove recursively, see steps 3-6 in execution_table
Concept Snapshot
rm -r command removes directories and their contents recursively.
Checks if target exists and is directory.
Deletes all files and subdirectories inside first.
Then deletes the directory itself.
Use carefully: no undo, deletes permanently.
Full Transcript
The rm -r command is used to remove directories and everything inside them. First, it checks if the target directory exists. If it does, it checks if it is a directory. If yes, it removes all files and subdirectories inside recursively. After all contents are deleted, it removes the directory itself. If the target is a file, it deletes it directly. This process ensures directories are empty before deletion. The execution table shows each step: checking existence, removing files, removing subdirectories, and finally removing the main directory. Beginners often wonder why files are removed first; this is because directories cannot be deleted unless empty. Also, if the directory is empty, the recursive steps are skipped and the directory is deleted immediately. Always be careful with rm -r because it deletes permanently without confirmation.

Practice

(1/5)
1. What does the command rm -r foldername do in Linux?
easy
A. Moves the folder named 'foldername' to the trash bin.
B. Deletes the folder named 'foldername' and all its contents recursively.
C. Renames the folder named 'foldername' to 'r'.
D. Creates a new folder named 'foldername'.

Solution

  1. Step 1: Understand the rm command

    The rm command is used to remove files or directories in Linux.
  2. Step 2: Understand the -r option

    The -r option means recursive, so it deletes the folder and everything inside it.
  3. Final Answer:

    Deletes the folder named 'foldername' and all its contents recursively. -> Option B
  4. Quick Check:

    rm -r deletes folders recursively = C [OK]
Hint: Remember: rm -r removes folders and all inside them [OK]
Common Mistakes:
  • Thinking rm -r moves files to trash
  • Confusing rm -r with renaming
  • Assuming rm -r only deletes empty folders
2. Which of the following is the correct syntax to remove a directory named data and all its contents?
easy
A. rm data -r
B. rmdir -r data
C. rm -r data
D. rm -f data

Solution

  1. Step 1: Check the position of the -r option

    The correct syntax places options before the target, so rm -r data is valid.
  2. Step 2: Validate other options

    rm data -r is incorrect syntax; rm -f data does not recurse on directories; rmdir -r data is invalid because rmdir does not support -r.
  3. Final Answer:

    rm -r data -> Option C
  4. Quick Check:

    Options come before target in rm command = D [OK]
Hint: Options go before folder name in rm command [OK]
Common Mistakes:
  • Placing options after folder name
  • Using rmdir with -r option
  • Confusing rm -r with rm -f
3. Given this directory structure:
project/
├── file1.txt
├── folderA/
│   └── file2.txt
└── folderB/
    └── file3.txt

What happens after running rm -r project/folderA?
medium
A. folderA and file2.txt are deleted, other files remain.
B. Only file2.txt is deleted, folderA remains.
C. Entire project folder is deleted.
D. Command fails with an error.

Solution

  1. Step 1: Identify target folder

    The command targets project/folderA, so only that folder and its contents are affected.
  2. Step 2: Understand recursive deletion

    Using -r deletes folderA and everything inside it, including file2.txt, but leaves other folders and files intact.
  3. Final Answer:

    folderA and file2.txt are deleted, other files remain. -> Option A
  4. Quick Check:

    rm -r deletes target folder and contents only = B [OK]
Hint: rm -r deletes only specified folder and contents, not siblings [OK]
Common Mistakes:
  • Thinking only files inside folderA are deleted but folder remains
  • Assuming entire project folder is deleted
  • Expecting command to fail without error
4. You run rm myfolder but get the error: rm: cannot remove 'myfolder': Is a directory. What is the likely cause?
medium
A. You forgot to use the -r option.
B. You do not have permission to delete the folder.
C. The folder is empty.
D. The folder name is misspelled.

Solution

  1. Step 1: Analyze the error message

    The error 'Is a directory' occurs when rm is used on a directory without the -r option.
  2. Step 2: Check the command

    The shown command lacks -r, confirming the recursive option was forgotten.
  3. Final Answer:

    You forgot to use the -r option. -> Option A
  4. Quick Check:

    rm dir without -r gives 'Is a directory' error = B [OK]
Hint: rm on directory without -r gives 'Is a directory' error [OK]
Common Mistakes:
  • Thinking it's a permission issue
  • Believing empty folders cause this error
  • Suspecting misspelled name
5. You want to safely delete a directory backup only if it exists. Which command sequence achieves this?
hard
A. rmdir backup
B. rm -r backup || echo 'No such directory'
C. rm backup -r
D. [ -d backup ] && rm -r backup

Solution

  1. Step 1: Check if directory exists

    The test [ -d backup ] returns true if 'backup' is a directory.
  2. Step 2: Conditionally remove directory

    The && operator runs rm -r backup only if the test is true, safely deleting the directory and contents.
  3. Final Answer:

    [ -d backup ] && rm -r backup -> Option D
  4. Quick Check:

    Test directory exists before rm -r = A [OK]
Hint: Use [ -d folder ] && rm -r folder to delete safely [OK]
Common Mistakes:
  • Using rm -r without checking existence
  • Using rmdir which fails if folder not empty
  • Incorrect option order in rm command
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard