Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I would like to delete folders with specified regex, but I could not build a good regex.

I have:

/root
  /my-picture-2015
    /my-picture-2015-1
    /my-picture-2015-2
  /my-picture-2015-1
  /my-picture-2015-2
  /my-picture-2015-3
etc.

Directory my-picture-2015 is a backup which I would like restored. It could stay in this directory, but I have to delete all my-picture-2015-X only from root directory. Directories in my-picture-2015 have to stay.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Simply:

rm -rf /root/my-picture-2015-*/
share|improve this answer
    
and it will not remove : /root/my-pictures-2015/my-pictures-2015-1 ? –  Mateusz Szymański Jul 10 at 10:59

If you are inside the root directory, then you can use this command to delete the required folders:

find -type d -maxdepth 1 -iregex ".*my-picture-[0-9]*-[0-9]" -exec rm -r {} +

Here,

-maxdepth   ==>   do no descend beyond the first level for searching
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.