Delete Empty Subfolders

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Delete Empty Subfolders Thu May 25, 2017 1:45 pm • by twz2004
Hey all!

I searched the forums already, I did find one topic on this but it didn't resolve my issue (and it was dated back in 2006)

I have a picture directory I setup some rules for to organization by the YEAR and then MONTH. After the process is completed, it does a great job! However, in my SOURCE directory where it pulled files from to restructure I have hundreds of empty directories. Some, very few, but some, have some remaining files.

How do I remove all the empty directories and not remove the directories that still have files?

Index Pictures
Sub Folder A <EMPTY>
Sub Folder A2 <EMPTY>
Sub Folder A3
FILE.JPG
Sub Folder A4
Sub Folder AA1 <EMPTY>
Sub Folder B <EMPTY>
Sub Folder C <EMPTY>
Sub Folder D <EMPTY>
Sub Folder E <EMPTY>

I tried this rule, but it doesn't work. It only will delete the empty folder from the root, i..e. Sub Folder A <EMPTY> etc.

Image

Thanks in advance for any help!
twz2004
 
Posts: 3
Joined: Thu May 25, 2017 1:33 pm

Re: Delete Empty Subfolders Fri May 26, 2017 10:42 am • by Mr_Noodle
Read this article on going into subfolders: https://www.noodlesoft.com/manual/hazel ... ubfolders/
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Delete Empty Subfolders Sun Aug 25, 2019 11:30 am • by LETRA
I have a problem with this. The structure of my folders is ARTIST / ALBUM. I want to delete album folders that contain only one file ("cover.jpg"). I have read the tutorial and I dare not execute the rule because I think it will delete all the folders in which the artist only has one album.

Image
LETRA
 
Posts: 3
Joined: Sun Aug 25, 2019 10:22 am

Re: Delete Empty Subfolders Mon Aug 26, 2019 11:04 am • by Mr_Noodle
I'd try it on a test set of files first. Also, you can match a folder based on its contents. Look up nested conditions. Using those, you can match a folder that only has a file specifically called cover.jpg.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Delete Empty Subfolders Mon Aug 26, 2019 11:10 am • by LETRA
Mr_Noodle wrote:I'd try it on a test set of files first. Also, you can match a folder based on its contents. Look up nested conditions. Using those, you can match a folder that only has a file specifically called cover.jpg.


In this way?

Image
LETRA
 
Posts: 3
Joined: Sun Aug 25, 2019 10:22 am

Re: Delete Empty Subfolders Mon Aug 26, 2019 12:10 pm • by luomat
The unix command `rmdir` will remove directories, but only if they are empty.

Code: Select all
rmdir -p /path/to/whatever/folder


will try to remove ' /path/to/whatever/folder’ and any of its “parent" folders if they are empty too.

Code: Select all
find /path/to/whatever -type d -depth -exec rmdir -p {} \; 2>/dev/null


will look in '/path/to/whatever’ for any directories, and try to remove them using `rmdir` which will only work if they are empty. If they aren’t empty it will throw a harmless error.
luomat
 
Posts: 78
Joined: Wed Mar 10, 2010 3:57 pm

Re: Delete Empty Subfolders Mon Aug 26, 2019 12:22 pm • by LETRA
luomat wrote:The unix command `rmdir` will remove directories, but only if they are empty.

Code: Select all
rmdir -p /path/to/whatever/folder


will try to remove ' /path/to/whatever/folder’ and any of its “parent" folders if they are empty too.

Code: Select all
find /path/to/whatever -type d -depth -exec rmdir -p {} \; 2>/dev/null


will look in '/path/to/whatever’ for any directories, and try to remove them using `rmdir` which will only work if they are empty. If they aren’t empty it will throw a harmless error.


I think you put the message in a wrong thread.
LETRA
 
Posts: 3
Joined: Sun Aug 25, 2019 10:22 am

Re: Delete Empty Subfolders Tue Aug 27, 2019 10:17 am • by Mr_Noodle
You need to use a nested condition. Check the manual as there's an explanation and examples there.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Delete Empty Subfolders Thu Jan 09, 2020 1:00 pm • by twz2004
Hey all, I know it has been awhile since this thread; anyone have a screenshot of this working with Hazel rules?

I would like a rule to run that checks under /Downloads/__INDEX for any empty directories and moves them to the recycle bin.
twz2004
 
Posts: 3
Joined: Thu May 25, 2017 1:33 pm

Re: Delete Empty Subfolders Tue Aug 30, 2022 5:07 am • by mabebi
twz2004 wrote:Hey all, I know it has been awhile since this thread; anyone have a screenshot of this working with Hazel rules?

I would like a rule to run that checks under /Downloads/__INDEX for any empty directories and moves them to the recycle bin.


I have the same question; I have a folder with hundrets of subfolders and I want a hazel rule to delete only empty subfolders. How can I do that?

Markus
mabebi
 
Posts: 9
Joined: Sat Jan 15, 2022 6:14 am

Re: Delete Empty Subfolders Tue Aug 30, 2022 8:52 am • by Mr_Noodle
Have you tried the above?
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support

cron