How do I scp all files except for ... ?
Hi, don't really know where to ask this question, so I'm putting it here (after all, I am fairly new).
Ok, so. I want to `scp' a whole bunch of files to another server : $ scp * username@otherserver:/dir/dir/dir/ ... which works fine. Next, I want to do the same thing but for all files EXCEPT for the files with 'foo' or 'bar' in the name. After sniffing around a while I found several different solutions one of them was to pipe ls into grep -v ... twice: $ ls | grep -v 'foo' | grep -v 'bar' file.1 lala.2 ... yadda.3 Ok, that worked great! Now let's see if I have this `command substitution` thing worked out: $ echo `ls | grep -v 'foo' | grep -v 'bar'` file.1 lala.2 ... yadda.3 Hooray! So now I can do my scp ... $ scp `ls | grep -v 'foo' | grep -v 'bar'` username@otherserver:/dir/dir/dir/ file.1: No such file or directory lala.2: No such file or directory ... yadda.3: No such file or directory : No such file or directory So, what went wrong? How can I copy a bunch of files except for those with foo or bar in the name? Is scp just annoying like this and I should be using something else?? At the moment I'm moving the foos and bars into a temporary directory, scp-ing everything, then moving them back, but after the tenth time it starts getting a bit tedious. |
I don't think scp alone can do what you ask.
Perhaps you can use rsync Kind regards |
Hi and welcome to LinuxQuestions!
I tried you code and it works for me. Anyway you can try some alternatives: Code:
find . -maxdepth 1 -type f -not \( -name \*foo\* -o -name \*bar\* \) -exec scp {} username@otherserver: \; Code:
find . -maxdepth 1 -type f -not \( -name \*foo\* -o -name \*bar\* \) | xargs -I{} scp {} username@otherserver: You can also try your ls + grep commands: Code:
ls | grep -v 'foo' | grep -v 'bar' | xargs -I{} scp {} username@otherserver: |
For some reason, xargs didn't like the -I option ... which is odd since it exists in the usage.
Quote:
$ scp `find . -maxdepth 1 -type f -not \( -name \*foo\* -o -name \*bar\* \)` username@otherserver:/dir/dir/dir/ SUCCESS! Maybe it was the ./ before the filesnames that did the trick. Thank you! |
Actually the -I option is lowercase in your version of xargs. Are you running a Linux OS or some othe *nix flavor?
Regarding the command substitution using find in place of ls + grep, it should not make any difference. Indeed the ls + grep worked on my system. FYI a more tricky find command to allow -exec with multiple arguments (only one scp command executed) is: Code:
find . -maxdepth 1 -type f -not \( -name \*foo\* -o -name \*bar\* \) -exec sh -c 'scp "$@" username@otherserver:/dir/dir/dir/' sh {} + Code:
-exec command \; |
All times are GMT -5. The time now is 02:46 PM. |