[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

Crucial P310 vs TEAMGROUP MP44L: Which One to Choose for PS5?

I'm in a bit of a dilemma and could use some advice. I'm trying to decide between the Crucial P310 and the TEAMGROUP MP44L...

Is It Worth Upgrading to an RTX 5070 Ti from a 3070?

I'm thinking about upgrading from my RTX 3070 to a 50 series card, specifically considering the RTX 5070 and the 5070 Ti. The 5070...

Getting Back Into Programming: Where Do I Start?

I'm looking to dive back into programming after a break since high school. I'm 22 now and want to use programming along with my...

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

Scavenger Hunt Team Randomizer

Planning a scavenger hunt and need to split participants into random teams? Whether you're organizing a school activity, a corporate team-building event, or a...

File Hash Generator Online – Get Instant MD5 and SHA-256 Hashes

Whether you are validating downloads, checking for corruption, or comparing files for duplicates, having a fast and secure way to generate file hashes is...

Visual CSS Editor for Modern Glass UI Effects

Modern UI design is all about clean, layered aesthetics, and few styles deliver this better than glassmorphism. If you're designing sleek user interfaces and...

Fast and Accurate Tap BPM Counter – Free Web Tool

Whether you're producing music, DJing live, or just figuring out the tempo of a song, knowing the BPM (beats per minute) can be critical....

Glassmorphism CSS Generator with Live Preview

Glassmorphism is one of the most visually striking design trends in modern UI. Its soft, frosted-glass effect adds depth and elegance to web interfaces,...

Add Custom Speech and Caption Boxes to Any Image Online

Creating comic-style images used to require complex design tools or specialist software. Whether you're making memes, teaching graphics, social media posts or lighthearted content,...

Latest Posts

Latest Questions