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 sleep from sorted by
Terminal - Commands tagged sleep - 29 results
while true; do clear;awk '{a[$3]+=1};END{for(x in a){print x,a[x]}}' /proc/[0-9]*/stat; sleep 1; done
sysctl kern.sleeptime
sysctl kern.sleeptime
sysctl -a | grep sleeptime
2013-07-29 07:57:01
User: themiurgo
Functions: grep sysctl
Tags: sleep mac os x
1

Similarly for last wake time:

sysctl -a | grep waketime
while curl -dsL example.com 2>&1 | grep 503;do sleep 8;done;echo server up
while true; do curl -vsL -o /dev/null example.com 2>&1 | grep 503 > /dev/null || echo "OK: server is up."; sleep 8; done
pmi action suspend
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

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 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.

sleep 3;import -window root output.png
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

echo mem|sudo tee /sys/power/state
delay until 16 && import_db.sh
2012-03-01 12:38:32
User: oernii2
1

the "delay" utility is an invaluable tool for me. with gnu-screen it allows you to schedule something and have it run and output to the current terminal, unlike "at".

You can also use it like "sleep" with seconds and also with date:

delay until 13:33 friday && echo test

get it from: http://onegeek.org/~tom/software/delay/current/delay.c

(author: Tom Rothamel)

for line in `cat $file`; do firefox -new-tab "$line" & 2>/dev/null; sleep 1; done
2011-11-12 13:47:24
User: hamsolo474
Functions: sleep
2

this will open a new tab in firefox for every line in a file

the sleep is removable but i found that if you have a large list of urls 50+, and no sleep, it will try to open all the urls at once and this will cause them all to load a lot slower, also depending on the ram of your system sleep gives you a chance to close the tabs before they overload your ram, removing & >2/dev/null will yield unpredictable results.

MIN=10;for ((i=MIN*60;i>=0;i--));do echo -ne "\r$(date -d"0+$i sec" +%H:%M:%S)";sleep 1;done
2011-02-20 11:56:28
User: flatcap
Functions: echo sleep
11

Countdown clock - Counts down from $MIN minutes to zero.

I let the date command do the maths.

This version doesn't use seq.

cycle(){ while :;do((i++));echo -n "${3:$(($i%${#3})):1}";sleep .$(($RANDOM%$2+$1));done;}
2010-10-08 23:45:40
User: putnamhill
Functions: echo sleep
Tags: sleep random
1

Cycles continuously through a string printing each character with a random delay less than 1 second. First parameter is min, 2nd is max. Example: 1 3 means sleep random .1 to .3. Experiment with different values. The 3rd parameter is the string. The sleep will help with battery life/power consumption.

cycle 1 3 $(openssl rand 100 | xxd -p)

Fans of "The Shining" might get a kick out of this:

cycle 1 4 ' All work and no play makes Jack a dull boy.'
dbus-send --system --print-reply --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend
2010-08-05 00:16:40
User: ianux
Tags: sleep suspend
1

No need to be root to do that. Relies on UPower (previously known as DeviceKit-Power).

sudo pm-suspend
sudo /etc/acpi/sleep.sh sleep
2010-08-03 23:54:49
User: jmfork
Functions: sudo
Tags: sleep suspend
-3

It suspends to RAM: you always need your batteries for the RAM but it saves time as there is no need to slowly archive everything on your hard disk.

It works fine with me but if anyone has a nicer way, please contribute.

tail -n0 -f access.log>/tmp/tmp.log & sleep 10; kill $! ; wc -l /tmp/tmp.log
2010-04-29 21:23:46
User: dooblem
Functions: kill sleep tail wc
Tags: tail kill wc sleep
1

Another way of counting the line output of tail over 10s not requiring pv.

Cut to have the average per second rate :

tail -n0 -f access.log>/tmp/tmp.log & sleep 10; kill $! ; wc -l /tmp/tmp.log | cut -c-2

You can also enclose it in a loop and send stderr to /dev/null :

while true; do tail -n0 -f access.log>/tmp/tmp.log & sleep 2; kill $! ; wc -l /tmp/tmp.log | cut -c-2; done 2>/dev/null

very_long_command& sleep 10; kill $!
2010-04-29 20:43:13
User: dooblem
Functions: kill sleep
5

or "Execute a command with a timeout"

Run a command in background, sleep 10 seconds, kill it.

! is the process id of the most recently executed background command.

You can test it with:

find /& sleep10; kill $!

dd if=fromfile of=tofile & DDPID=$! ; sleep 1 ; while kill -USR1 $DDPID ; do sleep 5; done
2010-01-12 15:01:44
User: deltaray
Functions: dd kill sleep
Tags: dd kill while sleep
4

This is a more accurate way to watch the progress of a dd process. The $DDPID=$! is needed so that you don't get the PID of the sleep. The sleep 1 is needed because in my testing at least, if you run kill -USR1 against dd too quickly, it will kill it off instead of display the status. So you need to wait a second, probably so that it can configure itself to trap the USR1 signal.

( trap '' 1; ( nice -n 19 sleep 2h && command rm -v -rf /garbage/ &>/dev/null && trap 1 ) & )
2

Check out the usage of 'trap', you may not have seen this one much. This command provides a way to schedule commands at certain times by running them after sleep finishes sleeping. In the example 'sleep 2h' sleeps for 2 hours. What is cool about this command is that it uses the 'trap' builtin bash command to remove the SIGHUP trap that normally exits all processes started by the shell upon logout. The 'trap 1' command then restores the normal SIGHUP behaviour.

It also uses the 'nice -n 19' command which causes the sleep process to be run with minimal CPU.

Further, it runs all the commands within the 2nd parentheses in the background. This is sweet cuz you can fire off as many of these as you want. Very helpful for shell scripts.