Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tar files using the -C option and wildcard

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.

like image 252
dlite922 Avatar asked Oct 23 '25 17:10

dlite922


2 Answers

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.

like image 95
Kevin Panko Avatar answered Oct 25 '25 08:10

Kevin Panko


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
like image 23
NovaDenizen Avatar answered Oct 25 '25 08:10

NovaDenizen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!