Delete Old files from Tar

Hi All,

Requirement:

I Have file.Tar.gz , I Need to Delete files from a tar , older than n days.

Without Extracting tar files to current directory, I need to Find out files using mtime, is it Possible ?

The best way to find out is to try...
What have you tried so far?

.. I suggest you look at gzcat command since your file is a .gz, then see what happens to the tar file when you extract e.g. one file you want to delete from ( once extracted, is the file still present in the archive?)...
Once you have done that, come back if you find any issues with that you have done, and what issue ( all error output from the system etc...) you are facing

I'm afraid you're out of luck. Not all tar versions provide a --delete option; e.g.

tar --version
bsdtar 2.8.4 - libarchive 2.8.4

doesn't. Those which do, like

tar --version
tar (GNU tar) 1.30

do not allow to delete from compressed archives, which yours is, as you specify. man tar :

2 Likes
         gunzip file.Tar.gz
         tar -xvf  file.Tar  -C tmp
         find  tmp -name "*.txt" -mtime +7 -printf '%f\n' > Indelte;
         tar -vf file.Tar --delete -T Indelte;

pax can do some modifactions on a tar stream.

man pax

Can You Provide Me Example if you have?

This MAY work to identify the target files. It produces an archive listing in long format including the date/time stamps, which ($4,$5) then are printed to stdout and a pipe, and, in parallel, the line No. and file name to a temporary file to be used later. On the other end of the pipe, date converts all the time stamps to epoch seconds, then piped into the next awk that compares them to the target threshold, i.e. the date/time 7 days ago. If condition is met, the line No. is retained in an array, and then used to identify the relevant lines in the temp file. Identified file names are printed and piped into tar to be used as the file for the -T option.
The --delete option won't work on compressed files, though, as stated before.

Be aware that the construct may suffer from race conditions, if the temp file is not on time available for reading in the last step. You might split the loooong pipe chain into separate steps, then.

tar tvf file.Tar.gz |
awk '
        {print $4,$5
         $1 = $2 = $3 = $4 = $5 = ""
         print NR, $0 > "/tmp/XYZ"
        }
' |
date -f- +%s |
awk '
BEGIN           {DT = srand() - 7 * 86400
                }
FNR == NR &&
$1 < DT         {T[NR]
                 next
                }
$1 in T         {print $2
                }
' - /tmp/XYZ |
tar tvf file.Tar.gz -T-
1 Like

After studying the man page myself I have come to the conclusion that pax wants to either read files from a filesystem or write files to a filesystem or both.
So my first perception was wrong - pax cannot read a tar file and produce another one.

1 Like