Skip to content

Instantly share code, notes, and snippets.

@aoki
Last active June 22, 2020 01:26
Show Gist options
  • Save aoki/7939324 to your computer and use it in GitHub Desktop.
Save aoki/7939324 to your computer and use it in GitHub Desktop.
Create `.gitkeep` files recursive.
#!/bin/bash
if ! git rev-parse 2> /dev/null; then
echo -e "\033[0;31mERROR\033[0;39m: This directory is not git repository \033[0;31m:(\033[0;39m"
exit 1
fi
Seek() {
for DIR in * ; do
if [ -d "${DIR}" ]; then
if [ -f "${DIR}/.gitkeep" ]; then
echo ".gitkeep already exists .gitkeep in `pwd`/${DIR}"
elif [ -z "$(ls -A ${DIR})" ]; then
touch ${DIR}/.gitkeep
echo "Create .gitkeep in `pwd`/${DIR}"
fi
(cd "${DIR}"; Seek;)
fi
done
}
Seek;
echo -e "done!! \033[0;32m:)\033[0;39m"
@pearj
Copy link

pearj commented Jun 22, 2020

This works great if you're inside busybox and your find command doesn't support searching for empty directories.

Otherwise if you have a modern find command use:

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \;

Taken from: https://stackoverflow.com/a/31249949

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment