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?
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.