If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > tar files based on date

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-28-10, 12:35
newdbaxchange newdbaxchange is offline
Registered User
 
Join Date: Jun 2010
Posts: 81
tar files based on date

Hi,

I have a list of files in a directory and I want to be able to compress and tar those found files into 1 tar file.
First is this feasible? And secondly can some one provide me with the syntax please.

I can do an ls -ltr |grep 'Jun 21' but afterwards I want to tar them into a tar file named Jun21.tar

I would like to do this on a one line command and not via a shell script

Many thanks for your help
Reply With Quote
  #2 (permalink)  
Old 07-28-10, 13:19
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,308
I'd use the -N parameter of tar.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #3 (permalink)  
Old 07-28-10, 14:22
newdbaxchange newdbaxchange is offline
Registered User
 
Join Date: Jun 2010
Posts: 81
Do you happen to have a more through explanation that that please

Do I add tar -N to the tail of the previous syntax? Apologies but your answer is not clear for a novice like myself

Thanks in advance
Reply With Quote
  #4 (permalink)  
Old 07-28-10, 15:23
kitaman kitaman is offline
Registered User
 
Join Date: Sep 2009
Location: Ontario
Posts: 526
The -N option on the tar statement will select files newer than the argument supplied.
The argument may be a date, or the name of a file (whose date of last change will be used as the date).
This is not quite the same as files equal to a specific date.
Are we to assume that you want this in one line in order to embed it in a compiled program?
It would be easier to write a script, and just call the script from the program. Are any of the files going to be over a year old?
Reply With Quote
  #5 (permalink)  
Old 07-29-10, 12:40
newdbaxchange newdbaxchange is offline
Registered User
 
Join Date: Jun 2010
Posts: 81
Hello,

no, none of the files are more than a year old. The most the files would be aged would be 3 months.
If you have a script to hand that would be most appreciated. I can then put that script into cron
and allow the script to move the files as and when required automatically

Thanks in advance
Reply With Quote
  #6 (permalink)  
Old 07-29-10, 17:23
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,578
Another alternative would be:
Code:
ls -ltr |grep 'Jun 21' | xargs tar cf <archive>
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 07-30-10, 16:34
kitaman kitaman is offline
Registered User
 
Join Date: Sep 2009
Location: Ontario
Posts: 526
The problem with this solution is that there are 9 arguments (for each file) to the ls -ltr command and each argument is passed to tar.
Reply With Quote
  #8 (permalink)  
Old 08-02-10, 05:00
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,578
Yes, you're right. But that's nothing you can't easily fix, for example with "cut" or "awk".
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 08-02-10, 08:22
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,258
i.e.
| awk '{print $9}'
Reply With Quote
  #10 (permalink)  
Old 08-02-10, 09:04
newdbaxchange newdbaxchange is offline
Registered User
 
Join Date: Jun 2010
Posts: 81
Hello,

thanks for all your comments but as of yet I have no definitive answer.

If the process is feasible on one line can someone provide the syntax for that

If the answer is in the form of a shell script can one of you experienced UNIX guys
display the lines / code required.

Many thanks in advance
Reply With Quote
  #11 (permalink)  
Old 08-02-10, 09:52
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,258
Code:
tar cvf Jun21.tar `ls -lrt | grep 'Jun 21' | awk '{print $9}'`
Reply With Quote
  #12 (permalink)  
Old 08-02-10, 14:15
newdbaxchange newdbaxchange is offline
Registered User
 
Join Date: Jun 2010
Posts: 81
It works

Hey,

thanks for that last syntax - it works like a dream.

Many many thanks
Reply With Quote
  #13 (permalink)  
Old 08-03-10, 06:58
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,578
Quote:
Originally Posted by pdreyer View Post
i.e.
| awk '{print $9}'
Something like that - except that it won't work if there are spaces in the file name.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On