How to cat a single file’s content from a tar without unpacking it on Linux?

Posted on In Tutorial

How to cat a single file’s content from a tar without unpacking it on Linux? For example, I know there is a file README.txt in a tar tools.tar.gz . How to cat the content of README.txt out?

You can do this by using a combination of tar‘s options and arguments.

-O, --to-stdout
  Extract files to standard output.
-x
  Extract files from an archive. Arguments are optional.
  When given, they specify names of the archive members to be extracted.

An example usage as follows.

$ tar -xOf tools.tar.gz README.txt
Sun 09 Aug 2020 01:03:57 PM HKT

Here, -O makes tar print to STDOUT, -x to extract files, and README.txt to specify only extract the file under this path.

Leave a Reply

Your email address will not be published. Required fields are marked *