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,856 results
ifconfig | awk '/inet addr/ {print $2 }' | sed 's/.*://g'
2010-03-04 08:15:08
User: Guyverix
Functions: awk ifconfig sed
-2

Easy way to grab the IP address of a machine for easy script use. If needed a "| grep -v 127.0.0.1" at the end will suppress localhost.

dpkg -L Your_Package
ssh -NL 12345:localhost:631 username@remote_server
sudo date mmddhhxxyyyy
find / | xargs ls -l | tr -s ' ' | cut -d ' ' -f 1,3,4,9
du -s `find . -maxdepth 1 \! -name '.'` | sort -n | tail
mount | awk '/:/ { print $3 } ' | xargs sudo umount
apt-cache show Your_package
perl -pi -e 's/([[:lower:]]+)/uc $1/gsex' file
2009-10-08 14:18:50
Functions: perl
Tags: perl
-2

same, except it works on any OS with Perl installed. DOS, Windose, whatever

find . -type f -depth -3 -mtime -5
2009-03-25 19:54:06
User: totoro
Functions: find
-2

Ever wanted to find the most recently modified files, but couldn't remember exactly where they were in a project directory with many subdirectories? The "find" command, using a combination of "-mtime -N" and "-depth -D" can be used to find those files. If your directory structure isn't very deep, just omit the "-depth -D", but if your directory structure is very deep, then you can limit the depth of the traversal using "-depth -D", where "D" is the maximum number of directory levels to descend.

ps ax| awk '/[h]ttpd/{print $1}'| xargs kill -9
find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
find `pwd` -name '.*' -prune -o \( -name *.h -o -name *.cpp \) -print | cscope -bi-
2011-11-02 08:43:40
User: kev
Functions: find
Tags: find cscope
-2

cd /

find `pwd` -name '.*' -prune -o \( -name *.h -o -name *.cpp \) -print | cscope -bi-

export CSCOPE_DB=/cscope.out

vim +'set cst'

ifconfig $DEVICE | perl -lne '/inet addr:([\d.]+)/ and print $1'
2009-07-21 13:48:19
User: jdob
Functions: ifconfig perl
Tags: IP
-2

Found this useful for scripts where I needed to work with the machine's IP. If $DEVICE is not specified, this will return all IPs on the machine. If $DEVICE is set to a network adapter, it will return just that adapter's IP.

FOR /F "tokens=3* delims=[]=," %A IN ('SET ARRAY[') DO ( echo %A -- %B )
2010-08-10 12:12:27
User: Marco
Functions: echo
-2

Loops over array of a system var, splits its values and puts the values into %A, %B, %C, %D, and so on.

Create array before, like

set ARRAY[0]=test1,100

and

set ARRAY[1]=test2,200

Be sure to replace %A, %B, etc. with %%A, %%B, etc. when using this from inside of batch files.

function crtonl { perl -i -ape 's/\r/\n/g;' $* ; }
2009-03-25 20:28:32
User: totoro
Functions: perl
Tags: files
-2

Many Mac OS X programs, especially those in Microsoft:Office, create ASCII files with lines terminated by CRs (carriage returns). Most Unix programs expect lines separated by NLs (newlines). This little command makes it trivial to convert them.

/sbin/ifconfig eth0 | grep "inet addr" | sed -e 's/.*inet addr:\(.*\) B.*/\1/g'
ruby -e 'File.foreach("list") {|i| print `nslookup #{i}`}'
locate munin | xargs rm -r
htpdate -P proxy www.google.com www.yahoo.com www.commandlinefu.com
2009-02-13 17:31:11
User: piyo
-2

HTP (HTTP Time Protocol) is an alternative way of getting "good enough" synchronized time. htpdate will give you near-second accuracy.

It works where NTP/SNTP does not because of firewalls and proxies. Of course, if NTP/SNTP can be used, use that instead.

http://www.clevervest.com/twiki/bin/view/HTP

htp is not in Ubuntu!

complete -W "$(while IFS=' ,' read host t; do echo $host; done < ~/.ssh/known_hosts)" ssh
2011-02-10 03:34:35
User: smax
-2

Simple and fast variant, not using external programs. Another variation:

complete -W "$(while read line; do echo ${line%%[, ]*}; done < ~/.ssh/known_hosts)" ssh

HashKnownHosts must be off, of course.

pss() { ps -eo pid,args | sed '/'"$1"'/!d;/sed/d' ; }
2011-03-14 15:51:49
User: vando
Functions: ps sed
-2

I know you can use pidof but with this you can know the specific PID with his command arguments (useful if you're running various proccess with same application)

command & echo $!
2011-06-08 18:16:38
User: Mahrud
Functions: command echo
-2

Actually $! is an internal variable containing PID of the last job in background.

More info: http://tldp.org/LDP/abs/html/internalvariables.html#PIDVARREF

Using $! for job control:

possibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; }
find . -user root | xargs sudo chown me:me
2012-04-24 18:29:13
Functions: chown find sudo xargs
-2

be careful where you execute this from

do a 'sudo ls' beforehand to prime sudo to not ask for your password

lgrep() { /bin/ls -A --color=always ${2:-.} | /bin/grep $1 ; }