Pregunta

I'm passing a tar command to shell executor in an application. But it seems that my tar syntax is incorrect. (This is Windows (bsdtar command) but works the same as Linux as far as I know; I can also test on Linux if need be.)

I'm trying to tar gz everything up all files ending in ext without storing the full path in my tar file.

tar -cvzf test.tar.gz -C C:/mydir/toTar/ *.ext

I get an error:

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

I can give the whole path but then my tar will contain C->mydir->toTar->. I just want the files, not mydir and toTar in the result.

So far only thing that is close to what I want is . instead of *.ext, but that tars other things too, which I obviously don't want.

¿Fue útil?

Solución

The problem is that * is a wildcard character that is expanded by the shell, but you are bypassing the shell and calling tar directly. The tar command is looking for one file which is named literally *.ext and it does not exist.

Your options are:

  1. Expand the list of files in your own code and pass that list to tar.
  2. Call the shell from your code by calling something like /bin/sh -c tar ...

With option 2 there may be security implications -- if the shell sees something it thinks is a command, it will run it. Option 1 is therefore safer, but it's up to you which makes more sense.

Otros consejos

I am befuddled by how you're using dos-style paths in an apparently linux-like context. But this is how I'd do it. Hopefully the concept is clear if the details may be incorrect.

cd C:/mydir/toTar/
mkdir ~/tmpwork
find . -name '*.ext' > ~/tmpwork/extfiles
tar czvfT ~/tmpwork/test.tar.gz ~/tmpwork/extfiles
rm ~/tmpfiles/extfiles

There is no way around the shell expansion without using pipes, etc.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top