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 - 637 results
export PATH=$PATH$(find "$PWD" -name '.*' -prune -o -type f -a -perm /u+x -printf ':%h\n' | sort -u | tr -d '\n'); echo $PATH
2011-03-11 19:31:17
User: dexterhu
Functions: echo export find sort tr
-1

(1) don't run twice, or the same folder will occur in $PATH.

(2) otherwise you need to start a new terminal

CLASSPATH=.; export CLASSPATH=$CLASSPATH$(find "$PWD" -name '*.jar' -type f -printf ':%p\n' | sort -u | tr -d '\n'); echo $CLASSPATH
sort -t"," -n -k5 file.csv # according to the 5th field NUMERICALLY!!
ps -eo pmem,pid,comm --no-headers | sort -k1 -rn | head -10
2011-03-11 04:51:35
User: dexterhu
Functions: head ps sort
0

Pros: the format is very simple, there is no need to show every columns, and full command with args

the first column is memory consumption %

the second column is pid

the third is just the command (without full arguments, most application's arguments are too long)

You can decide which application to kill then.

find . -maxdepth 1 -type d | grep -Pv "^.$" | sort -rn --field-separator="-" | sed -n '3,$p' | xargs rm -rf
yum whatprovides /usr/lib/libXX1.so /usr/lib/libXX2.so | grep fc | sed 's/^\(.*\)-[0-9.]*-.*$/\1/' | sort | uniq | xargs yum -y install
2011-03-07 04:51:35
User: laputa
Functions: fc grep sed sort uniq xargs
0

this command will install the packages which provides the libraries you need to link with, e.g. when you compile something needs opengl libraries:

gcc -o testgl testgl.c -lGLEW -lGL -lGLU -lglut

you can use `/usr/lib/libGLEW.so /usr/lib/libGL.so /usr/lib/libGLU.so /usr/lib/libglut.so'

find . -type f | awk -F'.' '{print $NF}' | sort| uniq -c | sort -g
ls | grep -Eo "\..+" | sort -u
ls -Xp | grep -Eo "\.[^/]+$" | sort | uniq
2011-02-10 20:47:59
User: Amarok
Functions: grep ls sort
Tags: uniq ls grep
4

Works on current directory, with built-in sorting.

find . -type f -name '*.pm' -printf '%6s %p\n' | sort -nr | head -n 50
2011-02-09 16:19:11
User: depesz
Functions: find head sort
0

Will work with filenames with spaces inside. Will not break in case of someone making directory that matches *.pm. And sorts from largest. Where largest is file size, not line count.

vzlist -a -H -o hostname,diskspace,diskspace.s,veid | awk '{ printf( "%2.f%\t%s\t%s\n"), $2*100/$3, $4, $1}' | sort -r
lsof | awk '/*:https?/{print $2}' | sort -u
2011-02-04 01:37:17
User: sugitaro
Functions: awk sort
Tags: sort awk lsof
-1

% lsof -v

lsof version information:

revision: 4.78

sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; }
2011-01-26 15:18:08
User: mpb
Functions: awk echo sed sort
0

"sort_csn" is a function to sort a comma separated list of numbers.

Define the the function with this:

sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; }

Use the function like this:

sort_csn 443,22,80,8200,1533,21,1723,1352,25

21,22,25,80,443,1352,1533,1723,8200

One example where this is useful is when port scanning with nmap and getting a list of open ports in random order. If you use Nessus, you may need to create a scan policy for that set of specific ports and it is clearer to read with the port numbers in ascending order (left to right).

Caveat: no spaces in the comma separated list (just number1,number2,number3,etc).

A variation of this to sort a comma separated list of strings:

sort_css () { echo "${1}" | sed -e "s/,/\n/g"| sort -u | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; }

usage:

sort_css apples,pears,grapes,melons,oranges

apples,grapes,melons,oranges,pears

lsof -nP -c COMMAND | egrep -o '(TCP|UDP).*$' | sort -u
2011-01-25 12:04:13
User: forcefsck
Functions: egrep sort
Tags: egrep lsof udp tcp
0

Where COMMAND is the process(es) name. I prefer to get all states but you may add ESTABLISHED in the grep regex.

lsof -c apache2 | egrep -o 'TCP.*ESTABLISHED.*$'

-nP flags are optional and UDP is irrelevant for established connections

Similar but using the process id:

lsof -nP -p PID | egrep -o '(TCP|UDP).*$'
cat YOUR_FILE|tr -d '[:punct:]'|tr '[:upper:]' '[:lower:]'|tr -s ' ' '\n'|sort|uniq -c|sort -rn
echo $(shuf -n 6 -i 1-49 | sort -n)
paste <(pacman -Q | awk '{ print $1; }' | xargs pacman -Qi | grep 'Size' | awk '{ print $4$5; }') <(pacman -Q | awk '{print $1; }') | sort -n | column -t
users | xargs -n1 echo | sort | uniq -c
seq -w 50 | sort -R | head -6 |fmt|tr " " "-"
shuf -i 1-49 -n 6 | sort -n | xargs
find -type f -exec du -sh {} + | sort -rh | head
find -type f | xargs -I{} du -s "{}" | sort -rn | head | cut -f2 | xargs -I{} du -sh "{}"
2011-01-04 11:10:56
User: glaudiston
Functions: cut du find head sort xargs
-1

Show the top file size in human readable form

find -type f | xargs -I{} du -sk "{}" | sort -rn | head
svn log -q | grep '^r[0-9]' | cut -f2 -d "|" | sort | uniq -c | sort -nr
2011-01-03 15:23:08
User: kkapron
Functions: cut grep sort uniq
2

list top committers (and number of their commits) of svn repository.

in this example it counts revisions of current directory.

du -sk * | sort -rn | head
2011-01-03 10:49:40
User: EBAH
Functions: du sort
3

Also:

* find . -type f -exec ls -s {} \; | sort -n -r | head -5

* find . -type f -exec ls -l {} \; | awk '{print $5 "\t" $9}' | sort -n -r | head -5