23

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?

msb
  • 3,899
  • 3
  • 31
  • 38
  • What distribution are you running? – Sean Bright Sep 05 '13 at 20:56
  • 2
    possible duplicate of [How to continuosly monitor the directory using dnotify /inotify command](http://stackoverflow.com/questions/7566569/how-to-continuosly-monitor-the-directory-using-dnotify-inotify-command) – sehe Sep 05 '13 at 20:56
  • I'm running peppermint. I'm looking into 'inotify', I had never heard of it, put apparently it doesn't do what I want, just possibly helps me program a script to do it. – msb Sep 05 '13 at 21:03
  • 1
    @msb certainly will, look at the second (lowvoted) answer there. It works out-of-box on my ubuntu box (sudo apt-get install inotify-tools) – sehe Sep 05 '13 at 21:06

3 Answers3

51

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" 
devnull
  • 118,548
  • 33
  • 236
  • 227
opentokix
  • 843
  • 1
  • 5
  • 11
  • This will omit files where the creator changes the file's times to zeroes very shortly after ceation. – ott-- Sep 05 '13 at 21:28
  • as mentioned in my original question, watch is not exactly what I want, since it will repeat the last files if there aren't enough new files, AND it has a ncurses interface. :-/ – msb Sep 05 '13 at 23:59
  • Oh, sorry - I missed your mention of curses in my excitment to answer. I use watch a lot. – opentokix Sep 06 '13 at 06:15
12

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
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • Thanks, that worked. I made it a script and I'm getting $WATCH as a parameter. Thanks! – msb Sep 05 '13 at 21:08
  • Just as an additional comment, the script lists all the new files but the filesize is usually zero, and that's because the ls is executed after the file is created but before its content is added. I added a usleep before the ls and now all is perfect. :-) Can you (or me) add that to your answer? – msb Sep 05 '13 at 21:22
  • 2
    Does changing the `-e create` to `-e close_write` fix that? – Sean Bright Sep 05 '13 at 21:38
  • :o yes it does! amazing this inotify thing, I'll definitely learn more about it. thanks again. :) – msb Sep 05 '13 at 23:57
-1

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
zx485
  • 28,498
  • 28
  • 50
  • 59