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 - 608 results
cut -d " " -f1,4 access_log | sort | uniq -c | sort -rn | head
2013-09-20 21:29:32
User: zlemini
Functions: cut sort uniq
1

Analyze an Apache access log for the time period with most activity and display the hit count, requesting IP and the timestamp. May help detect a brute force dos attack.

cut -d ' ' -f 1 /var/log/apache2/access_logs | uniq -c | sort -n
2013-09-17 20:05:03
User: BorneBjoern
Functions: cut sort uniq
0

avoiding UUOC!

cut can handle files as well. No neet for a cat.

grep "10/Sep/2013" access.log| cut -d[ -f2 | cut -d] -f1 | awk -F: '{print $2":"$3}' | sort -nk1 -nk2 | uniq -c | awk '{ if ($1 > 10) print $0}'
netstat -ntu | awk ' $5 ~ /^(::ffff:|[0-9|])/ { gsub("::ffff:","",$5); print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
2013-09-10 19:28:06
User: mrwulf
Functions: awk cut netstat sort uniq
1

Same as the rest, but handle IPv6 short IPs. Also, sort in the order that you're probably looking for.

du -hd1 | sort -hr
cat /var/log/apache2/access_logs | cut -d' ' -f1 | sort | uniq -c | sort -n
2013-09-07 23:57:31
User: while0pass
Functions: cat cut sort uniq
1

The first sort is necessary for ips in a list to be actually unique.

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n
2013-09-06 08:30:49
User: jedifu
Functions: sort
0

The wajig package is not installed by default.

cat /var/log/apache2/access_logs | cut -d ' ' -f 1 | uniq -c | sort -n
2013-09-02 13:04:47
User: basvdburg
Functions: cat cut sort uniq
1

Show's per IP of how many requests they did to the Apache webserver

