we want to tar.gz all the following jars files

so I do the following

cd /var/tmp
tar -czvf archive.tar.gz /usr/hdp/

but when I do

tar xvf archive.tar.gz

we get the usr folder


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


what is wrong with my syntax

 cd /var/tmp
 tar -czvf archive.tar.gz /usr/hdp/

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/ .

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

tar stores the full path passed on the command line.

If you want just the 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
  • 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.