Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2010
    Posts
    81

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

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd use the -N parameter of tar.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

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

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    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?

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

  6. #6
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    Another alternative would be:
    Code:
    ls -ltr |grep 'Jun 21' | xargs tar cf <archive>
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  7. #7
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    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.

  8. #8
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    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

  9. #9
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    i.e.
    | awk '{print $9}'

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

  11. #11
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Code:
    tar cvf Jun21.tar `ls -lrt | grep 'Jun 21' | awk '{print $9}'`

  12. #12
    Join Date
    Jun 2010
    Posts
    81

    It works

    Hey,

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

    Many many thanks

  13. #13
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •