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 want to maintain a folder structure that contains empty folders when I add a project to Git. As everyone knows, Git doesn't track empty folders. So you need to add some kind of placeholder inside of an empty folder.

So far I have managed to find out recursively empty folders with this command:

find ./* -type d -empty

How do I continue? So after a command every empty folder contains a file called "empty"?

share|improve this question
add comment

3 Answers 3

Run touch empty in the directory.

find ... -execdir touch empty \;
share|improve this answer
    
That one above only added empty on "." (current) folder. I'm running this on Linux if that helps...? I ran across to a blog post which claimed that there are some differences between MacOSX and Linux. Here: aplawrence.com/Unixart/find_execdir.html –  hemppa Jun 11 '13 at 20:30
    
Are you running the command exactly as Ignacio wrote? I think he means for you to run find . -type d -empty -execdir touch empty \; –  evilsoup Jun 11 '13 at 21:01
    
thanks for clarification. I was doing it wrong, but still it won't add that empty file to all folders... actually it adds it only in "."-folder and "./level1/"-folder, which is btw not empty. This is strange, find . -type d -empty displays all correct folders though.. –  hemppa Jun 11 '13 at 21:23
    
I've just tested it, and I'm getting the same behaviour... bizarrely, it seems to be touching the 'empty' files in the directory one above the empty directories. –  evilsoup Jun 11 '13 at 21:56
add comment

Mandatory 'you can do this with bash' answer:

shopt -s globstar nullglob
for f in ./**/; do [[ $(echo "$f"*) == '' ]] && touch "$f"empty; done

Globstar means that ** expands into 'every file in this directory, recursively'; **/ means that it will only expand to directories, rather than regular files (since filenames can't contain forward-slashes).

Nullglob means that globs which don't match anything expand to a nullbyte. The test checks if this happens (which would mean that there's nothing in the directory); if the test evaluates to true (which means there's nothing in the directory), then it touches empty in the directory.

share|improve this answer
add comment
find . -type d -empty -exec touch {}/empty \;
share|improve this answer
add comment

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.