Find the answer to your Linux question:
Results 1 to 8 of 8
Hi guys as part of my script I need to need to delete backups if they are older than 5 days at the moment the file name goes as backup-"$(date ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    21

    How to get my script to delete files if they are certian date


    Hi guys as part of my script I need to need to delete backups if they are older than 5 days

    at the moment the file name goes as backup-"$(date +%d-%b-%y)" so something like backup-13-Feb-11.

    I have already setup in my script to make sure the backup was successful and is not corrupt what I need to do next if for example

    if I backup for 21st feburary

    backup-21-Feb-11

    if it finds anything older than five days for example

    backup-13-Feb-11 it will delete this. I want the old delete part to work on the filename not a system time stamp

    Thanks for any help
    Tom

  2. #2
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    146
    You need to use find with -atime (access time) option --

    find <location> -atime 5

    This will list on basis of the access times. Also --

    -mtime n - File's data was last modified n*24 hours ago.

    Then use -delete at the end to delete all matching files.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    21
    Quote Originally Posted by dE_logics View Post
    You need to use find with -atime (access time) option --

    find <location> -atime 5

    This will list on basis of the access times. Also --

    -mtime n - File's data was last modified n*24 hours ago.

    Then use -delete at the end to delete all matching files.
    Thank you for your help so far is there no way to do this based on my filename rather than system time stamps.

    The way Iím thinking is if todayís date is the 22nd February the backup filename will be backup-22-Feb-11 so anything older than backup-17-feb-11 will be deleted. If I making any sense.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Feb 2007
    Posts
    22
    How about a loop, like:

    for a in {1..17}
    do
    rm "backup-"$a"-feb-11";
    done

    in a script to run once, then use the mtime function to run every day to keep on top of it

  6. #5
    Just Joined!
    Join Date
    Feb 2011
    Location
    Patagonia argentina
    Posts
    9
    Perhaps the simplest way would be to use Unix time instead of conventional time format.
    But it would certainly add some garbage to the filename :-s

  7. #6
    Just Joined!
    Join Date
    Jul 2008
    Posts
    93

    delete files by date

    Quote Originally Posted by thomas130 View Post
    Hi guys as part of my script I need to need to delete backups if they are older than 5 days

    at the moment the file name goes as backup-"$(date +%d-%b-%y)" so something like backup-13-Feb-11.

    I have already setup in my script to make sure the backup was successful and is not corrupt what I need to do next if for example

    if I backup for 21st feburary

    backup-21-Feb-11

    if it finds anything older than five days for example

    backup-13-Feb-11 it will delete this. I want the old delete part to work on the filename not a system time stamp

    Thanks for any help
    Tom
    Is there a particular reason for preferring your file naming convention to the operating system date stamp? If you use the system date, you do not have to worry about details like changing from one month to another, changing from one year to another, leap years, etc. Someone else has already done that for you.

    By the way, you can use the find command with the +n flag to specify a range of date stamps.
    Code:
    $ find /directory_name -mtime +5 -delete
    will delete files of age greater than 5 days. Maybe you want
    Code:
    -mtime +4
    Until you are sure of what you really want to do, be safe and use -print instead of -delete. That way you can look at what the command would do without accidentally destroying any data.

  8. #7
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    146
    Quote Originally Posted by thomas130 View Post
    Thank you for your help so far is there no way to do this based on my filename rather than system time stamps.

    The way Iím thinking is if todayís date is the 22nd February the backup filename will be backup-22-Feb-11 so anything older than backup-17-feb-11 will be deleted. If I making any sense.
    Oh... I didn't know the file names had the dates on them.

    A bit difficult to make this if you want this complete automated. I think, for this we need to have proper shell scripting skills.

    Will try it out.

  9. #8
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by thomas130 View Post
    The way Iím thinking is if todayís date is the 22nd February the backup filename will be backup-22-Feb-11 so anything older than backup-17-feb-11 will be deleted. If I making any sense.
    You're on a hiding to nothing trying to do it by filename - even if you chose a sensible way to name your files, like using the week number and the year. It's possible to script the deletion of specific dates but, as others have pointed out, coping with a range of filenames based on dates and coping with month and year ends will certainly hone your scripting skills - if you manage to do it. I've been scripting for over 30 years, and I wouldn't bother to attempt what you're trying to do in the way you're trying to do it. find is the right tool for this job.

Posting Permissions

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