17

I have a tar archive which contains several text files. I would like to write a script to display (stdout) the content of a file without extracting it to the current directory.

Actually I would like to do the same as:

tar tf myArchive.tar folder/someFile.txt
cat folder/someFile.txt
rm -R folder

but without the rm...

I tried this way but it didn't work:

tar tf myArchive.tar folder/someFile.txt | cat

Thanks

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
Maxbester
  • 2,435
  • 7
  • 42
  • 70

1 Answers1

36

Use x to extract, with f from archive file. Then add also option -O to direct extracted files to standard output.

tar xf myArchive.tar folder/someFile.txt -O
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
  • Actually it works fine on Linux. But when I run it on a HP-UX machine, the content of the file is not displayed. Instead, the folder is extracted. Is it normal? Any workaround? – Maxbester Oct 09 '12 at 15:10
  • 1
    `-O` is probably a GNU specific option of `tar`. Perfunctory with the full `--to-stdout` option? – Didier Trosset Oct 09 '12 at 15:31
  • You should have a look at the HP-UX manpage of tar: `man tar` to see if a similar option is available. – Didier Trosset Oct 09 '12 at 15:32
  • Nop... Here is the man of tar on HP-UX: http://www.cs.bgu.ac.il/~arik/usail/man/hpux/tar.1.html. Nothing interesting in this man. The solution must be tricky! – Maxbester Oct 09 '12 at 15:34
  • Have you tried to create a named pipe with `mkfifo` named after the file you are extracting, and `cat` it while extracting it? – Didier Trosset Oct 10 '12 at 07:56
  • Do you mean something like: `tar xf myArchive.tar folder/someFile.txt | mkfifo -m 644 folder/someFile.txt | cat`? If yes, this command does not work... The error I got is: `mkfifo: cannot access folder: No such file or directory`. – Maxbester Oct 10 '12 at 08:40
  • No, in the other order. First create the pipe. Then untar the file, in the hope `tar` does no delete and create the file, but only fills it. Then cat it, and last remove the pipe. – Didier Trosset Oct 10 '12 at 11:21
  • Could you give me the command to run? It would be simpler than words! Thanks – Maxbester Oct 10 '12 at 11:45
  • 1
    `mkdir folder ; mkfifo folder/someFile.txt ; tar xf myArchive.tar folder/someFile.txt ; cat folder/someFile.txt ; rm -r folder` The important thing is to check whether tar replaces the pipe or not. – Didier Trosset Oct 10 '12 at 14:36
  • Well this is not necessary to use mkfifo. `tar xf myArchive.tar folder/someFile.txt ; cat folder/someFile.txt ; rm -R folder` works. – Maxbester Oct 10 '12 at 15:05
  • It works, yes. But it extracts the file content to the filesystem. I was wondering why in the first place you didn't want the file to be extracted to the filesystem. Is it too large? – Didier Trosset Oct 10 '12 at 15:58
  • No this is just an operational directory and I didn't want to cram unnecessary file into it. But anyway, thanks. – Maxbester Oct 11 '12 at 14:55
  • At last resort, maybe you could compile GNU tar under HP/UX? – Didier Trosset Oct 12 '12 at 07:29
  • For the record, [a gtar version exists on HP-UX](http://hpux.connect.org.uk/hppd/hpux/Gnu/tar-1.26). – Maxbester Oct 12 '12 at 09:41
  • 2
    I find it handier to put all the options together at the start: `tar -Oxf folder/someFile.txt`, or `-xOf` if you don't like it looking like a hex number. – undefined Nov 26 '14 at 00:23