delete a file after xx mins

Hi,

I would like to be able to delete a file after 30mins, what would the could be for that to check to make sure it 30mins and older ?

Regards,

Garry

3 Replies

put that in crontab :)

0/30 * * * * rm file

That won't work. Say the file gets created at 00:20. Ten minutes later, at 00:30, the file gets removed, but it was only ten minutes old.

If it's a regular file, try

find filename -cmin +30 -print | xargs rm

find will only output the filename if it was created at least 30 minutes ago (technically, if its ctime is more than 30 minutes ago), so if the file is newer, its filename doesn't get piped to xargs.

@smerritt:

find filename -cmin +30 -print | xargs rm


A minor gotcha with this; if you can't trust the filenames (eg someone else could have put them in) then use "-print0" and "xargs -0" because filenames with spaces, embedded returns or other funny characters could cause problems.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct