มีเชลล์สคริปต์
#!/bin/sh
inotifywait --monitor --format="%e %f" --event=create,moved_from,moved_to,delete $directory |
while read event file
do
case "$event" in
# asterisks needed because directory events end in ",ISDIR"
(CREATE*)
echo "$(date +%s) $file" >>"$index"
;;
(MOVED_TO*)
# moved_to includes overwrites, so we have to grep out the file, to make sure it wasn't already there
# use `sponge` to enable safe reading and writing of the index in the same pipeline
grep -Fv " $file" "$index" | sponge "$index"
echo "$(date +%s) $file" >>"$index"
;;
(MOVED_FROM*|DELETE*)
grep -Fv " $file" "$index" | sponge "$index"
;;
esac
done
การเข้าถึงและการแก้ไขไฟล์จะถูกละเว้นและห้ามเปลี่ยนดัชนี อย่างไรก็ตามหากคุณเปลี่ยนชื่อไฟล์การประทับเวลาจะถูกอัปเดต
ไม่จำเป็นต้องsortมีดัชนีใด ๆ เราจะต่อท้ายไฟล์ใหม่ต่อท้ายดัชนีและลบออกจากตรงกลาง
inotifywaitตามที่เรียกใช้ในสคริปต์ฟังเหตุการณ์ไฟล์และส่งออกประเภทเหตุการณ์ช่องว่างชื่อไฟล์หรือไดเรกทอรีที่ทำให้เกิดเหตุการณ์และขึ้นบรรทัดใหม่ คุณอาจแทนที่ด้วยโปรแกรมที่เทียบเท่า
spongeเพียงอ่านอินพุตทั้งหมดจากstdinลงในบัฟเฟอร์ภายในจากนั้นเขียนลงไฟล์ คุณอาจจะแทนที่ด้วยเทียบเท่าหรือสลับกันเขียนออกไปแล้ว${index}.newmv -f "${index}.new" "$index"
date +%sส่งออกวันที่และเวลาปัจจุบันเป็นจำนวนวินาทีจาก Epoch การภาวนานี้ทำงานร่วมกับทั้ง GNU และ BSD dates คุณสามารถเปลี่ยนรูปแบบ (เช่นdate '+%F %T') สำหรับดัชนีที่มนุษย์สามารถอ่านได้หรือแทนที่dateด้วยค่าที่เทียบเท่า