Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to copy all files that do not begin with the letter "a", in ksh.

Copying from the source machine to destination machine is working fine:

scp -p !(a*) user@machine:/path/directory/.

But, if I am on the destination server and want to copy from the source server, is failing:

scp -p user@machine:/path/!(a*) .

Any ideas?

share|improve this question

3 Answers 3

I agree with vonbrand. Globing is handled by the shell you're running in. So scp user@machine:/path/* will expand /path/* to the files in the LOCAL /path/*, not on the remote machine.

However, just for grins I tried:

scp "user@machine:/path/[regex here]" .  

and .... it worked. Try that. Note the quotes. Very necessary. Let us know.

Note: the user account on the remote machine may default to a different shell program than yours, so the rules of wild card expansion may be different, so 'ware that.

share|improve this answer

This isn't "scp with regular expressions", the one handling the file globs is the shell. scp just gets handed the already expanded list of files. And the expansion happens on the machine where the command is run.

Sorry, can't (easily) do.

share|improve this answer

Remote names passed to scp are actually interpreted as a whitespace-separated list of patterns on the remote side. This is often an annoyance when you try to copy a file whose name contains spaces, but here it's useful: scp -p 'user@machine:/path/a*' . would copy all files whose name begins with a.

Your command will work if you quote the pattern so that it's interpreted remotely rather than locally, provided you pass another hurdle. The pattern !(a*) is not a basic shell pattern, it's a ksh extension (that bash and zsh also support if you set the appropriate option). So this will only work if your remote shell is ksh, not if it's some other shell and you exec ksh from .profile or something similar.

The easy way, unless you're in some kind of restricted or antique environment, is to forget about scp. You'd like remote files to work just like local files, so make them local files: mount the remote directory with sshfs. This requires FUSE on the local machine and an SFTP server (i.e. an sftp-server executable that sshd launches) on the remote machine.

mkdir ~/net/machine
sshfs user@machine:/ ~/net/machine
cp -p ~/net/machine/path/!(a*) .
share|improve this answer

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.