š Scenario: You are managing files and folders on your computer. Sometimes, you need to delete folders that contain files or other folders inside them.
šÆ Goal: Learn how to safely remove directories and their contents using the rm -r command in the Linux terminal.
š What You'll Learn
Create a directory named test_folder with two files inside.
Create a variable to hold the directory name.
Use the rm -r command with the variable to remove the directory and its contents.
Display a message confirming the directory was removed.
š” Why This Matters
š Real World
Deleting folders and their contents is common when cleaning up files or uninstalling software.
š¼ Career
System administrators and developers often use <code>rm -r</code> to manage files and directories efficiently.
Progress0 / 4 steps
1
Create a directory with files
Create a directory named test_folder and inside it create two empty files named file1.txt and file2.txt using the mkdir and touch commands.
Linux CLI
Hint
Use mkdir test_folder to create the folder. Then use touch test_folder/file1.txt test_folder/file2.txt to create files inside it.
2
Set a variable for the directory name
Create a variable called dir_name and set it to the string test_folder.
Linux CLI
Hint
Use dir_name="test_folder" to store the folder name in a variable.
3
Remove the directory and its contents
Use the rm -r command with the variable dir_name to remove the directory and everything inside it.
Linux CLI
Hint
Use rm -r "$dir_name" to delete the folder and all files inside it.
4
Confirm the directory removal
Print the message "Directory test_folder removed successfully." to confirm the folder was deleted.
Linux CLI
Hint
Use echo "Directory test_folder removed successfully." to print the message.
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
Step 1: Understand the rm command
The rm command is used to remove files or directories in Linux.
Step 2: Understand the -r option
The -r option means recursive, so it deletes the folder and everything inside it.
Final Answer:
Deletes the folder named 'foldername' and all its contents recursively. -> Option B
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
Step 1: Check the position of the -r option
The correct syntax places options before the target, so rm -r data is valid.
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.
Final Answer:
rm -r data -> Option C
Quick Check:
Options come before target in rm command = D [OK]
Hint: Options go before folder name in rm command [OK]