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 - 241 results
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
while (ps -ef | grep [r]unning_program_name); do sleep 10; done; command_to_execute
2010-01-14 16:26:34
User: m_a_xim
Functions: grep ps sleep
-2

The '[r]' is to avoid grep from grepping itself. (interchange 'r' by the appropriate letter)

Here is an example that I use a lot (as root or halt will not work):

while (ps -ef | grep [w]get); do sleep 10; done; sleep 60; halt

I add the 'sleep 60' command just in case something went wrong; so that I have time to cancel.

Very useful if you are going to bed while downloading something and do not want your computer running all night.

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.

dd if=/dev/sdc of=/dev/sdd conv=notrunc & while killall -USR1 dd; do sleep 5; done
2010-01-12 14:09:40
User: bw
Functions: dd killall sleep
3

The following command will clone usb stick inside /dev/sdc to /dev/sdd

Double check you got the correct usb sticks (origional-clone)with fdisk -l.

p(){ printf "\033[%d;%dH\033[4%dm \033[m" $((RANDOM%LINES+1)) $((RANDOM%COLUMNS+1)) $((RANDOM%8)); }; clear;while :;do p; sleep .001;done
while [ 1 ]; do banner 'ze missiles, zey are coming! ' | while IFS="\n" read l; do echo "$l"; sleep 0.01; done; done
2009-12-14 07:40:07
User: craigds
Functions: banner echo read sleep
10

Displays a scrolling banner which loops until you hit Ctrl-C to terminate it.

Make sure you finish your banner message with a space so it will loop nicely.

while [ "$(ls $filePart)" != "" ]; do sleep 5; done; mpg123 /home/.../warning.mp3
2009-12-06 10:55:09
User: mrttlemonde
Functions: mpg123 sleep
1

This command can be set as an alias in ~/.bashrc

For example (in my case) I have :

alias watchDl='while [ "$(ls $filePart)" != "" ]; do sleep 5; done; mpg123 /home/.../warning.mp3" '

Then I just need to

- initialize filePart (e.g. filePart="*bigFile*rar.part")

- launch watchDl

rdp() { ssh $1 sh -c 'PATH=$PATH:/usr/local/bin; x11vnc -q -rfbauth ~/.vnc/passwd -display :0' & sleep 4; vncviewer $1:0 & }
2009-11-25 07:21:02
User: bhepple
Functions: sh sleep ssh
1

If the remote doesn't export its desktop (eg fluxbox, blackbox etc) then you need to run a x11vnc server there and a vncviewer at the local end. This command does the lot for you - it assumes that you can 'ssh' to the box without a password and that x11vnc is installed at the remote end.

while true; do echo -ne "$(date)\r"; sleep 1; done
2009-11-17 22:45:37
User: polaco
Functions: echo sleep
12

The above code is just an example of printing on the same line, hit Ctrl + C to stop

When using echo -ne "something\r", echo will:

- print "something"

- dont print a new line (-n)

- interpret \r as carriage return, going back to the start of the line (-e)

Remember to print some white spaces after the output if your command will print lines of different sizes, mainly if one line will be smaller than the previous

Edit from reading comments: You can achieve the same effect using printf (more standardized than echo): while true; do printf "%-80s\r" "$(date)"; sleep 1; done

while killall -USR1 dd; do sleep 5; done
2009-11-09 00:27:33
User: Mikachu
Functions: killall sleep
-1

Stops when the (last) dd process exits.

wmctrl -o 2560,0 ;sleep 2 ; echo "FIRE 001" | osd_cat -o 470 -s 8 -c red -d 10 -f -*-bitstream\ vera\ sans-*-*-*--250-*-*-*-*-*-*-* ; sleep 1; wmctrl -o 0,0
2009-10-23 10:00:51
User: m33600
Functions: echo sleep
5

Strip my code to:

wmctrl -o 0,0 # autorotates to the first face. In fact [0-1279],0

wmctrl - 1280,0 # goes to the second face

wmctrl -o 2560,0 # goes to the third face, and so on.

# Use multiples of the horizontal display resolution.

My example work for 1280x800 display, been 1280 the number of interest.

Tweak the number, try a biiiig one and see your cube spinning...

I put a complex example to show how fun things can be, even for my ademco and paradox alarm central network advisor interface xpto etc. It rotates two faces, print the alarm message, and goes back tho where it was.

Tested on BIGLINUX 4.2, equivalent to ubuntu LTS hardy.

Do not forget to activate 3D efects ( compiz cube )

x=1 ; while [ $x -le 10 ] ; do lynx -dump http://www.alexa.com/siteinfo/http://[YOUR WEBSITE] | grep Global | sed 's/ \|Global\|\,//g' >> /var/log/alexa-stats.txt ; sleep 5h ; done &
2009-10-17 13:48:05
User: felix001
Functions: grep sed sleep
0

This will record the Alexa Traffic Stats to a file and run every 5 hours.

-- www.fir3net.com --

eject /dev/sdb; sleep 1; eject -t /dev/sdb
2009-10-11 23:16:49
User: Escher
Functions: eject sleep
7

Remounts a usb disk /dev/sdb, without having to physically remove and reinsert. (Gnome desktop)

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

tput smcup; echo "Doing some things..."; sleep 2; tput rmcup
2009-10-08 16:48:04
User: jgc
Functions: echo sleep tput
8

Very useful for interactive scripts where you would like to return the terminal contents to its original state before the script was run. This would be similar to how vi exits and returns you to your original terminal screen.

Save and clear the terminal contents with:

tput smcup

Execute some commands, then restore the saved terminal contents with:

tput rmcup
check_dns_no() { for i in $* ; do if `wget -O - -q http://www.norid.no/domenenavnbaser/whois/?query=$i.no | grep "no match" &>/dev/null` ; then echo $i.no "available" ; fi ; sleep 1 ;done }
2009-09-30 21:17:33
User: xeor
Functions: echo grep sleep
Tags: wget dig dns
0

Mostly for Norwegians, but easily adoptable to others. Very handy if you are brainstorming for a new domainname.

Will only display the available ones..

You can usually do this better with dig, but if you dont have dig, or the TLD only have an online service to check with, this will be usefull..

echo "Starting Daemon"; ( while :; do sleep 15; echo "I am still running =]"; done ) & disown -h -ar $!
while true; do curl -s http://www.commandlinefu.com/commands/view/3643/log-a-commands-votes | grep 'id="num-votes-' | sed 's;.*id="num-votes-[0-9]*">\([0-9\-]*\)</div>;\1;' >> votes; sleep 10; done
2009-09-26 00:55:24
User: matthewbauer
Functions: grep sleep
0

Log a command's votes,

then run:

gnuplot -persist <(echo "plot 'votes' with lines")
timeout 10 sleep 11