[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

Should I Upgrade My GTX 1070 Ti to an RTX 2080 Ti or Wait?

I've got a gaming machine that's about ten years old but still serves me well, even if I'm not playing too many AAA titles....

Why Am I Hearing Voices on My PC?

I've been hearing voices coming from my PC a few times a day, and I want to figure out what's going on. Even though...

How Can I Start Gaining Work Experience in Frontend Development at 15?

I'm a 15-year-old who knows a bit of frontend coding, including HTML, CSS, and JavaScript. I'm eager to gain some online job experience and...

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

OpenAI Token Calculator

This tool is a simple OpenAI token calculator, web-based utility designed to help you quickly estimate the number of tokens in your text when...

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...

Latest Posts

Latest Questions