user113531 user113531 - 1 year ago 82
Linux Question

bash script file copied with additional character in filename

I am writing a bash script to copy some config files. I run the file using

sudo bash

cp config/ocr_pattern /usr/share/tesseract-ocr/tessdata/ocr_pattern
cp config/ocr_config /usr/share/tesseract-ocr/tessdata/tessconfigs/ocr_config

However when I view the changes made,
is copied correctly but
is copied with
as the filename instead of
. There is an additional character
behind in the filename for
. What is the issue here?

cat -A

cp config/ocr_pattern /usr/share/tesseract-ocr/tessdata/ocr_pattern^M
cp config/ocr_config /usr/share/tesseract-ocr/tessdata/tessconfigs/ocr_config

Answer Source

As shown by the output of cat -A, you have carriage return (\r) at the end of some lines causing the mentioned issues.

Remove those:

sed -i 's/\r$//'

or just use dos2unix:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download