4

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

ohho
  • 1,005
  • 8
  • 19
  • 34
  • 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. –  May 23 '11 at 01:53

2 Answers2

3

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 03: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... –  May 23 '11 at 07: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 08: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 08:56
3

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.

dmourati
  • 25,540
  • 2
  • 42
  • 72