Monday, April 11, 2022

Using SCP with Filenames That Contain Special Characters

 Are you having trouble using scp to transfer files that have special characters? The error looks something like this:

syntax error near unexpected token `('

Your next move is to try escaping those special characters with a '\' preceding each one. But you still may get the same error.

Then you may try to put the filename and path in single quotes. Alas, the error persists. After some searching on the interwebs, you find that nested quotation marks may be the answer. Something like this:

scp joe@remote.server.com:"'/home/joe/Documents/This is a really stupid way to name files.txt'"

Now you may get the following error because of security updates to software that added a check for nefarious commands.

protocol error: filename does not match request

However, along with the update, there was added a '-T' parameter to scp command to work around this added check, for use in cases where it is preventing WANTED files from being transferred.

So a nested quote with -T will do the trick, thusly:

scp -T joe@remote.server.com:"'/home/joe/Documents/This is a really stupid way to name files.txt'"