AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.

 View Only
  • 1.  Delete files based on date

    Posted Wed July 09, 2008 05:52 AM

    Originally posted by: SystemAdmin


    Any of u have command/script to delete files based on date,without using mtime

    I used this command
    #find ./* -prune -mtime +2 -print | xargs rm


  • 2.  Re: Delete files based on date

    Posted Wed July 09, 2008 02:26 PM

    Originally posted by: alethad


    I have a for statement to calculate the number of days old to remove files. You should be able to modify it to fit your needs. I send output to a temp file for my own use. I also got an awk script(awkc) to help me with grep'ing columns which I use for alot of scripts so that I don't re-invent the wheel everytime I need to write a script.
    <hr />
    for i in $TARGET_DIR/$file.*
    do
    #echo $i
    fd=`ls -al $i |awkc 7` #get file date

    (( n = $dom - $fd )) #calculate days old
    #echo $n

    if (( $n >= 1 )); then #if greater than 1 day remove it
    #echo $dom $fd $n
    echo Removing $i >>/tmp/archive_remove.out
    rm $i 2>>/tmp/archive_remove.out
    #echo Removing $i
    fi
    done
    <hr />
    Hope that helps you.


  • 3.  Re: Delete files based on date

    Posted Thu July 10, 2008 04:45 PM

    Originally posted by: SystemAdmin


    Can you please include the awkc script? THX.


  • 4.  Re: Delete files based on date

    Posted Thu July 10, 2008 05:00 PM

    Originally posted by: esv


    what are you trying to accomplish? does the solution has to be written in ksh/awk combo? could perl be used??

    it would be a lot easier to begin with your goal and constrains, explaining carefully what have you done, then we might be able to help much better.

    best regards,
    esv.


  • 5.  Re: Delete files based on date

    Posted Fri July 11, 2008 12:08 PM

    Originally posted by: alethad


    Here's a copy of the awkc script file. I don't know perl very well but I collect different perl and unix scripts for my own use. I'm sure you could probably come up with something better if you know perl.

    The default field separator is a space. But you can change it to fit your needs.
    <hr />
    #!/bin/ksh
    1. awkc - print out one or more columns

    p=\$$( echo $1 | sed 's/,/,\$/g' )
    shift
    eval "awk '{ print $p }'" $*

    1. eof
    <hr />
    Examples of how to use the awkc. Just pipe any unix command to it to get whichever column of data you need.

    ps -ef | grep $process |awkc 2

    fd=`ls -al $i |awkc 7` #get file date
    Hope that helps.


  • 6.  Re: Delete files based on date

    Posted Fri July 11, 2008 01:54 PM

    Originally posted by: alethad


    For some reason my reply for the awkc script inserted a number where a pound sign should be.
    So ignore the 1. in the middle of the script and put in a pound sign.

    Sorry for the trouble. Not sure why it did that.


  • 7.  Re: Delete files based on date

    Posted Sun July 13, 2008 02:29 AM

    Originally posted by: kappa


    Sorry, alethad,
    I have missed the point:
    Where is the difference between awkc 2 and awk '{print $2}' ?
    You could write awk \'{print $1}\' # $1 holding the Positional Param: awkc 2 => $1=2
    Is it the same? (awkc code: I have no idea what it could do.)

    I prefer things like:
    ls *.log | perl -lne 'unlink if -M > 30' # remove files older than 30 days

    It is faster, because unlink (=rm) is a syscall , not a process.
    But I see, you prefer awkc as a filter utility.


  • 8.  Re: Delete files based on date

    Posted Thu July 10, 2008 09:03 AM

    Originally posted by: esv


    I cheat big time.

    I create a flag file with the touch command and a specific date (use -t flag)

    touch -t 200807010000 /tmp/file

    then find and delete all files older than it.
    find /tmp -type f \! -newer /tmp/file | xargs rm

    good luck.
    esv.


  • 9.  Re: Delete files based on date

    Posted Thu July 10, 2008 02:14 PM

    Originally posted by: kappa


    #find ./* -prune -name date* -print | xargs rm
    -> files based on date
    -> without using mtime

    Bad answer for a bad question.

    Please, be precise!
    What means "based on date"?
    Why not mtime? (Do you need ctime, do you need find at all?)

    Do you pay dollars for each word in your question?