Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to use PuTTY to connect via SSH to a Mac and download some files specified using a find command. The files have spaces in them that may be causing issues and I'm not sure how to specify a local Windows path.

I'm trying something like this, unsuccessfully so far:

find . -iname "*searchtext*" | xargs -0 scp user@host:{} localpath

Any ideas?

If it's not possible using one line, I don't mind a Bash script that can be called via PuTTY?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You are best off just creating a zip or tgz of the files on the remote server

zip found-files.zip $(find . -iname "*searchtext*")

And then just use winscp to download the found-files.zip file. Winscp will authenticate the same as putty (passwords or uses pagent if running). If you prefer the dos command prompt you can use pscp

share|improve this answer
    
(virtual +1) @Sodved Thanks. I went for the zip solution. Had to change it to deal with spaces issue. @grawity's suggestions helped. find . -iname "searchtext" -print0 | xargs -0 zip found-files.zip and then pscp user@host:found-files.zip <localpath> –  Vivek Ayer Apr 7 '11 at 16:23

I'm trying to use PuTTY to connect via SSH to a Mac and download some files specified using a find command.

Are you trying to download the files to your local Windows system?

Remember that PuTTY is a SSH client. All it does is send typed text to your Mac and display received output. Therefore, when you run scp like this, it runs on the Mac and the file names must be specified as seen by the Mac.

If you want to transfer files from Mac to Windows, then it's scp macpath user@windowshost:windowspath. (You seem to have this reversed in your example command.)

This also means that your Windows computer needs a SSH server installed with SCP capabilities, and that it must allow incoming SSH connections.


I'm not sure how to specify a local Windows path.

It depends on the SSH server that you installed on Windows. (See paragraph above.) But usually, x:\dir\file can be converted to /x/dir/file. Read the manual.


The files have spaces in them that may be causing issues

Yes, this causes problem with xargs. You went to the right direction with xargs -0, but you need to do the same with findfind .... -print0 or find .... -printf "%p\0".

If the version of find in Mac OS X supports neither -print0 nor -printf, you can get away with using newlines as separators. In this case, do not add any special options to find, but change xargs -0 to xargs -d '\n'.

A better alternative to xargs is find -exec.


xargs -0 scp user@host:{} localpath

There is one more mistake here. By default, xargs does not replace {} with the file name; you have to specifically tell it to, using -I "{}".

You might have confused this with the -exec option in find, which does work like this:

find . -iname "*searchtext*" -exec scp user@host:{} localpath \;

Above, I left scp argument order exactly as it was in your original example. But like I said before, it's incorrect.

share|improve this answer
    
Thanks for the detailed explanation. Yes I'm trying to download files to the local Windows system. So I guess my approach is wrong. Is there a way for scp (or pscsp.exe in my case) to copy files that are outputted from a find command executed remotely? Or is there any other way to achieve this? –  Vivek Ayer Apr 7 '11 at 14:59
    
@Vivek: You could use psftp.exe in batch mode: 1) create a file with the results: find .... -print | sed 's/^/get /' > psftp-batch.txt 2) copy the file to Windows machine and run psftp -b psftp-batch.txt -bc user@machost. –  grawity Apr 7 '11 at 15:26
    
Cheers. (virtual +1) –  Vivek Ayer Apr 7 '11 at 16:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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