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
find . -type f -exec ls -l --full-time {} + | sort -k 6,7
2012-08-03 22:22:51
User: quadcore
Functions: find ls sort
Tags: sort find ls
2

This sorts files in multiple directories by their modification date. Note that sorting is done at the end using "sort", instead of using the "-ltr" options to "ls". This ensures correct results when sorting a large number of files, in which case "find" will call "ls" multiple times.

find . -type f -exec du -sh {} + | sort -hr | head
egrep 'ServerAlias|ServerName' /etc/apache2/sites-enabled/*.conf | awk '{printf "%s\t%s\n",$2,$3}' | sed 's/www.//' | sort | uniq
2012-07-25 12:51:15
User: staeff
Functions: awk egrep sed sort
0

Get a list of all the unique hostnames from the apache configuration files. Handy to see what sites are running on a server. When i saw the command i had some ideas to make it shorter. Here is my version.

git shortlog -s | sort -rn | head
cat ~/.bash_history | perl -lane 'if($F[0] eq "sudo"){$hash{$F[1]}++}else{$hash{$F[0]}++};$all++;END {@top = map {[$_, $hash{$_}]} sort {$hash{$b}<=>$hash{$a}} keys %hash;printf("%10s%10d%10.2f%%\n", $_->[0],$_->[1],$_->[1]/$all*100) for @top[0..9]}'
find . -type f -print0 | xargs -0 du -h | sort -hr | head -10
find . -type f -print0 | xargs -0 du -h | sort -hr | head
for i in G M K; do du -hx /var/ | grep [0-9]$i | sort -nr -k 1; done | less
2012-06-26 22:57:17
User: jlaunay
Functions: du grep sort
Tags: du
1

This command give a human readable result without messing up the sorting.

sort namesd.txt | uniq ?cd
2012-06-26 19:23:58
User: ankush108
Functions: sort uniq
0

The following displays only the entries that are duplicates.

sort namesd.txt | uniq
2012-06-26 19:22:34
User: ankush108
Functions: sort
Tags: sort uniq unique
0

Uniq command is mostly used in combination with sort command, as

uniq removes duplicates only from a sorted file. i.e In order for uniq to

work, all the duplicate entries should be in the adjacent lines.

ls -al | sort +4n
2012-06-26 19:20:05
User: ankush108
Functions: ls sort
Tags: size sort files
0

ls -al gives all files, sort +4n sorts by 5th field numerically

sort -t: -k 2 names.txt
2012-06-26 19:15:30
User: ankush108
Functions: sort
Tags: sort sorting
0

Sort using kth column using : delimiter

du -x / | sort -rn | less
2012-06-26 15:29:26
User: harpo
Functions: du sort
Tags: du
2

I had the problem that our monitoring showed that the "/" filesystem is >90% full. This command helped me to find out fast which subdirs are the biggest. The system has many NFS-mounts therefore the -x.

netstat -tn | grep :80 | awk '{print $5}'| grep -v ':80' | cut -f1 -d: |cut -f1,2,3 -d. | sort | uniq -c| sort -n
2012-06-26 08:29:37
User: krishnan
Functions: awk cut grep netstat sort uniq
0

cut -f1,2 - IP range 16

cut -f1,2,3 - IP range 24

cut -f1,2,3,4 - IP range 24

nl -ba FILE | sort -nr | cut -f2-
2012-06-24 23:07:06
User: maher
Functions: cut nl sort
0

"nl -ba" numbers all lines in the file (including empty lines), "sort -nr"

sorts the lines in descending order, and the "cut" command finally removes

the line numbers again.

find . -type f -exec stat -f '%m %N' {} \; | sort -n
du -hs * | sort -h
expac -S -H M "%m %n"|sort -n
2012-06-14 19:41:21
User: gtmanfred
Functions: sort
0

M is size in megabytes, man expac to see other sizes

%m is install size

%k is download size

rpm -qa | sort | sed -n -e "s/\-[0-9].[0-9]*.*//p" | uniq
2012-06-04 12:36:39
User: tuxknowledge
Functions: rpm sed sort
0

This will list all installed packages on a RedHat/CentOS based system, sort them alphabetically, Parse off the version numbers, and delete any duplicate entries.

This is good if you need to build out a mirrored system or rebuild a failing system.

git ls-files | xargs -n1 git blame --line-porcelain | sed -n 's/^author //p' | sort -f | uniq -ic | sort -nr
2012-06-02 22:04:36
User: hugopeixoto
Functions: sed sort uniq xargs
Tags: statistics git
1

Uses line-porcelain in git blame, which makes it easier to parse the output.

cp $(find /media/ -type f -name "*.wav" -printf "%T@ %h/%f\n" | sort | tail -1 | awk '{ print $2 }') .
2012-06-01 12:45:43
User: hamoid
Functions: awk cp find sort tail
0

Watch out if you have several USB drives plugged in: it scans the whole /media/ folder !!! You can replace /media/ by the path of a specific USB drive (something like /media/F77A-530B/)

I use a sound recorder and I want to plug the recorder and grab the most recent sound.

That's what this command does.

Use mv instead of cp to move instead of copy.

Change *.wav to the required file type.

mysqlbinlog <logfiles> | grep exec | grep end_log_pos | grep -v exec_time=0 | sed 's/^\(.*exec_time=\([0-9]\+\).*\)/\2 - \1 /' | sort -n
2012-05-30 09:38:02
User: theist
Functions: exec grep sed sort
0

Shows sorted by query time, the headers of mysqlbinlog entries. Then is easy to locate the heavier events on the raw log dump

find /proc/sys/vm -maxdepth 1 -type f | while read i ; do printf "%-35s\t%s\n" "$i" "$(<$i)" ; done | sort -t/ -k4
2012-05-25 16:34:16
User: SEJeff
Functions: find printf read sort
0

Sometimes you want to see all of the systcls for a given $thing. I happened to need to easily look at all of the vm sysctls between two boxes and compare them. This is what I came up with.

lsof +c 15 | awk '{print $1}' | sort | uniq -c | sort -rn | head
ls -l| sort +4n|tail -1| awk '{print $NF}'