How to Delete Files and Directories in Linux (rm command)
Learn how to quickly and safely remove files and directories from your Linux system. Discover the many commands at your disposal and how to use them appropriately. Begin learning how to delete on Linux right now.
Table of Contents
Table for Delete files and directories in Linux
Command and Option | Description |
rm -d | Use the rm command to delete an empty directory. |
rm -r | Remove the contents of a non-empty directory. |
rm -f | Ignore any prompt when deleting a write-protected file. |
rm -rf | When removing a non-empty write-protected folder, ignore any prompts. |
rm -i | Output a prompt before deleting every file. |
rm -I | Output a prompt only once before deleting more than three files. |
rm * | Multiple characters are denoted by the wildcard. |
rm ? | Wildcard that represents a single character. |
rmdir -p | Remove the parent directory and any empty subdirectories. |
rmdir -v | The deletion of the given directory will be printed. |
Deleting Files
rm filename_or_path
Removing Folder in Linux (rmdir)
- Use the
rmdir
orrm -d
command to remove empty directories. - Use the
rm -r
command to remove non-empty directories.
rmdir folder_name
Note: If the directory still contains files or subdirectories, the rmdir
command does not remove the directory.
rm -d directory_name
rm /path/to/dir/directory_name
rm /path/to/directory/*
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r.
rm -r folder_name
Use rm
with the -r
(recursive) and -f
arguments to delete non-empty folders and all files without prompting:
rm -rf folder_name
How to Delete Files in Linux
- unlink
- rm
The unlink
command can only delete a single file, whereas rm
may remove many files at once.
unlink filename
rm filename
Delete multiple files at once
rm filename_1 filename_2 filename_3 filename_4
rm *.png
Conclusion
You should now be able to securely remove files and directories from the command line, having a thorough grasp of how to use the Linux rm, rmdir, and unlink functions.