How to remove carriage-return character in multiple files

I was trying to move several PHP files from and old REDHAT5 server to new REDHAT7 server. What we saw was that in every file a ^M character was present.

This was ok on the old server, but everything crashed on the new server.

This is how I removed this carriage-return character from every file:

First find all php files:

find . -type f -name '*.php'

then use sed to replace each character:

sed -i 's/\x0d/\n/g'

All put together:

for i in `find . -type f -name '*.php' | sed 's/\.\///g'`; do echo "sed -i 's/\x0d/\n/g' " $i; done

The command above will print out the necessary commands that you can run directly:

sed -i 's/\x0d/\n/g' finnfeleskrin.php
sed -i 's/\x0d/\n/g' search_oeld.php
sed -i 's/\x0d/\n/g' litt_test.php
sed -i 's/\x0d/\n/g' finnfele.php
sed -i 's/\x0d/\n/g' manager/bibliografi.php
sed -i 's/\x0d/\n/g' manager/list.php
sed -i 's/\x0d/\n/g' manager/linklist.php
...

or send them to a bash script. For instance:

for i in `find . -type f -name '*.php' | sed 's/\.\///g'`; do echo "sed -i 's/\x0d/\n/g' " $i; done > runme.sh; ./runme.sh

Leave a Reply

Your email address will not be published. Required fields are marked *