HP-UX - How can I read a text file from tar archive without extracting it?
Asked Answered
G

1

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

Gleeman answered 9/10, 2012 at 14:49 Comment(0)
D
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
Dorsman answered 9/10, 2012 at 14:51 Comment(15)
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?Gleeman
-O is probably a GNU specific option of tar. Perfunctory with the full --to-stdout option?Dorsman
You should have a look at the HP-UX manpage of tar: man tar to see if a similar option is available.Dorsman
Nop... Here is the man of tar on HP-UX: cs.bgu.ac.il/~arik/usail/man/hpux/tar.1.html. Nothing interesting in this man. The solution must be tricky!Gleeman
Have you tried to create a named pipe with mkfifo named after the file you are extracting, and cat it while extracting it?Dorsman
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.Gleeman
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.Dorsman
Could you give me the command to run? It would be simpler than words! ThanksGleeman
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.Dorsman
Well this is not necessary to use mkfifo. tar xf myArchive.tar folder/someFile.txt ; cat folder/someFile.txt ; rm -R folder works.Gleeman
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?Dorsman
No this is just an operational directory and I didn't want to cram unnecessary file into it. But anyway, thanks.Gleeman
At last resort, maybe you could compile GNU tar under HP/UX?Dorsman
For the record, a gtar version exists on HP-UX.Gleeman
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.Monitory

© 2022 - 2024 — McMap. All rights reserved.