I am using the command cp error.log error$HOSTNAME.log

the command works properly while running it separately in a server, but if it is run as script which uses ssh I am getting a ? appended to the filename. In the script I am copying an error log locally using ssh from other server and in the name of arerro$HOSTNAME.log but this copies as arerror$HOSTNAME.log?

  • Try scp instead of ssh and cp – Milind Dumbare Mar 16 '15 at 14:36
  • 2
    Use dos2unix on your script. And is it really error one place, then arerro, then arerror?! Details like this can matter a lot. – wurtel Mar 16 '15 at 15:39

It's highly likely that the file name does not in fact contain a question mark. Rather, the ls command displays this instead of a non-printable character. Given that this character comes at the end of a file name which you specified in your script, it's probably a carriage return.

Why would you have a carriage return in your script? Because you edited it in a Windows editor. Windows represents newlines with the two-character sequence CR, LF (carriage return, line feed), whereas Unix uses just the LF character. So from Unix's perspective your script contains a line on which the last character is the ordinary character CR.

Either edit your script with a Unix editor, or use a Windows editor that allows you to save with Unix line endings.

To fix the files that you've already created, you can use the following command:

cr=$(printf '\r')
find . -name "*$cr" -exec sh -c 'for x do mv "$x" "${x%.}"; done' _ {} +
  • Thanks alot Gilles!! I will try to use a unix editor and will update the result. – Hussain Kaja Mar 17 '15 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.