Sponsored Content
Full Discussion: Delete Old files from Tar
Top Forums UNIX for Beginners Questions & Answers Delete Old files from Tar Post 303045288 by RudiC on Sunday 15th of March 2020 04:44:09 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Tar(3)							User Contributed Perl Documentation						    Tar(3)

NAME
Tar - module for manipulation of tar archives. SYNOPSIS
use Archive::Tar; Archive::Tar->create_archive ("my.tar.gz", 9, "/this/file", "/that/file"); print join " ", Archive::Tar->list_archive ("my.tar.gz"), ""; $tar = Archive::Tar->new(); $tar->read("origin.tar.gz",1); $tar->add_files("file/foo.c", "file/bar.c"); $tar->add_data("file/baz.c","This is the file contents"); $tar->write("files.tar"); DESCRIPTION
This is a module for the handling of tar archives. Archive::Tar provides an object oriented mechanism for handling tar files. It provides class methods for quick and easy files handling while also allowing for the creation of tar file objects for custom manipulation. If you have the Compress::Zlib module installed, Ar- chive::Tar will also support compressed or gzipped tar files. Class Methods The class methods should be sufficient for most tar file interaction. create_archive ($file, $compression, @filelist) Creates a tar file from the list of files provided. The first argument can either be the name of the tar file to create or a reference to an open file handle (e.g. a GLOB reference). The second argument specifies the level of compression to be used, if any. Compression of tar files requires the installation of the Compress::Zlib module. Specific levels or compression may be requested by passing a value between 2 and 9 as the second argument. Any other value evaluating as true will result in the default compression level being used. The remaining arguments list the files to be included in the tar file. These files must all exist. Any files which don't exist or can't be read are silently ignored. If the archive creation fails for any reason, "create_archive" will return undef. Please use the "error" method to find the cause of the failure. list_archive ($file, ['property', 'property',...]) list_archive ($file) Returns a list of the names of all the files in the archive. The first argument can either be the name of the tar file to create or a reference to an open file handle (e.g. a GLOB reference). If "list_archive()" is passed an array reference as its second argument it returns a list of hash references containing the requested properties of each file. The following list of properties is supported: name, size, mtime (last modified date), mode, uid, gid, linkname, uname, gname, devmajor, devminor, prefix. Passing an array reference containing only one element, 'name', is special cased to return a list of names rather than a list of hash references. extract_archive ($file) Extracts the contents of the tar file. The first argument can either be the name of the tar file to create or a reference to an open file handle (e.g. a GLOB reference). All relative paths in the tar file will be created underneath the current working directory. If the archive extraction fails for any reason, "extract_archive" will return undef. Please use the "error" method to find the cause of the failure. new ($file) new () Returns a new Tar object. If given any arguments, "new()" calls the "read()" method automatically, parsing on the arguments provided read(). If "new()" is invoked with arguments and the read method fails for any reason, "new()" returns undef. Instance Methods read ($ref, $compressed) Read the given tar file into memory. The first argument can either be the name of a file or a reference to an already open file handle (e.g. a GLOB reference). The second argument indicates whether the file referenced by the first argument is compressed. The second argument is now optional as Archive::Tar will automatically detect compressed archives. The "read" will replace any previous content in $tar! add_files(@filenamelist) Takes a list of filenames and adds them to the in-memory archive. On MacOS, the path to the file is automatically converted to a Unix like equivalent for use in the archive, and the file's modification time is converted from the MacOS epoch to the Unix epoch. So tar archives created on MacOS with Archive::Tar can be read both with tar on Unix and applications like suntar or Stuffit Expander on Mac- OS. Be aware that the file's type/creator and resource fork will be lost, which is usually what you want in cross-platform archives. add_data ($filename, $data, $opthashref) Takes a filename, a scalar full of data and optionally a reference to a hash with specific options. Will add a file to the in-memory archive, with name $filename and content $data. Specific properties can be set using $opthashref, The following list of properties is supported: name, size, mtime (last modified date), mode, uid, gid, linkname, uname, gname, devmajor, devminor, prefix. (On MacOS, the file's path and modification times are converted to Unix equivalents.) remove (@filenamelist) Removes any entries with names matching any of the given filenames from the in-memory archive. String comparisons are done with "eq". write ($file, $compressed) Write the in-memory archive to disk. The first argument can either be the name of a file or a reference to an already open file handle (be a GLOB reference). If the second argument is true, the module will use Compress::Zlib to write the file in a compressed format. If Compress:Zlib is not available, the "write" method will fail. Specific levels of compression can be chosen by passing the values 2 through 9 as the second parameter. If no arguments are given, "write" returns the entire formatted archive as a string, which could be useful if you'd like to stuff the archive into a socket or a pipe to gzip or something. This functionality may be deprecated later, however, as you can also do this using a GLOB reference for the first argument. extract(@filenames) Write files whose names are equivalent to any of the names in @filenames to disk, creating subdirectories as necessary. This might not work too well under VMS. Under MacPerl, the file's modification time will be converted to the MacOS zero of time, and appropriate conversions will be done to the path. However, the length of each element of the path is not inspected to see whether it's longer than MacOS currently allows (32 characters). If "extract" is called without a list of file names, the entire contents of the archive are extracted. list_files(['property', 'property',...]) list_files() Returns a list of the names of all the files in the archive. If "list_files()" is passed an array reference as its first argument it returns a list of hash references containing the requested properties of each file. The following list of properties is supported: name, size, mtime (last modified date), mode, uid, gid, linkname, uname, gname, devmajor, devminor, prefix. Passing an array reference containing only one element, 'name', is special cased to return a list of names rather than a list of hash references. get_content($file) Return the content of the named file. replace_content($file,$content) Make the string $content be the content for the file named $file. CHANGES
Version 0.20 Added class methods for creation, extraction and listing of tar files. No longer maintain a complete copy of the tar file in memory. Removed the "data()" method. Version 0.10 Numerous changes. Brought source under CVS. All changes now recorded in ChangeLog file in distribution. Version 0.08 New developer/maintainer. Calle has carpal-tunnel syndrome and cannot type a great deal. Get better as soon as you can, Calle. Added proper support for MacOS. Thanks to Paul J. Schinder <schinder@leprss.gsfc.nasa.gov>. Version 0.071 Minor release. Arrange to chmod() at the very end in case it makes the file read only. Win32 is actually picky about that. SunOS 4.x tar makes tarfiles that contain directory entries that don't have typeflag set properly. We use the trailing slash to recognise directories in such tar files. Version 0.07 Fixed (hopefully) broken portability to MacOS, reported by Paul J. Schinder at Goddard Space Flight Center. Fixed two bugs with symlink handling, reported in excellent detail by an admin at teleport.com called Chris. Primitive tar program (called ptar) included with distribution. Usage should be pretty obvious if you've used a normal tar program. Added methods get_content and replace_content. Added support for paths longer than 100 characters, according to POSIX. This is compatible with just about everything except GNU tar. Way to go, GNU tar (use a better tar, or GNU cpio). NOTE: When adding files to an archive, files with basenames longer than 100 characters will be silently ignored. If the prefix part of a path is longer than 155 characters, only the last 155 characters will be stored. Version 0.06 Added list_files() method, as requested by Michael Wiedman. Fixed a couple of dysfunctions when run under Windows NT. Michael Wiedmann reported the bugs. Changed the documentation to reflect reality a bit better. Fixed bug in format_tar_entry. Bug reported by Michael Schilli. Version 0.05 Quoted lots of barewords to make "use strict;" stop complaining under perl version 5.003. Ties to Compress::Zlib put in. Will warn if it isn't available. $tar->write() with no argument now returns the formatted archive. Version 0.04 Made changes to write_tar so that Solaris tar likes the resulting archives better. Protected the calls to readlink() and symlink(). AFAIK this module should now run just fine on Windows NT. Add method to write a single entry to disk (extract) Added method to add entries entirely from scratch (add_data) Changed name of add() to add_file() All calls to croak() removed and replaced with returning undef and setting Tar::error. Better handling of tarfiles with garbage at the end. perl v5.8.0 2000-04-27 Tar(3)
All times are GMT -4. The time now is 09:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy