Skip to content

Instantly share code, notes, and snippets.

@taktran
Created May 10, 2018 13:58
Show Gist options
  • Save taktran/32ae9d551d99416b0f26fdf93fef001d to your computer and use it in GitHub Desktop.
Save taktran/32ae9d551d99416b0f26fdf93fef001d to your computer and use it in GitHub Desktop.
Update indent of files from 4 to 2 spaces
#!/bin/bash
#
# Usage:
#
# Find all files
#
# ls -1 $PWD/*.js > files.txt
#
# Run script
#
# ./update-indent.sh files.txt > files-output.log
#
# Based on https://superuser.com/a/860725/28191
while read filename; do
if ! [[ -r "$filename" ]]; then
echo "Skipping '$filename' because not readable"
continue
fi
tempfile="$(mktemp)"
if perl -ne '$_ =~ s|^(( )+)|" " x (length($1)/4)|eg; print $_' < "$filename" > "$tempfile"; then
# Uncomment if you want the original file
#mv "$filename" "$filename".orig
mv "$tempfile" "$filename"
echo "Success processing '$filename'"
else
echo "Failure processing '$filename'"
fi
done < "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment