Special Char in Multiple Files

We develop a file in windows and move to unix box as a part of deployment. When we do this, we get ctrl-M(^M) character added to the file. So we need to remove ctrl-M(^M) character from all the files from deployment folder and all subfolders folder. Currently we move to individual folders and subfolders and run dos2unix command to remove the ^M character. Can you please help us create a script which replaces all the ^M character by running this script.

I am looking for kind of loop which traverse all the subfolders and remove ^M if found.

Are these all only text files? Running dos2unix on, say, image files will destroy them.

This should convert all files in a given folder:

find /path/to/dir/ -type f -exec dos2unix '{}' ';'

Hi,

I have tried this option. Seem good for those file which does contain only text content. For xml files which has special characters like "" are replaced by \357\273\277 character. This is know issue with dos2unix command and this lead to compilation error of XML parser. Can we have better filter which replaces only ^M character with the your command?

Also I need to know how to search and find out files which contain ^M in the list of files from the root to all the subdirectories. The output of the command should be path and file name of the file which contains ^M character.

Thinakar

you can use this command :

tr -d \\r < dosfile > newfile

to replace any character you want.
Perl alternative :

perl -i -pe 's/\r//' file

to find them all : grep -R

I will check your 'tr' command and reply. Can you please give me some quick reply to find out list of files which contains ^M Character? I use following command but not working as expected.

find . -type f -name *.jsp | xargs grep "^M"

"^M" won't work. Try "\r".

For that matter, .jsp won't work either, the shell will expand it instead of find. Try '.jsp'