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 grep from sorted by
Terminal - Commands using grep - 1,530 results
netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
egrep -ie "<*HREF=(.*?)>" index.html | awk -F\" '{print $2}' | grep ://
for i in `ps aux | grep httpd | awk '{print $2}'`; do lsof -n -p $i | grep ESTABLISHED; done;
2009-02-05 17:50:52
User: Shadow
Functions: awk grep
6

Sometimes apache will get stuck in an established state where you can't get a list of the connecting IP's from mod_status... not a good thing when you need to ban an abusive ip.

svn st | grep "^\?" | awk "{print \$2}" | xargs svn add $1
for x in `git status | grep deleted | awk '{print $3}'`; do git rm $x; done
ps -ef | grep APP | awk '/grep/!{print$2}' | xargs -i kill {}
2009-02-05 16:10:05
User: hooobs
Functions: awk grep kill ps xargs
-2

Probably posted previously, I use this all the time to find and kill a process for "APP". Simply replace "APP" with the name of the process you're looking to kill.

alias pi='`cat ~/.pi | grep ' ; alias addpi='echo "cd `pwd`" >> ~/.pi'
2009-02-05 15:46:59
User: senthil
Functions: alias grep
8

example:

--------------------------------------------------------------------------------------------

user@ubuntu:~/workspace/SVN/haystak-repos/trunk/internal/src$ addpi

--------------------------------------------------------------------------------------------

Now that directory is in the list of fast access directories.

You can switch to it anytime like this:

--------------------------------------------------------------------------------------------

user@ubuntu:~$ pi internal`

user@ubuntu:~/workspace/SVN/haystak-repos/trunk/internal/src$ --------------------------------------------------------------------------------------------

Please note the backquote ( the symbol that shares its key with ~ in the keyboard )

pi will switch you to that directory.

To see the list of all fast access directories you have to say "cat ~/.pi"

ps -e | grep SearchStringHere
lsof -i -n | grep ESTABLISHED
2009-02-05 15:28:11
User: systemj
Functions: grep
5

Uses lsof to list open network connections (file descriptors), grepping for only those in an established state

ps -ef | grep $USER
2009-02-05 14:57:39
User: ryodoan
Functions: grep ps
0

I like to make it an alias in my .bashrc file, as such:

alias psme='ps -ef | grep $USER'

mysql -u uname dbname -e "show tables" | grep -v Tables_in | grep -v "+" | gawk '{print "optimize table " $1 ";"}' | mysql -u uname dbname
mysql -u uname dbname -e "show tables" | grep -v Tables_in | grep -v "+" | gawk '{print "drop table " $1 ";"}' | mysql -u uname dbname
lspci | grep Ether | awk '{ VAR=$1; split(VAR,ARR,"."); count[ARR[1]]++; LINE=$0; split(LINE,LINEARR,":"); LINECOUNT[ARR[1]]=LINEARR[3]; } END { for(i in count) { printf("PCI address: %s\nPorts: %d\nCard Type: %s\n", i, count[i], LINECOUNT[i]) } }'
echo "SHOW PROCESSLIST\G" | mysql -u root -p | grep "Info:" | awk -F":" '{count[$NF]++}END{for(i in count){printf("%d: %s\n", count[i], i)}}' | sort -n
ps awwwux | grep httpd | grep -v grep | awk '{mem = $6; tot = $6 + tot; total++} END{printf("Total procs: %d\nAvg Size: %d KB\nTotal Mem Used: %f GB\n", total, mem / total, tot / 1024 / 1024)}'
sudo lsof | awk '{printf("%s %s %s\n", $1, $3, $NF)}' | grep -v "(" | sort -k 4 | gawk '$NF==prv{ct++;next} {printf("%d %s\n",ct,$0);ct=1;prv=$NF}' | uniq | sort -nr
grep -v "^#" file.txt | more
ps -ef | grep [f]oo | awk '{print $2}' | xargs kill -9
2009-02-05 13:43:01
User: eredicatorx
Functions: awk grep kill ps xargs
1

Kill all processes with foo in them. Similar to pkill but more complete and also works when there is no pkill command.

Works on almost every Linux/Unix platform I have tried.

alias 'ps?'='ps ax | grep '
nm --defined-only --print-file-name lib*so 2>/dev/null | grep ' pthread_create$'
strings libc-2.2.5.so | grep stat.h
diff -q dir1/ dir2/ | grep differ | awk '{ print "vimdiff " $2 " " $4 }'
2009-02-05 12:37:00
User: binaryslate
Functions: awk diff grep
1

We use this to quickly highlight differences and provide a quick way to cut and paste the command to view the files using the marvellous vimdiff

screen -ls | grep main && urxvt -name screen -e screen -x main || urxvt -name screen -e screen -R -S main
2009-02-05 10:51:31
User: trey
Functions: grep screen
0

Open a terminal (urxvt in this case) to connect to a screen session (named "main") and create the screen session if necessary.

find . -type f -print0 | grep -vzZ '.svn' | xargs -0 grep --color -nEHi "SEARCHTERM"
2009-02-05 10:27:15
User: troelskn
Functions: find grep xargs
-3

Variant of find grep that ignores files with .svn in the name. Useful for searching through a local repository of source code.

zcat /usr/share/man/man1/grep.1.gz | grep "color"
2009-02-04 09:38:45
User: root
Functions: grep zcat
-7

This decompresses the file and sends the output to STDOUT so it can be grepped. A good one to put in loops for searching directories of gzipped files, such as man pages.