I want to check a specific directory in linux bash every 2 second and if any file exists there make a copy of that file to a directory before a running daemon process hijacks that file (make some processing and modification on that file and quickly move the processed file to another directory).
I tried this:
while true; do
cp * /home/a/*;
sleep 1;
done
But it does not work: I guess my cp
syntax must be wrong. I tried /home/a/*
, /home/a
and /home/a/
but none of them worked.
Is there any way I can do this in bash shell?
You're nearly there, but you need to account for the case where you have no files in that directory.
With the right options in bash
, it's fairly easy:
#! /bin/bash
shopt -s nullglob # to get an empty list if no file is present
while true ; do
for i in * ; do
echo "Intercepted $i"
cp "$i" /home/a/
done
sleep 1
done
(Note that this is pretty unreliable in general. You could copy partially written-to files, miss files completely, etc.)