Skip to content

Instantly share code, notes, and snippets.

@SebastiaanDeJonge
Last active June 1, 2020 13:43
Show Gist options
  • Save SebastiaanDeJonge/fcd55831e70de6f2435cae3d0432d629 to your computer and use it in GitHub Desktop.
Save SebastiaanDeJonge/fcd55831e70de6f2435cae3d0432d629 to your computer and use it in GitHub Desktop.
A quick bash script to git-keep all subdirectories of the given directory by adding .gitkeep when no contents are present.
#!/usr/bin/env bash
files_created=0
echo_usage()
{
echo "Usage: keep-everything.sh /path/which/to/look/in"
echo ""
}
echo_green()
{
echo -e "\033[32m${1}\033[0m"
}
echo_red()
{
echo -e "\033[31m${1}\033[0m"
}
traverse_directory()
{
entries=0
for d in "$1"/*
do
if [[ -d ${d} ]]; then
traverse_directory ${d}
entries=$((entries+1))
elif [[ -f ${d} ]]; then
entries=$((entries+1))
fi
done
if [[ ${entries} == 0 ]]; then
ignore_file="${1}/.gitignore"
if [[ ! -f $ignore_file ]]; then
touch ${ignore_file}
echo_green "Created '${ignore_file}'"
files_created=$((files_created + 1))
fi
fi
}
if [[ "${1}" == "" ]]; then
echo_usage
echo_red "ERROR: No path given!"
exit
fi
if [[ ! -d ${1} || -r ${d} ]]; then
echo_usage
echo_red "ERROR: Directory not valid or not readable!"
exit
fi
echo "Git-keeping all directories inside ${1}..."
traverse_directory $1
if [[ ${files_created} == 1 ]]; then
echo_green "Created 1 .gitignore file"
else
echo_green "Created ${files_created} .gitignore files"
fi
echo "All done!"
@SebastiaanDeJonge
Copy link
Author

Doesn't check for hidden files (yet)

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