How to Delete Files in Linux: The Ultimate Guide for Beginners (and Pros!)
Sometimes, it is necessary to delete files in Linux. As always, be careful when deleting files because once they are gone, they are gone. There are lots of different ways to delete files in Linux, this procedure will focus on how to do it from the command line.
Using the command line, learn how to safely and effectively remove unwanted files from a Linux system.
Prerequisites:
- Access:
Ensure you have appropriate permissions to delete the file(s). Every file in Linux has permissions attached that allow (or deny) read, write, and execute permissions.
It’s also important that your user is in the correct group. If you are a system administrator, you can always use the sudo command to override permissions. - Location:
Know the exact path to the file(s) you want to delete. You can always use thepwdcommand to get your present working directory.
Step 1 – Verification
- Confirm you have the correct file name and path. Typos can lead to deleting the wrong items. You can use the
lscommand to list your current directory.
#Example
cd ~
ls -l- Use the
lscommand to list files in the directory and verify the target file’s existence.
Step 2 – Choose Deletion Method:
How to Delete A Single File:
The simplest command to delete a single file.
rm filenameYou can add an interactive prompt asking for confirmation before deletion (recommended for safety).
rm -i filenameConfirmation (If using -i):
If you used rm -i, the system will prompt you for each file:
y: Confirm deletion.
n: Skip deletion.
Verification (After Deletion):
Use ls again to verify the file(s) have been removed.
How to Delete Multiple Files:
rm file1 file2 file3Delete multiple files listed one after another.
How to delete all files in a folder
You can recursively delete all files within a folder if needed.
rm -frCautionary Steps (Important):
rm is Permanent: The rm command permanently deletes files. There’s no “Trash” or “Recycle Bin” in the Linux command line.
rm -rf (DANGER): NEVER use rm -rf on system directories. This recursively (-r) forces (-f) deletion and can wipe out your entire system if used incorrectly
Using Wildcards to Delete Files
Wildcards are special characters that help you select multiple files at once based on patterns in their names. This makes it easier to manage large groups of files without having to type each filename individually.
Here’s how you can use wildcards with the rm command:
- Asterisk (
*): Matches any number of characters (including zero).- Example:
rm *.txtdeletes all files in the current directory ending with the extension “.txt” (e.g., report.txt, notes.txt, old_file.txt).
- Example:
rm *.txt
#Deletes all files ending in .txt.- Question Mark (
?): Matches exactly one character.- Example:
rm file_?.txtdeletes files like file_1.txt, file_2.txt, but not file_10.txt or file_abc.txt.
- Example:
rm file_?.txt
#Deletes files named file_1.txt, file_2.txt, etc.Additional Tips:
- Be Careful!:
Wildcards are powerful. Double-check your patterns before pressing Enter, as you could accidentally delete files you didn’t intend to. - Dry Run (
-i):
To practice and see which files would be deleted without actually removing them, use the-ioption with thermcommand:
rm -i *.tmp
#Dry Run: will ask you for confirmation before deleting each file matching the pattern.- Combining Wildcards:
You can use multiple wildcards together. For example,rm image_??_*.jpgcould delete files like image_01_small.jpg, image_02_large.jpg, but not image_01.jpg or image_123_screenshot.png. - Other Wildcards:
Bash supports additional wildcards:[characters]: Matches any single character within the brackets. (e.g.,rm log_[1-5].txtdeletes log_1.txt, log_2.txt, etc.)[!characters]: Matches any single character not within the brackets. (e.g.,rm file_[!0-9].txtdeletes files like file_a.txt, file_b.txt){pattern1,pattern2,...}: Matches any of the comma-separated patterns. (e.g.,rm {old,temp,backup}_*.zipdeletes files starting with “old_”, “temp_”, or “backup_” followed by anything and ending in “.zip”)
Hidden Files: Be aware of hidden files (starting with a dot, e.g., .bashrc). You might need ls -a to see them.
That’s it, thanks for reading. We welcome all comments and feedback.
For more Linux Tech Quickys, check out our Linux Pages

Recent Comments