diff <(sort <(md5deep -b -r /directory/1/) ) <(sort <(md5deep -b -r /directory/2/)
2013-08-19 18:20:49
Functions: diff sort
Tags: bash Linux diff
0

Compares the md5 checksums of the contents of two directories, outputting the checksum and filename where any files differ. Shows only the file name, not the full path.

diff <(sort <(md5deep -r /directory/1/) |cut -f1 -d' ') <(sort <(md5deep -r /directory/2/) |cut -f1 -d' ')
2013-08-18 22:13:07
Functions: cut diff sort
Tags: bash Linux diff
1

Compute the md5 checksums for the contents of two mirrored directories, then sort and diff the results. If everything matches, nothing is returned. Otherwise, any checksums which do not match, or which exist in one tree but not the other, are returned. As you might imagine, the output is useful only if no errors are found, because only the checksums, not filenames, are returned. I hope to address this, or that someone else will!

sudo netstat -plntu --inet | sort -t: -k2,2n | sort --stable -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n | sort -s -t" " -k1,1
2013-08-13 09:21:26
User: thechile
Functions: netstat sort sudo
0

bit of a contrived example and playing to my OCD but nice for quick scripted output of listening ports which is sorted by port, ip address and protocol.

find . -type f -print0 | xargs -0 stat -c'%Y :%y %12s %n' | sort -nr | cut -d: -f2- | head
2013-08-03 09:53:46
User: HerbCSO
Functions: cut find sort stat xargs
5

Goes through all files in the directory specified, uses `stat` to print out last modification time, then sorts numerically in reverse, then uses cut to remove the modified epoch timestamp and finally head to only output the last 10 modified files.

Note that on a Mac `stat` won't work like this, you'll need to use either:

find . -type f -print0 | xargs -0 stat -f '%m%t%Sm %12z %N' | sort -nr | cut -f2- | head

or alternatively do a `brew install coreutils` and then replace `stat` with `gstat` in the original command.

dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' `aptitude --display-format '%p' search '?installed!?automatic'` | sort -k 2 -n | grep -v deinstall | awk '{printf "%.3f MB \t %s\n", $2/(1024), $1}'
2013-07-26 23:18:20
User: EvilDennisR
Functions: awk grep sort
0

The other commands were good, but they included packages that were installed and then removed.

This command only shows packages that are currently installed, sorts smallest to largest, and formats the sizes to be human readable.

du -m --max-depth=1 [DIR] | sort -nr
du -hs `du -sk * | sort -rn | cut -f2-`
find /logs -ls -xdev | sort -nrk 7 | head -10
2013-07-22 08:36:03
User: vidya99343
Functions: find head sort
0

bash-3.2$ find /logs -ls -xdev | sort -nrk 7 | head -10

1761905 205380 -rwxrwxr-x 1 wsadmin logadmin 210095353 Jul 22 01:33 /logs/intlpymt/Trace.log

652689 187360 -rwxrwxr-x 1 wsadmin logadmin 191663182 Jul 21 23:00 /logs/websphere/wsfpp1lppwa1213omsecureServer/SystemOut_13.07.21_23.00.12.log

2380449 186536 -rwxrwxr-x 1 wsadmin logadmin 190819939 Jul 16 14:03 /logs/omset/traceIntl.log.201307161403.lppwa1213.gz

2119524 183888 -rwxrwxr-x 1 wsadmin logadmin 188110111 Jul 22 01:33 /logs/intlpymt/intlpymtria/Trace.log

652816 160332 -rwxrwxr-x 1 wsadmin logadmin 164011871 Aug 14 2012 /logs/websphere/wsfpp1lppwa1213omsecureServer/SystemOut.log_08142012.gzip

653312 128916 -rwxrwxr-x 1 wsadmin logadmin 131873943 Jul 18 10:49 /logs/websphere/heapdump.20130718.104150.27592.0006.phd.201307181406.lppwa1213.gz

653320 128916 -rwxrwxr-x 1 wsadmin logadmin 131873735 Jul 18 10:40 /logs/websphere/heapdump.20130718.104012.27592.0002.phd.201307181406.lppwa1213.gz

653309 128912 -rwxrwxr-x 1 wsadmin logadmin 131867602 Jul 18 10:46 /logs/websphere/heapdump.20130718.104008.27592.0001.phd.201307181405.lppwa1213.gz

653323 128872 -rwxrwxr-x 1 wsadmin logadmin 131828157 Jul 18 10:41 /logs/websphere/heapdump.20130718.104109.27592.0004.phd.201307181407.lppwa1213.gz

652783 120288 -rwxrwxr-x 1 wsadmin logadmin 123047750 Aug 13 2012 /logs/websphere/wsfpp1lppwa1213omsecureServer/SystemOut.log_0813.2012.gzip

bash-3.2$

find /logs -ls -xdev | sort -nrk 7 | head -10
du -xks * | sort -n
2013-07-18 16:10:28
User: rdc
Functions: du sort
1

This command is useful for finding out which directories below the current location use the most space. It is summarised by directory and excludes mounted filesystems. Finally it is sorted by size.

cut -d, -f1 /var/opt/example/dumpfile.130610_subscriber.csv | cut -c3-5 | sort | uniq -c | sed -e 's/^ *//;/^$/d' | awk -F" " '{print $2 "," $1}' > SubsxPrefix.csv
2013-07-17 07:58:56
User: neomefistox
Functions: awk cut sed sort uniq
Tags: Linux UNIX
0

dumpfile is a CSV file, which its 1st field is a phone number in format CC+10 digits

Empty lines are deleted, before the output in format "prefix,ocurrences"

find . | sort | awk 'NR%2==0' | xargs rm $1
2013-07-11 07:36:18
User: sucotronic
Functions: awk find rm sort xargs
-1

If you have a directory with lot of backups (full backups I mean), when it gets to some size, you could want to empty some space. With this command you'll remove half of the files. The command assumes that your backup files starts with YYYYMMDD or that they go some alphabetical order.

history | awk '{CMD[$4]++;count++;} END { for (a in CMD )print CMD[a] " " CMD[a]/count*100 "% " a }' | sort -nr | nl | column -t | head -n 10
find . -type d -print0 | xargs -0 du -s | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {} | sort -rn
sudo /usr/sbin/exim -bp | sed -n '/\*\*\* frozen \*\*\*/,+1!p' | awk '{print $1}' | tr -d [:blank:] | grep @ | sort | uniq -c | sort -n
du -mx [directory] | grep -P '^\d{4}' | sort -rn
2013-05-24 09:52:41
User: mc0e
Functions: du grep sort
Tags: bash Linux du
0

I don't like doing a massive sort on all the directory names just to get a small set of them. the above shows a sorted list of all directories over 1GB. use head as well if you want.

du's "-x" flag limits this to one file system. That's mostly useful when you run it on "/" but don't want "/proc" and "/dev" and so forth. Remember though that it will also exclude "/home" or "/var" if those are separate partitions.

the "-a" option is often useful too, for listing large files as well as large directories. Might be slower.

du -xB M --max-depth=2 /var | sort -rn | head -n 15