Mandemon
Mandemon

Reputation: 422

Delete folders that have numbers in them but don't delete folders that don't have numbers

So there was a bug in our recent code, and instead of creating bunch of numbered folders in a root folder, it created bunch of folders right next to root folder

To put it simply, what we wanted was:

customers
|-18
|-158
|-405
|-1238
|-1447
...
|-4797

Unfortunately, what we got was:

customers
customers18
customers158
customers405
customers1238
customers1447
..
customers4797

Now, there are about 1000 folders (with their internal sub-folder structure) that we need to delete. I tried to look up regex and other filtering method, but it seems like they don't work on rm command.

What is the command line I need to delete all the "customers[numbers]" folders but NOT the "customers" folder?

Upvotes: 0

Views: 197

Answers (1)

Md Golam Rahman Tushar
Md Golam Rahman Tushar

Reputation: 2375

Try the following command. It should work -

ls | grep -P "customers[0-9]+" | xargs -d"\n" rm -R

Upvotes: 2

Related Questions