1

I'm creating a bash script that automatically copies C source and header files from a particular directory in another host to the directory I'm in on the current host.

It currently generates this command: ssh host2 "cd dir1/dir2 && find . -maxdepth 1 -regextype posix-extended -regex '.*.(c|h)' | tar -T - -c -f -" | tar xf -

The problem is I get this error: "tar: This does not look like a tar archive"

This error is output by the final tar (tar xf -) because for some reason the tar file that gets piped is corrupt.

If I do it instead using a combination of ssh, tar and scp it works fine, but that requires me to enter my password (for host2) twice (once for the ssh and once for the scp). I could enter the password as an argument to the script, but I don't want it to appear in my bash history.

Any ideas?

2 Answers 2

3

Have you considered using rsync with appropriate --include and --exclude flags? rsync can copy over ssh, and you should be able to copy the entire tree (or an subset) with a single command.

Also, if your only concern is having to type in your password you could also just try using authorized keys (and possibly ssh-agent) so you don't need to type in your password to access the other host.

1
  • Thanks. I managed to do it with: rsync --include=*.c --include=*.h --exclude=* $SOURCE_HOST:$PATH/* .
    – Tom G
    Sep 30, 2011 at 16:12
2

Why not use rsync to do this job instead? It supports --filter which fits your needs exactly. Here's a sample:

http://ubuntuforums.org/showthread.php?t=1240150

2
  • Thanks. I managed to do it with: rsync --include=*.c --include=*.h --exclude=* $SOURCE_HOST:$PATH/* .
    – Tom G
    Sep 30, 2011 at 16:09
  • It's not working for me: rsync -avz --include=*.h --exclude=* SRC_DIR DST_DIR copies the .h files from SRC_DIR but NOT RECURSIVELY. Applying the --recursive option does not help :(
    – kappa
    Nov 27, 2012 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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