[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

WordPress Table of Contents Plus Not Working

I have been using this plugin for a while and i really like it. It seems to have completely stopped working recently. I can...

Function Keys Reversing Between Fn Actions And Normal

My keyboard has the usual F1 to F12 keys along the top. I use these for shortcuts in various applications. These keys also have...

Whirlpool Oven F6E6: Appliance Manager 1 Board Communication

I have a brand new Whirlpool oven W11I OM1 4MS2 H or (859991549450). I bought it alongside the microwave combi oven. I have had...


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

List Sorting Tool

Welcome to our innovative list ordering and management tool. This next-level platform enables you to sort a list of items in ascending or descending...

Sudoku Solver

Welcome to our free online Sudoku solving tool, an interactive platform for puzzle enthusiasts seeking a break from a Sudoku conundrum. This advanced platform...

Apply Image Filters To Image

Digital imagery in the modern world is all about reinforcing emotions and stories behind each photo we take. To amplify this storytelling, we are...

Add Watermark To Image

As the world is increasingly consumed by digital media, protecting your original images is paramount. We are thrilled to introduce you to our innovative...

CSV To Xml Converter

Welcome to our CSV to XML converter tool, a convenient and user-friendly solution for all your data conversion needs. This versatile tool on our...

RGB Image Splitter

Welcome to our innovative RGB Splitter - a unique image analyzer tool that offers an in-depth peek into the building blocks of your photos....

Latest Posts

Latest Questions