why does this tar command fail when passed as string in ssh call?

1.2k Views Asked by At
ssh user@server "sudo tar -czf - -C /path/To/Directory *.properties" | tar xzf -

It fails with following error -

tar: *.properties: Cannot stat: No such file or directory

tar: Exiting with failure status due to previous errors

If I ssh to server and then on server, instead of tar -czf - use tar -czf abc.tgz and remove the piped command at the end, it works correctly i.e. compress all the .properties file from the directory?

I have tried using --wildcards parameter as well as -P and the complete path ending with *.properties, but they didn't work either.

1

There are 1 best solutions below

5
On BEST ANSWER

The shell at the far end expands *.properties in user's home directory, before it execs tar. I'm guessing there's no matching files in ~user.

What you probably want is something more like (assuming user can access the directory)

ssh user@server 'cd /path/To/Directory && sudo tar -czf - *.properties' \
    | tar xzf -

Or, use tar's --wildcards option, and stop the remote shell expanding the argument:

ssh user@server 'sudo tar -czf - -C /path/To/Directory --wildcards \*.properties'