Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Ubuntu 12.04 and I am here to know: is there any way to limit the life time of a file?

I mean, for example, I have created a file that has to be automatically deleted 10 minutes after I close it. Is there any way to do it?

share|improve this question
up vote 2 down vote accepted

This can be easily accomplished using inotifywait from inotify-tools package (it's not installed by default):

inotifywait -e close /path/to/file && sleep 600 && rm /path/to/file

This will set up a watch on /path/to/file and wait for it to be closed. Then after 10 minutes (600 seconds) the file will be removed.

Here's a script to make things easier:

if [ $# != 2 ]; then
        echo "$0 <file> <time>"
        exit 1

if [ ! -f $1 ]; then
        echo "File $1 not found."
        exit 1

if (( $2 < 1 )); then
        echo "Time must be > 0."

(inotifywait -e close $1
sleep $2
rm $1) 2>1 >/dev/null &

Call the script like this: /path/to/file 600.

share|improve this answer

You need to write a script to perform this.

Using the commands

  • access (read the file's contents) - atime
  • change the status (modify the file or its attributes) - ctime
  • modify (change the file's contents) - mtime

  • cron

  • find

Sample Script to remove the '.bak' file

  find <location> -mtime <value> -type f -name "*.bak" -exec rm -f {} \;

Change the permission of the script (execute)

add the script to cron and make it run when needed.

share|improve this answer
Thank you for the answer +1 :) , but Eric gave me as exactly i need. :D – Raja Sep 7 '12 at 18:23
yes @EricCarvalho answer must perfectly solve your issue. – devav2 Sep 7 '12 at 18:34

Your Answer


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.