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,619 results
/sbin/ifconfig eth0 | grep 'inet6 addr:' | awk {'print $3'}
ls -ltp | sed '1 d' | head -n1
2011-10-17 16:21:15
Functions: head ls sed
-2

wrap it in a function if you like...

lastfile () { ls -ltp | sed '1 d' | head -n1 }
rsync -avvvz -e "ssh -i /root/.ec2/id_rsa-gsg-keypair" --archive --progress /root/.ec2/id_rsa-gsg-keypair root@ec2-75-101-212-113.compute-1.amazonaws.com:/root
2010-01-22 16:53:42
User: svnlabs
Functions: rsync
-2

Connect EC2 server with public keys "/root/.ec2/id_rsa-gsg-keypair" or "/root/.ec2/keypair.pem"

alias cd='pushd'; alias cd-='popd'
2010-11-30 16:44:46
User: conan
Functions: alias
-2

You know there 'cd -' to go to the previous directory you were standing before, but it will no record more than one. With these alias you can now record all your directory changes and go back whenever you need it. However you will have to get accustomed to use 'cd ~' from now on to go to your home directory.

for fn in xkcd*.png xkcd*.jpg; do echo $fn; read xw xh <<<$(identify -format '%w %h' $fn); nn="$(echo $fn | sed 's/xkcd-\([^-]\+\)-.*/\1/')"; wget -q -O xkcd-${nn}.json http://xkcd.com/$nn/info.0.json; tt="$(sed 's/.*"title": "\([^"]\+\)",.*/\1/' ...
2012-01-06 20:26:11
User: fpunktk
Functions: echo read wget
-2

full command:

for fn in xkcd*.png xkcd*.jpg; do; echo $fn; read xw xh <<<$(identify -format '%w %h' $fn); nn="$(echo $fn | sed 's/xkcd-\([0-9]\+\)-.*/\1/')"; wget -q -O xkcd-${nn}.json http://xkcd.com/$nn/info.0.json; tt="$(sed 's/.*"title": "\([^"]*\)", .*/\1/' xkcd-${nn}.json)"; at="$(sed 's/.*alt": "\(.*\)", .*/\1/' xkcd-${nn}.json)"; convert -background white -fill black -font /usr/share/fonts/truetype/freefont/FreeSansBold.ttf -pointsize 26 -size ${xw}x -gravity Center caption:"$tt" tt.png; convert -background '#FFF9BD' -border 1x1 -bordercolor black -fill black -font /usr/share/fonts/truetype/freefont/FreeSans.ttf -pointsize 16 -size $(($xw - 2))x -gravity Center caption:"$at" at.png; th=$(identify -format '%h' tt.png); ah=$(identify -format '%h' at.png); convert -size ${xw}x$(($xh+$th+$ah+5)) "xc:white" tt.png -geometry +0+0 -composite $fn -geometry +0+$th -composite at.png -geometry +0+$(($th+$xh+5)) -composite ${fn%\.*}_cmp.png; echo -e "$fn $nn $xw $xh $th $ah \n$tt \n$at\n"; done

this assumes that all comics are saved as xkcd-[number]-[title].{png|jpg}.

it will then download the title and alt-text, create pictures from them, and put everything together in a new png-file.

it's not perfect, but it worked for nearly all my comics.

it uses the xkcd-json-interface.

though it's poorly written, it doesn't completely break on http://xkcd.com/859/

ifconfig eth0 | grep 'HWaddr' | awk '{print $5}'
ip -f inet addr show eth0
netstat -an | grep -i listen
2009-02-19 19:27:49
User: scubacuda
Functions: grep netstat
-2

From 'man netstat'

"netstat -i | -I interface [-abdnt] [-f address_family] [-M core] [-N system]

Show the state of all network interfaces or a single interface

which have been auto-configured (interfaces statically configured

into a system, but not located at boot time are not shown). An

asterisk (``*'') after an interface name indicates that the

interface is ``down''. If -a is also present, multicast

addresses currently in use are shown for each Ethernet interface

and for each IP interface address. Multicast addresses are shown

on separate lines following the interface address with which they

are associated. If -b is also present, show the number of bytes

