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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Commands tagged grep from sorted by
Terminal - Commands tagged grep - 360 results
ack; pcregrep
2011-08-14 18:01:36
User: depesz
Tags: perl grep regex

Much better alternatives - grep-alikes using perl regexps. With more options, and nicer outputs.

httpd -V | grep -i SERVER_CONFIG_FILE | cut -f2 -d'"' | xargs grep -i '^DocumentRoot' | cut -f2 -d'"'
2011-08-13 20:45:05
User: dcpesses
Functions: cut grep

Grabs the Apache config file (yielded from httpd) and returns the path specified as DocumentRoot.

prep () { perl -nle 'print if '"$1"';' $2 }
2011-08-13 14:29:26
User: dbbolton
Functions: perl
Tags: perl grep regex

If you've ever tried "grep -P" you know how terrible it is. Even the man page describes it as "highly experimental". This function will let you 'grep' pipes and files using Perl syntax for regular expressions.

The first argument is the pattern, e.g. '/foo/'. The second argument is a filename (optional).

ls -l | grep ^d | sed 's:.*\ ::g'
find * -type d -maxdepth 0
2011-08-07 06:04:50
User: edogawaconan
Functions: find
Tags: find ls grep sh


ls -F | grep /\$

but will break on directories containing newlines. Or the safe, POSIX sh way (but will miss dotfiles):

for i in *; do test -d "./$i" && printf "%s\n" "$i"; done
ls -l | grep ^d | sed 's:.*\ ::g'
2011-08-06 23:52:46
User: LinuxMan
Functions: grep ls sed
Tags: bash sed ls grep

Normally, if you just want to see directories you'd use brianmuckian's command 'ls -d *\', but I ran into problems trying to use that command in my script because there are often multiple directories per line. If you need to script something with directories and want to guarantee that there is only one entry per line, this is the fastest way i know

vim -p `grep -r PATTERN TARGET_DIR | cut -f1 -d: | sort | uniq | xargs echo -n`
php --ini
export GREP_OPTIONS='--color=auto'
2011-07-24 01:32:10
User: h3xx
Functions: export
Tags: color grep

This will affect all invocations of grep, even when it is called from inside a script.

curl -s --compressed http://funnyjunk.com | awk -F'"' '/ '"'"'mainpagetop24h'"'"'/ { print "http://funnyjunk.com"$4 }' | xargs curl -s | grep -o 'ht.*m/pictures/.*\.jpg\|ht.*m/gifs/.*\.gif' | grep "_......_" | uniq | xargs wget
2011-07-21 15:57:21
User: laniner
Functions: awk uniq xargs

If your version of curl does not support the --compressed option, use

curl -s http://funnyjunk.com | gunzip

instead of

curl -s --compressed http://funnyjunk.com
find . -name '*.java' -o -name '*.xml' | grep -v '\.svn' | xargs wc -l
2011-06-30 12:45:40
User: ewilson
Functions: find grep wc xargs
Tags: find grep wc

There's nothing particularly novel about this combination of find, grep, and wc, I'm just putting it here in case I want it again.

ack "search pharse" *.[ch]
pkill -KILL -u username
2011-06-16 11:40:57
User: SuperFly
Tags: awk grep who

How to force a userid to log out of a Linux host, by killing all processes owned by the user, including login shells:

gpg --refresh-keys
curl -s http://example.com | grep -o -P "<a.*href.*>" | grep -o "http.*.pdf" | xargs -d"\n" -n1 wget -c
2011-06-09 14:42:46
User: b_t
Functions: grep wget xargs

This example command fetches 'example.com' webpage and then fetches+saves all PDF files listed (linked to) on that webpage.

[*Note: of course there are no PDFs on example.com. This is just an example]

find . -name "*.[ch]" -print | xargs grep -i -H "search phrase"
2011-06-05 23:27:30
User: jblaine
Functions: find grep xargs
Tags: find grep

Original submitter's command spawns a "grep" process for every file found. Mine spawns one grep with a long list of all matching files to search in. Learn xargs, everyone! It's a very powerful and always available tool.

ifconfig | awk '/HWaddr/ { print $1, $5 }'
ifconfig | grep HWaddr | awk '{print $1,$5}'
grep -E '<DT><A|<DT><H3' bookmarks.html | sed 's/<DT>//' | sed '/Bookmarks bar/d' | sed 's/ ADD_DATE=\".*\"//g' | sed 's/^[ \t]*//' | tr '<A HREF' '<a href'
2011-05-26 22:21:01
User: chrismccoy
Functions: grep sed tr
Tags: sed grep chrome

chrome only lets you export in html format, with a lot of table junk, this command will just export the titles of the links and the links without all that extra junk

grep -v ^# /etc/somefile.conf | grep .
apt-get install `ssh [email protected]_you_want_to_clone "dpkg -l | grep ii" | awk '{print $2}'`
domain=google.com; for ns in $(whois $domain | awk -F: '/Name Server/{print $2}'); do echo ">>> Nameservers for $domain from $a <<<"; dig @$ns $domain ns +short; echo; done;
2011-05-08 04:46:34
User: laebshade
Functions: awk dig echo whois

Change the $domain variable to whichever domain you wish to query.

Works with the majority of whois info; for some that won't, you may have to compromise:

domain=google.com; for a in $(whois $domain | grep "Domain servers in listed order:" --after 3 | grep -v "Domain servers in listed order:"); do echo ">>> Nameservers for $domain from $a

Note that this doesn't work as well as the first one; if they have more than 3 nameservers, it won't hit them all.

As the summary states, this can be useful for making sure the whois nameservers for a domain match the nameserver records (NS records) from the nameservers themselves.

sudo aptitude remove -P $(dpkg -l|awk '/^ii linux-image-2/{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)}')
2011-04-25 05:19:57
User: Bonster
Functions: awk sed sudo

Same as 7272 but that one was too dangerous

so i added -P to prompt users to continue or cancel

Note the double space: "...^ii␣␣linux-image-2..."

Like 5813, but fixes two bugs: [1]This leaves the meta-packages 'linux-headers-generic' and 'linux-image-generic' alone so that automatic upgrades work correctly in the future. [2]Kernels newer than the currently running one are left alone (this can happen if you didn't reboot after installing a new kernel).

find . -maxdepth 1 -type d | grep -Pv "^.$" | sort -rn --field-separator="-" | sed -n '3,$p' | xargs rm -rf
cgrep() { GREP_COLOR="1;3$((RANDOM%6+1))" grep --color=always "$@" }
2011-03-04 18:45:58
User: derekschrock
Functions: grep
Tags: grep

Randomize GNU grep's colors 31-36 excluding black and white.