Skip to main content

tar - List of extracted files [Resolved]

How can I extract files from a .tar.gz archive while keeping a trace of the extracted files?

For example, let's say I have the following file structure...

ROOT
 ¦-? children
 ¦    ¦-? joe.txt
 ¦    +-? george.txt
 ¦-? bar.txt
 ¦-? foo.txt
 +-? A_R_C_H_I_V_E_.t_a_r_.b_z_
      +-? children
      ¦    +-? joe.txt
      ¦    +-? bob.txt
      +-? hello.txt
      +-? world.txt
      +-? foo.txt

Now, if I extract the files from the archive while keeping the newer files in place I'd like to know which ones have been extracted so I can do something like this:
tar xf ./ARCHIVE.tar.gz --keep-newer-files | xargs -I EXTRACTED_FILE echo EXTRACTED_FILE


Question Credit: DrunkenPoney
Question Reference
Asked October 9, 2019
Tags: , archive
Posted Under: Unix Linux
9 views
1 Answers

I will use the option --to-command to trigger the print of all extracted files

the argument is a command that will use the env variable TAR_FILENAME to have the file name , and will receive thee file on STDIN

So you must read STDIN and create the extracted file , you can print on STDOUT the file you created

tar -xzf ../ARCH.tgz --keep-newer-files \
   --to-command="sh -c $(printf '%q' 'dd of="$TAR_FILENAME" >/dev/null 2>&1 ; echo "#EXTRACTED#$TAR_FILENAME" ')" > output_tar.txt

and in output_tar.txt you will have a line for each extracted file .

see https://www.gnu.org/software/tar/manual/html_node/Writing-to-an-External-Program.html


credit: EchoMike444
Answered October 9, 2019
Your Answer