Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
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. –  sam May 23 '11 at 1:53
add comment

2 Answers

up vote 3 down vote accepted

Try using find:

find ./ -mmin -5 -exec rm -i '{}' \;
share|improve this answer
    
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... –  kce 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
add comment

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|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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