3

This question already has an answer here:

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.

marked as duplicate by don_crissti, Jeff Schaller, Stephen Kitt, thrig, Tim Kennedy Apr 8 '17 at 15:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I'm not sure what this question means. Can you try to clarify? – Faheem Mitha Mar 8 '15 at 7: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 8 '15 at 7: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 8 '15 at 7:39
  • @FaheemMitha, I've ugpraded question with simple example, what I want to achieve. – KernelPanic Mar 8 '15 at 7: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 8 '15 at 7:47
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.

  • 1
    @MarkoFrelih The man page for that option is IMO overly simplistic – Anthon Mar 8 '15 at 8:02

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