Delete files created 5 mins or earlier in a folder

6,216

Solution 1

Try using find:

find ./ -mmin -5 -exec rm -i '{}' \;

Solution 2

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.

Share:
6,216

Related videos on Youtube

ohho
Author by

ohho

If you happen to drop by Hong Kong, let's have a beer together ;-)

Updated on September 18, 2022

Comments

  • ohho
    ohho over 1 year

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

    • Toby Mao
      Toby Mao almost 13 years
      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.
  • ohho
    ohho almost 13 years
    find ./ -mmin -5 returns nothing .. even there are some old files in the current folder
  • ohho
    ohho almost 13 years
    I tried find /tmp -type f -cmin -5 and it returns nothing (find /tmp -type f returns all files in /tmp. (CentOS)
  • Admin
    Admin almost 13 years
    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...
  • dmourati
    dmourati almost 13 years
    OK. Well, maybe there are really no files that new under /tmp.
  • ohho
    ohho almost 13 years
    # find --version GNU find version 4.2.27 Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX on CentOS
  • ohho
    ohho almost 13 years
    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