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
find . -name "*noticia*" -name "*jhtm*" -name "*.tpl" -exec grep -li "id=\"col-direita\"" '{}' \; | xargs -n1 mate
grep -ZlRr -e BAD_SCRIPT_LINE * |xargs -0 sed -i 's/BAD_SCRIPT_LINE//g'
2010-08-30 22:12:57
User: homoludens
Functions: grep sed xargs
0

recursive find and replace. important stuff are grep -Z and zargs -0 which add zero byte after file name so sed can work even with file names with spaces.

marbles () { c=''; for i in $(seq $1); do c+='{b,r}'; done; x=$(eval echo $c); p=''; for i in $(seq $2); do p+='b*r'; done; y=$(grep -wo "${p}b*" <<< $x); wc -l <<< "$y"; grep -vc 'rr' <<< "$y"; }
2010-08-27 23:04:33
User: quintic
Functions: echo eval grep seq wc
-4

Suppose you have 11 marbles, 4 of which are red, the rest being blue. The marbles are indistinguishable, apart from colour. How many different ways are there to arrange the marbles in a line? And how many ways are there to arrange them so that no two red marbles are adjacent?

There are simple mathematical solutions to these questions, but it's also possible to generate and count all possibilities directly on the command line, using little more than brace expansion, grep and wc!

The answer to the question posed above is that there are 330 ways of arranging the marbles in a line, 70 of which have no two red marbles adjacent. See the sample output.

To follow the call to marbles 11 4: after c=''; for i in $(seq $1); do c+='{b,r}'; done;, $c equals {b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}

After x=$(eval echo $c), and brace expansion, $x equals bbbbbbbbbbb bbbbbbbbbbr ... rrrrrrrrrrb rrrrrrrrrrr, which is all 2^11 = 2048 strings of 11 b's and r's.

After p=''; for i in $(seq $2); do p+='b*r'; done;, $p equals b*rb*rb*rb*r

