How To Remove Files or Directories Recursively in Linux

Here are the steps to recursively remove files or directories/folders in Linux

Accidentally removed important files on a Linux device?

No worries. Wondershare Recoverit can get back your lost files within 3 steps. The ultimate and professional Linux data recovery software is totally reliable and 100% safe.

A directory can have many subdirectories and files within it. Removing all files and subdirectories in a directory may be necessary when working with the Linux filesystem. It is referred to as recursive deletion. Use the rm command to recursively remove files or directories (also known as folders in Windows) in Linux. The rmdir command only removes empty directories. This guide will present the steps to recursively remove files and directories in Linux.

Part 1. How To Remove Directory Recursively in Linux Using rm Command

A summary of the rm command syntax for removing directories/folders recursively:

Command Syntax
Description
-f A strong option. Never prompt for nonexistent files or arguments.
-r Recursively remove directories and their contents
-v Excessive output
rm -- '-dir1' Remove a directory or file whose name begins with a '-'.
rm ./-dir1 The same as before
rm -rfv 'dir name here' Put quotes around your problematic filename/folder.
rm -rfv \$dirname1 The same as before

In Linux and Unix-like systems, everything is considered a file. In other words, "files" include photos, documents, directories/folders, SSD/hard drives, NIC, USB devices, keyboards, printers, and network communications.

Examples Of How To Delete A Folder Recursively

In this example, we will delete the data folder in the current home directory recursively:

rm -r /home/vivek/data/

Before removing the data directory, the specified /home/vivek/data/ directory will be emptied of all subdirectories, including their subdirectories and files. Unless the -f (force) option is specified on the command line, the user is prompted to remove any write-protected files in the directories:

rm -rf dirname-here

Alternatively, you can use the command that follows:

rm -r -f /path/to/folder/

To remove a folder whose name begins with a “-,” such as “-dsaatia,” use one of the following commands:

rm -rf -- --dsaatia

You can also run the following command:

rm -rf ./--dsaatia

To see verbose outputs, we can use the -v option. In other words, the rm command on Linux will explain what happens to our files and folders. As an example:

rm -rfv /path/to/dir1
rm -r -f -v /home/vivek/oldpartpics

Delete Folders With Unique Name Characters

Your folders and files may contain while spaces, semicolons, backslashes, and other characters in Linux. As an example:

ls -l

Assume we have a folder called "Our Sales Data" and "baddir#" or "dir2 ;#" in it. So, how do we get rid of those directories with unusual names? The solution is straightforward. We will enclose the problematic filename or folder name in quotation marks. As an example:

rm 'Our Sales Data'
rm -rfv '/path/to/Dir 1 ;'
rm -r -f -v "baddir#"
rm a\ long \dir1 \name

Sometimes a backslash (\) is required before the meta-character in your filename or folder name:

rm \$dir1

remove a directory or folder recursively

Part 2. How To Delete Files Recursively in Linux Using rm Command

Delete All Files Recursively

When used with the -r flag, the rm command will remove the contents of all types of files.

But first, using the ls command, let's look at the home directories. In this example, there are five directories: Desktop, dir2, Documents, Downloads, and removerecurdir.

ls ~/

use the ls command

Assume we want to delete the removecurdir and its contents, including all files and subdirectories. Run the syntax below.

rm -r removerecurdir/

The ls output shows the directory and its contents were successfully removed.

delete files recursively in linux

Remove Files by Size Recursively

Here are the steps to recursively delete files (<10 MB) using the command find.

The find syntax is as follows:

find <ParentDirectory> -type f -size -<size and units> -exec rm {} +

You can use sudo to access the protected files in the example below.

sudo find /var/log -type f -size -10M -exec rm {} +

The minus sign (-) must be replaced with a plus sign (+).

find <ParentDirectory> -type f -size +<size and units> -exec rm {} +

In the following example, I will use the previous syntax to remove files larger than 1 GB.

find /var/log -type f -size +1G -exec rm {} +

Remove Files by Extension Recursively (File type)

The following section describes how to delete files recursively by extension or file type.

Look for the content of the home directory, which is called testhint with the tree command.

tree testhint/

The parent directory testhint has a file (file1.txt) and two subdirectories: testhint2 has file3.txt, and testhint3 has file3 and something.txt.

parent directory testhint

Assume you want to remove all txt files recursively. The syntax is as follows:

find <ParentDirectory> -type f -name '*.<Extension>' -print -delete

