Skip to content

Instantly share code, notes, and snippets.

@wteuber
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wteuber/7696eb4138b616daeb0d to your computer and use it in GitHub Desktop.
Save wteuber/7696eb4138b616daeb0d to your computer and use it in GitHub Desktop.
No newline at end of file? These bash snippets will help you add new line at end of files.
#define file extensions that should be regarded
EXT=(css ejs erb gemspec gitignore html js json md mustache rake rb rdoc rspec ru scss sh sql txt ui xml xsd yardopts yml)
EXT=$(printf "\|%s" "${EXT[@]}")
EXT=${EXT:2}
# git:
git ls-files "*.*" | grep "\.\($EXT\)$" | sed "s/^/\'/; s/$/\'/;" | xargs sed -i -e '$a\'
# plain:
find . -name "*.*" | grep "\.\($EXT\)$" | sed "s/^/\'/; s/$/\'/;" | xargs sed -i -e '$a\'
# Explained:
# EXT=(ext1 ext2 ...) # Array of strings that will be used as file extensions
#
# EXT=$(printf "\|%s" "${EXT[@]}") # Join elements with '\|' for file name filtering later
#
# EXT=${EXT:2} # Prepend first element of file extensions array (without leading '\|')
#
# find . -name "*.*" # List all files recursivly that contain a '.'
# git ls-files "*.*" # List all tracked files recursivly that contain a '.'
#
# grep "\.\($EXT\)$" # Filter output for defined file extensions
#
# sed "s/^/\'/; s/$/\'/;" # Quote file names
#
# xargs sed -i -e '$a\' # Actually add "\n" at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment