Remove files older than x days, filtered by files that i don't want to delete

Discussion in 'General Linux' started by Bram, Jul 3, 2012.

  1. Bram

    Bram New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi All

    I need to perform routine maintenance on a server every 3 weeks, and will need to remove various files and directories. But files in one directory, i need to keep. From what i have found, this is how to remove files older than x days:


    find /path/to/files* -mtime +5 -exec rm {} \;

    Does this mean that only the files highlighted in red will be deleted? The server is running on SuSe 9.
  2. Kerms

    Kerms New Member

    Messages:
    59
    Likes Received:
    12
    Trophy Points:
    0
    -mtime has the resolution to the day; if the file was created at 00:00 or at 23:59, regardless of when during the day the command was executed, it'll count it.

    find /path/to/files/ -mtime +21 -exec rm -rf {} +
    This finds all files AND folders older than three weeks worth of days.

    If you just want files, append ` -type f ' after the /path/to/files/
    If you want to have a sharper resolution, try ` -mmin +n', where n is the number of minutes the file has to be older than (Same rules follow, but for minutes).

    Also, +n means older than n time, n (by itself) means exactly n time, -n means less than n time.

    Finally: to keep that special folder:
    find /path/to/special/folder/ -exec touch {} +
    This touches everything in the folder (including the folder itself), and changes the modification time to the current time of the execution.

    If the modification dates in the special directory is important, I suggest to move it out of the routinely-cleaned directory, and symbolically link it back in (although find will detect it if it hasn't been re created).

    I'd run all of this as a cron job:

    rm /path/to/files/symlink
    ln -s /path/to/moved/special /path/to/files/symlink #To update symlink (touch doesn't work on symlinks)
    find /path/to/files/ -mmin +4320 -exec rm -rf {} + #4320 == 3 weeks in minutes

    Lastly I highly suggest reading the man pages of find and other utilities, and play with them (in isolated testing grounds, of course!)

    (Sorry for necrobump.)
  3. diegosuse

    diegosuse New Member

    Messages:
    65
    Likes Received:
    3
    Trophy Points:
    0
    cron

    Can be done by a cron job. May be the easy way.

Share This Page