I am trying to unzip folder using shell script file(.sh). it is extracting files in the zip folder, but my destination directory created ends with ^M,

I am using this command in .sh file

unzip /tmp/mram/zip/ApNonProjectExpVO.zip -d /tmp/mram/zip/

and showing logs in putty as


Tue Jan 10 23:21:21 CST 2017

Archive:  /tmp/mram/zip/ApNonProjectExpVO.zip
   creating: /tmp/mram/zip/^M/ApNonProjectExpVO/
  inflating: /tmp/mram/zip/^M/ApNonProjectExpVO/ApNonProjectExpVO.xml
  • 1
    That sounds like the problem was caused at the zip creation, not during extraction. – Julie Pelletier Jan 11 '17 at 5:51
  • 3
    There is likely a folder inside the zip file with that name. – Dan Jan 11 '17 at 6:02
  • Do you have the same output when extracting with Winzip/7zip in Windows? – Zumo de Vidrio Jan 11 '17 at 7:58
  • 1
    @JuliePelletier, Dan: No, the CR evidently comes from the unzip line in the script. – Gilles Jan 11 '17 at 22:51
  • @Gilles: Nice catch! – Julie Pelletier Jan 12 '17 at 3:29

The argument of the -d option tells unzip in what directory to put the file. Since you told unzip to put the file in a directory called /tmp/mram/zip/^M (where ^M is a carriage return), it did exactly what you told it to do.

Fix your script by removing all the CR characters at the end of lines, as they're likely to cause other problems. In Unix, CR is an ordinary character, not part of an end-of-line marker like in Windows.

When you edit shell scripts under Windows, always remember to configure your editor to use Unix line endings (LF only).

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.