in and out. If -d is also present, show the number of dropped

packets. If -t is also present, show the contents of watchdog

timers."

rsync -avvvz -e "ssh -i /root/.ec2/id_rsa-gsg-keypair" --archive --progress /root/.ec2/id_rsa-gsg-keypair root@ec2-75-101-212-113.compute-1.amazonaws.com:/root
2010-01-22 17:21:58
User: lalit241
Functions: rsync
-2

Connect EC2 server with public keys "/root/.ec2/id_rsa-gsg-keypair" or "/root/.ec2/keypair.pem"

function fur () { curl -sL 'http://www.commandlinefu.com/commands/random/plaintext' | grep -v "^# commandlinefu" }
IFS=?" ; for i in * ; do mv -v $i `echo $i|tr ???????????????????\ aaaeeiooAAAEEIOOOcC_` ; done
ls -lFart |tail -n1
2011-10-17 19:49:14
User: jambino
Functions: ls tail
Tags: tail pipe ls
-2

List all files in a directory in reverse order by modified timestamp. When piped through tail the user will see the most recent file name.

cat /dev/zero > /dev/null &
2009-03-02 18:18:52
User: gustavold
Functions: cat
-2

Just waste some resources in a philosophical way

history | sed -e 's/^sudo //' | awk '{print $2}' | sort | uniq -c | sort -rn | head
2012-01-07 22:29:54
User: bibe
Functions: awk sed sort uniq
-2

I make an extensive use of sudo, so I had to exclude the sudo part of the command history

lynx -source http://www.commandlinefu.com/commands/random | sed 's/<[^>]*>//g' | head -1037 | tail -10 | sed -e 's/^[ \t]*//' | sed '/^$/d' | head -2
watch -tn1 'date -u +%T -d @$(expr $(date -d HH:MM +%s) - $(date +%s)) | toilet -f bigmono12'
2010-06-26 11:56:11
User: prayer
Functions: date expr watch
Tags: date time
-2

Change HH:MM with your target time.

This is for a Debian/Ubuntu GNU system. You need bash (package bash), date (package coreutils) and toilet (package toilet). Install with:

# apt-get install bash coreutils toilet toilet-fonts

find . -depth \( -path '*/*.java.orig' -o -path '*/*.java.orig/*' \) -delete
find . -name "*.pyc" -exec rm {} \;
2009-02-16 17:15:58
User: nsmgr8
Functions: find rm
-2

For a python project, sometimes I need to clean all the compiled python files. I have an alias 'rmpyc' to this command. This really saves me a lot of typing and hunting throughout the folders to delete those files.

grep -r 'keyword keyword2' your/path/ | grep -v svn
2009-07-08 20:48:11
User: anis
Functions: grep
-2

this command searches for a keyword or an expression in a path and avoids versionned files

mount | grep : | tr -s ' ' -d 3 | xargs umount -v
ls -F | sed -n 's/@$//p'
echo sleep() begins: %TIME% && FOR /l %a IN (10,-1,1) do (ECHO 1 >NUL %as&ping -n 2 -w 1 127.0.0.1>NUL) && echo sleep() end: %TIME%
2009-08-19 13:43:09
User: pfredrik
Functions: echo
-2

Enable 'sleep' function in Windows environment where this does not exist, although not exact in time. (there is a delay for each ping) This is a simple way to separate commands with a time-period.

find . -type d -name "*TestDir*" | wc -l
2011-01-24 22:38:27
User: linuxgeek
Functions: find wc
-2

A simple command to find the total number of subdirectories in current directory starting with specific name.

echo "1 2 3+p" | dc
find . -type f|perl -lne '@x=sort {$b->[0]<=>$a->[0]}[(stat($_))[7],$_],@x;splice(@x,11);print "@{$x[0]}";END{for(@x){print "@$_"}'
2012-01-08 14:43:43
User: bazzargh
Functions: find perl
Tags: sort perl find
-2

A different approach to the problem - maintain a small sorted list, print the largest as we go, then the top 10 at the end. I often find that the find and sort take a long time, and the large file might appear near the start of the find. By printing as we go, I get better feedback. The sort used in this will be much slower on perls older than 5.8.