Removing Carriage Returns from file after transfer to Unix/Linux

Carriage returns will sometimes be present after cross plateform transfer of files from various operating systems. They are normally represented in UNIX and Linux as a ^M marker at the end of each line. This can cause issues in the execution of shell scripts, sql scripts and be interpreted as actual text in file. There are various methods to remove these carriage returns, but I find the easiest method is a Perl call, because there are vary few UNIX/Linux plateforms that do not support Perl natively.

To remove all carriage returns in all the files located in the present directory use:

# all files in present directory

perl -p -i -e 's/\r//g' *

To remove all carriage returns in a single file use:

# specific files 


perl -p -i -e 's/\r//g' <file_name>

Leave a Reply