On CentOS, how can I delete files which are created 5 mins ago or earlier in a folder? Thanks!

  • As a starting point, it would most likely involve crond, find and awk. I'll try and figure out the commands but there is probably someone on here more qualified than me to answer this. – Smudge May 23 '11 at 1:53

Try using find:

find ./ -mmin -5 -exec rm -i '{}' \;
  • find ./ -mmin -5 returns nothing .. even there are some old files in the current folder – ohho May 23 '11 at 3:41
  • find ./ -mmin -5 should return a list of all files modified in less than or equal to five minutes. I tested this on my Debian system with success. Regardless, you want to use find to do this... – user62491 May 23 '11 at 7:00
  • # find --version GNU find version 4.2.27 Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX on CentOS – ohho May 25 '11 at 8:52
  • 1
    for files created 5 mins or earlier, it's find . -cmin +5. find . -cmin -5` is files created within last 5 minutes. -cmin +5 is different from -cmin 5. thanks – ohho May 25 '11 at 8:56

Simmilar to kce:

find /path/to/folder -type f -cmin -5 -print0|xargs -r0 rm 

Added a search for files only and used cmin instead of mmin as you asked for files created in the past five minutes. I like to combine find with xargs.

In UNIX, we typically refer to them as directories, not folders. I used your name in the path above.

  • I tried find /tmp -type f -cmin -5 and it returns nothing (find /tmp -type f returns all files in /tmp. (CentOS) – ohho May 23 '11 at 6:22
  • OK. Well, maybe there are really no files that new under /tmp. – dmourati May 23 '11 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.