- How To Delete a File in Linux?
- Deleting Files Is Important—But So Is Managing Them Right
- Delete a File in Linux Using rm Command
- Other Ways to Delete a File in Linux (Beyond rm)
- How to Delete a Directory in Linux
- Conclusion
- Frequently Asked Questions
How To Delete A File In Linux: All Methods Explained (+Examples)
Knowing how to delete a file in Linux is a basic yet critical skill for managing your system efficiently. Unlike other operating systems, Linux offers multiple methods to remove files—from simple deletions using the command line to secure erasures that ensure the data cannot be recovered. Whether you're cleaning up space or maintaining file hygiene, it’s important to choose the right command or tool based on your needs.
In this guide, we’ll explore various ways to delete files and directories in Linux, covering commands like rm, unlink, find, and even GUI-based options. You'll also learn how to delete files based on name, type, size, date, and more.
How To Delete a File in Linux?
Deleting a file in Linux can be done through both the command-line interface (CLI) and graphical user interface (GUI), depending on the user’s preference and the type of task at hand.
Here are some of the most common ways to delete files in Linux:
- Using the rm command – The most frequently used command-line tool to delete one or more files.
- Using the unlink command – A simpler command meant for removing a single file.
- Using the find command – Useful when you want to delete files based on filters like name, size, or last modified date.
- Using shred – For secure deletion that prevents file recovery.
- Using GUI file manager – Ideal for beginners who prefer visual interactions over terminal commands.
- Other methods – Commands like unsync, trash-cli, or custom shell scripts offer additional control and flexibility.
Each of these methods is suited for different use cases—some are quick and simple, while others offer more granular control or enhanced security.
Deleting Files Is Important—But So Is Managing Them Right
In this guide, we’ll focus entirely on how to delete files and directories in Linux—from basic to advanced methods. But sometimes, deleting a file is just one part of the job.
If you're also looking to create, open, copy, or secure your files before deleting them, check out these helpful guides (or bookmark them for future use):
- Create a File in Linux – Learn how to use touch, cat, or editors like nano to make files.
- Open a File in Linux – From simple viewing to editing, this guide covers it all.
- Copy a File in Linux – Understand the cp command and how to duplicate your files safely.
- Linux File Permissions – Learn how to manage who can read, write, or execute your files.
- 150+ Linux Commands – A comprehensive list of commands you need to know.
With that in place, let’s now explore how to permanently remove files the right way in Linux.
Delete a File in Linux Using rm Command
The rm (short for remove) command is the most commonly used method to delete a file in Linux via the command line. It's powerful, flexible, and works across various file types and scenarios. It allows users to delete single files, multiple files, entire directories, or even files based on patterns and size.
However, its power comes with risk—files deleted using rm do not go to Trash and cannot be recovered unless you have a backup or file recovery tool in place.
That’s why it’s important to understand the command thoroughly and use it carefully, especially with flags like -rf.
Basic Syntax: (Delete a Single File)
rm filename
This removes a single file from the current or specified directory. All you have to do is replace ‘filename’ with the actual name of the file you want to delete. For example (if the filename is notes):
rm notes.txt
If the file is located in a different directory, include the full or relative path. For example:
rm /home/user/documents/filename.txt
If the file is write-protected, Linux will prompt you for confirmation before deleting it. You can suppress these prompts using the -f (force) flag.
rm -f directory
rm -f important.txt
Caution: Only use -f if you're absolutely sure you want to delete the file without confirmation.
Delete Multiple Files in Linux at Once
Use the rm command to delete several files in one go by list the file names with spaces:
rm file1.txt file2.txt file3.txt
- Use Case: Helpful for batch deletions without looping through each filename.
- Tip: Use tab completion to avoid typos that can delete unintended files.
Delete All Files in a Linux Directory (Without Deleting the Directory)
The rm command to delete all files in a Linux directory without removing the directory itself:
rm /path/to/directory/*
- Effect: Deletes all visible files inside the directory but leaves the folder intact.
- Caution: Does not remove hidden files (those starting with a dot).
To include hidden files as well:
rm /path/to/directory/{*,.*}
Pro Tip: Always use this with echo first to preview the files:
echo /path/to/directory/{*,.*}
Delete Files in Linux by Extension (File Type)
There might be instances where you want to delete all files with a specific extension. The command for this is:
rm *.log
This removes all files ending in .log in the current directory. Use Case: Useful for cleaning up temporary or debug logs after a build or deployment.
Delete Files by Pattern Matching
You can use wildcard characters to delete files matching a pattern. The basic command is:
rm *backup*
This command will delete any file with "backup" in its name—like db_backup_01.sql or project_backup.txt. Best Practice: Test with ls before running:
ls *backup*
Delete Files by Date Modified
You can also delete Linux files using a date-related parameter. For this, you must use the find command along with the rm command. For example, say you want to delete files modified more than 30 days ago, the command would be:
find . -type f -mtime +30 -exec rm {} \;
This command searches for files in the current directory (.) modified more than 30 days ago and deletes them.
- . – current directory
- -type f – find files only
- -mtime +30 – modified more than 30 days ago
- -exec rm {} \; – delete each file found
- Use Case: Cleaning up old logs, reports, or temporary data
Delete Files by Size
It is possible to delete Linux files using size as a parameter. For example, say you want to delete files larger than 100MB, the command would be:
find . -type f -size +100M -exec rm {} \;
- Use Case: Useful when managing disk space—removing files larger than 100MB.
- Variations:
- +100M → More than 100 MB
- -100M → Less than 100 MB
You can also combine size and modification date:
find . -type f -size +100M -mtime +60 -exec rm {} \;
Delete a Directory Using rm -rf
You can also use the rm command to delete an entire directory and its contents (including subdirectories). For this, you must use the -r (recursive) and -f (force) flags:
rm -rf /path/to/directory
- -r: Recursively delete folder contents
- -f: Force deletion without confirmation
- Use Case: When you need to wipe an entire project or dataset folder
Be extremely careful with this command—it permanently deletes everything in the specified directory without confirmation prompts. We will discuss how to delete directories in Linux in a later section.
Other Ways to Delete a File in Linux (Beyond rm)
While the rm command is the most common tool to delete files in Linux, it’s far from the only one. Depending on your need—secure deletion, safe deletion (trash), or graphical tools—there are several other commands and utilities that get the job done, each with its own advantages.
In this section, we will discuss some useful alternatives to delete a file in Linux.
Method 1: unlink Command – Simple File Deletion in Linux
The unlink command is the most minimal way to delete a single file in Linux. The basic syntax is:
unlink filename
When to use:
- Deleting one file at a time.
- When you want minimalism with no options or flags.
Caution Note: You cannot use unlink to delete directories or multiple files. It’s best suited for scripting situations where a single, controlled file needs removal.
Method 2: srm Command – Secure File Deletion with Overwriting
The srm command functions like rm but with added security: it overwrites the file’s content multiple times with random data before deletion, making a recovery nearly impossible. Syntax:
srm filename
When to use:
- When working with sensitive files such as passwords, financial records, or confidential documents.
- You want a quick way to avoid leaving recoverable traces.
Note: The srm command is not installed by default on most Linux distributions. You may need to install it manually using your package manager. After installation, you can use the srm tool to remove files or directories. Install via:
sudo apt install secure-delete
Method 3: shred Command– Advanced Secure Deletion
The shred command gives you fine-grained control over secure deletion in Linux. It allows you to securely delete files and devices by overwriting their contents multiple times with random data. This is why it is a more powerful and flexible alternative to the rm and srm commands. Syntax:
shred [options] filename
By default, shred overwrites the contents of the file 3 times with random data. You can specify the number of times to overwrite the file using the -n option. For example, to overwrite the file 7 times, you can use the following command:
shred -n 7 -u secret.txt
- -n 7: Overwrite 7 times
- -u: Remove file after shredding
When to use:
- For highly sensitive files where overwriting + deletion is a must.
- When you need to customize the number of overwrite passes.
Limitation: May not work well on files stored on SSDs or journaled filesystems due to write optimizations.
Method 4: find + -delete Commands– Powerful Bulk Deletion Tool
The find command isn’t just for locating files—it can also delete them based on complex rules like time, size, and name patterns. Example:
find /path/to/folder -type f -name "*.tmp" -delete
This command deletes all .tmp files in the folder and subfolders. When to use:
- You want to delete files based on specific criteria: name, age, type, or size.
- Ideal for automation scripts or server cleanups.
Method 5: trash-cli Command– Safe Deletion with Recovery Option
The trash-cli command provides a safer option by moving deleted files in Linux to the Trash (just like in a desktop environment) instead of permanently removing them. Here is the basic syntax for this command:
trash-put filename
Benefits:
- Allows file recovery later via trash-list or graphical trash bin.
- Works well in shared or production environments where accidental deletes can be costly.
- Installation: sudo apt install trash-cli
Method 6: Midnight Commander (mc) – GUI-Based File Manager
If you're more comfortable with visual tools, Midnight Commander is a text-based user interface for managing files, including deletions. Installation:
sudo apt install mc
- Once installed, launch using mc in the terminal.
- Navigate to files and press F8 to delete.
Ideal for:
- Beginners not comfortable with CLI.
- Managing bulk files without remembering command syntax.
How to Delete a File in Linux (Overview)
|
Tool/Command |
Best For |
Interface |
Recovery Possible? |
|
rm |
General use |
CLI |
❌ |
|
unlink |
One-off deletes |
CLI |
❌ |
|
srm |
Secure deletes |
CLI |
❌ |
|
shred |
Advanced secure deletes |
CLI |
❌ |
|
find -delete |
Pattern-based deletes |
CLI |
❌ |
|
trash-cli |
Safer deletes |
CLI |
✅ |
|
mc |
Visual deletion |
GUI (TUI) |
Depends |
These commands ensure that whether you’re deleting critical files, automating cleanups, or just avoiding mistakes, you have the flexibility to do it your way—safely, securely, or simply.
How to Delete a Directory in Linux
Just like you can delete files in Linux, the same is possible for directories. However, deleting directories depends on whether the directory is empty or contains files. Linux offers several commands that give you control, safety, and flexibility while removing directories.
Here’s a breakdown of the main methods:
Deleting an Empty Linux Directory
If the directory contains no files or subdirectories, you can use either the rmdir command or the rm -d command to delete it in Linux.
1. Using rmdir
The remdir command is the most common way to delete a directory in Linux. The basic syntax is:
rmdir directory_name
Note: It works only on empty directories. If the directory is not empty, it throws an error.
2. Using rm -d
The alternative command to delete a directory in Linux is:
rm -d directory_name
Note: This also works only for empty directories. It’s useful if you’re already comfortable using the rm command for other file operations.
Deleting a Non-Empty Directory
If the directory has files, subfolders, or hidden files, you’ll need to use the rm command with recursive flags.
1. Using rm -r or rm -rf
The most common way to delete a non-empty directory in Linux is to use the rm (remove) command. The basic syntax for this is:
rm -r directory_name
- Here, -r stands for recursive, which means it deletes everything inside the folder first and then removes the folder itself.
- This will prompt you for confirmation if needed.
To skip prompts:
rm -rf directory_name
- -f stands for force, which disables all prompts and removes everything silently.
- Use with caution — files deleted with rm -rf cannot be recovered.
Preview Directory Structure Before Deletion
To better understand what you're deleting with rm -rf, you can use the tree command to view the directory structure:
sudo apt-get install tree
tree /path/to/directory
This provides a clear view of subfolders and files that will be affected, helping you avoid accidental mass deletion. The rm command is efficient and fast—but final. There is no undo. So before using powerful flags like -rf, double-check the path and files to prevent irreversible data loss.
Bonus: Create a Safe rm Alias
For additional safety, many users alias rm to move files to Trash instead:
alias rm='mv --target-directory=/home/username/.Trash'
You can also use utilities like trash-cli to intercept deletes safely. By understanding the full range of rm's capabilities—and the risks—you can clean up your system confidently while avoiding accidental data loss.
Safety Tip: Test Before You Wreck
To safely test what would be deleted, use the ls or find command before deletion:
ls directory_name
find directory_name
If you're working on critical servers or production environments, avoid rm -rf without full certainty.
GUI-Based Method (For Desktop Linux Users)
If you're using a Linux system with a desktop environment (like Ubuntu or Fedora), you can simply:
- Open your File Manager
- Right-click the directory
- Select "Move to Trash" or "Delete"
Pro tip: Files deleted via GUI often go to a trash folder, allowing you to recover them if needed.
Summary Table
|
Method |
Use Case |
Deletes Subdirectories? |
Prompts? |
Safe for Beginners? |
|
rmdir |
Empty directories |
❌ |
❌ |
✅ |
|
rm -d |
Empty directories |
❌ |
❌ |
✅ |
|
rm -r |
Non-empty directories |
✅ |
✅ |
⚠️ (Caution) |
|
rm -rf |
Non-empty (force delete) |
✅ |
❌ |
❌ |
|
File Manager |
Desktop users |
✅ |
✅ |
✅ |
Conclusion
Deleting files and directories in Linux might seem straightforward at first glance, but there’s more to it than just running a simple command. Whether you're working with single files, large folders, or sensitive data, Linux gives you the flexibility to delete them safely, forcefully, or securely using tools like rm, unlink, shred, and srm.
Knowing which method to use and when can help you manage your files confidently while avoiding accidental data loss. Always double-check before hitting enter on powerful commands like rm -rf.
Frequently Asked Questions
Q1. How do I delete a file in Linux?
You can delete a file using the rm command followed by the filename:
rm example.txt
If the file is write-protected, you’ll be prompted for confirmation. Add -f to skip prompts:
rm -f example.txt
Remember, this action is permanent.
Q2. Can I recover a deleted file in Linux?
Once a file is deleted using rm or similar commands, it's typically not recoverable via basic methods. However, tools like extundelete, PhotoRec, or TestDisk may help — if the file hasn't been overwritten. It's best to stop using the drive immediately and attempt recovery from a Live USB or another partition.
Q3. How do I delete a directory and its contents in Linux?
Use the recursive flag -r with rm:
rm -r mydirectory/
To force deletion without confirmation:
rm -rf mydirectory/
Avoid using -rf unless you’re sure — there’s no undo!
Q4. What’s the difference between rm and unlink?
- rm is more versatile — it can delete files and directories with the right flags.
- unlink is limited to deleting a single file only and has no options for directories:
unlink filename.txt
Q5. How can I delete files securely in Linux?
Use the shred command to overwrite a file multiple times before deletion:
shred -n 3 -u sensitive.txt
- -n specifies the number of overwrite passes (default is 3)
- -u deletes the file after shredding
This makes data recovery much harder.
Q6. Is there a recycle bin or trash in Linux like Windows?
Yes, desktop environments like GNOME or KDE move deleted files to the Trash. If you're using the command line, consider installing:
sudo apt install trash-cli
Then use:
trash-put filename.txt
This sends the file to Trash instead of permanently deleting it.
By now, you must know all about how to delete a file in Linux. Do check the following out too:
An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Subscribe
to our newsletter
Blogs you need to hog!
This Is My First Hackathon, How Should I Prepare? (Tips & Hackathon Questions Inside)
10 Best C++ IDEs That Developers Mention The Most!
Advantages and Disadvantages of Cloud Computing That You Should Know!
Comments
Add comment