Compress .tar.gz without directories in Perl script

367 Views Asked by At

Seems like a stupid question, but I can't find anything via google or man page...I'm sure it's something with my quotes...When I run the following from the command line, it runs as expected/desired(i.e. only files without any directories):

tar cfz /path/to/file.tar.gz --directory="/path/to/" .

However, I have this analogous line in perl, albeit with a variable as the path ($folder_errors='/path/to'):

system('tar cfz '."\"${folder_errors}\"/errors.tar.gz --directory=\"\"${folder_errors}\"/\" .");

also tried

system('tar cfz '."\"${folder_errors}\"/errors.tar.gz --directory=\"${folder_errors}\"/ .");

with the same (unexpected) results.

It compresses the correct files, however, inside of the compressed file is the file structure ./ where all of the files reside.

2

There are 2 best solutions below

2
On

Try perl's quote operators. qq[...] acts like double-quotes, except you don't have to escape the literal " inside them.

system qq[tar cfz "$folder_errors/errors.tar.gz" --directory="$folder_errors"];
0
On

Your quotes seem corrects. Have you noticed that you are creating the tar.gz in the same directory where files to archives reside? I think you are exposing yourself to troubles. If you can not change the target directory of tar, you should avoid recursion with:

system qq[tar cfz "$folder_errors/errors.tar.gz" --exclude "./errors.tar.gz" --directory="$folder_errors" . ];