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,569 results
kill -HUP ` ps -aef | grep -i firefox | sort -k 2 -r | sed 1d | awk ' { print $2 } ' `
mount | grep : | tr -s ' ' -d 3 | xargs umount -v
tail -f <filename> | grep -C <# of lines to show above and below> <text>
cflow file.c | grep ':$' | sed 's/ <.*//'
guid(){ lynx -nonumbers -dump http://www.famkruithof.net/uuid/uuidgen | grep "\w\{8\}-" | tr -d ' '; }
sed "s/^ABC/+ABC/" <file | sed "s/DEF$/DEF+/" | tr "\n" "~" | tr "+" "\n" | grep "^ABC" | tr "~" "\n"
2009-02-06 11:29:29
User: poboxy
Functions: grep sed tr
2

If the file content is :

-

Blah blah blah

ABC

hello blah blah blah

bloh bloh bloh

DEF

Bah bah bah

-

You'll get:

-

ABC

hello blah blah blah

bloh bloh bloh

DEF

find . -exec grep "test" '{}' /dev/null \; -print
grep -r -l xxxxx . | xargs perl -i -pe "s/xxxxx/yyyyy/g"
2009-02-06 08:18:50
User: hassylin
Functions: grep perl xargs
-1

This script first find all files which contains word xxxxx recursively. Then replace the word xxxxx to yyyyy of the files.

Use case:

- Web site domain change

- Function name change of the program

find DIR -name "FILE" -exec grep -IHn STRING {} \;
kill -HUP `ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]' | awk '{print $2}'`
2009-02-06 02:42:14
User: liupeng
Functions: awk grep kill
-1

You cannot kill zombies, as they are already dead. But if you have too many zombies then kill parent process or restart service.

You can kill zombie process using PID obtained from the above command. For example kill zombie proces having PID 4104:

# kill -9 4104

Please note that kill -9 does not guarantee to kill a zombie process.

ps aux | awk '{ print $8 " " $2 " " $11}' | grep -w Z
cleartool co -nc `cleartool ls -recurse | grep "hijacked" | sed s/\@\@.*// | xargs`
cvs -Q status | grep -i locally
find $PWD -exec rpm --query -f {} \; | sort -u | grep -v "not owned"
2009-02-05 19:33:43
User: wwest4
Functions: find grep rpm sort
0

shows all RPMs with files in the current directory & its subdirectories.

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