Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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
find . -type d -empty -exec touch {}/empty \;
share|improve this answer
This should be the accepted answer. I tried both this and what Ignacio wrote, and this one works. The other one uses the -execdir option, which always runs the command at the directory containing the file. Since the "file" is an empty directory, it runs touch empty at the directory containing the empty directory, not the empty directory itself. Rick got it right in his answer. – tralston Jun 11 '15 at 18:14

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

Your Answer


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.