LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script to list the contents of the latest archive? (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-list-the-contents-of-the-latest-archive-4175452209/)

Batistuta_g_2000 03-01-2013 03:49 AM

Script to list the contents of the latest archive?
 
Hi,

I am looking to write a script that will:

Get the latest Archive from a list
List the contents of the tar.gz
Print the name, size, and date of modification

I have:

Code:

gunzip -c ~/backups/PROJECTNAME_2013-02-28-16:27:27.tar.gz | tar -tvf -
Which gives me:

r--r-- ubuntu/ubuntu 5 2013-02-19 03:15 file0
-rw-r--r-- ubuntu/ubuntu 0 2013-02-28 16:47 fil1
-rwxr-xr-x ubuntu/ubuntu 795 2013-02-19 01:51 fil2
-rwxr-xr-x ubuntu/ubuntu 643 2011-11-20 14:13 fil3

But I want to sort this by last modification date:

Code:

gunzip -c ~/backups/PROJECTNAME_2013-02-28-16:27:27.tar.gz | tar -tvf - | sort -n
gives me:

-rw-r--r-- ubuntu/ubuntu 0 2013-02-28 16:47 fil1
-rw-r--r-- ubuntu/ubuntu 5 2013-02-19 03:15 fil0
-rwxr-xr-x ubuntu/ubuntu 643 2011-11-20 14:13 fil3
-rwxr-xr-x ubuntu/ubuntu 795 2013-02-19 01:51 fil2

Any ideas? to get order right:

name, size, and date of modification

colucix 03-01-2013 04:24 AM

You have to specify the field to be sorted out, using the -k option:
Code:

sort -k4n
Moreover, using GNU tar you don't need to uncompress the archive. Therefore you will end up with something like this:
Code:

tar tvf ~/backups/PROJECTNAME_2013-02-28-16:27:27.tar.gz | sort -k4n | cut -d' ' -f6-,3,4
Hope this helps.

Batistuta_g_2000 03-01-2013 08:23 AM

Quote:

Originally Posted by colucix (Post 4902291)
You have to specify the field to be sorted out, using the -k option:
Code:

sort -k4n
Moreover, using GNU tar you don't need to uncompress the archive. Therefore you will end up with something like this:
Code:

tar tvf ~/backups/PROJECTNAME_2013-02-28-16:27:27.tar.gz | sort -k4n | cut -d' ' -f6-,3,4
Hope this helps.

Excellent, thanks very much, gerat help.


All times are GMT -5. The time now is 08:39 AM.