How does tar.gz
file gets uncompressed with all contents to arbitrary directory, existing or not using tar using single line command? If it does not exists, then is created (directory). To clarify, let consider example:
I've downloaded archive-latest.tar.gz
from internet into directory /home/user/Downloads
. I have microSD card target partition mounted on /mnt/archive
. Once downloaded, I want to extract downloaded /home/user/Downloads/archive-latest.tar.gz
directly on /mnt/archive
using single line command. I've read man tar
manual, but I cannot find option for specifying target dir.
Asked
Active
Viewed 7,569 times
3

Gilles 'SO- stop being evil'
- 829,060

KernelPanic
- 1,236
1 Answers
6
The -C
option makes tar change to an existing directory before starting to extract:
tar xv -C /mnt/archive -f /home/user/Downloads/archive-latest.tar.gz
If you are already located in the directory /home/user
or /home/user/Downloads
you can shorten the path after -f
accordingly.
If there is a chance the target path doesn't exist you can created it with mkdir
:
mkdir -p /mnt/archive; tar xv -C /mnt/archive -f /home/user/Downloads/archive-latest.tar.gz
With the -p
option mkdir
doesn't complain in case the directory already exists.

Anthon
- 79,293
/tmp/test
. But I want it to be done regardless of/tmp/test
directory existence. If target dir does not exists, then it must be automatically created and then the archive is extracted into it. – KernelPanic Mar 08 '15 at 07:37/tmp/test
? And where would the original tar file be placed? I suggest an "imaginary" example session to show the kind of behavior you want. – Faheem Mitha Mar 08 '15 at 07:39