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 sort from sorted by
Terminal - Commands using sort - 639 results
tr A-Z a-z | tr -cs a-z '\n' | sort | uniq -c
tr A-Z a-z | tr -d "[[:punct:]][[:digit:]]" | tr ' /_' '\n' | sort | uniq -c
tr A-Z a-z | tr -d 0-9\[\],\*-.?\:\"\(\)#\;\<\>\@ | tr ' /_' '\n' | sort | uniq -c
du --max-depth=1|sort -n|cut -f2|tr '\n' '\0'|xargs -0 du -sh 2>/dev/null
svn log -q | grep -v "^-" | cut -d "|" -f 2 | sort -u
svn log --stop-on-copy | grep r[0-9] | awk '{print $1}' | sed "s/r//" | sort -n | head -1
du -a --max-depth=1 | sort -n
sudo du -sm * | sort -n
2010-09-24 17:56:41
User: wabi
Functions: du sort sudo
0

sudo is optional, but to find out about all files, it is nice, or else run as superuser, ie: su -c 'du -sm * | sort -n'

sort -R SOMEFILE
2010-09-16 22:29:27
User: miniker84
Functions: sort
4

Works in sort (GNU coreutils) 7.4, don't know when it was implemented but sometime the last 6 years.

netstat -n | grep '^tcp.*<IP>:<PORT>' | tr " " | awk 'BEGIN{FS="( |:)"}{print $6}' | sort | uniq -c | sort -n -k1 | awk '{if ($1 >= 10){print $2}}'
2010-09-16 21:06:30
User: guptavi
Functions: awk grep netstat sort tr uniq
1

This command is primarily going to work on linux boxes.

and needs to be changed, for example

IP=10\.194\.194\.2

PORT=389

svn log 2>&1 | egrep '^r[0-9]+' | cut -d "|" -f2 | sort | uniq -c
feh --bg-center `ls -U1 |sort -R |head -1`
wget -O- -q http://www.nomachine.com/download-package.php?Prod_Id=2067 | sed -n -e 'H;${x;s/\n/ /g;p;}' | sed -e "s/[Hh][Rr][Ee][Ff]=\"/\n/g" | cut -d "\"" -f1 | sort -u | grep deb$
wget http://forums.dropbox.com && wget $(cat index.html|grep "Latest Forum Build"|cut -d"\"" -f2) && wget $(cat topic.php*|grep "Linux x86:"|cut -d"\"" -f2|sort -r|head -n1) && rm -rf ~/.dropbox* && rm index.html *.php* && tar zxvf dropbox-*.tar.gz -C ~/
find /DirectoryWhereMyMp3sAre/ -regextype posix-egrep -iregex '.*?\.(ogg|mp3)' | sort > ~/mylist.m3u
Array1=( "one" "two" "three" "four" "five" );Array2=( "four" "five" "six" "seven" );savedIFS="${IFS}";IFS=$'\n';Array3=($(comm -12 <(echo "${Array1[*]}" |sort -u) <(echo "${Array2[*]}" | sort -u)));IFS=$savedIFS
2010-08-23 19:25:22
User: elofland
Functions: comm echo sort
0

the comm utility (opposite of diff) show commonalities in files (in this case strings)

find /path/to/dir -type f -name '*.*' | sed 's@.*/.*\.@.@' | sort | uniq
2010-08-12 15:48:54
User: putnamhill
Functions: find sed sort
1

If your grep doesn't have an -o option, you can use sed instead.

find /path/to/dir -type f | grep -o '\.[^./]*$' | sort | uniq
svn status | awk '{print $2}' | xargs du | sort -n | tail
git log --format='%aN <%aE>' | awk '{arr[$0]++} END{for (i in arr){print arr[i], i;}}' | sort -rn | cut -d\ -f2-
2010-07-19 11:40:52
Functions: awk cut sort
Tags: git
2

Gets the authors, sorts by number of commits (as a vague way of estimating how much of the project is their work, i.e. the higher in the list, the more they've done) and then outputs the results.

sort -t $'\t' -k 2 input.txt
2010-07-11 12:58:51
User: postrational
Functions: sort
4

Use this BASH trick to create a variable containing the TAB character and pass it as the argument to sort, join, cut and other commands which don't understand the \t notation.

sort -t $'\t' ... join -t $'\t' ... cut -d $'\t' ...
netstat -an | grep ESTABLISHED | awk '\''{print $5}'\'' | awk -F: '\''{print $1}'\'' | sort | uniq -c | awk '\''{ printf("%s\t%s\t",$2,$1); for (i = 0; i < $1; i++) {printf("*")}; print ""}'\''
2010-07-09 00:25:45
User: mramos
Functions: awk grep netstat sort uniq
7

Displays a connection histogram of active tcp connections. Works even better under an alias. Thanks @Areis1 for sharing this one.

ps -axgu | cut -f1 -d' ' | sort -u
ps -eo user | sort -u
2010-07-07 12:28:44
User: dfaulkner
Functions: ps sort
2

Shows a list of users that currently running processes are executing as.

YMMV regarding ps and it's many variants. For example, you might need:

ps -axgu | cut -f1 -d' ' | sort -u
sudo lsof|sed 's/ */ /g'|cut -f3 -d' '|sort -u
2010-07-07 08:20:28
User: binaryten
Functions: cut sed sort sudo
-4

Most systems (at least my macbook) have system users defined, such as _www and using "users" for example will not list them. This command allows you to see who the 'virtual' users are on your system.