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 tagged grep from sorted by
Terminal - Commands tagged grep - 343 results
grep -r -Z -l "<text>" . | xargs -0 echo rm
2012-06-14 08:09:46
User: limonte
Functions: echo grep xargs
Tags: grep
4

-r recursively

-Z zero byte after each file name instead of the usual newline

-l only filenames

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
mysqlbinlog <logfiles> | grep exec | grep end_log_pos | cut -d' ' -f2- | cut -d: -f-2 | uniq -c
2012-05-30 09:42:21
User: theist
Functions: cut exec grep uniq
1

shows number of mysql bin log events (which are mysql server events) per minute, useful to check stress times postmortem

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

cal 04 2012 | awk 'NF <= 7 { print $7 }' | grep -v "^$" | tail -1
2012-05-03 16:57:45
User: javidjamae
Functions: awk cal grep tail
-2

This is a little trickier than finding the last Sunday, because you know the last Sunday is in the first position of the last line. The trick is to use the NF less than or equal to 7 so it picks up all the lines then grep out any empty lines.

while true; do iptables -nvL > /tmp/now; diff -U0 /tmp/prev /tmp/now > /tmp/diff; clear; cat /tmp/diff; mv /tmp/now /tmp/prev; slee p 1; done
2012-04-15 00:02:33
Functions: cat diff iptables mv
0

this alternative shows the differences as they occur so that they are made plain

find ./src -type d -name "Entity" | xargs ls -A | cut -d . -f1 | sed 's_^_app/console doctrine:generate:entities YourOwnBundleName:_'
2012-04-11 21:28:02
User: renoirb
Functions: cut find ls sed xargs
0

When I do a major change in my entities, I want to find a way to find all my Entities names and create the commande for me.

So instead of doing ls src/Your/OwnBundle... and then do it manually, this helps a lot.

egrep "([_a-zA-Z][_a-zA-Z0-9]*) *= *\1 *[*/+-] *[0-9]+ *;"
2012-04-09 12:42:07
User: aandre
Functions: egrep
0

Search for java explicit incrementation in order to replace it with postfix or assignment operator

find /some/path -type f -and -printf "%f\n" | egrep -io '\.[^.]*$' | sort | uniq -c | sort -rn
2012-04-02 19:25:35
User: kyle0r
Functions: egrep find sort uniq
Tags: uniq ls grep
0

the

find -printf "%f\n" prints just the file name from the given path. This means directory paths which contain extensions will not be considered.
sudo apt-get remove $(dpkg -l|awk '/^ii linux-image-/{print $2}'|sed 's/linux-image-//'|awk -v v=`uname -r` 'v>$0'|sed 's/-generic//'|awk '{printf("linux-headers-%s\nlinux-headers-%s-generic\nlinux-image-%s-generic\n",$0,$0,$0)}')
2012-04-02 10:53:40
User: mtron
Functions: awk sed sudo
-1

small update for this command to work with linux kernels 3.x

curl -s -d "CSField=Name" -d "CSInput=BostonUCompSci" http://torstatus.blutmagie.de/index.php | grep -oP "ip=\K(\d+)(\.\d+){3}"
2012-03-09 16:52:27
User: JisSey
Functions: grep
0

Reciprocally, we could get the node name from a give Tor IP address =>

ip2node() { curl -s -d "QueryIP=$1" http://torstatus.blutmagie.de/tor_exit_query.php | grep -oP "Server name:.*'>\K\w+" ; }

ip2node 204.8.156.142

BostonUCompSci

alias sortfast='sort -S$(($(sed '\''/MemF/!d;s/[^0-9]*//g'\'' /proc/meminfo)/2048)) $([ `nproc` -gt 1 ]&&echo -n --parallel=`nproc`)'
3

sort is way slow by default. This tells sort to use a buffer equal to half of the available free memory. It also will use multiple process for the sort equal to the number of cpus on your machine (if greater than 1). For me, it is magnitudes faster.

If you put this in your bash_profile or startup file, it will be set correctly when bash is started.

sort -S1 --parallel=2 <(echo) &>/dev/null && alias sortfast='sort -S$(($(sed '\''/MemF/!d;s/[^0-9]*//g'\'' /proc/meminfo)/2048)) $([ `nproc` -gt 1 ]&&echo -n --parallel=`nproc`)'

Alternative

echo|sort -S10M --parallel=2 &>/dev/null && alias sortfast="command sort -S$(($(sed '/MemT/!d;s/[^0-9]*//g' /proc/meminfo)/1024-200)) --parallel=$(($(command grep -c ^proc /proc/cpuinfo)*2))"
grep $'\t' sample.txt
cmdfu(){ local t=~/cmdfu;echo -e "\n# $1 {{{1">>$t;curl -s "commandlinefu.com/commands/matching/$1/`echo -n $1|base64`/plaintext"|sed '1,2d;s/^#.*/& {{{2/g'>$t;vim -u /dev/null -c "set ft=sh fdm=marker fdl=1 noswf" -M $t;rm $t; }
2012-02-21 05:43:16
User: AskApache
Functions: echo rm sed vim
6

Here is the full function (got trunctated), which is much better and works for multiple queries.

function cmdfu () {

local t=~/cmdfu;

until [[ -z $1 ]]; do

echo -e "\n# $1 {{{1" >> $t;

curl -s "commandlinefu.com/commands/matching/$1/`echo -n $1|base64`/plaintext" | sed '1,2d;s/^#.*/& {{{2/g' | tee -a $t > $t.c;

sed -i "s/^# $1 {/# $1 - `grep -c '^#' $t.c` {/" $t;

shift;

done;

vim -u /dev/null -c "set ft=sh fdm=marker fdl=1 noswf" -M $t;

rm $t $t.c

}

Searches commandlinefu for single/multiple queries and displays syntax-highlighted, folded, and numbered results in vim.

rgrep <searchterm> *
grep -r <searchterm> .
2012-02-08 11:59:17
Functions: grep
Tags: grep directory
0

You don't need to use "*", it will fail if the working directory has too many fails due parameter extension, you can simply pass the working directory using ".".

grep -r <searchterm> .
2012-02-08 11:58:21
Functions: grep
Tags: grep directory
0

You don't need to use "*", it will fail if the working directory has too many fails due parameter extension, you can simply pass the working directory using ".".

grep -r <searchterm> *
sudo dmidecode -s system-product-name
sudo dmidecode | grep Product
grep -Ev '^( *$|#)' < conf.file
2012-02-03 23:43:43
Functions: grep
Tags: grep
0

Removes comments and blank lines from configuration files, leaving only settings

print "$(lsvg -Lo |xargs lsvg -L|grep "TOTAL PPs"|awk -F"(" '{print$2}'|sed -e "s/)//g" -e "s/megabytes/+/g"|xargs|sed -e "s/^/(/g" -e "s/+$/)\/1000/g"|bc ) GB"
2012-02-03 13:58:41
0

Not figured by me, but a colleague of mine.

See the total amount of data on an AIX machine.

netstat -Aan | grep .80 | grep -v 127.0.0.1 | grep EST | awk '{print $6}' | cut -d "." -f1,2,3,4 | sort | uniq
2012-02-03 13:54:11
Functions: awk cut grep netstat sort
0

See who is using a specific port. Especially when you're using AIX. In Ubuntu, for example, this can easily be seen with the netstat command.

ps -fea | grep PATTERN | awk {'print $2'} | xargs kill -9
grep . "$f" | head -n1
2012-01-27 02:58:07
User: captaincomic
Functions: grep head
Tags: grep
0

Use this command if your file may contain empty lines and you need to optain the first non-empty line.