Delete A Directory
Part of the Fundamentals section of Coddy's Terminal journey — lesson 19 of 82.
To delete a directory in the terminal, you have two options depending on whether the directory is empty or not.
Deleting an empty directory:
The rmdir command stands for remove directory. It can only delete a directory that is completely empty:
rmdir empty_folderIf the folder contains any files or subdirectories, rmdir will fail with an error.
Deleting a directory and all its contents:
To delete a directory along with everything inside it, use rm with the -r flag (recursive):
rm -r documentsThis removes the documents folder and every file and subfolder inside it.
You can also combine it with -f to force delete without any confirmation prompts:
rm -rf documentsWarning:
rm -rfis one of the most powerful and dangerous commands in the terminal. It permanently deletes everything without asking. Always double-check your path before running it — there is no undo.
Challenge
BeginnerDelete the documents directory and all its contents using rm -r, then use ls to confirm it has been removed.
Warning: This will permanently delete the directory and everything inside it. There is no way to recover deleted directories in the terminal!
Cheat sheet
To delete an empty directory, use rmdir:
rmdir empty_folderTo delete a directory and all its contents, use rm -r (recursive):
rm -r documentsTo force delete without confirmation prompts, combine with -f:
rm -rf documentsWarning: rm -rf permanently deletes everything without asking and cannot be undone.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline