Question

I am monitoring the new files created in a folder in linux. Every now and then I issue an "ls -ltr" in it. But I wish there was a program/script that would automatically print it, and only the latest entries. I did a short while loop to list it, but it would repeat the entries that were not new and it would keep my screen rolling up when there were no new files. I've learned about "watch", which does show what I want and refreshes every N seconds, but I don't want a ncurses interface, I'm looking for something like tail:

  • continuous
  • shows only the new stuff
  • prints in my terminal, so I can run it in the background and do other things and see the output every now and then getting mixed with whatever I'm doing :D

Summarizing: get the input, compare to a previous input, output only what is new. Something that do that doesn't sound like such an odd tool, I can see it being used for other situations also, so I would expect it to already exist, but I couldn't find anything. Suggestions?

Was it helpful?

Solution 2

If you have access to inotifywait (available from the inotify-tools package if you are on Debian/Ubuntu) you could write a script like this:

#!/bin/bash

WATCH=/tmp

inotifywait -q -m -e create --format %f $WATCH | while read event
do
    ls -ltr $WATCH/$event
done

This is a one-liner that won't give you the same information that ls does, but it will print out the filename:

inotifywait -q -m -e create --format %w%f /some/directory

OTHER TIPS

You can use the very handy command watch

watch -n 10 "ls -ltr"

And you will get a ls every 10 seconds.

And if you add a tail -10 you will only get the 10 newest.

watch -n 10 "ls -ltr|tail -10" 

This works in cygwin and Linux. Some of the previous solutions which write a file will cause the disk to thrash.

This script does not have that problem:

SIG=1
SIG0=SIG
while [ $SIG != 0 ] ; do
 while [ $SIG = $SIG0 ] ; do
   SIG=`ls -1 | md5sum | cut -c1-32`
   sleep 10
 done
 SIG0=$SIG
 ls -lrt | tail -n 1
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top