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 - 234 results
while (true); do clear; uname -n; echo ""; df -h /; echo ""; tail -5 /var/log/auth.log; echo ""; vmstat 1 5; sleep 15; done
2010-08-23 04:37:58
User: roknir
Functions: df echo sleep tail uname vmstat
1

You can use this one-liner for a quick and dirty (more customizable) alternative to the watch command. The keys to making this work: everything exists in an infinite loop; the loop starts with a clear; the loop ends with a sleep. Enter whatever you'd like to keep an eye on in the middle.

chvt 7 ; sleep 2 ; DISPLAY=:0.0 import -window root screenshot.png
2010-08-20 17:28:49
User: camocrazed
Functions: chvt sleep
3

when using Gnome or KDE, you will have a hard time getting a screenshot of something like a login screen, or any other screen that occurs before the desktop environment is up and monitoring the printscreen key. (this probably applies for other DEs as well, but I haven't used them)

What this command is meant to do is take a screenshot of an X window using a command you can run from your virtual terminals (actual text terminals, not just an emulator) To do this:

Press CTRL+ALT+F1 to go to a virtual (text) terminal once your login window comes up

Login to the virtual terminal and enter the command (you'll have to type it in)

You should now have a file called screenshot.png in your home directory with your screenshot in it.

For those of you who are new to the virtual terminal thing, you can use CTRL+ALT+F7 to get back to your regular GUI

From http://www.gnome.org

maxplayer (){ while :; do xte 'mousermove -4 20'; sleep 1s; xte 'mousermove 4 -20'; sleep 2m; done& mplayer -fs "$1"; fg; }
( ( while [ 2000 -ge "$(free -m | awk '/buffers.cache:/ {print $4}')" ] || [ $(echo "$(uptime | awk '{print $10}' | sed -e 's/,$//' -e 's/,/./') >= $(grep -c ^processor /proc/cpuinfo)" | bc) -eq 1 ]; do sleep 10; done; my-command > output.txt ) & )
2010-07-13 09:12:11
User: michelsberg
Functions: echo sleep
4

[ 2000 -ge "$(free -m | awk '/buffers.cache:/ {print $4}')" ] returns true if less than 2000 MB of RAM are available, so adjust this number to your needs.

[ $(echo "$(uptime | awk '{print $10}' | sed -e 's/,$//' -e 's/,/./') >= $(grep -c ^processor /proc/cpuinfo)" | bc) -eq 1 ] returns true if the current machine load is at least equal to the number of CPUs.

If either of the tests returns true we wait 10 seconds and check again. If both tests return false, i.e. 2GB are available and machine load falls below number of CPUs, we start our command and save it's output in a text file.

The ( ( ... ) & ) construct lets the command run in background even if we log out. See http://www.commandlinefu.com/commands/view/3115/ .

while (( 1==1 )); do du -c . >> output.log; sleep 2; done; tail -f output.log
2010-07-12 17:23:45
User: aceiro
Functions: du sleep tail
-5

this command shows the space used in postgres directory.

c=0; n=8; while true; do r=`echo $RANDOM%5 |bc`; echo "sleep $r"; sleep $r& 2>&1 >/dev/null && ((c++)); [ `echo "$c%$n" | bc` -eq 0 ] && echo "$c waiting" && wait; done
2010-07-08 13:56:28
User: cp
Functions: echo sleep
5

the block of the loop is useful whenever you have huge junks of similar jobs, e.g., convert high res images to thumbnails, and make usage out of all the SMP power on your compute box without flooding the system.

note: c is used as counter and the random sleep

r=`echo $RANDOM%5 |bc`; echo "sleep $r"; sleep $r

is just used as a dummy command.

sleep 6s && notify-send -t 10000 -u critical "remember to think" &
2010-07-01 02:17:24
User: zed
Functions: sleep
5

This will be seen through your system's visual notification system, notify-osd, notification-daemon, etc.

---

sleep accepts s,m,h,d and floats (date; sleep .25m; date)

---

notify-send (-t is in milliseconds && -u low / normal / critical)

man notify-send for more information

---

notification-daemon can use b/i/u/a HTML

function countdown { case "$1" in -s) shift;; *) set $(($1 * 60));; esac; local S=" "; for i in $(seq "$1" -1 1); do echo -ne "$S\r $i\r"; sleep 1; done; echo -e "$S\rBOOM!"; }
2010-06-30 12:20:01
User: kniht
Functions: echo seq set sleep
Tags: timer counter
1

