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

All commands from sorted by
Terminal - All commands - 11,611 results
diff <(echo "$a") <(echo "$b")
2009-07-15 07:26:23
User: olorin
Functions: diff echo
25

You got some results in two variables within your shell script and would like to find the differences? Changes in process lists, reworked file contents, ... . No need to write to temporary files. You can use all the diff parameters you'll need. Maybe anything like $ grep "^>"

is helpful afterwards.

perl -e 'print "P1\n256 256\n", map {$_&($_>>8)?1:0} (0..0xffff)' | display
2009-07-08 17:50:23
User: dstahlke
Functions: perl
25

OK, not the most useful but a good way to impress friends. Requires the "display" command from ImageMagick.

lsof -c dhcpd
2009-04-17 07:18:38
User: d4n3sh
25

List all file opened by a particular command based on it's command name.

curl -Is slashdot.org | egrep '^X-(F|B|L)' | cut -d \- -f 2
lynx -accept_all_cookies -cmd_script=/your/keystroke-file
2009-03-17 00:38:36
User: Alanceil
25

This command will tell lynx to read keystrokes from the specified file - which can be used in a cronjob to auto-login on websites that give you points for logging in once a day *cough cough* (which is why I used -accept_all_cookies).

For creating your keystroke file, use:

lynx -cmd_log yourfile
$ grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'
2009-03-03 20:10:19
User: netfortius
Functions: grep sed
Tags: perl sed
25

recursively traverse the directory structure from . down, look for string "oldstring" in all files, and replace it with "newstring", wherever found

also:

grep -rl oldstring . |xargs perl -pi~ -e 's/oldstring/newstring'
xset dpms force standby
file -s /dev/sd*
2010-12-28 09:56:03
User: mhs
Functions: file
Tags: file devfs
24

file(1) can print details about certain devices in the /dev/ directory (block devices in this example). This helped me to know at a glance the location and revision of my bootloader, UUIDs, filesystem status, which partitions were primaries / logicals, etc.. without running several commands.

See also:

file -s /dev/dm-* file -s /dev/cciss/*

etc..

diff <(lsusb) <(sleep 3s && lsusb)
2010-12-27 22:46:54
User: Juluan
Functions: diff sleep
24

I often use it to find recently added ou removed device, or using find in /dev, or anything similar.

Just run the command, plug the device, and wait to see him and only him

python -m http.server
2010-12-17 12:52:45
User: Alanceil
Functions: python
24

In Python version 3, the module was merged into http.server. Gentlemen, change your aliases.

find / -type f -size +500M
rm *.txt <TAB> <TAB>
2010-11-04 13:58:15
User: boschi
Functions: rm
24

alternative for "echo rm *.txt". Just doubletab the command you are willing to use and it will show you the affected files.

echo $[RANDOM%X+1]
2010-08-07 02:43:46
Functions: echo
24

If X is 5, it will about a number between 1 and 5 inclusive.

This works in bash and zsh.

If you want between 0 and 4, remove the +1.

PROMPT_COMMAND='if [ $RANDOM -le 3200 ]; then printf "\0337\033[%d;%dH\033[4%dm \033[m\0338" $((RANDOM%LINES+1)) $((RANDOM%COLUMNS+1)) $((RANDOM%8)); fi'
2010-04-01 06:52:32
User: hotdog003
Functions: printf
Tags: bash
24

Add this to a fiend's .bashrc.

PROMPT_COMMAND will run just before a prompt is drawn.

RANDOM will be between 0 and 32768; in this case, it'll run about 1/10th of the time.

\033 is the escape character. I'll call it \e for short.

\e7 -- save cursor position.

\e[%d;%dH -- move cursor to absolute position

\e[4%dm \e[m -- draw a random color at that point

\e8 -- restore position.

:w !sudo tee > /dev/null %
2010-03-24 20:04:26
User: idemal
Functions: tee
Tags: vim sudo tee
24

Write a file you edited in Vim but that you do not have the permissions to write to (unless you use sudo.) Same as #1204 but without the echo to stdout that I find annoying.

xkcd(){ wget -qO- http://xkcd.com/|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';}
2009-11-27 09:11:47
User: eightmillion
Functions: grep tee wget
24

This function displays the latest comic from xkcd.com. One of the best things about xkcd is the title text when you hover over the comic, so this function also displays that after you close the comic.

To get a random xkcd comic, I also use the following:

xkcdrandom(){ wget -qO- dynamic.xkcd.com/comic/random|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';}
read -sn 1 -p "Press any key to continue..."
clfavs(){ URL="http://www.commandlinefu.com";wget -O - --save-cookies c --post-data "username=$1&password=$2&submit=Let+me+in" $URL/users/signin;for i in `seq 0 25 $3`;do wget -O - --load-cookies c $URL/commands/favourites/plaintext/$i >>$4;done;rm -f c;}
2009-09-30 16:43:08
User: suhasgupta
Functions: c++ wget
24

Usage: clfavs username password num_favourite_commands file_in_which_to_backup

tar -cj /backup | cstream -t 777k | ssh host 'tar -xj -C /backup'
2009-07-02 10:05:53
User: wires
Functions: host ssh tar
24

this bzips a folder and transfers it over the network to "host" at 777k bit/s.

cstream can do a lot more, have a look http://www.cons.org/cracauer/cstream.html#usage

for example:

echo w00t, i'm 733+ | cstream -b1 -t2

hehe :)

./my-really-long-job.sh && notify-send "Job finished"
2009-05-07 15:50:27
User: root
Tags: notify-send
24

You will need libnotify-bin for this to work:

sudo aptitude install libnotify-bin
bash -x ./post_to_commandlinefu.sh
lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\'
2009-02-25 17:16:56
User: leftyfb
Tags: bash
24

I save this to bin/iptrace and run "iptrace ipaddress" to get the Country, City and State of an ip address using the http://ipadress.com service.

I add the following to my script to get a tinyurl of the map as well:

URL=`lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep details|awk '{print $2}'`

lynx -dump http://tinyurl.com/create.php?url=$URL|grep tinyurl|grep "19. http"|awk '{print $2}'

man -t UNIX_COMMAND | open -f -a preview
[[ test_condition ]] && if_true_do_this || otherwise_do_that
2009-02-20 21:45:21
User: stallmer
24

instead of writing:

if [[ "$1" == "$2" ]]; then

echo "$1 is equal $2"

else

echo "$1 differs from $2"

fi

do write:

[[ "$1" == "$2" ]] && echo "$1 is equal $2" || echo "$1 differs from $2"

find ~ -name '*.sqlite' -exec sqlite3 '{}' 'VACUUM;' \;
2009-02-07 10:45:41
User: ggyy_sun
Functions: find
24

Compress information DBs of firefox to speed up the launch of browser.