Remove ^M charecters in all files

Hi,

I wish to remove special charecters at the end of each line "^M" from all files under a folder and sub-folders.

I do not seem to have dos2unix or Perl and my OS is Linux mymachine 2.6.32-431.5.1.el6.x86_64 #1 SMP Fri Jan 10 04:11:43 IST 2014 x86_64 x86_64 x86_64 GNU/Linux

Hello mohtashims,

Following may help you in same.

tr -d '\r' < Input_file >Output_file

Thanks,
R. Singh

This may work on a single file .... my requirement is that the command should traverse all directories and sub-directories for all files and remove the special characters.

Are these files being delivered by FTP? Perhaps you could force the sending option to ASCII to see if that helps prevent this in the first place.

Kind regards,
Robin

I zipped the folder with all files on Windows & used WinScp with SFTP and SCP protocols to upload them to Linux where i see these specil characters. Any guesses ?

Try (using GNU sed) :

find /path/to/folder -type f -exec sed -i 's/\r$//' {} +
1 Like

This works as champ !!

Careful, it will just go ahead and modify any file underneath that directory...
I made a small modification in post #6, which should speed up the command..

if is just 1 file you can just use vi itself and remove it.

:%s/^M//g

then to input the control character with the letter m use left hand depress the ctrl key button then other hand type vm

do not type in the the carrot key ^