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,605 results
rpm -qa --last
2009-02-05 16:00:56
User: systemj
Functions: rpm
2

the newest rpms are at the top; individual packages can also be queried this way:

rpm --last -q package

vimdiff tera.py <(ssh -A testserver "cat tera.py")
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"

python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
2009-02-05 15:43:43
User: anokneemouse
Functions: python
0

Want to show something on your machine to someone over the web? Don't copy it or upload it somewhere. Just run "webshare" and the current directory and everything beneath it will be served from a new web server listening on port 8000. When your pal is finished, hit control-c.

Found at www.shell-fu.org/lister.php?id=54

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

perl -pi -e 's/localhost/replacementhost/g' *.php
echo | openssl s_client -connect www.google.com:443 2>/dev/null |openssl x509 -dates -noout
2009-02-05 15:21:50
User: systemj
Functions: echo
30

remotely connects to an https site, fetches the ssl certificate and displays the valid dates for the cert

NAME=$(nslookup $IP | sed -n 's/.*arpa.*name = \(.*\)/\1/p'); test -z "$NAME" && NAME="NO_NAME"; echo "$NAME"
for i in *.bak ; do nuname=`echo $i | sed 's/\.[^\.]*$//'`; echo renaming $i to $nuname;mv $i $nuname; done
awk '/pattern1/ && /pattern2/ && !/pattern3/ {print}'
2009-02-05 15:18:19
User: themensch
Functions: awk
12

Rather than chain a string of greps together and pipe them to awk, use awk to do all the work. In the above example, a string would be output to stdout if it matched pattern1 AND pattern2, but NOT pattern3.

for i in * ; do cp $i $i.bak; done
2009-02-05 15:15:40
User: swinz
Functions: cp
-2

quick in directory backup of all files in this directory. Adds the .bak extension to all copies.

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
curl -I www.commandlinefu.com
awk 'length($0)!=12 {print}' your_file_name
2009-02-05 14:27:52
User: hannahbrian
Functions: awk
1

Alternatively, print all the lines that are a certain length:

awk 'length($0)==12 {print}' your_file_name
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
awk '{count[length]++}END{for(i in count){printf("%d: %d\n", count[i], i)}}'
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
for i in $(locate your_search_phrase); do dirname $i; done | sort | uniq
2009-02-05 14:03:20
User: realbrewer
Functions: dirname locate sort
1

Ever use 'locate' to find a common phrase in a filename or directory name? Often you'll get a huge list of matches, many of which are redundant, and typically the results are not sorted. This command will 'locate' your search phrase, then show you a sorted list of just the relevant directories, with no duplications. So, for example, maybe you have installed several versions of the java jre and you want to track down every directory where files matching "java" might exist. Well, a 'locate java' is likely to return a huge list with many repeated directories since many files in one directory could contain the phrase "java". This command will whittle down the results to a minimal list of unique directory names where your search phrase finds a match.

package-cleanup --leaves --all
2009-02-05 13:51:27
User: drebes
0

This command lists all packages in a yum based system that no other packages depend on. Hence, these packages are good candidates for removal. It's a great command for cleaning up a yum based distribution after installation.