ssh Mac Unix: copy a file with special characters and spaces

2

I am remote logged into a Mac and trying to copy a music file from one directory to another, but I am getting an error. I think it's caused by spaces or special characters, but not sure.

the file is:

01 Your Everything (feat. Louisahhh!).m4a

I tried a few variations of the following to cp the file (this is just a test to rename):

cp 01\ Your\ Everything\ (feat.\ Louisahhh!).m4a dd.m4a

I am getting the following error:

-bash: !: event not found

Thanks for reading!

Mrwolfy

Posted 2013-01-12T02:51:28.683

Reputation: 135

1Ahh, wait adding escape characters to the ! and () characters did the trick. – Mrwolfy – 2013-01-12T02:54:14.717

2brackets should be fine, but it don't like that bang! I was going to suggest using autocomplete (<tab>) to see what the system thinks it's called, but no need now. Maybe next time – mcalex – 2013-01-12T02:56:40.487

1Thanks. I'll give it a try tomorrow, but escaping the brackets as well as the bang seemed to work. – Mrwolfy – 2013-01-12T04:23:35.560

Next time just do cp "long filename with special characters.m4a" dd.m4a – terdon – 2013-01-12T05:37:08.930

2@terdon: double-quotes won't work, since it still interprets history substitutions (!) in them -- but single-quotes would do the trick. – Gordon Davisson – 2013-01-12T06:20:55.737

Answers

3

You need either to enclose your filename in single quotes ' or to escape all the special characters.

  • In your example spaces, parenthesis and the quotation mark need to be escaped. See this list or the shell manual page for the list of special characters and their meaning.

    01\ Your\ Everything\ \(feat.\ Louisahhh\!\).m4a 
    
  • Or you can use single quotes ' (not double quotes since several characters are still interpreted).

    '01 Your Everything (feat. Louisahhh!).m4a'
    

    Keep in mind that you if you have a single quote in a file you will have to quote it differently. For a file called It's done:

    'It'\''s done'
    

Matteo

Posted 2013-01-12T02:51:28.683

Reputation: 6 553

+1. You can simplify the last example a bit: 'It'\''s done' – glenn jackman – 2013-01-12T11:39:34.480