4

I have some folders that have been created in error. I would like remove these folders.

¬ 636
¬ 636.empty
¬ 637
¬ 638.empty
¬ 639
¬ 639.empty

In this situation I would like to remove 636.empty and leave the remaining none empty folder.

After I have ran the terminal command I would like all folders that a sister folder to be removed.

¬ 636
¬ 637
¬ 638.empty
¬ 639

Removed folders would be:

¬ 636.empty
¬ 639.empty

I have some 25,000 folders maybe 1000 of these are duplicates with the empty folder

  • 2
    I guess that the folders you want to delete don't end always in .empty. Is that correct? – guillermo chamorro Oct 30 '19 at 15:18
  • I have updated the question. I am also sorry that I do not have any working examples of code that I have tried to write myself. – Jamie Hutber Oct 30 '19 at 15:21
  • @Jamie Hutber To make sure we know what you want, 1) do you want to delete all directories which end in .empty? Yes/No? 2) Are all the directories you wish to remove under the same directory? Yes/No? Please click edit and provide more clarification. Please do not use Add Comment, but instead just use edit. – K7AAY Oct 30 '19 at 15:32
7

Loop through all directories named *empty and check if same directory without .empty exists and delete if true:

for d in *.empty; do
    [ -d "$d" ] && [ -d "${d%.empty}" ] && rmdir "$d"
done

Replace rmdir with rm -rf if the directory is not empty despite its name ;-)


A find alternative:

find -maxdepth 1 -type d -name '*.empty' \
  -exec sh -c '[ -d "${1%.empty}" ]' find-sh {} \; \
  -delete

Replace -delete with -exec rm -rf {} \; if the directory is not empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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