Moving a Windows .txt file to a Linux Server

Problem scenario:  When you move a Windows .txt file to a Linux server, new characters can be introduced.  For example, the content of the file can have a "^M" (with no quotes) at the end of every line.  Sometimes tr, sed, and awk won't work to remove this new jibberish (extraneous characters).  Moreover, sometimes the substitute command in vi will fail to do anything about these extraneous characters (^M).  How do you eliminate these extra characters at the end of every line? 

Solution:  Let's assume the file is named foo.txt.  Run this command:
perl -p -i -e "s/\r//g" foo.txt
It's as simple as that assuming you have write permissions to the file foo.txt.

--------------

Problem scenario:  When running a Bash script, you receive this message:

" /bin/sh^M: bad interpreter: No such file or directory"

Root cause:  The script was being housed on a Microsoft Windows file share or Windows folder.

Solution: First, install dos2unix.  Secondly, run it like this where foobar.sh is the script that is having the problem:

dos2unix foobar.sh

Leave a comment

Your email address will not be published.