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 awk from sorted by
Terminal - Commands using awk - 1,118 results
ls | paste --delimiters='*' - ./zzz | awk ' BEGIN{FS="*";} { system("mv " $1 " \"" $2 "\"") }'
2013-05-13 15:44:07
User: skilowatt
Functions: awk ls paste
0

Rename all files in current directory by names from text file 'zzz'

ps aux | sort -n -k2 | awk '{if ($2 < 300) print($0)}'
2013-05-09 13:09:58
User: lili
Functions: awk ps sort
0

Display all pid less the 300 processes info

awk '!($0 in array) { array[$0]; print }' temp
ps aux | awk '/chrome/ {s+=$6}END{print s/1024}';
ps -A -o rss,command | grep [C]hrome | awk '{sum+=$1} END {printf("%sMB\n",sum/1024)}'
for i in `cat /proc/mounts | awk '{print $2}' | grep ${CDIR} |sort -r` ; do umount $i; done
awk '{print $1}' ~/.bash_history | sort | uniq -c | sort -rn | head -n 10
svn ls -R | egrep -v -e "\/$" | xargs svn blame | awk '{count[$2]++}END{for(j in count) print count[j] "\t" j}' | sort -rn
2013-05-03 01:45:12
User: kurzum
Functions: awk egrep ls sort xargs
Tags: svn count
0

This one has a better performance, as it is a one pass count with awk. For this script it might not matter, but for others it is a good optiomization.

awk '{ print $9 }' access.log | sort | uniq -c | sort -nr | head -n 10
awk '/Dec\/2012/ {print $1,$8}' logfile | grep -ivE '(.gif|.jpg|.png|favicon|.css|.js|robots.txt|wp-l|wp-term)' | sort | uniq -c | sort -rn | head -n 20
vim -p `git --porcelain | awk {print $2}`
2013-04-29 21:52:23
User: cnelsonsic
Functions: awk vim
Tags: vim git
0

Opens all files in the index (modified plus not added yet) in tabs in vim.

eval `ls -1d * | awk '{print "zip -r "$1".zip "$1";"}'`
dd if=/dev/zero of=/fs/to/fill/dummy00 bs=8192 count=$(df --block-size=8192 / | awk 'NR!=1 {print $4-100}')
find . -type f -name '*.gz'|awk '{print "zcat", $1, "| bzip2 -c >", $0.".tmp", "&& rename", "s/.gz.tmp/.bz2/", "*.gz.tmp", "&& rm", $0}'|bash
2013-04-11 10:17:57
User: Ztyx
Functions: awk find
-2

This solution is similar to [1] except that it does not have any dependency on GNU Parallel. Also, it tries to minimize the impact on the running system (using ionice and nice).

[1] http://www.commandlinefu.com/commands/view/7009/recompress-all-.gz-files-in-current-directory-using-bzip2-running-1-job-per-cpu-core-in-parallel

svn ls -R | egrep -v -e "\/$" | tr '\n' '\0' | xargs -0 svn blame | awk '{print $2}' | sort | uniq -c | sort -nr
2013-04-10 19:37:53
User: rymo
Functions: awk egrep ls sort tr uniq xargs
Tags: svn count
1

make usable on OSX with filenames containing spaces. note: will still break if filenames contain newlines... possible, but who does that?!

ifconfig -a | awk '/Bcast/{print $2}' | cut -c 5-19
ifconfig -a | awk '/Bcast/{print $2}' | cut -c 5-19
ip -o -4 addr show | awk -F '[ /]+' '/global/ {print $4}'
2013-04-09 16:34:39
User: atoponce
Functions: awk
1

Why use many different utilities all piped together, when you only need two?

netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ips[ip], ip | "sort -k1 -nr"}'
netstat -antu | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -n
2013-04-08 19:46:41
User: wejn
Functions: awk netstat sort uniq
-1

Output contains also garbage (text parts from netstat's output) but it's good enough for quick check who's overloading your server.

for ii in $(find /path/to/docroot -type f -name \*.php); do echo $ii; wc -lc $ii | awk '{ nr=$2/($1 + 1); printf("%d\n",nr); }'; done
2013-04-05 19:06:17
Functions: awk echo find wc
0

I have found that base64 encoded webshells and the like contain lots of data but hardly any newlines due to the formatting of their payloads. Checking the "width" will not catch everything, but then again, this is a fuzzy problem that relies on broad generalizations and heuristics that are never going to be perfect.

What I have done is set an arbitrary threshold (200 for example) and compare the values that are produced by this script, only displaying those above the threshold. One webshell I tested this on scored 5000+ so I know it works for at least one piece of malware.

cat /sys/block/md1/holders/dm*/dm/name | awk -F- '{print $1}' | sort -u
cat /sys/block/{*,*/*}/holders/dm*/dm/name | awk -F- '{print $1}' | sort -u
ps aux | grep [process] | awk '{print $2}' | xargs -I % ls /proc/%/fd | wc -l
git log | grep Date | awk '{print " : "$4" "$3" "$6}' | uniq -c