3

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.

KernelPanic
  • 1,236
  • I'm not sure what this question means. Can you try to clarify? – Faheem Mitha Mar 08 '15 at 07:32
  • @FaheemMitha It is simple task but unfortunately I've forgotten it. I want to uncompress .tar.gz archive into some arbitrary directory, for instance /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
  • You mean the extracted tar directory would be inside /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
  • @FaheemMitha, I've ugpraded question with simple example, what I want to achieve. – KernelPanic Mar 08 '15 at 07:45
  • You made some comment about a directory being created as necessary. What directory would need to be created in your example? – Faheem Mitha Mar 08 '15 at 07:47
  • @FaheemMitha, let's forget about that part. – KernelPanic Mar 08 '15 at 07:47

1 Answers1

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