-1

we want to tar.gz all the following jars files

2.6.4.0-91/spark2/jars/super-csv-2.2.0.jar
2.6.4.0-91/spark2/jars/univocity-parsers-2.2.1.jar
2.6.4.0-91/spark2/jars/validation-api-1.1.0.Final.jar
2.6.4.0-91/spark2/jars/xbean-asm5-shaded-4.4.jar
2.6.4.0-91/spark2/jars/xercesImpl-2.9.1.jar
2.6.4.0-91/spark2/jars/xmlenc-0.52.jar
2.6.4.0-91/spark2/jars/xz-1.0.jar
2.6.4.0-91/spark2/jars/zookeeper-3.4.6.2.6.4.0-91.jar

so I do the following

cd /var/tmp
tar -czvf archive.tar.gz /usr/hdp/2.6.4.0-91/spark2/jars

but when I do

tar xvf archive.tar.gz

we get the usr folder

usr 

while the expected results should be only the jars as ( without the sub folders )

    super-csv-2.2.0.jar
    univocity-parsers-2.2.1.jar
    validation-api-1.1.0.Final.jar
    xbean-asm5-shaded-4.4.jar
    xercesImpl-2.9.1.jar
    jxmlenc-0.52.jar
    xz-1.0.jar
    zookeeper-3.4.6.2.6.4.0-91.jar

what is wrong with my syntax

 cd /var/tmp
 tar -czvf archive.tar.gz /usr/hdp/2.6.4.0-91/spark2/jars
1

Use the -C option to let tar chdir into the directory before starting to archive the files:

tar -czf archive.tar.gz -C /usr/hdp/2.6.4.0-91/spark2/jars .

Do not forget the dot as last argument

Note that the first argument after the f argument is the file name to output the archive to and the following option -C takes the directory name as argument (where tar chdirs to) and then the dot is the directory to start archiving.

  • it create the "-C" file – yael Sep 6 '18 at 10:48
  • the problem is that - after tar xvf archive.tar.gz , we see also the "-c" file and the file itself archive.tar.gz – yael Sep 6 '18 at 10:52
  • You do not get it, but if your local tar implementation really behaves the way you claim, you need to throw it away as it is broken. – schily Sep 6 '18 at 10:53
0

tar stores the full path passed on the command line.

If you want just the 2.6.4.0-91 directory, simple cd into it before calling tar.

Please keep in mind that in this case, you should specify where you want the archive to end with the -C option.

As an example, say you are in /home/user/:

cd /usr/hdp/
tar -czvf -C /home/user/archive.tar.gz 2.6.4.0-91/spark2/jars/
  • This creates a file named -C – schily Sep 6 '18 at 10:44

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