Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created December 18, 2025 13:13
Show Gist options
  • Select an option

  • Save atifaziz/abfd5667937283c585d788b84b4f925e to your computer and use it in GitHub Desktop.

Select an option

Save atifaziz/abfd5667937283c585d788b84b4f925e to your computer and use it in GitHub Desktop.
Bash script to lint Git-tracked text files for final newline character
#!/usr/bin/env bash
#? Lint Git-tracked text files for final newline character
set -eo pipefail
declare -i errcnt=0
declare -i total=0
declare -i done=0
while IFS= read -r file; do
if [ "$file" = "/" ]; then
done=1
break
fi
# Check if the file is non-empty via `-s` and does not end with a newline
# character. `tail` command gets the last byte, `od` converts it to hex,
# `tr` removes spaces Compare against `0a` which is the hex code for
# newline.
if [ -s "$file" ] &&
[ "$(tail -c1 "$file" | od -An -tx1 | tr -d ' ')" != "0a" ]; then
echo >&2 "$file"
errcnt=$((errcnt + 1))
fi
total=$((total + 1))
done < <(
git ls-files --eol | # List all tracked files with their EOL status
grep -vE '\b[iw]/-text\b' | # Exclude files marked as binary or with no EOL normalization
cut -f2 | # Extract the file paths
grep -vE '^(data|experiments|spikes)/' # Exclude specific directories
echo /
)
if [ "$done" -eq 0 ]; then
echo >&2 "File enumeration was not completed."
exit 1
fi
echo >&2 "$total"' files checked, '"$errcnt"' files missing a trailing newline.'
[ "$errcnt" -eq 0 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment