0

I'm attempting to write a bash script to hide empty folders recursively within the current directory.

This will ultimately be used as part of an Alfred workflow, allowing me to hide/show extra folders in my default project folder hierarchy. The goal is to keep my sanity when reacquainting myself with a project, but keep folder structure in place so I can keep things consistent from project to project.

I've been experimenting with this terminal command

find . -empty -type d -exec chflags hidden {} +

This works in theory, but the problem is Mac OS X adds system files to folders that I'd like to consider empty for my purposes.

How can I ignore files like .DS_Store when hiding directories?

christian
  • 2,279
  • 4
  • 31
  • 42
  • The only possible solution I've come up with is deleting these files first, but then you lose whatever settings they contain. – christian Jul 19 '15 at 16:19

1 Answers1

0

You could do:

find . -type d -exec bash -c "echo -ne '{}\t'; ls -a1 '{}' | wc -l" \; |   awk -F"\t" '$NF<=3{ system("chflags hidden " $1 ) }'

Where $NF<=3 would translate to hide all the folders that have 3 or less items inside, this would typically be:

. 
..
.DS_Store

You can of course replace that part with something more sophisticated, but you should get the idea.

Or you can precise the search to eliminate specific files with grep, e.g.

find . -type d -exec bash -c "echo -ne '{}\t'; ls -a1 '{}' | egrep -v  '^\.(\.?|DS_Store)$' | wc -l" \; |   awk -F"\t" '$NF<=0{ system("chflags hidden " $1 ) }'

This will hide folders only if ls returns nothing else but one or more of (./../.DS_Store), as defined within the condition above.

D.K.
  • 478
  • 2
  • 6
  • Wow, that's great! The only issue I'm having is chflags fails to find folders containing spaces. – christian Jul 19 '15 at 17:05
  • This should solve it: `find . -type d -exec bash -c "echo -ne '{}\t'; ls -a1 '{}' | egrep -v '^\.(\.?|DS_Store)$' | wc -l" \; | awk -F"\t" '$NF<=0{ system("chflags hidden \"" $1 "\"" ) }'` – D.K. Jul 19 '15 at 17:09
  • Thanks for the help! Seems like there's still something going on with recursion. If a folder contains another "empty" folder it is not treated as empty. – christian Jul 19 '15 at 17:15
  • I thought by recursion you meant hiding empty folders from the current level down, not hiding folders with empty subfolders. This should also hide folders with empty subfolders: `find . -type d -exec bash -c "echo -ne '{}\t'; ls -al '{}' | tr -s ' ' | cut -d' ' -f1,9-255 | egrep -v '(^.{10}@.*|^(total|.{11}\.(\.?|DS_Store))$)' | wc -l" \; | awk -F"\t" '$NF<=0{ system("chflags hidden \"" $1 "\"") }'` You might need to run the command a few times to get the whole structure straight though... – D.K. Jul 19 '15 at 18:35
  • Sorry for the confusion. I essentially want to hide any folder structures that do not contain files. This includes "empty" folders. I will update the question to be clearer. – christian Jul 19 '15 at 18:39
  • Yea, I figured later. That's what the code included in my last comment should do. – D.K. Jul 19 '15 at 18:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83682/discussion-between-christian-and-d-k). – christian Jul 19 '15 at 19:09