How to specify file extension when using inotifywait

235 views Asked by At

How do I specify file extensions to be monitored when using inotifywait ?

Given this code

cd "tmp"
inotifywait -me close_write --format %f . | while read file
do  rclone move "$file" …
done

Where to add the file extensions in the code? I tried replacing

--format %f .

with

--format %f *.tmp,*.tmp2 

But still doesnt work as intended

2

There are 2 answers

0
igor_andreev On BEST ANSWER
cd tmp
inotifywait -m . -e close_write -e moved_from | # -m is --monitor, -e is --event, what you need
while read path action file; do
 if [[ "$file" =~ .*txt$ ]]; then # if suffix is '.txt'
  echo ${path}${file} ': '${action}
  rclone moveto $file remote:dir/$file
  echo 'Continue watching...'
 fi
done
0
Alex  Bachynskyi On

I'm using "--include" for this

inotifywait -m -e modify "/tmp/" --include '.*\.log$' |
while read -r dir action file; do
    echo "The file '$file' modified in directory '$dir' via '$action'"
done

Will track if files with .log extension have been modified