Skip to content

Instantly share code, notes, and snippets.

@abargh
Last active August 24, 2018 06:54
Show Gist options
  • Save abargh/1eb5b57a6e8ba7fa0acb968badc9318e to your computer and use it in GitHub Desktop.
Save abargh/1eb5b57a6e8ba7fa0acb968badc9318e to your computer and use it in GitHub Desktop.
Script to create .gitkeep files in all empty directories within an initialised Git project
#!/bin/bash
# Recursively create .gitkeep files
# Git tracks files, and not directories. This means any empty directories in your source project will be
# omitted whenever the source code is cloned via Git. Sometimes though you want to keep these directories
# (a typical example being when you want to a directory to hold log files or some other runtime artefacts).
# To preserve empty directories within a Git project, users typically create a hidden `.gitkeep` file and commit
# that file to source control for each empty directory they wish to keep. Creating these by hand can be a bit of
# a pain though.
# To save you from having to create these manually, execute the following bash snippet from within the root of
# you initialized git project:
find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \;
# The snippet will create a .gitkeep file in all empty subdirectories of your project (excluding the .git/
# repository database) so you don't have to.
# As you can probably work out, find starts in the current directory and looks for all directories (-type d)
# that are empty (-empty) but are not in the `.git` directory (-not -path "./.git/*") and then touches a
# `.gitkeep` file within discovered directory (-exec touch {} /.gitkeep \;)
# Modified from: http://cavaliercoder.com/blog/recursively-create-gitkeep-files.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment