1

I have a tar archive foo.tar.gz, inside of which a directory containing another directory contains a file: folder_1/folder_2/file.txt.

I want to untar file.txt without extracting folder_1/folder_2/.

Using

tar -xvf foo.tar.gz folder_1/folder_2/file.txt

I get file.txt inside the subdirectories that contained it inside the tar archive (folder_1/folder_2/file.txt).

Is there a way of getting only file.txt and nothing else in a single command, avoiding extracting everything?

2

You can extract the file to standard output and redirect that:

tar -xvf foo.tar.gz -O folder_1/folder_2/file.txt > file.txt

(Note that the option is a capital “O”, not the digit “0”.)

  • I am getting this printed out, and my file.txt gets extracted without any content: tar: Options '-[0-7][lmh]' not supported by *this* tar Try 'tar --help' or 'tar --usage' for more information. – erik Feb 8 '18 at 14:00
  • @tli Are you using -O (capital letter O), or -0 (number 0)? The capital letter O option is a GNU tar extension to extract to stdout. – Andrew Henle Feb 8 '18 at 14:06
  • @AndrewHenle my bad, thanks for pointing that out, now it works! – erik Feb 8 '18 at 14:12
  • The option is also supported on other platforms, e.g. FreeBSD. – Stephen Kitt Feb 8 '18 at 14:13

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