Next, after y=$(grep -wo "${p}b*"

Finally, grep -vc 'rr'

git grep "search for something" $(git log -g --pretty=format:%h -S"search for something")
grep -a -B 25 -A 100 'some string in the file' /dev/sda1 > results.txt
2010-08-19 20:07:42
User: olalonde
Functions: grep
22

grep searches through a file and prints out all the lines that match some pattern. Here, the pattern is some string that is known to be in the deleted file. The more specific this string can be, the better. The file being searched by grep (/dev/sda1) is the partition of the hard drive the deleted file used to reside in. The ?-a? flag tells grep to treat the hard drive partition, which is actually a binary file, as text. Since recovering the entire file would be nice instead of just the lines that are already known, context control is used. The flags ?-B 25 -A 100? tell grep to print out 25 lines before a match and 100 lines after a match. Be conservative with estimates on these numbers to ensure the entire file is included (when in doubt, guess bigger numbers). Excess data is easy to trim out of results, but if you find yourself with a truncated or incomplete file, you need to do this all over again. Finally, the ?> results.txt? instructs the computer to store the output of grep in a file called results.txt.

Source: http://spin.atomicobject.com/2010/08/18/undelete?utm_source=y-combinator&utm_medium=social-media&utm_campaign=technical

svn status | grep "^\?" | awk '{print $2}' | xargs svn add
2010-08-14 18:56:15
User: kureikain
Functions: awk grep xargs
Tags: svn awk grep
1

When working on a big proeject with SVN, you create quite much files, for now! Can just sit here and type svn add for all of them!

svn status will return a list of all of file which get ?(not add), "M"(Modified), "D"(Deleted)! This code just grep "?" flag, then add it into SVN again!

ls -Xp /path/to/dir | grep -Eo "\.[^/]+$" | uniq
2010-08-12 16:32:54
User: karpoke
Functions: grep ls
Tags: uniq ls grep
0

If we want files with more than one extension, like .tar.gz, only appear the latest, .gz:

ls -Xp /path/to/dir | grep -Eo "\.[^./]+$" | uniq
find /path/to/dir -type f | grep -o '\.[^./]*$' | sort | uniq
svn st | grep -e '^M' | awk '{print $2}' | xargs svn revert
netstat -rn | grep UG | tr -s " " | cut -d" " -f2
find . -name '*.txt' -print0 | parallel -0 -j+0 lzma
2010-07-28 21:01:12
Functions: find
Tags: find grep lzma
1

This will deal nicely with filenames containing newlines and will run one lzma process per CPU core. It requires GNU Parallel http://www.youtube.com/watch?v=OpaiGYxkSuQ

find . -name '*.txt' | grep -v '\.lzma$' | xargs -n 1 lzma -f -v -3
rm $( ls | egrep -v 'abc|\s' )
2010-07-18 10:59:15
User: dbbolton
Functions: egrep ls rm
Tags: grep rm
-1

Really, you deserve whatever happens if you have a whitespace character in a file name, but this has a small safety net. The truly paranoid will use '-i'.

alias dush="du -xsm * | sort -n | awk '{ printf(\"%4s MB ./\",\$1) ; for (i=1;i<=NF;i++) { if (i>1) printf(\"%s \",\$i) } ; printf(\"\n\") }' | tail"
2010-07-15 10:38:27
User: dopeman
Functions: alias
-1

Essentially the same as funky's alias, but will not traverse filesystems and has nicer formatting.

grep -R --include=*.cpp --include=*.h --exclude=*.inl.h "string" .
2010-07-14 16:32:28
User: sweinst
Functions: grep
Tags: find xargs grep
0

Gnu grep allows to restrict the search to files only matching a given pattern. It also allows to exclude files.

find . -name '*.?pp' -exec grep -H "string" {} \;
find . -name '*.?pp' | xargs grep -H "string"
2010-07-14 14:41:07
User: cout
Functions: find grep xargs
Tags: find xargs grep
2

I like this better than some of the alternatives using -exec, because if I want to change the string, it's right there at the end of the command line. That means less editing effort and more time to drink coffee.

grep -i '^DocumentRoot' /etc/httpd/conf/httpd.conf | cut -f2 -d'"'
vim --version | grep -P '^(\+|\-)' | sed 's/\s/\n/g' | grep -Pv '^ ?$'
2010-07-02 02:57:19
User: evaryont
Functions: grep sed vim
Tags: vim sed grep
2

The above output is for a custom compiled version of Vim on Arch Linux.

Just a quick shell one liner, and presents a list of all the enabled and disabled (those prefixed with a '-') features.

curl -s "http://www.socrata.com/api/views/vedg-c5sb/rows.json?search=Axelrod" | grep "data\" :" | awk '{ print $17 }'
2010-07-01 23:54:54
User: mheadd
Functions: awk grep
Tags: awk grep curl
2

Query the Socrata Open Data API being used by the White House to find any employee's salary using curl, grep and awk.

Change the value of the search parameter (example uses Axelrod) to the name of any White House staffer to see their annual salary.

function mg(){ man ${1} | egrep ${2} | more; }
2010-07-01 21:14:24
User: quincymd
Functions: egrep man
Tags: man grep
0

Quicker way to search man pages of command for key word

ifconfig eth0 | grep -o "inet [^ ]*" | cut -d: -f2
ifconfig eth0 | awk '/inet / {print $2}' | cut -d ':' -f2
wget -O - http://www.commandlinefu.com/commands/browse/rss 2>/dev/null | awk '/\s*<title/ {z=match($0, /CDATA\[([^\]]*)\]/, b);print b[1]} /\s*<description/ {c=match($0, /code>(.*)<\/code>/, d);print d[1]} ' | grep -v "^$"
2010-06-29 16:22:03
User: nikunj
Functions: awk grep wget
Tags: awk grep meta
2

A Quick variation to the latest commands list with the new-lines skipped. This is faster to read.

for f in $(find /path/to/base -type f | grep -vw CVS); do grep -Hn PATTERN $f; done