Remove CR from CRLF

GIT does not accept text files in DOS style with CRLF characters at the end of the lines. Although it’s possible to configure repository to auto-convert line endings, the files in your local copy will remain in DOS-style. The script below, assuming it’s saved as, will prepare your files by removing CR character.

It can be triggered in two ways:

conversion of a single file style.css

conversion of multiple files

ls *.php | xargs

Copy the test below and save it as file in a directory in your PATH. Make sure it’s executable

chmod +x

and run it from any place


# remove CR from the given file(s)
die () {
 echo >&2 "$@"
 exit 1
if [ -z "$1" ]
 die "Usage: `basename $0` file_to_convert \n You can also xargs multiple file by: ls *.php | xargs `basename $0`"

for toconv in "$@"
 cat $toconv | tr -d '\015' > $toconv.tmp
 rm $toconv
 mv $toconv.tmp $toconv

Leave a Reply

Your email address will not be published.

Please, enter correct number below to prevent spam (required)