Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 6455  / 2 Years ago, sat, january 8, 2022, 8:08:38

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?


More From » files

 Answers
3

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.


[#35695] Monday, January 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hirtieve

Total Points: 207
Total Questions: 104
Total Answers: 114

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
;