Bird
Raised Fist0
Linux CLIscripting~20 mins

rm -r (remove directories) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
šŸŽ–ļø
Master of rm -r
Get all challenges correct to earn this badge!
Test your skills under time pressure!
šŸ’» Command Output
intermediate
2:00remaining
What is the output of this command?
You have a directory named testdir containing files and subdirectories. What happens when you run rm -r testdir?
Linux CLI
rm -r testdir
AThe command deletes <code>testdir</code> but leaves subdirectories intact.
BThe directory <code>testdir</code> and all its contents are deleted without any prompt.
CThe command fails with an error because <code>rm</code> cannot remove directories.
DOnly the files inside <code>testdir</code> are deleted, but the directory remains.
Attempts:
2 left
šŸ’” Hint
Think about what the -r option means for rm.
šŸ’» Command Output
intermediate
2:00remaining
What error does this command produce?
You run rm -r mydir but get the error: rm: cannot remove 'mydir': Permission denied. What is the cause?
Linux CLI
rm -r mydir
AThe directory <code>mydir</code> is empty, so <code>rm</code> refuses to delete it.
BThe directory <code>mydir</code> does not exist.
CThe <code>-r</code> option is invalid and causes an error.
DYou do not have permission to delete <code>mydir</code> or its contents.
Attempts:
2 left
šŸ’” Hint
Check the permissions of the directory and your user rights.
šŸ“ Syntax
advanced
2:00remaining
Which command correctly removes a directory named data and all its contents?
Choose the correct syntax to remove the directory data and everything inside it.
Arm -rf data
Brm data -r
Crm -r data
Drm --recursive data
Attempts:
2 left
šŸ’” Hint
Consider the common options used with rm for recursive and forced deletion.
šŸš€ Application
advanced
2:00remaining
How to safely remove a directory only if it is empty?
You want to remove a directory named logs but only if it is empty. Which command should you use?
Armdir logs
Brm -r logs
Crm logs
Drm -rf logs
Attempts:
2 left
šŸ’” Hint
There is a command specifically designed to remove empty directories.
šŸ”§ Debug
expert
3:00remaining
Why does this script fail to delete all directories?
Consider this bash script:
for d in */; do
  rm -r $d
  echo "Deleted $d"
done
It fails with errors when directory names contain spaces. What is the cause?
Linux CLI
for d in */; do
  rm -r $d
  echo "Deleted $d"
done
AThe loop does not iterate over directories correctly because of the trailing slash.
BThe rm command requires the -f option to force deletion.
CThe variable $d is not quoted, so directories with spaces cause errors.
DThe echo command syntax is incorrect and causes the script to fail.
Attempts:
2 left
šŸ’” Hint
Think about how shell variables with spaces should be handled.

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