Then, run this command to remove all text files recursively within the parent directory testhint.

find ~/testhint -type f -name '*.txt' -print -delete

All text files were deleted, leaving only file3 without an extension.

tree testhint

remove all txt files recursively

You can also delete files by extension by the find together with exec commands.

find together with exec commands

The syntax for removing files by extension with -exec is as follows:

find <ParentDirectory> -type f -name '*.<Extension>' -exec rm -f {} \;

Run the command below to remove the .log files.

find ~/testhint -type f -name '*.log' -exec rm -f {} \;

The xargs command provides a similar solution. The distinction between xargs and exec is that exec executes the rm function whenever a file matches the condition. The command xargs run the rm command once for each file found that matches the condition.

To remove all files by extension using find and xargs, use the following syntax:

find <ParentDirectory> -name "*.<FileExtension>" -print0 | xargs -0 rm

remove extension using find and xargs

Enter the command shown below to remove all .c files with xargs.

find . -name "*.c" -print0 | xargs -0 rm

Once again, the selected extension files were successfully deleted.

successfully deleted selected extension files

Recursively Deleting All Files Based On Permissions

Let's take a look at the new content in the testhint directory. Four files have full access (file2, file3.c, file6.c, and file7).

content in the testhint directory

Assume you want to locate and delete all files with full permissions for everyone. The syntax is as follows:

find <ParentDirectory> -perm <Permissions> -print0 | xargs -0 rm

Run the command below to remove all files with full access for all users.

find ~/testhint -perm 777 -print0 | xargs -0 rm

remove all files based on permissions

Delete Files Recursively Based on Their Modification Or Creation Time

Execute the syntax that follows:

find ~/testhint -perm 777 -print0 | xargs -0 rm

If you want to delete files created or modified on the last day (last 24 hours), use the following command, where 1 denotes the number of days, and the minus (-) symbol denotes files created or modified before the defined number of days.

find <Directory> -type f -mtime -1 -delete

Replace the minus symbol with a plus symbol to remove files created or modified before a day, before 24 hours.

find <Directory> -type f -mtime +1 -delete

Part 3. What To Do If You've Accidentally Deleted a File or Folder in Linux?

In Linux, unintentionally deleting a file or folder can be a real hassle, but there are actions you can take to improve your chances of successful recovery:

  • Stop using the computer: To prevent further data loss, stop using the computer immediately and avoid writing any new data to the disk where the deleted file was located.
  • Act quickly: The longer you wait to start the recovery process, the greater the risk of permanent data loss.
  • Create backups regularly: Regular backups can ensure that you have a recent copy of your important data in case of accidental deletion.
  • Keep your file system in good condition: Regular disk maintenance, such as checking for errors and fixing them, can help keep your file system in good condition and make recovery easier.

The best Linux data recovery tools can also help you recover lost or deleted files. Following these tips and using the correct data recovery tool can increase your chances of successfully recovering your deleted files and folders in Linux.

Wondershare Recoverit - Your Safe and Reliable Linux Recovery Software

5,481,435 people have downloaded it.

Recovers lost or deleted documents, photos, videos, music, emails, and other 1000+ file types effectively, safely, and completely.

Compatible with all mainstream Linux distros, including Ubuntu, Linux Mint, Debian, Fedora, Solus, Opensuse, Manjaro, etc.

Assists in 500+ data loss scenarios, such as deletion, disk formatting, OS crash, power outage, virus attack, lost partition, and many more.

The simple point-and-click interface allows you to recover data from Linux hard drives in just a few clicks.

Works through a remote connection. You can recover lost data even when your Linux device is crashed.

Summing Up

Because Linux is such a versatile and adaptable operating system, users can recursively use various methods to remove files or directories (or folders) in Linux. All of the alternatives described above apply to almost every Linux distribution. Furthermore, if you accidentally delete files and folders, Linux data recovery tools are available.

You May Also Like

How To Use Foremost to Recover Files on Linux and Its Alternative

Here’s how to install and use Foremost to recover data in Linux and its best alternative.

How To Install and Use TestDisk on Linux and Its Alternative

Detailed here are the ways to install and use the TestDisk Linux data recovery tool, plus its alternative.

Top 10 Linux Partition Recovery Tools

Restore your deleted or lost data with the 10 best Linux partition recovery software.

How to Recover Deleted Files from Linux Partition?

Try these tested and proven methods to recover deleted files from Linux partitions.