Bash script to check if a new file has been created on a directory after run a command
Thanks to informative comments, I've just realized that I've missed the basics of bash script but finally made that work. I'll leave my solution here as an answer for those who struggle like me.:
WATCH_DIR=./tmp
FILES_BEFORE=$(ls $WATCH_DIR)
echo >$WATCH_DIR/filename
FILES_AFTER=$(ls $WATCH_DIR)
if diff <(echo "$FILES_AFTER") <(echo "$FILES_BEFORE")
then
echo "No changes"
else
echo "Changes"
fi
It outputs "Changes" on the first run and "No Changes" for the other unless you delete the newly added documents.
Monitor Pre-existing and new files in a directory with bash
Once inotifywait
is up and waiting, it will print the message Watches established.
to standard error. So you need to go through existing files after that point.
So, one approach is to write something that will process standard error, and when it sees that message, lists all the existing files. You can wrap that functionality in a function for convenience:
function list-existing-and-follow-modify() {
local path="$1"
inotifywait --monitor \
--event modify \
--format %f \
-- \
"$path" \
2> >( while IFS= read -r line ; do
printf '%s\n' "$line" >&2
if [[ "$line" = 'Watches established.' ]] ; then
for file in "$path"/* ; do
if [[ -e "$file" ]] ; then
basename "$file"
fi
done
break
fi
done
cat >&2
)
}
and then write:
list-existing-and-follow-modify "$path" \
| while IFS= read -r file
# ... work on/with "$file"
# move "$file" to a new directory
done
Notes:
- If you're not familiar with the
>(...)
notation that I used, it's called "process substitution"; see https://www.gnu.org/software/bash/manual/bash.html#Process-Substitution for details. - The above will now have the opposite race condition from your original one: if a file is created shortly after
inotifywait
starts up, thenlist-existing-and-follow-modify
may list it twice. But you can easily handle that inside yourwhile
-loop by usingif [[ -e "$file" ]]
to make sure the file still exists before you operate on it. - I'm a bit skeptical that your
inotifywait
options are really quite what you want;modify
, in particular, seems like the wrong event. But I'm sure you can adjust them as needed. The only change I've made above, other than switching to long options for clarity/explicitly and adding--
for robustness, is to add--format %f
so that you get the filenames without extraneous details. - There doesn't seem to be any way to tell
inotifywait
to use a separator other than newlines, so, I just rolled with that. Make sure to avoid filenames that include newlines.
How to track order of newly created files in directory with fswatch
Bash script handle_order.sh
that will maintain order in the track_order
text file
#!/bin/bash
echo "new file is added $1"
filename=$(basename ${1})
echo $filename
if [ $filename != "track_order.txt" ]; then
echo "${filename}" >> track_order.txt
else
echo "order is updated"
fi
fswatch
command
fswatch ./DRS-1.1.0/ --event Created | xargs -I '{}' sh -c './handle_order.sh "$1"' - {}
(./DRS-1.1.0/)
your directory name which needs to be monitor(handle_order.sh)
bash script that will handle the order and update track_order.txt file(Created)
only create an event
Updated:
remove the handle_order.sh
and run as a single command
fswatch ./DRS-1.1.0/ --event Created | xargs -I '{}' sh -c 'filename=$(basename ${1});echo $filename;if [ $filename != "track_order.txt" ]; then echo "${filename}" >> track_order.txt; else echo "order is updated";fi' - {}
Related Topics
Check If Opencv Is Compiled with Tbb
Jna Link Issue While Starting Cassandra Rhel 6.5
How to Timeout a Group of Commands in Bash
Flags in Objdump Output of Object File
Filename Last Modification Date Shell in Script
Makefile for Linux Kernel Module
Linux How to Add a File to a Specific Folder Within a Zip File
Passing Environment Variables Not Working with Docker
How to Create a Zip File Without Entire Directory Structure
Sshpass: Command Not Found Error
Padding Empty Field in Unix Join Operation
How to Check If a File Contains Only Zeros in a Linux Shell
Installing Gcc on Linux Without C Compiler
Why Do We Need to Call Poll_Wait in Poll
Sorting CSV File by 5Th Column Using Bash