30

Possible Duplicate:
Is it possible to follow a command (run repeatedly)? as one would follow a file using tail -f?

I would like to monitor files that are being downloaded to a directory in real time on screen in bash.

Is there an easy way in Linux to do the equivalent of tail -f but on a directory, perhaps using ls?

marked as duplicate by Gilles, jasonwryan, daisy, Renan, Stéphane Gimenez Sep 15 '12 at 16:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 8
    watch -n1 ls? – jordanm Sep 11 '12 at 1:34
  • 2
    If you want to react to new files being created, what you need is inotify. – Gilles Sep 12 '12 at 1:38
46

Use the "watch" command:

watch ls

This will run the "ls" command every 2 seconds.

  • 4
    This is the simplest answer that worked. I wanted to add that -n# dictates how often to refresh. eg. watch -n1 ls - will run ls every second and refresh the data on screen. – T. Brian Jones Sep 11 '12 at 18:48
  • [For my own notes as Google will bring me back here] On OSX you can do while :; do clear; ls; sleep 2; done – Dan Rosenstark Nov 22 '17 at 20:10
8

watch is definitely the best answer if you've got it. But for completeness, you could simulate something crudely similar with a simple bit of shell-script:

while (true)
do
     date
     ls -lrt | tail -n 10
     sleep 2
     clear
done

Another way could be using diff. This one will also point out if files are removed that used to be present. An advantage is it doesn't clear the terminal, so you can see the complete record of all changes by scrolling up. A disadvantage is that it creates two temporary files to manage the state change reasoning.

ls1="/tmp/listing1.dat"
ls2="/tmp/listing2.dat"
ls -lrt > ${ls1}
ls -lrt > ${ls2}

while (true)
do
     diff ${ls1} ${ls2} | grep '[<>]'
     sleep 2
     cp ${ls2} ${ls1}
     ls -lrt > ${ls2}
done
  • Recovery boot does not have watch so this is handy. – a--m Jun 12 '18 at 4:57
5

Check out inotifywait,

e.g to monitor folder abc, you could do:

while inotifywait -e close_write abc; do
   # do `ls` when abc changed
   ls abc
done
  • This will not run the ls until the download has completed (or at least it calls close() on the file). Still, +1 for using inotify. – jordanm Sep 11 '12 at 2:37

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