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 sleep from sorted by
Terminal - Commands using sleep - 232 results
while :; do (mpc current | grep -i nickleback && mpc next); sleep 5; done
2013-01-15 13:27:20
User: moopet
Functions: grep sleep
1

Case insensitive. Also you can pull in the songs from a blacklist, one per line -

while :; do (mpc current | grep -i -f blacklist.txt && mpc next); sleep 5; done

while(true); do printf "%s\f" $(date +%T); sleep 1; done | sm -
2013-01-14 17:13:34
User: claudius
Functions: date printf sleep
Tags: time clock sm
1

http://www.joachim-breitner.de/projects#screen-message now also supports reading stdin continuously to update what it shows, different ?slides? separated by a form feed character. Here, we feed the current time into it each second to create a large clock.

FILE='mp3.list';LNNO=`wc -l $FILE|cut -d' ' -f 1`;LIST=( `cat $FILE` );for((;;)) do SEED=$((RANDOM % $LNNO));RNNO=$(python -c "print int('`openssl rand -rand ${LIST[$SEED]} 8 -hex 2>/dev/null`', 16) % $LNNO");mplayer ${LIST[$RNNO]};sleep 2s; done
while true; do eject && sleep `expr $RANDOM % 5` && eject -t; done;
2013-01-10 18:40:50
User: joeheyming
Functions: eject sleep
Tags: prank eject
0

Ssh to your co-worker's box and watch them go crazy when the cd player ejects

while sleep 1; do (mpc status;mpc currentsong)|awk 'BEGIN{FS=": "}/^Artist:/{r=r""$2};/^Title:/{r=r" - "$2};/^time:/{r=$2" "r};/^state: play/{f=1}END{if(f==1){print r}}'|echo -ne "\e[s\e[0;0H\e[K\e[0;44m\e[1;33m$(cat -)\e[0m\e[u";done &
2012-12-24 18:03:29
User: ichbins
Functions: awk echo sleep
Tags: bash mpd,
0

Actually this is a shorter version that fits the 255 chars limit of this resource. The full version shows status in the right top corner:

