Visit Our UNIX and Linux User Community


Delete Old files from Tar


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Delete Old files from Tar
# 1  
Old 03-14-2020
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 ?

Last edited by shekar7; 03-14-2020 at 11:51 PM..
# 2  
Old 03-15-2020
Quote:
is it Possible ?
The best way to find out is to try...
What have you tried so far?
Quote:
Without Extracting tar files
.. 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
# 3  
Old 03-15-2020
I'm afraid you're out of luck. Not all tar versions provide a --delete option; e.g.

Code:
tar --version
bsdtar 2.8.4 - libarchive 2.8.4

doesn't. Those which do, like
Code:
tar --version
tar (GNU tar) 1.30


do not allow to delete from compressed archives, which yours is, as you specify. man tar:
Quote:
--delete
Delete from the archive. The arguments supply names of the archive members to be removed. At least one argument must be given.

This option does not operate on compressed archives. There is no short option equivalent.
These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 03-15-2020
Quote:
Originally Posted by vbe
What have you tried so far?

Sorry "Vbe" I Could Have Mentioned My code , I am Getting Result as Expected.

But I Want to Know Without Extracting files to tmp directory, find out files older than n days.

As You Mentioned, I tried to use gzcat it is not working, But zcat is Working and it is displaying the file content, I don't Want that.
Code:
         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;


Last edited by shekar7; 03-15-2020 at 10:50 AM.. Reason: spelling mistake
# 5  
Old 03-15-2020
pax can do some modifactions on a tar stream.
Code:
man pax

# 6  
Old 03-15-2020
Quote:
Originally Posted by MadeInGermany
pax can do some modifactions on a tar stream.
Code:
man pax

Can You Provide Me Example if you have?
# 7  
Old 03-15-2020
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.



Code:
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-


Last edited by RudiC; 03-15-2020 at 05:54 PM..
This User Gave Thanks to RudiC For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #320
Difficulty: Medium
Intel created the first consumer 64-bit processor.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

tar command to explore multiple layers of tar and tar.gz files

Hi all, I have a tar file and inside that tar file is a folder with additional tar.gz files. What I want to do is look inside the first tar file and then find the second tar file I'm looking for, look inside that tar.gz file to find a certain directory. I'm encountering issues by trying to... (1 Reply)
Discussion started by: bashnewbee
1 Replies

3. UNIX for Dummies Questions & Answers

How to delete original files after using a tar operation.

I have a list of log files in a directory. Once i tar them I need to remove the original log files. How do i do it? (4 Replies)
Discussion started by: manutd
4 Replies

4. UNIX for Dummies Questions & Answers

tar -cvf test.tar `find . -mtime -1 -type f` only tar 1 file

Hi all, 4 files are returned when i issue 'find . -mtime -1 -type f -ls'. ./ora_475244.aud ./ora_671958.aud ./ora_934052.aud ./ora_934050.aud However, when I issued the below command: tar -cvf test.tar `find . -mtime -1 -type f`, the tar file only contains the 1st file -... (2 Replies)
Discussion started by: ahSher
2 Replies

5. Solaris

option to delete .tar file while extracting

Is there an option in tar which deletes the .tar file as soon as it is successfully extracted. (5 Replies)
Discussion started by: vickylife
5 Replies

6. UNIX for Advanced & Expert Users

How to create a Tar of multiple Files in Unix and FTP the tar to Windows.

Hi, On my Unix Server in my directory, I have 70 files distributed in the following directories (which have several other files too). These files include C Source Files, Shell Script Source Files, Binary Files, Object Files. a) /usr/users/oracle/bin b) /usr/users/oracle... (1 Reply)
Discussion started by: marconi
1 Replies

7. Shell Programming and Scripting

Delete a file from XXX.tar.Z

Hi All can u please let me know how to delete a file from XXX.tar.Z file with out uncompressing this file. thanks in advance. --Bali (0 Replies)
Discussion started by: balireddy_77
0 Replies

8. Linux

tar usage and delete/create username

dear Linux expert, I am using Fedora R5 how to use tar to archive /var to a test_var.tar and compressing it? how to delete a user? just remove the line in /etc/passwd? and then what is the procedure to create a new user ? many thank (2 Replies)
Discussion started by: zp523444
2 Replies

9. UNIX for Advanced & Expert Users

Untaring *.tar.tar files

Hi all, How to untar a file with .tar.tar extension. A utility that i downloaded from net had this extension. Thanks in advance, bubeshj. (6 Replies)
Discussion started by: bubeshj
6 Replies

10. UNIX for Dummies Questions & Answers

delete a file from an existing Solaris tar

How can I delete a file from an existing Solaris tar file ? (not gtar) (2 Replies)
Discussion started by: avnerht
2 Replies

Featured Tech Videos