News for hackers. Stuff that matters.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by imurray on Tuesday October 26, 2010 @10:42AM (#1834353)
How many times did I want to watch a directory waiting for a file to appear in it?

For this sort of thing I use a simple perl script called atchange. I could do:

   atchange . ls
And I'd see list output whenever a new file appeared in the directory. The hacky thing I usually use it for:

   atchange my_source_file make
to make things rebuild when I save my_source_file in my editor.

The script is here: http://www.ccrnp.ncifcrf.gov/~toms/ftp/atchange The webpage about it is here: http://www-lmmb.ncifcrf.gov/~toms/atchange.html

I first learnt about it years ago while reading the entertaining ``Work'' Columns by Jeffreys Copeland & Haemer: http://alumnus.caltech.edu/~copeland/work/

Re:Watch – A Useful Linux Command You May Have Never Heard Of by jballanc on Wednesday October 27, 2010 @05:17AM (#1837865)
You can do something similar on OS X using Kicker (http://github.com/alloy/kicker), which is based on the FSEvents library.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by JoeAltmaier on Tuesday October 26, 2010 @12:05PM (#1834726)
Much better, because its doesn't keep spamming you even when nothing has changed so far.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by nitrogen on Tuesday October 26, 2010 @03:03PM (#1835552)
I was thinking you could pipe watch into uniq, but then you wouldn't get any results until you killed it. The hexdump utility has adjacent duplicate line detection, and something similar would be trivial to create.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by konad on Wednesday October 27, 2010 @06:18AM (#1837983)
Or just let the kernel tell you via inotify http://en.wikipedia.org/wiki/Inotify
Re:Watch – A Useful Linux Command You May Have Never Heard Of by imurray on Wednesday October 27, 2010 @06:32AM (#1838010)
Sort of, on Linux. See my comments elsewhere on this page for an inotify "replacement" of atchange, and notes on why it won't do quite the same thing as the perl script.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by runjake on Tuesday October 26, 2010 @11:53AM (#1834661)
A nitpicky thing, but kudos to the post author for including the word "may" in the title.

I'm not sure if my ego is being stoked or bruised, but it irks me every time I see a post like "10 Commands You've Never Heard Of"... and it's either a list of esoteric commands used by low level coders or commands that have fallen out of favor over the years.

And I'm familiar with every one of them. I've seen this round of knowledge cycle full circle a couple times now. I can't imagine the eye rolling of neckbeards who've been around professionally since the 80s.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by mkr-hn on Tuesday October 26, 2010 @02:25PM (#1835398)
What happens is someone will read 30 blogs on blogging that say "hey lists are awesome," then whip up a random list without much thought, expecting fame and fortune.

It only works if you're either exceptionally clever, or are passing out some original information.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by runjake on Tuesday October 26, 2010 @02:53PM (#1835502)
It seems like the normal readership are drawn to these kinds of list posts. Perhaps because they are "busy" and want to learn quickly by absorbing information nuggets. I question that they actually learn anything.

MSNBC (and by extension, their advertisers) has really been taking advantage of it by drawing readers to their many recent "top ten" lists.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by mkr-hn on Wednesday October 27, 2010 @12:06PM (#1839155)
Well there's different kinds of lists. A list could be a bunch of bullet points and <= 3 sentences each, or a narrow list of 3-5 where a lot is covered in each.

The latter can be extremely informative and provide a good starting point for someone. The former is filler.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by archon810 on Wednesday October 27, 2010 @04:46AM (#1837807)
That's exactly how I felt about this too, and therefore included the word "may."

I was wondering what the hell was going on with comments on that page today, before looking at the stats: http://i.imgur.com/tVhwK.png

Re:Watch – A Useful Linux Command You May Have Never Heard Of by pak on Tuesday October 26, 2010 @12:57PM (#1834978)
For the problem of watching the last lines of something: a few options, you can pipe to tac, e.g.

  watch 'ls -l | tac'
which reverses the lines of the output, or if it is something like a log file, you can forget about watch completely and just use tail -f, e.g.

  tail -f logfile.log
which will print new lines as they are added to the file.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by nodata on Tuesday October 26, 2010 @01:50PM (#1835246)
ls -lr
Re:Watch – A Useful Linux Command You May Have Never Heard Of by nphase on Tuesday October 26, 2010 @11:28AM (#1834571)
I use watch continuously for just about everything.

watch -n 1 "ps aux | grep something"

watch -n 1 "killall some-runaway-process"

etc

Re:Watch – A Useful Linux Command You May Have Never Heard Of by thingie on Tuesday October 26, 2010 @11:13AM (#1834501)
Well, checking when your directories and files change should be a job for some inotify based tool, not periodically launched ls, sadly, inotify based version of common unix utilities are still very optional and hackish, not found in the base distro.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by imurray on Tuesday October 26, 2010 @11:51AM (#1834653)
Thanks! When I first used atchange (detailed in another comment) I saw mention of things like inotify, but didn't have the kernel support on the system I was using.

Fast-forwarding to now, having just installed inotify-tools (Debian/Ubuntu package):

   while true; do inotifywait . &> /dev/null ; ls ; done
is a much more responsive version of:

   atchange . ls
I should probably make a wrapper that uses inotify when available and degrades to atchange's brute force approach when necessary.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by dbro on Tuesday October 26, 2010 @01:12PM (#1835047)
To resemble the atchange script, try this usage of inotifywait:

  while true; do inotifywait -qq -e modify [filename]; [command]; done
or as a shell function, defined in .bashrc to have the same form as the atchange perl script:

  function atchange(){
    while true;
    do inotifywait -qq -e modify $1;
    $2;
    done
Re:Watch – A Useful Linux Command You May Have Never Heard Of by imurray on Tuesday October 26, 2010 @03:58PM (#1835805)
Thanks. Below is the version I'm trying. It allows multiple-argument commands, doesn't stomp on atchange if inotifywait isn't installed and I wanted it to fire when touching files as well as modifying them.

  if which inotifywait > /dev/null ; then
      function atchange(){
          FILE="$1"
          shift
          while inotifywait -qq -e modify -e attrib "$FILE"; do
              "$@";
      done;}
  fi
Looking again at atchange it deliberately waits after first noticing the file has been modified, in case something is still modifying it. It fires once writing has stopped for at least a second. I'm not sure if I need that in my use cases, but its authors clearly did so I'm pointing out that the above shell function does not provide a true like-for-like replacement. (Also atchange allows a single argument to be a command_file, functionality I never use.)

EDIT changed the "while true" so that the function stops on an error rather than infinitely looping when something goes wrong, like FILE ceasing to exist. A file temporarily disappearing would scupper the script though, whereas the perl script deals quietly with that.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by dowskitest on Tuesday October 26, 2010 @11:22AM (#1834541)
I use 'watch' to continuously run my suite of unit tests. I write a new test, it starts failing while looping under 'watch', then I write code to make it pass.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by arsh on Tuesday October 26, 2010 @12:13PM (#1834759)
I have used it for ram memory (free) and processes (ps) monitoring.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by eru on Tuesday October 26, 2010 @01:14PM (#1835056)
Doesn't top do the same job?
Re:Watch – A Useful Linux Command You May Have Never Heard Of by djacobs on Tuesday October 26, 2010 @10:54AM (#1834421)
This would've been better as a simple link to a man page, I think.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by hugh3 on Tuesday October 26, 2010 @12:38PM (#1834887)
I think the article does a better job of explaining why you might want to use watch than man watch does.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by djacobs on Tuesday October 26, 2010 @12:58PM (#1834984)
Really? What part of the article was more useful than the man page?

  diff the-article the-man-page | wc -w
  => 75
Re:Watch – A Useful Linux Command You May Have Never Heard Of by hugh3 on Tuesday October 26, 2010 @02:50PM (#1835490)
Well then, I guess it must be the 75 words.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by djacobs on Tuesday October 26, 2010 @03:32PM (#1835695)
The summary of those 75 words is "I want to watch for file changes but am too lazy to write a Perl script. Look at this. `man watch`"

I guess we just disagree about what part of the post is useful.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by chesspro on Wednesday October 27, 2010 @01:45AM (#1837428)
Hm, is there a similar command for this on the mac (that is installed by default)?

I looked around and this is all I could find: http://www.sveinbjorn.org/watch_macosx

I have a bunch of while loops written in bash, but would prefer something cleaner.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by ldh on Thursday October 28, 2010 @11:40AM (#1843313)
You can install it using MacPorts.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by dpatru on Tuesday October 26, 2010 @09:46PM (#1836916)
Just this week I found the program "gorun" which does this. Unlike watch, gorun does not poll. It uses inotify. http://github.com/peterbe/python-gorun
Re:Watch – A Useful Linux Command You May Have Never Heard Of by surki on Tuesday October 26, 2010 @02:53PM (#1835504)
I use something like this to watch the processes that are running or waiting for IO

  watch -n 1 ps -eo pid,ppid,wchan=WIDE-WCHAN-COLUMN,stat,command r
Re:Watch – A Useful Linux Command You May Have Never Heard Of by nitrogen on Tuesday October 26, 2010 @03:06PM (#1835563)
Nice. However, it doesn't account for activity during the gap between samples, while sorting by CPU usage in top or by I/O usage in iotop does.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by fragmede on Tuesday October 26, 2010 @03:22PM (#1835640)
For the impatient, watch takes decimals:

  watch -n .2 <cmd>
and it'll go every .2 seconds
Re:Watch – A Useful Linux Command You May Have Never Heard Of by Someone on Tuesday October 26, 2010 @07:42PM (#1836626)
What an awful name for such a tool. Unix commands have the command as subject, not the user (examples: ls, find, make, touch, shutdown). From the name and the 'ls' example, I expected something that reacted to file system notifications, but this doesn't watch, it repeats a command, clearing the screen before every iteration.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by sofuture on Tuesday October 26, 2010 @08:30PM (#1836755)
You're watching the output of a command. In what world exactly, is the name 'watch' unclear or a poor choice?
Re:Watch – A Useful Linux Command You May Have Never Heard Of by thristian on Wednesday October 27, 2010 @02:48AM (#1837575)
I think the person you're responding to felt that Unix command should describe what you're telling the computer to do: "ls" makes the computer list files, "find" makes the computer find things, "echo" makes the computer echo what you give it, etc.

"watch" does not make the computer watch anything (at least in its default mode of operation); it makes the computer run a command repeatedly and the user watches it. "repeat" or "refresh" might have been more consistent names.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by Someone on Wednesday October 27, 2010 @03:35AM (#1837685)
Yes, that is what I was complaining about. An alternative, consistent with 'at', could be 'every':

  *every 2s ls*

  *echo "ls" | every 5m
For readability, I would then drop the default for the time interval; the command without the time interval (every ls) reads awfully.

And yes, that first way of specifying what to repeat is different from the way at does it. I am not sure I would want to follow 'at' in that respect.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by waynecolvin on Wednesday October 27, 2010 @06:26AM (#1838002)
It looks like watch is usually used on Linux. watch(8) for FreeBSD is something totally different.

http://www.linuxquestions.org/questions/*bsd-17/linux-watch-... gnu-watch, cmdwatch, gnuwatch etc.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by jfb on Tuesday October 26, 2010 @01:55PM (#1835269)
A couple of months ago, I was doing some nasty shell hacking, and I came across 'tsort'. It is of limited utility in my day to day existence, but when I needed it, I really needed it. Thanks, whichever hacker decided it should be a standalone utility!
Re:Watch – A Useful Linux Command You May Have Never Heard Of by silentbicycle on Tuesday October 26, 2010 @03:29PM (#1835675)
Context: tsort is a topological sorter, which is nice for handling & finding loops in dependency graphs.

IIRC there's an example in _The Awk Programming Language_ implementing a toy make out of a topological sorter. (Edit: pgs. 170-8.)

Re:Watch – A Useful Linux Command You May Have Never Heard Of by jfb on Tuesday October 26, 2010 @03:33PM (#1835698)
Right, I should have said.

EDIT: Topological sorting is one of those tools that really comes in handy -- it's easy enough to implement, but I didn't want to spend the time either (a) writing it in zsh or (b) rewriting the rest of the shell pipeline in Python or something. 'tsort' to the rescue! I just wish I could remember where I read about it so I could go buy whoever was at the other end of the Google search a beer.

Re:Watch – A Useful Linux Command You May Have Never Heard Of by brendoncrawford on Tuesday October 26, 2010 @05:45PM (#1836246)
If you want to see which mysql queries are running in almost-realtime, you could do:

  watch -n 1 -d 'echo "SHOW PROCESSLIST;" | mysql'
Re:Watch – A Useful Linux Command You May Have Never Heard Of by halfasleep on Wednesday October 27, 2010 @02:19AM (#1837501)
You might find mytop easier for this, it also allows hiding sleeping processes etc.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by brendoncrawford on Wednesday October 27, 2010 @02:41AM (#1837549)
Good tip. Thank you for pointing this out.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by alexyoung on Tuesday October 26, 2010 @07:48PM (#1836645)
It's a GNU command rather than "Linux". Does anybody care or know the difference?
Re:Watch – A Useful Linux Command You May Have Never Heard Of by endgame on Tuesday October 26, 2010 @10:46PM (#1837032)
I was wondering if it was required by SUS, and it doesn't look like it: http://www.opengroup.org/onlinepubs/9699919799/ . On my system running the Ubuntu distribution of GNU/Linux, it is provided by the procps package, with homepage at http://procps.sourceforge.net/ . I grabbed the source and had a look and there's nothing that says that it's part of GNU, nor are is the copyright owned by the FSF. Contrast this with the GNU coreutils, which mention GNU specifically in the README and have the FSF named as the copyright holder in the source files. There's very little to suggest that it's a GNU package, although calling it a "Linux" command is still technically inaccurate, I suppose, even if everyone knows what you mean. It seems to me to be like other common abuses of notation, such as saying f(x) = O(x) as opposed to f(x) \in O(x).
Re:Watch – A Useful Linux Command You May Have Never Heard Of by eru on Wednesday October 27, 2010 @01:46AM (#1837430)
Nitpick: f(x) \in O(x) is still an abuse. f \in O(id) would be better. Or f \in O(fun x -> x). Where f \in (R -> R).
Re:Watch – A Useful Linux Command You May Have Never Heard Of by endgame on Wednesday October 27, 2010 @09:49PM (#1841339)
Noted. Thanks.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by geoka9 on Wednesday October 27, 2010 @05:38AM (#1837907)
Well, it's not GNU, and it is written for Linux. So, I think calling it a Linux command is OK.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by jpablo on Tuesday October 26, 2010 @11:16AM (#1834513)
while 1; do ls; sleep 5; done
Re:Watch – A Useful Linux Command You May Have Never Heard Of by drats on Tuesday October 26, 2010 @11:30AM (#1834580)
Watch has a nice difference flag, so that whatever is different from the last iteration is highlighted. As in:

watch -d sudo netstat -tnp

Re:Watch – A Useful Linux Command You May Have Never Heard Of by jcsalterego on Tuesday October 26, 2010 @11:19AM (#1834531)
Closer to watch -

  while 1; do datetime; ls; sleep 2; done
Re:Watch – A Useful Linux Command You May Have Never Heard Of by fragmede on Tuesday October 26, 2010 @06:20PM (#1836377)
I used to do:

  while true; do date; ls; sleep 2; clear; done
but got tired of it and searched apt for watch.

  apt-get install procps
Re:Watch – A Useful Linux Command You May Have Never Heard Of by fragmede on Tuesday October 26, 2010 @06:20PM (#1836379)
I used to do:

  while true; do date; ls; sleep 2; clear; done
but got tired of that and installed procps via apt.
Re:Watch – A Useful Linux Command You May Have Never Heard Of by tedunangst on Tuesday October 26, 2010 @04:53PM (#1836042)
1: command not found
Re:Watch – A Useful Linux Command You May Have Never Heard Of by erikano on Tuesday October 26, 2010 @06:46PM (#1836461)
try this instead:

while true; do ls; sleep 5; done

Re:Watch – A Useful Linux Command You May Have Never Heard Of by naz on Tuesday October 26, 2010 @04:10PM (#1835866)
Is there an equivalent syscall for this?
Re:Watch – A Useful Linux Command You May Have Never Heard Of by konad on Wednesday October 27, 2010 @06:17AM (#1837979)
Ah, the larval stage, how cute.

When I want to monitor a directory, I let the kernel tell me via inotify.