Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged watch from sorted by
Terminal - Commands tagged watch - 44 results
watch -n1 iptables -vnL
watch -n1 iptables -vnL
watch -d -n 2 iptables -nvL
2014-02-23 16:35:03
User: xxdesmus
Functions: iptables watch
0

This will highlight (with a box over it) any changes since the last refresh.

watch -d "ls -l /proc/$!/fd"
2014-01-31 23:51:17
User: flatcap
Functions: watch
1

You're running a program that reads LOTS of files and takes a long time.

But it doesn't tell you about its progress.

First, run a command in the background, e.g.

find /usr/share/doc -type f -exec cat {} + > output_file.txt

Then run the watch command.

"watch -d" highlights the changes as they happen

In bash: $! is the process id (pid) of the last command run in the background.

You can change this to $(pidof my_command) to watch something in particular.

watch -d 'echo -e "Remaining: `(nodetool netstats | grep " 0%" | wc -l)` \nCurrent: `(nodetool netstats | grep "%" | grep -v " 0%")`"'
2014-01-01 16:59:01
User: mrwulf
Functions: watch
0

When bootstrapping or repairing a node this is a simple way to keep tabs on what a node is actively doing.

watch -d "rabbitmqctl -q list_connections | awk '{gsub(/[ \t]+/, \"\", \$1); print \$1}' | sort | uniq -c | sort -nr"
2013-09-10 20:52:01
User: mrwulf
Functions: watch
0

Must have rabbitmqctl: https://www.rabbitmq.com/man/rabbitmqctl.1.man.html

See connections as the change, by user, sorted.

watch 'ls -tr1 | tail -n1 | xargs tail'
2013-05-09 11:37:59
User: batandwa
Functions: tail watch xargs
Tags: tail ls xargs watch
-5

Watches for file modifications in the current directory and tails the file.

watch "lsof -i -P |grep ESTABLISHED |awk '{printf \"%15.15s \\t%s\\n\", \$1, \$9}'"
2013-04-03 02:04:11
User: skarfacegc
Functions: watch
0

Shows which applications are making connections, and the addresses they're connecting to. Refreshes every 2 seconds (watch's default). Test on OSX, should work anywhere watch and lsof work.

watch -d=c -n3 'lsof -itcp -iudp -c php'
2013-03-14 01:24:50
User: AskApache
Functions: watch
Tags: lsof PHP watch
0

Shows files and processes of the command php

while sleep 1; do clear; cat /tmp/whatever.cue; done
while sleep 1; do foo; done
2012-09-14 20:21:04
User: lowbatteries
Functions: sleep
0

For use when you can't use "watch" (user-defined functions, aliases). This isn't mine - its an alternate posted in the comments by flatcap, and is the shortest and easiest to remember.

watch "asterisk -vvvvvrx 'core show channels' | egrep \"(call|channel)\""
2012-08-29 13:40:45
User: rowshi
Functions: watch
2

This handles when you have a single call or channel. Other commands will strip out the result if there is a single channel or call active because the output changes the noun to be singular instead of plural.

top -p $(pgrep -d , foo)
top '-p' $(pgrep -d ' -p ' foo)
top $(pgrep foo | sed 's|^|-p |g')
2012-06-14 15:13:00
User: michelsberg
Functions: sed top
5
pgrep foo

may return several pids for process foobar footy01 etc. like this:

11427

12576

12577

sed puts "-p " in front and we pass a list to top:

top -p 11427 -p 12576 -p 12577
watch -n 1 "awk 'NR==3 {print \"Signal strength = \" \$3 \"00 %\"}''' /proc/net/wireless"
watch -n 1 cat /proc/net/wireless
2012-06-07 10:38:21
User: cantormath
Functions: cat watch
-3

Values will depend on the driver and the hardware specifics, so you need to refer to your driver documentation for proper interpretation of those values.

watch 'curl -s --location -I http://any.site.or.url | grep -e "\(HTTP\|Location\)"'
2012-04-23 17:05:29
User: theist
Functions: grep watch
3

Watches the headers of a curl, following any redirects and printing only the HTTP status and the location of the possible redirects.

while true; do iptables -nvL > /tmp/now; diff -U0 /tmp/prev /tmp/now > /tmp/diff; clear; cat /tmp/diff; mv /tmp/now /tmp/prev; slee p 1; done
2012-04-15 00:02:33
Functions: cat diff iptables mv
0

this alternative shows the differences as they occur so that they are made plain

watch() { while true; do echo "<Ctrl+V><Ctrl+L>Every 2.0s: $@"; date; eval "$@"; sleep 2; done }
2012-03-07 09:30:15
User: hfs
Functions: echo eval sleep watch
Tags: watch
0

Usage:

watch ls -l

Basic but usable replacement for the "watch" command for those systems which don't have it (e.g. the Solaris I'm trapped on).

Type Ctrl+V to escape the following Ctrl+L which clears the screen. It will be displayed as "^L".

/usr/sbin/asterisk -rx 'core show channels' | grep -m1 "call" | cut -d' ' -f1
while true ; do echo -n "`date`";curl localhost:3000/site/sha;echo -e;sleep 1; done
2011-10-14 21:00:41
User: donnoman
Functions: echo sleep true
1

In this case it runs the command 'curl localhost:3000/site/sha' waiting the amount of time in sleep, ie: 1 second between runs, appending each run to the console.

This works well for any command where the output is less than your line width

This is unlike watch, because watch always clears the display.

tail -f ~/.bash_history
2011-09-15 19:35:09
User: totti
Functions: tail
Tags: watch
-1

Changes are displayed when they are written to the file

to exit

watch -n 2 -d '/sbin/ifconfig eth0'
$ history