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

07-28-10, 12:35
|
|
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
|
|

07-28-10, 13:19
|
|
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.
|
|

07-28-10, 14:22
|
|
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
|
|

07-28-10, 15:23
|
|
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?
|
|

07-29-10, 12:40
|
|
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
|
|

07-29-10, 17:23
|
|
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
|
|

07-30-10, 16:34
|
|
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.
|
|

08-02-10, 05:00
|
|
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
|
|

08-02-10, 08:22
|
|
Registered User
|
|
Join Date: May 2005
Location: South Africa
Posts: 1,258
|
|
|
|

08-02-10, 09:04
|
|
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
|
|

08-02-10, 09:52
|
|
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}'`
|
|

08-02-10, 14:15
|
|
Registered User
|
|
Join Date: Jun 2010
Posts: 81
|
|
|
It works
Hey,
thanks for that last syntax - it works like a dream.
Many many thanks
|
|

08-03-10, 06:58
|
|
Registered User
|
|
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,578
|
|
Quote:
Originally Posted by pdreyer
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|