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?

4
  • What distribution are you running? Sep 5, 2013 at 20:56
  • 2
    possible duplicate of How to continuosly monitor the directory using dnotify /inotify command
    – sehe
    Sep 5, 2013 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 5, 2013 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 5, 2013 at 21:06

3 Answers 3

50

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" 
3
  • This will omit files where the creator changes the file's times to zeroes very shortly after ceation.
    – ott--
    Sep 5, 2013 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 5, 2013 at 23:59
  • Oh, sorry - I missed your mention of curses in my excitment to answer. I use watch a lot.
    – opentokix
    Sep 6, 2013 at 6: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
4
  • Thanks, that worked. I made it a script and I'm getting $WATCH as a parameter. Thanks!
    – msb
    Sep 5, 2013 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 5, 2013 at 21:22
  • 2
    Does changing the -e create to -e close_write fix that? Sep 5, 2013 at 21:38
  • :o yes it does! amazing this inotify thing, I'll definitely learn more about it. thanks again. :)
    – msb
    Sep 5, 2013 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.