Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to ease scp for files with odd names UNIX
Here's a perl script that will escape filenames and send the files to a remote server using scp. I save it as scp-to-coppit.org in my user's bin folder, and make it executable (chmod a+x scp-to-coppit.org). Then from the command line, I can do this: scp-to-coppit.org File with weird char's.txt. Here's the code:
#!/usr/bin/perl

@ARGV = map { s/'/\\'/g; $_; } @ARGV;
my $files = "'" . join("' '", @ARGV) . "'";
my $results = `/usr/bin/scp -rBq $files dcoppit\@coppit.org:.`;
print "Output: $results" if $results ne '';
[robg adds: I haven't tested this one.]
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[8,804 views]  

A script to ease scp for files with odd names | 12 comments | Create New Account
Click here to return to the 'A script to ease scp for files with odd names' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to ease scp for files with odd names
Authored by: marroug on Sep 13, '07 08:28:48AM
Can you clarify this fun bit of perl/grep: map { s/'/\'/g; $_; } @ARGV

[ Reply to This | # ]
A script to ease scp for files with odd names
Authored by: jeremyp on Sep 13, '07 09:17:01AM

It's just escaping all the single quote characters in the string.



[ Reply to This | # ]
A script to ease scp for files with odd names
Authored by: jeremyp on Sep 13, '07 09:18:56AM

Clarification:

It's escaping all the single quote characters in each string in the array @ARGV



[ Reply to This | # ]
A script to ease scp for files with odd names
Authored by: merlyn on Sep 13, '07 08:45:14AM

I really wish people understood that in Perl's "map", the $_ is an alias to the original data. And best practice is never to alter $_ inside that block, because you're altering the original data, turning it from functional programming to imperative programming. You could leave the assignment to @ARGV out, and the result would still be the same! Spooky, eh? That's why you don't do that.



[ Reply to This | # ]
A script to ease scp for files with odd names
Authored by: merlyn on Sep 13, '07 08:47:32AM
This whole script can be replaced with (much more safely, as spaces and vertical bars and all other shell metachars won't be troublesome):

exec "/usr/bin/scp","-rBq", @ARGV, "dcoppit\@coppit.org:.";
It's an example of over-engineering with under-results. :)

[ Reply to This | # ]
A script to ease scp for files with odd names
Authored by: 6GTXEK on Sep 13, '07 08:54:42AM

This seems like a quite an odd and dangerous thing to do. It's also fairly broken: it won't work with filenames that have multiple spaces in a row, double quotes, or backslashes in them. And if you have a pipe character, you're in real trouble. That's just at first glance; it probably has plenty of other issues.

I don't understand why you would type out that whole filename and go to the trouble of writing a script to escape it. Either drag-and-drop the file from the Finder into a Terminal window, which will paste the name nicely escaped for you, or use tab completion, which, again, should auto escape the filename. And even if you're dead-set on typing a long filename, it's not that hard to understand the shell's quoting rules and do it correctly yourself.



[ Reply to This | # ]
A script to ease scp for files with odd names
Authored by: Anonymous on Sep 13, '07 10:22:19AM

...or use wildcards for the weirdnesses.



[ Reply to This | # ]
Simpler and safer
Authored by: SOX on Sep 13, '07 08:59:43AM
A much simpler and much safer way: use "system"

System is safer because it enforces that there cannot be any command injection loopholes in the args and also it does not need to escape the meta chearaters.


perl -we 'system( "scp", "servrer:file", @ARGV)'


[ Reply to This | # ]
A script to ease scp for files with odd names
Authored by: TGV on Sep 13, '07 12:30:03PM
scp-to-coppit.org File with weird char's.txt
No, you can't. Not in the shell anyway, since it will look for the closing quote and not include the quotes in the text. In this case, the shell will simply give you the secondary prompt and wait until you've typed another quote or escaped your way out (^G, ^C). And if you've typed the second quote, the error is most likely going to be "file not found".

According to me, by I'm a command line user since the early 80's, it's much easier and safer to use the built in completion, e.g. using the tab key.

Suppose my directory contains these three files

  • File with weird char's.txt
  • File with another weird@character.txt
  • anotherfile.txt

Then typing

$ scp F<TAB>

will show

$ scp File\ with\

because that's the unique starting part. The choice has now boiled down to the first two files. At that point, typing w and another TAB will fill in the entiry name, resulting in

$ scp File\ with\ weird\ char\'s.txt

an you're ready to type the destination. Or you could make the script with the destination in it, but without the weird and potentially dangerous argument mangling.

And it is dangerous, since the arguments are joined with single spaces and thus (under bizarre circumstances admittedly (*)) you could be overwriting an unwanted file at the destination.

(*) Think of file names with more than one consecutive space and differing with another file in only the spaces, not to mention files with backslashes in their name...

[ Reply to This | # ]

A script to ease scp for files with odd names
Authored by: rjbailey on Sep 14, '07 10:17:36AM

Then how to do batch scp transfers, e.g. "scp *.txt" and the like? Tab-completion won't work in those cases.



[ Reply to This | # ]
A script to ease scp for files with odd names
Authored by: TGV on Sep 14, '07 11:00:49PM

That's true, but the original script doesn't do that either: if you type a * in calling, it will be expanded to contain all files in the directory.

If you want to do that kind of thing and have trouble using the command line, I recommend something like CyberDuck or Yummy FTP, which can (apparently) do FTP over SSH (or actually, SFTP, which is part of it).



[ Reply to This | # ]
A script to ease scp for files with odd names
Authored by: fastolfe on Sep 30, '07 09:03:24AM

Do not use this. Learn how to quote in your shell:

scp "File with weird char's[sic].txt" destination:
scp 'He said, "She said."' destination:
scp '$$$ MAKE "MONEY" FAST $$$.txt' destination:

Single- and double-quotes wrap the argument so that spaces don't break it up into multiple arguments. Or use backslashes to escape the problem characters specifically:

scp one\ two\ three\ four.txt destination:



[ Reply to This | # ]