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 - 593 results
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.

for x in *.dat;do sort -k 3 $x >tmp && mv -f tmp $x;done
2010-07-07 07:57:37
User: rajarshi
Functions: mv sort
Tags: sorting
-2

We normally get tasks in which one has to sort a data file according to some column. For a single file say foo, we would use

sort -k 3 foo >tmp && tmp foo

The for loop is useful when we have to do it on a number of files.

qlist --exact "$pkg" | sudo scanelf --needed --quiet --format '%n#F' | tr ',' '\n' | sort -u | qfile --from -
2010-07-06 14:39:15
User: Flameeyes
Functions: sort sudo tr
2

The output is only partial because runtime dependencies should count in also commands executed via system() and libraries loaded with dlopen(), but at least it gives an idea of what a package directly links to.

Note: this is meaningful *only* if you're using -Wl,--as-needed in your LDFLAGS, otherwise it'll bring you a bunch of false positives.

cat WAR_AND_PEACE_By_LeoTolstoi.txt | tr -cs "[:alnum:]" "\n"| tr "[:lower:]" "[:upper:]" | awk '{h[$1]++}END{for (i in h){print h[i]" "i}}'|sort -nr | cat -n | head -n 30
2010-07-05 06:39:20
User: cp
Functions: awk cat head sort tr
11

using

cat WAR_AND_PEACE_By_LeoTolstoi.txt | tr -cs "[:alnum:]" "\n"| tr "[:lower:]" "[:upper:]" | sort -S16M | uniq -c |sort -nr | cat -n | head -n 30

("sort -S1G" - Linux/GNU sort only) will also do the job but as some drawbacks (caused by space/time complexity of sorting) for bigger files...

find -name "*.<suffix>" -exec wc -l "{}" \; | sort -n | tail
2010-06-29 05:53:43
User: cooper
Functions: find sort wc
Tags: wc source java line
0

Find the source file which contains most number of lines in your workspace

:)

HISTTIMEFORMAT='' history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head > /tmp/cmds ; gnuplot -persist <<<'plot "/tmp/cmds" using 1:xticlabels(2) with boxes'
2010-06-17 17:38:16
User: narcelio
Functions: awk head sort
-1

This alternative cleans HISTTIMEFORMAT environment variable and calls gnuplot just after /tmp/cmds is closed, to avoid some errors.

history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head > /tmp/cmds | gnuplot -persist <(echo 'plot "/tmp/cmds" using 1:xticlabels(2) with boxes')
2010-06-13 23:35:13
User: sthrs
Functions: awk echo head sort
14

Plot your most used commands with gnuplot.

history | awk '{a[$'$(echo "1 2 $HISTTIMEFORMAT" | wc -w)']++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
find . -type f -exec ls -s \{\} \; | sort -n
2010-06-02 11:03:31
User: Insti
Functions: find ls sort
-2

Finds all files below the current directory.

Orders the result from smallest to largest.

Good for finding the largest files in the tree.

echo '<html><body><table>' > /tmp/bar.html && find / -name '*.ogg' | sort | awk '{print "<tr><td>"$1"</td><td><audio src=\""$1"\" controls='controls'></audio></td></tr>" }' >> /tmp/bar.html && echo '</table></body></html>' >> /tmp/bar.html
2010-06-01 17:40:50
User: copremesis
Functions: awk echo find sort
2

find OGG audio files on your *nix box and listen to them using your web browser

sort file1 file2 | uniq -d
2010-05-28 10:25:31
User: emacs
Functions: sort uniq
-1

print members both in file1 and file2

ls -rl --time-style=+%s * | sed '/^$/,/^total [0-9]*$/d' | sort -nk6
$grep -hIr -m 1 em:name ~/.mozilla/firefox/*.default/extensions|sed 's#\s*##'|tr '<>=' '"""'|cut -f3 -d'"'|sort -u
2010-05-24 08:03:53
User: raj77_in
Functions: sed sort tr
-1

with grep for em:name rather than name, you will get much better result.

cut -d'/' -f3 file | sort | uniq -c
2010-05-23 16:02:51
User: rubenmoran
Functions: cut file sort uniq
2

count the times a domain appears on a file which lines are URLs in the form http://domain/resource.

sed -n 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/\nip&\n/gp' ips.txt | grep ip | sed 's/ip//'| sort | uniq
2010-05-23 11:26:32
User: rubenmoran
Functions: grep sed sort
Tags: sed ip address
1

Extracts ip addressess from file using sed. Uses a tag(ip) to grep the IP lines after extracting. Must be a way to just output regex matched on sed.

grep -Eo \([0-9]\{1,3\}[\.]\)\{3\}[0-9] file | sort | uniq
grep -R usepackage * | cut -d']' -f2 | cut -s -d'{' -f 2 | sed s/"}"/.sty"}"/g | cut -d'}' -f1 | sort | uniq | xargs dpkg -S | cut -d':' -f1 | sort | uniq
sortwc () { local L;while read -r L;do builtin printf "${#L}@%s\n" "$L";done|sort -n|sed -u 's/^[^@]*@//'; }
2010-05-20 20:13:52
User: AskApache
Functions: printf read sed sort
2

This provides a way to sort output based on the length of the line, so that shorter lines appear before longer lines. It's an addon to the sort that I've wanted for years, sometimes it's very useful. Taken from my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

grep -hIr -m 1 :name ~/.mozilla/firefox/*.$USER/extensions | tr '<>=' '"""' | cut -f3 -d'"' | sort -u
2010-05-18 14:49:44
User: new_user
Functions: grep sort tr
-1

1.) my profile ends with $USER not with .default

2.) only grep for the first occurrence because some extensions have the translated name also inside the install.rdf

grep -hIr :name ~/.mozilla/firefox/*.default/extensions | tr '<>=' '"""' | cut -f3 -d'"' | sort -u
du -s * | sort -nr | head
git log --all --pretty=format:" " --name-only | sort -u
2010-05-11 16:06:42
Functions: sort
Tags: history git
2

What was the name of that module we wrote and deleted about 3 months ago? windowing-something?

git log --all --pretty=format:" " --name-only | sort -u | grep -i window