Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

2 Answers 2

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:

selfdestroy.sh:

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

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

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

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

Call the script like this: selfdestroy.sh /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

 #!/bin/bash
  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
1  
yes @EricCarvalho answer must perfectly solve your issue. –  devav2 Sep 7 '12 at 18:34

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.