alias mpdd='while sleep 1; do _r=$(awk '\''BEGIN{FS=": "}/^Artist:/{r=r""$2};/^Title:/{r=r" - "$2};/^time:/{r=$2" "r};/^state: play/{f=1}END{if(f==1){print r}}'\'' <(mpc status;mpc currentsong));_l=${#_r};[ $_l -eq 0 ] && continue;[ -z "$_p" ] && _p=$_l;echo -ne "\e[s\e[0;${_p}H\e[K\e[u";_p=$((COLUMNS - _l));echo -ne "\e[s\e[0;${_p}H\e[K\e[0;44m\e[1;33m${_r}\e[0m\e[u";done &'

mpc is defined like this:

function mpc() {

echo "$*" | nc 192.168.1.1 6600

}

while true; do date; ps auxf | awk '{if($8=="D") print $0;}'; sleep 1; done
sleep $((3600 - ($(date +%s) % 3600) ))
2012-12-09 16:21:57
User: Mozai
Functions: date sleep
2

pauses exactly long enough to wake at the top of the hour

while sleep 1; do clear; cat /tmp/whatever.cue; done
mencoder tv:// -tv driver=v4l2:width=320:height=240:device=/dev/video1 -nosound -ovc lavc -really-quiet -quiet -o - | (sleep 10m; cat) | mplayer - -cache 512
while true;do n="$(curl -s http://news.yahoo.com/rss/|sed 's/</\n/g'|grep "title>"|sed -e '/^\// d' -e 's/title>/---------- /g' -e '1,3d'|tr '\n' ' ')";for i in $(eval echo {0..${#n}});do echo -ne "\e[s\e[0;0H${n:$i:$COLUMNS}\e[u";sleep .15;done;done &
2012-11-17 23:56:17
User: SQUIIDUX
Functions: echo eval sleep
7

This creates a permanent stock ticker in the terminal. it has scrolling action and refreshes when each cycle is done to get the latest news.

for host in $HOSTNAMES; do ping -q -c3 $host && ssh $host 'command' & for count in {1..15}; do sleep 1; jobs | wc -l | grep -q ^0\$ && continue; done; kill %1; done &>/dev/null
for host in $MYHOSTS; do ping -q -c3 $H 2>&1 1>/dev/null && ssh -o 'AllowedAuthe ntications publickey' $host 'command1; command2' & for count in 1 2 3 4 5; do sleep 1; jobs | wc -l | grep -q ^0\$ && continue; done; kill %1; done
2012-11-13 23:12:27
User: a8ksh4
Functions: grep host jobs kill ping sleep ssh wc
0

Execute commands serially on a list of hosts. Each ssh connection is made in the background so that if, after five seconds, it hasn't closed, it will be killed and the script will go on to the next system.

Maybe there's an easier way to set a timeout in the ssh options...

while true; do echo -ne "\e[s\e[0;$((COLUMNS-27))H$(date)\e[u"; sleep 1; done &
2012-11-11 02:16:21
User: SQUIIDUX
Functions: echo sleep
3

This puts a clock in the top right of the terminal. This version doesn't use tput, but uses escape codes

NUMCPUS=`grep ^proc /proc/cpuinfo | wc -l`; FIRST=`cat /proc/stat | awk '/^cpu / {print $5}'`; sleep 1; SECOND=`cat /proc/stat | awk '/^cpu / {print $5}'`; USED=`echo 2 k 100 $SECOND $FIRST - $NUMCPUS / - p | dc`; echo ${USED}% CPU Usage
2012-10-02 03:57:51
User: toxick
Functions: awk echo sleep wc
0

Using the output of 'ps' to determine CPU usage is misleading, as the CPU column in 'ps' shows CPU usage per process over the entire lifetime of the process. In order to get *current* CPU usage (without scraping a top screen) you need to pull some numbers from /proc/stat. Here, we take two readings, once second apart, determine how much IDLE time was spent across all CPUs, divide by the number of CPUs, and then subtract from 100 to get non-idle time.

while [ 1 ] ; do echo -en "$(date +%T)\r" ; sleep 1; done
xterm +u8 -fn mtx -maximized -T "There is no spoon!" -e cmatrix -bxa -u 3 & sleep .1s && wmctrl -r :ACTIVE: -b add,fullscreen && transset-df -a &
2012-09-28 04:46:28
User: bugmenot
Functions: sleep
-1
apt-get install cmatrix cmatrix-xfont wmctrl transset-df

Need to restart computer for mtx font to work

Commands while cmatrix is running:

1 - 9 = Change speed

SHIFT + 1 - 7 = change colors

Q = Quit

Tested in Ubuntu 10.04

for i in $(seq 1 100 | sort -R); do echo $i; sleep 5; done
2012-09-25 17:47:32
Functions: echo seq sleep sort
3

Random choose numbers from 1 to 100 with 5 seconds interval without duplicates.

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.

alarmclock() { [ $1 ] || echo Parameter TIME is missing. 1>&2 && return 1 ; ( sleep $1 ; for x in 9 8 7 6 5 4 3 2 1 ; do for y in `seq 0 $[ 10 - $x ] ` ; do printf "\a"; sleep 0.$x ; done ; done ) & }
2012-08-16 15:35:15
User: lkj
Functions: echo printf return sleep
0

usage: alarmclock TIME

TIME is a sleep(1) parameter which tells function how long to wait until raise the alarm.

sleep 3;import -window root output.png
while x=0; do foo ; sleep 1 ; done
2012-07-11 13:22:21
User: eastwind
Functions: sleep
-2

repeat a command every X seconds , output show the creation of partition image using fsarchiver, each line show the size of the image.

hourglass(){ trap 'tput cnorm' 0 1 2 15 RETURN;local s=$(($SECONDS +$1));(tput civis;while (($SECONDS<$s));do for f in '|' '\' '-' '/';do echo -n "$f";sleep .2s;echo -n $'\b';done;done;);}
2012-06-21 05:40:22
User: AskApache
Functions: echo sleep tput trap
14

Displays an animated hourglass for x amount of seconds

while pgrep wget || sudo shutdown -P now; do sleep 1m; done
slow () { [ -n $1 ] && while kill -STOP $1; do sleep 1; kill -CONT $1; sleep 1; done }
2012-05-16 19:16:50
User: makidoko
Functions: kill sleep
Tags: io
-1

compare to alternative :

- directly tests the -STOP of the process to continue or stop loop,

- background operator should be set (or not) at the call of the function

For extension i suggest a slowPID() based on kill like above and a slowCMD based on killall.

slow () { [ -n $1 ] && while ps -p $1 >/dev/null ; do kill -STOP $1; sleep 1; kill -CONT $1; sleep 1; done & }
2012-05-16 12:13:30
User: igorfu
Functions: kill ps sleep
Tags: io
1

Some IO intensive process make the system unresponsive. This function periodically starts/stops a process, which hopefully releases some resources for other activities.

This function is useful when ionice is not available