and thanks in advance for any advice.
I'm having trouble finding an answer to my question elsewhere. I've been working on a script that will watch a directory, and then move files to various other directories based on certain criteria. Here's what I've got so far:
cd `dirname $0`/watch
while true
do
inotifywait -e moved_to ../watch --format "%f" | while read FILE
do
if $(echo "$FILE" | grep -q ".jpg"); then
cp "./$FILE" "../images/"
elif $(echo "$FILE" | grep -q ".mp4"); then
cp "./$FILE" "../videos/"
elif $(echo "$FILE" | grep -q ".mp3"); then
cp "./$FILE" "../audio/"
else
cp "./$FILE" "../other/"
fi
done
done
This script works as it should if one file at a time is moved into ./watch, but if multiple files are moved, only the first of them gets moved from ./watch to the corresponding directory. Here's another idea I have, but am unsure how this would handle problems such as trying to sort/move incomplete files:
while true
do
inotifywait -e moved_to `dirname $0`/watch && /bin/bash `dirname $0`/autotorrent.sh
done
which will launch the following script when files are added:
cd `dirname $0`/watch
for i in *; do
if $(echo "$FILE" | grep -q ".jpg"); then
cp "./$FILE" "../images/"
elif $(echo "$FILE" | grep -q ".mp4"); then
cp "./$FILE" "../videos/"
elif $(echo "$FILE" | grep -q ".mp3"); then
cp "./$FILE" "../audio/"
else
cp "./$FILE" "../other/"
fi
done
I'm just not sure how best to handle moving multiple files which may or may not take considerable time to copy. I've got several ideas, but none of them really seem to be the robust solution I'm looking for.
Thanks again to anyone who has some advice for me
grep
commands really should begrep -qF ".mp4"
or, even better,grep -q '\.mp4$'
etc. The dot in regular expressions matches any character, so you really should escape it to make it literal. Otherwise e.g. stuff likefoobarmp4
will be matched.