Remove files of specific file type as soon as the file appears

73 views Asked by At

My company's program generates lots of data with many different file types. One of them, .log file type uses much space and since I do not need log files I would like to remove them as soon as the program creates them. Unfortunatelly I cannot disable log creation option, as the program simply does not offer such an option. I wonder: is running find with -delete option in a while true loop best option, or is there any better/recommended option?

while true
do
    find -type f -name "*.log" -delete
done
2

There are 2 answers

0
tojo On BEST ANSWER

You could use incrond daemon to listen filesystem changes and delete file when it appears or just use cron to delete *.log files in every minute as more simpler solution.

2
Jürgen Hötzel On

If you have inotify-tools (which should be part of every Linux distribution) installed, this script will block-wait for file-creation events and remove the corresponding files:

inotifywait -e create --monitor --format "%w%f"  ${YOUR_LOG_DIR} | egrep --  line-buffered "\.log$" \
        | while read -r file; do
            rm -v $file;
          done