The biggest advantage over atoponce's nifty original is not killing the scrollback. Written assuming bash, but shouldn't be terribly difficult to port to other shells. S should be multiple spaces, but I can't get commandlinefu to save/show them properly, any help?

sleep 8h && while [ 1 ] ; do date "+Good Morning. It is time to wake up. The time is %I %M %p" | espeak -v english -p 0 -s 150 -a 100 ; sleep 1m; done
2010-06-23 17:34:54
User: copremesis
Functions: date sleep
3

This ran on a ubuntu box using espeak for speaking text with the bash shell. On a mac you should use 'say'. Also you can change your alarm interval and your snooze interval which are currently 8 hours and 1 minute. I would run this via cron yet it's easier to disable if you run it as a command like this :P

i=$((15*60)); while [ $i -gt 0 ]; do clear; echo $i | figlet; sleep 1; i=$(($i-1)); done;
2010-06-22 17:49:36
User: atoponce
Functions: echo sleep
Tags: figlet timer
1

Requires figlet. Other than that, this should be portable enough across all the Bourne-compatible shells (sh, bash, ksh, zsh, etc).

Produces a massive number using figlet that counts down the number of seconds for any given minute interval. For example, here's a 4-minute timer:

i=$((4*60)); while [ $i -gt 0 ]; do clear; echo $i | figlet; sleep 1; i=$(($i-1)); done;

And a 1-minute timer:

i=$((1*60)); while [ $i -gt 0 ]; do clear; echo $i | figlet; sleep 1; i=$(($i-1)); done;
MIN=1 && for i in $(seq $(($MIN*60)) -1 1); do echo -n "$i, "; sleep 1; done; echo -e "\n\nBOOOM! Time to start."
2010-06-20 15:19:12
User: atoponce
Functions: echo seq sleep
Tags: timer counter
10

Simple countdown clock that should be quite portable across any Bourne-compatible shell. I used to teach for a living, and I would run this code when it was time for a break. Usually, I would set "MIN" to 15 for a 15-minute break. The computer would be connected to a projector, so this would be projected on screen, front and center, for all to see.

