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.

You will learn how to safely and effectively remove unwanted files from a Linux system using the command line.

Prerequisites:

  • Access:
    Ensure you have appropriate permissions to delete the file(s) in question. Every file in Linux has permissions attached to it 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 the pwd command 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 ls command to list your current directory.
Bash
#Example
cd ~
ls -l
  • Use the ls command 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.

Bash
rm filename

You can add an interactive prompt asking for confirmation before deletion (recommended for safety).


Bash
rm -i filename

Confirmation (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:

Bash
rm file1 file2 file3

Delete 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.

Bash
rm -fr

Cautionary 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 *.txt deletes all files in the current directory ending with the extension “.txt” (e.g., report.txt, notes.txt, old_file.txt).

Bash
rm *.txt 

#Deletes all files ending in .txt.

  • Question Mark (?): Matches exactly one character.
    • Example: rm file_?.txt deletes files like file_1.txt, file_2.txt, but not file_10.txt or file_abc.txt.
Bash
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 -i option with the rm command:
Bash
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_??_*.jpg could 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].txt deletes log_1.txt, log_2.txt, etc.)
    • [!characters]: Matches any single character not within the brackets. (e.g., rm file_[!0-9].txt deletes files like file_a.txt, file_b.txt)
    • {pattern1,pattern2,...}: Matches any of the comma-separated patterns. (e.g., rm {old,temp,backup}_*.zip deletes 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

Elsewhere On TurboGeek:  How to Install Python3 (3.12.1) on RHEL6 & RHEL7

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate ยป