Use the find exec rm Command to Find and Delete Files in Linux

In this article, we'll discuss a frequent issue encountered by certain users: how to delete unwanted data permanently and safely. This tutorial will explain six ways to remove data from a server using find exec rm. Moreover, we'll go through the best data recovery software in case you ever erase or lose information by mistake. Okay, so let's begin!

Part 1. What Is the find exec rm Command?

You may remove certain files or directories from a system with the find exec rm command. This program uses a trio of other commands to permanently delete any files or folders it finds. Find exec rm is an invaluable tool for bulk deleting files and directories according to many criteria (such as permissions, type, date, ownership, size, and more).

The exec command is the most powerful part of the find command since it enables the Linux user to execute any command on the retrieved files. As a result of the search, certain operations may be carried out on the uncovered data.

find exec rm command prompt

Syntax of the find exec rm Command

When using find, we would use the following syntax:

find [options] [path] [expression]

Wherein:

  • options: This is entirely optional. It may need to be included the majority of the time. We may consult the locate command's documentation page for a complete list of its options.
  • path: The path to a directory to search.
  • expression: This is where we enter our search parameters for the item we want to locate, whether by name, size, etc.

Part 2. How To Use find exec rm Command to Delete Files in Linux?

Sometimes it is important to locate all files and delete them simultaneously. However, search criteria are not supported by the rm command.

For such purposes, you must use the find command to search for and delete files in a directory on the fly. It is possible to mix the search and rm commands.

1. Find and Delete Files With a Specific Name

To find and delete files with a specific name, we’ll use this command:

find . -type f -name "*.x" -exec rm -f {} \;

Here’s an example. Each time find detects a file with the extension .txt, the rm command deletes that file. We will run the following syntax:

find . -type f -name "*.txt" -exec rm -f {} \;

2. Find and Delete Files in Multiple Directories

We will employ the following command:

find /root folder -name failed*.* -type f -exec rm {} \;

For example, to delete several directories simultaneously, run the rm command followed by the directory names separated by a space. You must run this command:

find /path1 /path2 -name failed*.* -type f -exec rm {} \;

3. Find and Delete Files Older Than x Days

To find and delete files older than x days, run the following syntax:

find /path/to/files* -mtime +x -exec rm {} \;

For example, using the command below, search all folders under /var/log modified over the last 30 days.

find /var/log -type d -mtime +30 -exec rm -rf {} \;

4. Find and Delete Files With Modifying Time

Run the command that follows:

find /path -mmin -x -exec rm {} \;

For example,To remove the files that are modified within 60 minutes, type:

find /path -mmin -60 -exec rm {} \;

5. Find and Delete Large Files

We should employ:

find /path -type f -size +x -exec rm {} \;

For example, find files larger than 200 MB. Remove these files. This can be done by:

find /path -type f -size +200M -exec rm {} \;

6. Other Advanced find exec rm Examples

Finds and deletes all *.bak files in the current directory with user confirmation:

find . -type f -name "*.bak" -exec rm -i {} \;

Find and remove all.mp3 files larger than 10 MB with a single command.

find / -type f -name *.mp3 -size +10M -exec rm {} \;

To delete all a.out and *.o files that have not been viewed for a week and are not mounted using nfs, enter:

find / \( -name a.out -o -name ‘*.o’ \) -atime +7 ! -fstype nfs -exec rm {} \;

Part 3. What To Do If You've Accidentally Deleted an Important File in Linux?

Once you run the find and rm command, the files are deleted irretrievable. But don’t be panic. There are some Linux data recovery tools that allow you to get back deleted files. The most recommended one is Recoverit Linux Recovery. This comprehensive data recovery program protects your Linux data from unintentional deletion, hardware failure, and natural calamities.

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.

You can follow the step-by-step guide for quick recovery or watch the video below for a more comprehensive explanation.

Step 1. Choose the Linux Recovery Option

Start up the application, and then choose NAS and Linux. It's on the primary screen's left side. To continue, please click the Linux Recovery option.

linux recovery on nas and linux
Free Download

For Windows Vista/7/8/10/11

Free Download

For macOS X 10.10 or later

Step 2. Assess the Linux Computer

Enter the required data to establish a distant connection. When you're ready, choose Connect from the menu.

establishing a remote connection

It would be best if you held off till the link is made. After a successful connection, Recoverit will immediately begin scanning your Linux computer for your missing data.

Step 3. Preview Deleted/Lost Files and Recover Them

Depending on the quantity of data being scanned, the scanning procedure might take some time. On the bottom bar, the scanning progress is shown. The nicest part about utilizing Recoverit is that you may stop searching whenever you want after you've found the file you want to recover.

automatically scan mistakenly deleted files

When the scanning is finished, Recoverit allows you to examine the files to check they are the ones you want to recover. Now, choose Recover to save the file to your Linux device.

preview and recover lost data

The program will prompt you to choose a destination folder to recover files. To get the recovered information, click the Recover button. Remember the location path you took before losing your files and choose a new one this time.

transfer the retrieved files to a new folder
Free Download

For Windows Vista/7/8/10/11

Free Download

For macOS X 10.10 or later

Apart from using Linux data recovery software like Recoverit, there are many other ways to recover deleted files in Linux. We've produced a list of 8 ways to recover deleted files in Linux you can use without hesitation to quickly restore lost files.

Conclusion

You may now use the find exec rm command to permanently delete files without being asked for confirmation. You may use the 6 ways mentioned above to choose the appropriate actions when finding and deleting files. However, Wondershare Recoverit will rescue you if you remove the mistaken files. Try it out now!

You May Also Like

How To Remove Files or Directories Recursively in Linux

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

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 Wipe Your Disk in Linux?

Linux wipe disk is necessary when you’re facing storage issues. Users can check the disk space status in Linux and wipe Linux hard drive.