while true;do clear;echo -n Current\ `grep voltage /proc/acpi/battery/BAT0/state | awk '{print $2" "$3" "$4}'`;for i in `seq 1 5`;do sleep 1;echo -n .;done;done
IFS=`echo -en "\n\b"`; for i in $(curl http://feeds.digg.com/digg/container/technology/popular.rss | grep '<title>' | sed -e 's#<[^>]*>##g' | tail -n10); do echo $i; echo $i | sed 's/^/Did you hear about /g' | say; sleep 30; done
2010-06-07 22:16:19
User: echosedawk
Functions: echo grep sed sleep tail
Tags: bash sed curl osx
-2

Instead of having someone else read you the Digg headlines, Have OSX do it. Requires Curl+Sed+Say. This could probably be easily modified to use espeak for Linux.

$COMMAND 2>&1 >/dev/null & WPID=$!; sleep $TIMEOUT && kill $! & KPID=$!; wait $WPID
2010-05-26 11:12:26
User: keymon
Functions: kill sleep wait
3

I like much more the perl solution, but without using perl. It launches a backgroup process that will kill the command if it lasts too much.

A bigger function:

check_with_timeout() {

[ "$DEBUG" ] && set -x

COMMAND=$1

TIMEOUT=$2

RET=0

# Launch command in backgroup

[ ! "$DEBUG" ] && exec 6>&2 # Link file descriptor #6 with stderr.

[ ! "$DEBUG" ] && exec 2> /dev/null # Send stderr to null (avoid the Terminated messages)

$COMMAND 2>&1 >/dev/null &

COMMAND_PID=$!

[ "$DEBUG" ] && echo "Background command pid $COMMAND_PID, parent pid $$"

# Timer that will kill the command if timesout

sleep $TIMEOUT && ps -p $COMMAND_PID -o pid,ppid |grep $$ | awk '{print $1}' | xargs kill &

KILLER_PID=$!

[ "$DEBUG" ] && echo "Killer command pid $KILLER_PID, parent pid $$"

wait $COMMAND_PID

RET=$?

# Kill the killer timer

[ "$DEBUG" ] && ps -e -o pid,ppid |grep $KILLER_PID | awk '{print $1}' | xargs echo "Killing processes: "

ps -e -o pid,ppid |grep -v PID | grep $KILLER_PID | awk '{print $1}' | xargs kill

wait

sleep 1

[ ! "$DEBUG" ] && exec 2>&6 6>&- # Restore stderr and close file descriptor #6.

return $RET

}

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 $!

mv ubuntu-10.04-rc-desktop-amd64.iso ubuntu-10.04-desktop-amd64.iso; i=http://releases.ubuntu.com/10.04/ubuntu-10.04-desktop-amd64.iso.zsync; while true; do if wget $i; then zsync $i; date; break; else sleep 30; fi; done
2010-04-29 15:49:43
Functions: mv sleep wget
4

Need to have rc iso pre-downloaded before running command.

while true; do if wget http://releases.ubuntu.com/10.04/ubuntu-10.04-desktop-i386.iso.torrent; then ktorrent --silent ubuntu-10.04-desktop-i386.iso.torrent ; date; break; else sleep 5m; fi; done
2010-04-29 13:22:54
User: ppaschka
Functions: sleep wget
1

Tested with 9.10 release. Choose whatever torrent client you prefer.

diff <(lsof -p 1234) <(sleep 10; lsof -p 1234)
2010-03-15 22:55:32
User: zlemini
Functions: diff sleep
35

This command takes a snapshot of the open files for a PID 1234 then waits 10 seconds and takes another snapshot of the same PID, it then displays the difference between each snapshot to give you an insight into what the application is doing.

while true; do xdotool getmouselocation | sed 's/x:\(.*\) y:\(.*\) screen:.*/\1, \2/' >> ./mouse-tracking; sleep 10; done
2010-02-27 04:00:13
User: matthewbauer
Functions: sed sleep
3

Will track your mouse and save it to a file.

You can use gnuplot to graph it:

gnuplot -persist <(echo "unset key;unset border;unset yzeroaxis;unset xtics;unset ytics;unset ztics;plot './mouse-tracking' with points lt 1 pt 6 ps variable")
echo "Starting Daemon"; ( while :; do sleep 15; echo "I am still running =]"; done ) & disown -h $!
2010-02-23 19:17:05
User: daleki
Functions: echo sleep
-2

You don't want the -ar parameters in this case. The man page for BASH_BUILTINS(1) states:

"-a option means to remove or mark all jobs" and

"-r option without a jobspec argument restricts operation to running jobs"

In this case we are supplying the process id of the job to disown so neither of these should be used.

sleep 3 && xdotool type --delay 0ms texthere
2010-02-18 11:44:18
User: drinkcat
Functions: sleep type
Tags: Linux
5

Just after you type enter, you have 3 seconds to switch window, then "texthere" will be "typed" in the X11 application that has focus. Very useful to beat your score at games such as "How fast can you type A-Z".

while true; do xvkbd -xsendevent -text "\[$KEY]" && sleep 2; done
2010-01-28 03:11:10
User: matthewbauer
Functions: sleep
1

Press a key automatically via xvkbd.

podwebserver& sleep 2; elinks 'http://127.0.0.1:8020'
2010-01-27 10:57:34
User: vlan7
Functions: sleep
5

Prerequisites: module Pod::Webserver installed. You can install it typing:

sudo perl -MCPAN -e 'install Pod::Webserver'

You can replace elinks with your fav browser. For FF:

podwebserver& sleep 2; firefox -remote 'openurl( http://127.0.0.1:8020/, new-tab )'

If you have Firefox open, this will pop-up the index web in a new tab.

(IFS=; sed 's/^[]0;[^^G]*^G/^M/g' <SessionLog> | while read -n 1 ITEM; do [ "$ITEM" = "^M" ] && ITEM=$'\n'; echo -ne "$ITEM"; sleep 0.05; done; echo)
2010-01-20 16:11:32
User: jgc
Functions: echo read sed sleep
Tags: read script
0

This command will play back each keystroke in a session log recorded using the script command. You'll need to replace the ^[ ^G and ^M characters with CTRL-[, CTRL-G and CTRL-M. To do this you need to press CTRL-V CTRL-[ or CTRL-V CTRL-G or CTRL-V CTRL-M.

You can adjust the playback typing speed by modifying the sleep.

If you're not bothered about seeing each keypress then you could just use:

cat session.log