I had followed several threads here in SO to copy files from one directory to another. I am using inotifywait for my purposes which is working perfectly for all BUT one scenario. It's also copying files that start with a DOT prefix (ex .tmp.swp) which I don't want.
I tried this but this even causing files with the -json
postfix to NOT get copied.
I don't want .tmp.abcd-json
to be copied. If I remove the check AFTER the &&
everything is getting copied over including the .tmp.abcd-json
:
These are some of the contents of the directory. The .tmp
ones are unwanted but it's not ALWAYS guaranteed they will always start with .tmp
. I've seen other files start with .
prefixes randomly which also need to be ignored:-
abcd-json
.tmp.abcd-json
#!/bin/sh
dir=/var/lib/docker/containers
target=/var/log/splunkf
inotifywait -m -r "$dir" --format '%w%f' -e create -e modify \
| while read file;
do
if [[ $file == "-json"* ]] && [[ $file != "."* ]];
then
echo Copying $file to $target
cp -- "$file" "$target";
else
echo NOT Copying $file to $target
fi
done
-rw-r-----. 1 root root 385710 Jan 25 19:40 82adf74ac58a5f29ae539b8ebbee360b21dda11e1c4767e4696ad264cb9a558f-json.log -rw-------. 1 root root 7185 Jan 25 19:40 .tmp-config.v2.json162008322
– Dorian McAllister Jan 25 '21 at 19:42.
and ones without.Copying /var/lib/docker/containers/a7079968c23e9da10a1969c7eb343a98e12ea685f8b3f3d2834666b0df5ab8c8/.tmp-hostconfig.json847502457 to /var/log/splunkf
– Dorian McAllister Jan 25 '21 at 19:48$file
is the whole absolute path of file, which of course will never start with a dot and always starts with/
. – binarysta Jan 25 '21 at 19:51/
and find if a file starts with.
? any suggestions how we can do that? – Dorian McAllister Jan 25 '21 at 19:53basename
it's easily possible. – binarysta Jan 25 '21 at 19:59! [[ $f == .* ]]
would also work. There's no need for a regular expression match. – Kusalananda Jan 25 '21 at 20:20