How to track order of newly created files in directory with fswatch

1.2k Views Asked by At

I need to monitor a downloads directory, and keep track of the order in which files were written to the directory. The only way I know this is possible in macos is fswatch, but the documentation seems quite sparse compared to inotifywatch on linux.

For example, if 001.jpg then 003.jpg then 002.jpg were downloaded to the directory (in that order), I would need a variable that looks like this: 001.jpg 003.jpg 002.jpg

I only need to monitor new files in this case, not modified files. For example, if 001.jpg is modified, it should not be moved to the end of the list.

1

There are 1 best solutions below

2
Adiii On BEST ANSWER

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' - {}

enter image description here