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 using watch from sorted by
Terminal - Commands using watch - 128 results
watch -d 'sudo smartctl -a /dev/sda | grep Load_Cycle_Count ; sudo smartctl -a /dev/sda | grep Temp'
2009-12-15 00:15:24
User: vxbinaca
Functions: grep sudo watch
2

This command is a great way to check to see if acpi is doing damage to your disks by agressivly parking the read arm and wearing down it's life. As you can see, mine has lost half its life. I'm sure this could be shortened though somehow. It will use smartctl to dump the stats and then grep out just the temperature and load cycles for the disk (a load cycle is when a the read arm comes out of park and wears on the drive).

watch -n 1 -d "finger"
watch vmstat -sSM
2009-12-04 22:35:45
Functions: vmstat watch
4

Monitor with watch command and vmstat, memory usage

watch -n 1 "ps aux | sed -n 's/ D /&/p'"
2009-12-03 18:22:44
User: lunarblu
Functions: watch
0

On my cluster a D in the states column means it is time to reboot the server.

watch -n1 -d !!
2009-11-24 21:01:14
User: TeacherTiger
Functions: watch
1

If you just executed some long command, like "ps -aefww | grep -i [m]yProcess", and if you don't want to retype it or cycle backwards in history and waste time quoting it, then you can use history substitution.

watch -n 10 killall -USR1 dd
watch -n 1 'echo "obase=2;`date +%s`" | bc'
watch -n 60 du /var/log/messages
export I=$(date +%s); watch -t -n 1 'T=$(date +%s);E=$(($T-$I));hours=$((E / 3600)) ; seconds=$((E % 3600)) ; minutes=$((seconds / 60)) ; seconds=$((seconds % 60)) ; echo $(printf "%02d:%02d:%02d" $hours $minutes $seconds) | toilet -f shadow'
2009-10-23 07:56:30
User: m33600
Functions: date echo export printf watch
0

already described on the other two versions, this one uses ascii characters on game style to display elapsed time.

export I=$(date +%s); watch -t -n 1 'T=$(date +%s);E=$(($T-$I));hours=$((E / 3600)) ; seconds=$((E % 3600)) ; minutes=$((seconds / 60)) ; seconds=$((seconds % 60)) ; echo $(printf "%02d:%02d:%02d" $hours $minutes $seconds) | osd_cat -o 20 -d 1 -p bottom'
2009-10-23 07:47:11
User: m33600
Functions: date echo export printf watch
0

Variation of the theme, this one blinks in low profile on top level of X, ie, it is visible, indeed small.

Try changing fonts and sizes of osd_cat

export I=$(date +%s); watch -t -n 1 'T=$(date +%s);E=$(($T-$I));hours=$((E / 3600)) ; seconds=$((E % 3600)) ; minutes=$((seconds / 60)) ; seconds=$((seconds % 60)) ; echo $(printf "%02d:%02d:%02d" $hours $minutes $seconds)'
2009-10-23 07:31:44
User: m33600
Functions: date echo export printf watch
3

Works on real time clock, unix time based, decrementing the actual time from initial time saved in an environment variable exported to child process inside watch

Shows elapsed time from start of script in hh:mm:ss format

Non afected by system slow down due to the use of date.

echo start > battery.txt; watch -n 60 'date >> battery.txt ; acpi -b >> battery.txt'
2009-10-19 05:28:15
User: m33600
Functions: acpi echo watch
1

This time I added a print to reemaining energy, every minute, time stamped.

The example shown here is complete and point to large discrepancies as time passes, converging to accuracy near the end.

echo start > battery.txt; watch -n 60 'date >> battery.txt'
2009-10-18 07:00:26
User: m33600
Functions: echo watch
0

Fully recharge your computer battery and start this script.

It will create or clean the file named battery.txt, print a start on it and every minute it will append a time stamp to it.

Batteries last few hours, and each hour will have 60 lines of time stamping. Really good for assuring the system was tested in real life with no surprises.

The last time stamp inside the battery.txt file is of interest. It is the time the computer went off, as the battery was dead!

Turn on your computer after that, on AC power of course, and open battery.txt. Read the first and last time stamps and now you really know if you can trust your computer sensors.

If you want a simple line of text inside the battery.txt file, use this:

watch -n 60 'date > battery.txt'

The time of death will be printed inside

watch -n60 du /var/log/messages
2009-10-09 18:37:45
User: matrixguy
Functions: du watch
8

use "watch" instead of while-loops in these simple cases

watch -n <seconds> <command>
watch -d 'ls -l'
2009-09-03 20:12:36
User: 0disse0
Functions: watch
Tags: watch stats
0

To highlight the difference between screen updates

watch 'cat /proc/loadavg'
2009-09-03 20:10:46
User: 0disse0
Functions: watch
Tags: status watch
1

If you need to keep an eye on a command whose output is changing, use the watch command. For example, to keep an eye on your load average

watch --interval 0 'iptables -nvL | grep -v "0 0"'
2009-08-22 18:18:05
User: Code_Bleu
Functions: grep watch
6

This will allow you to watch as matches occur in real-time. To filter out only ACCEPT, DROP, LOG..etc, then run the following command: watch 'iptables -nvL | grep -v "0 0" && grep "ACCEPT"' The -v is used to do an inverted filter. ie. NOT "0 0"

watch -n .5 "iwlist wlan0 scan"
2009-08-20 23:05:04
User: Abiden
Functions: watch
1

If you're like some individuals who rely on ndiswrapper and cannot use kismet, this command may be of service.

watch -n .5 "iwlist wlan0 scan | egrep 'ESSID|Encryption'"

Or...

watch -n .5 "iwlist wlan0 scan | egrep 'ESSID|Encryption' | egrep 'linksys'"

:-) Hopefully you'll find some dd-wrt compatible routers.

watch() { t=$1; shift; while test :; do clear; date=$(date); echo -e "Every "$t"s: $@ \t\t\t\t $date"; $@; sleep $t; done }
watch() { while test :; do clear; date=$(date); echo -e "Every "$1"s: $2 \t\t\t\t $date"; $2; sleep $1; done }
watch -n 0.5 ssh [user]@[host] mysqladmin -u [mysql_user] -p[password] processlist | tee -a /to/a/file
2009-08-19 14:21:27
User: lunarblu
Functions: ssh tee watch
-1

Locally watch MySQL process list update every 5s on a remote host. While you watch pipe to a file. The file out put is messy though but hey at least you have a history of what you see.

watch -n1 'cat /proc/interrupts
watch -n 10 'du -sk testfile'
2009-08-14 12:35:21
User: ianux
Functions: watch
2

watch is a command especially designed for doing this job

watch "ps auxw | grep [d]efunct"
2009-08-12 08:11:16
User: alvinx
Functions: watch
6

to omit "grep -v", put some brackets around a single character