Convert text files within a directory from Windows to Unix format

by
on
January 7, 2009

When a file is saved in Windows and then moved to a Linux system the formatting differences can cause a variety of problems. To effectively use these files you will need to change the format from Windows/DOS to Unix. This conversion occurs by simply removing the Windows carriage return characters.

I have explained how to use the tr command remove these windows carriage returns, but when you have a large amount of files to convert this can become tedious. As a solution to this I have written a BASH script to convert all text files within a directory.

#!/bin/bash

for file in /directory/to/convert/*
do
  if [[ -f $file && `file $file | grep text` ]]
  then
    tr -d '\r' < $file > "$file"_clear
    mv "$file"_clear $file
  fi
done

1 Comment
bash
, , , , , , , ,

Related posts:

  1. Remove Windows carriage returns with tr
  2. Convert PDF file to text with pdftotext
  3. Convert a relative path to absolute path in BASH
  4. Have a Bash script determine it’s own location
  5. Delete a specific line from a text file with sed

Comments (1)

recursive (script name must be kk.sh or change the name below):

for file in “$1″*; do
if [[ -f $file && `file $file | grep text` && $file != "kk.sh" ]]; then
tr ‘\r’ ‘\n’ “$file”_clear
mv “$file”_clear $file
elif [[ -d $file && $file != "kk.sh" ]]; then
chmod +x kk.sh
echo “$file/”
./kk.sh “$file/”
fi
done

Trackbacks (0)

No trackbacks yet

Leave a Comment

(displayed with your post)
(will not be published)
(optional)

Copyright 2008-2010 WiredRevolution.com. All rights reserved.