How do I delete a file in terminal?
Categories:
How to Delete Files and Directories in the Linux Terminal

Learn the essential commands to safely and effectively remove files and directories using the command line in Ubuntu and other Linux distributions.
Deleting files and directories is a fundamental task when working in the Linux terminal. Whether you're cleaning up old data, removing temporary files, or managing your project's structure, mastering the rm
and rmdir
commands is crucial. This article will guide you through the various options and best practices for file deletion, ensuring you can manage your file system efficiently and safely.
Understanding the rm
Command
The rm
(remove) command is the primary tool for deleting files and directories in the Linux terminal. It's powerful, and by default, it does not send files to a 'trash' or 'recycle bin'; once deleted, they are typically gone permanently. Therefore, it's essential to use it with caution.
rm filename.txt
Basic command to delete a single file.
rm file1.txt file2.txt file3.doc
Deleting multiple files at once.
rm *.log
Deleting files matching a pattern (e.g., all .log files).
*
) with rm
, especially in combination with sudo
. A misplaced wildcard can lead to unintended and irreversible data loss.Deleting Directories with rm
and rmdir
While rm
is primarily for files, it can also delete directories, especially non-empty ones. For empty directories, rmdir
is a safer and more specific command.
rmdir empty_directory/
Deleting an empty directory using rmdir
.
rmdir
command will only delete a directory if it is completely empty. If the directory contains any files or subdirectories, rmdir
will fail.rm -r non_empty_directory/
Recursively deleting a non-empty directory with rm -r
.
rm -rf stubborn_directory/
Forcefully deleting a non-empty directory and its contents without prompting.
rm -rf
command is extremely dangerous. It recursively deletes files and directories without prompting for confirmation. Never use it unless you are absolutely certain of what you are doing, as it can lead to catastrophic data loss, including system files if used incorrectly.Interactive Deletion and Confirmation
To add a layer of safety, you can use the interactive flag with rm
. This prompts you for confirmation before deleting each file or directory.
rm -i file_to_delete.txt
# Output: rm: remove regular empty file 'file_to_delete.txt'? y
Interactive deletion of a file.
rm -ri directory_to_delete/
Interactive recursive deletion of a directory.
flowchart TD A[Start Deletion Process] --> B{Is it a file or directory?} B -->|File| C[Use 'rm'] B -->|Directory| D{Is directory empty?} D -->|Yes| E[Use 'rmdir'] D -->|No| F[Use 'rm -r'] C --> G{Need confirmation?} F --> G G -->|Yes| H[Add '-i' flag] G -->|No| I[Proceed with caution] H --> J[Confirm each item] I --> K[Deletion complete] E --> K J --> K
Decision flow for deleting files and directories.
Practical Steps for Deleting Files
Follow these steps to safely delete files and directories in your terminal.
1. Navigate to the directory
Use the cd
command to change to the directory containing the files or directories you wish to delete. This helps prevent accidental deletion of items in other locations.
2. List contents to verify
Before deleting, use ls
or ls -l
to list the contents of the directory and confirm the exact names of the files or directories you intend to remove.
3. Delete a single file
To delete a single file, use rm filename.txt
. Consider adding the -i
flag for a confirmation prompt: rm -i filename.txt
.
4. Delete an empty directory
If the directory is empty, use rmdir directory_name/
. If it's not empty, this command will fail.
5. Delete a non-empty directory
To delete a directory and all its contents (files and subdirectories), use rm -r directory_name/
. For added safety, use rm -ri directory_name/
to be prompted for each item.