•Arnošt Havelka
Removing Files and Directories
Learn rm and rmdir to delete files and directories safely.
Start Interactive LessonThe rm command deletes files permanently. Use it carefully—there's no trash can in the terminal!
Remove a Single File
Use rm filename to delete a file.
Try the command
Delete a file!
Build the command
rmfilename
rmRemove file - permanently deletes the specified file.
Terminal
C:\Users\User>rm filename
Delete instantly:
Command Prompt
important.txt
trash.txt
C:\Users\User>ls
Remove Multiple Files
Delete several files at once by listing them.
Try the command
Delete multiple files!
Build the command
rmfile1 file2 file3
file1 file2 file3Multiple files - deletes all specified files.
Terminal
C:\Users\User>rm file1 file2 file3
Batch deletion:
Command Prompt
keep.txt
delete1.txt
delete2.txt
C:\Users\User>ls
Remove Empty Directories: rmdir
The rmdir command removes empty directories. It refuses to delete non-empty ones.
Try the command
Delete an empty directory!
Build the command
rmdirdirectory
rmdirRemove directory - deletes only empty directories.
Terminal
C:\Users\User>rmdir directory
Remove empty folders:
Command Prompt
Project/
EmptyFolder/
ls EmptyFolder/
C:\Users\User>ls
Remove Directories with Contents: rm -r
Use rm -r to delete directories and all their contents. Be very careful with this!
Try the command
Delete a directory with files!
Build the command
rm-rdirectory
-rRecursive - deletes directory and everything inside it.
Terminal
C:\Users\User>rm -r directory
Delete entire trees:
Command Prompt
Project/
ls Project/
C:\Users\User>ls
Knowledge Check
1 / 3What does rm do?
References
These documentation links provide authoritative details for the commands used in this article.