0
0
Linux CLIscripting~20 mins

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

Choose your learning style9 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.