[Centos] Delete All Files And Folders That Contain a String

Deleting files on a linux system is normally something you use the RM command for. In some cases you might want to be more selective about the delete. While RM can support wildcard deleting, it is also possible to use the find command which gives you more flexability in how you delete stuff.

Testing Your Query – Find Files That Contain *Wildcard*

Using the find command is simple. Lets say you have a script that has not been cleaning up after itself and you now have a directory that has all of its subdirectories filled with a folder called /tmpdata/ And these directories have a bunch of files in them.

find . -path "*/tmpdata/*" -type f

The command above will list all of the files inside any subdirectory. Check over this list and make sure everything looks good. If you are happy that all of the files are garbage, you will want to delete them. Run the same command again, but with -delete at the end in order to remove them.

Deleting All Files that Contain A Wildcard String

find . -path “*/tmpdata/*” -type f -delete

Now all of the files are gone, you will need to delete the directories. You are unable to delete the directory if it contains something. There isn’t a recursive way to do this with a single find command, so you will need to run the command above first to delete files before deleting the directories.

You will need to modify this command slightly to target directories instead of files. This is done using the type argument. Changing it to D will change it to target directories. Make sure you also delete the slash as this will not exist on the end of an empty directory.

find . -path “*/tmpdata*” -type d -delete

find: cannot delete directory not empty Error

This error occurrs when you try to use the -delete command with find, but there are files inside the directory that must be removed first. If you view the commands above you will see how to delete the files first before deleting the directory itself.

Related Articles

Related Questions

Help! Can’t Install Realtek Drivers Without Windows Reinstalling Old Ones

Hey everyone, I'm trying to install the Realtek audio driver, but I'm running into a frustrating issue. Whenever I install the drivers, they delete...

What Linux distros offer more attractive designs?

I'm transitioning from Windows 10 to Linux and I've always loved the visual style of Windows 7. I'm considering using Linux Mint because it...

Which RAM Specs Matter More for Gaming: Speed or Timings?

Hey everyone! I'm looking to upgrade my RAM and hoping you can help me out. I'm trying to decide between two options: should I...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

Online Hash Generator – String to Hash Converter

Need to quickly generate a hash from a string? Whether you're verifying file integrity, securing data, or just experimenting with cryptographic tools, this simple...

Convert CSV To HTML Table

Need to quickly turn CSV data into an HTML table? Whether you're copying data from Excel, Google Sheets, or another spreadsheet, this tool makes...

Student Group Randomizer

Creating fair and balanced groups in the classroom can be time-consuming — especially when you're trying to avoid repetition, manage different skill levels, or...

Random Group Generator

Need to split a list of people, items, or ideas into random groups? Our free Random Group Generator makes it quick and easy. Whether...

Flip Text Upside Down – Free Online Tool

Ever wanted to flip your text upside down just for fun or to grab someone’s attention in a creative way? This free online Upside...

Raffle Ticket Generator

If you're running a fundraiser, charity draw, or local event and need raffle tickets fast, this free online tool lets you generate and print...

Latest Posts

Latest Questions