How to delete files more than 30 minutes old

Posted on 2005-05-12
Last Modified: 2010-04-21
I just need the simplest way to create a shell script/cron
that will delete files once they are older then 5 mins or 30 minutes.
the files would be limited to one directory and all have the same ending prefix that will be uniq to
them. for examp  1233.tmp 1234.tmp 1235.tmp


Question by:Troder
    LVL 45

    Expert Comment


    touch -t`date +%Y%m%d%H%M --date "30 min ago"` tmpfile
    for file in /dir/*
    [[ "$file" -ot tmpfile ]] && echo "$file is older"
    rm -f tmpfile

    That may work for you.  What's your host O/S?

    LVL 61

    Accepted Solution

    find . -type f -ctime +5m -name [0-9]+\.tmp -print | xargs -n 5 rm -f

    each option
    LVL 61

    Expert Comment

    1) it finds files
    2) created so long ago
    3) my guess for regex to match your filename examples

    and finally - dlete five of them at a time.

    What are you trying to accomplish by such a script ???
    It will not help with disk space shortage. Open files take space until closed. If you post uname -a you will get info how to handle them
    LVL 23

    Expert Comment

    gheist, you need to quote the regex filename pattern to prevent the shell
    from expanding it.

    find . -type f -ctime +5m -name "[0-9]+\.tmp" -print | xargs -n 5 rm -f

    LVL 61

    Expert Comment

    right ...
    LVL 61

    Expert Comment

    brett actually fixed my command line ....

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. Please see for the updated article. It is avail…
    This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now