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 . -type f -name "*.tar" -printf [%f]\\n -exec tar -tf {} \; | grep -iE "[\[]|<filename>"
2011-01-06 13:01:38
Functions: find grep tar
Tags: find grep tar
1

A quick find command to identify all TAR files in a given path, extract a list of files contained within the tar, then search for a given string in the filelist. Returns to the user as a list of TAR files found (enclosed in []) followed by any matching files that exist in that archive. TAR can easily be swapped for JAR if required.

find /name/of/dir/ -name '*.txt' | xargs grep 'text I am searching for'
2011-01-05 15:20:40
User: erickb
Functions: find grep xargs
Tags: find xargs grep
1

recursively search dir for a a particular file type, search each file for a particular text.

dpkg -l python
2011-01-05 06:15:13
User: hackerb9
1

If the first two letters are "ii", then the package is installed. You can also use wildcards. For example,

.

dpkg -l openoffice*

.

Note that dpkg will usually not report packages which are available but uninstalled. If you want to see both which versions are installed and which versions are available, use this command instead:

.

apt-cache policy python
apt-show-versions | grep '\bpython\b'
ps -ef | grep c\\ommand
grep -i "$*" /usr/lib/perl5/Unicode/CharName.pm | while read a b; do /usr/bin/printf "\u$a\tU+%s\t%s\n" "$b"; done
2011-01-04 11:30:16
User: ioggstream
Functions: grep read
1

No need for further filedes or substitution for splitting. Simply use read a b

svn log -q | grep '^r[0-9]' | cut -f2 -d "|" | sort | uniq -c | sort -nr
2011-01-03 15:23:08
User: kkapron
Functions: cut grep sort uniq
2

list top committers (and number of their commits) of svn repository.

in this example it counts revisions of current directory.

egrep -i "^[0-9a-f]{4,} .*$*" $(locate CharName.pm) | while read h d; do /usr/bin/printf "\U$(printf "%08x" 0x$h)\tU+%s\t%s\n" $h "$d"; done
2010-12-31 16:47:59
User: hackerb9
Functions: egrep locate read
12

[Update! Thanks to a tip from ioggstream, I've fixed both of the bugs mentioned below.]

You, yes, 𝙔𝙊𝙐, can be the terror of the Internet! Why use normal, boring bullet points in your text, when you could use a ROTATED HEAVY BLACK HEART BULLET (❥)!? (Which would also be an awesome band name, by the way).



This script makes it easy to find unusual characters from the command line. You can then cut and paste them or, if you're using a GTK application, type Control+Shift+U followed by the code point number (e.g., 2765) and then a SPACE.



USAGE: Put this script in a file (I called mine "ugrep") and make it executable. Run it from the command line like so,



ugrep heart



The output will look like this,



☙ U+2619 REVERSED ROTATED FLORAL HEART BULLET

♡ U+2661 WHITE HEART SUIT

♥ U+2665 BLACK HEART SUIT

❣ U+2763 HEAVY HEART EXCLAMATION MARK ORNAMENT

❤ U+2764 HEAVY BLACK HEART

❥ U+2765 ROTATED HEAVY BLACK HEART BULLET

❦ U+2766 FLORAL HEART

❧ U+2767 ROTATED FLORAL HEART BULLET

⺖ U+2E96 CJK RADICAL HEART ONE

⺗ U+2E97 CJK RADICAL HEART TWO

⼼ U+2F3C KANGXI RADICAL HEART



You can, of course, use regular expressions. For example, if you are looking for the "pi" symbol, you could do this:



ugrep '\bpi\b'



REQUIREMENTS: Although this is written in Bash, it assumes you have Perl installed because it greps through the Perl Unicode character name module (/usr/lib/perl5/Unicode/CharName.pm). Note that it would not have made more sense to write this in Perl, since the CharName.pm module doesn't actually include a subroutine for looking up a character based on the description. (Weird.)



BUGS: In order to fit this script in the commandlinefu limits, a couple bugs were added. ① Astral characters beyond the BMP (basic multilingual plane) are not displayed correctly, but see below. ② Perl code from the perl module being grepped is sometimes extraneously matched.



MISFEATURES: Bash's printf cannot, given a Unicode codepoint, print the resulting character to the terminal. GNU's coreutils printf (usually "/usr/bin/printf") can do so, but it is brokenly pedantic about how many hexadecimal digits follow the escape sequence and will actually die with an error if you give the wrong number. This is especially annoying since Unicode code points are usually variable length with implied leading zeros. The CharNames.pm file represents BMP characters as 4 hexits, but astral characters as 5. In the actual version of this script that I use, I've kludged around this misfeature by zero-padding to 8 hexits like so,



/usr/bin/printf "\U$(printf "%08x" 0x$hex)"



TIP 1: The author recommends "xsel" for command line cut-and-paste. For example,



ugrep biohazard | xsel



TIP 2: In Emacs, instead of running this command in a subshell, you can type Unicode code points directly by pressing Control-Q first, but you'll likely want to change the default input from octal to hexadecimal. (setq read-quoted-char-radix 16).



TIP 3: Of course, if you're using X, and you want to type one of the more common unusual characters, it's easiest of all to do it with your Compose (aka Multi) key. For example, hitting [Compose] <3 types ♥.

svn log -r '{YYYY-MM-DD}:{YYYY-MM-DD}' | sed -n '1p; 2,/^-/d; /USERNAME/,/^-/p' | grep -E -v '^(r[0-9]|---|$)' | sed 's/^/* /g'
2010-12-22 17:52:19
User: antic
Functions: grep sed
1

* Replace USERNAME with the desired svn username

* Replace the first YYYY-MM-DD with the date you want to get the log (this starts at the midnight event that starts this date)

* Replace the second YYYY-MM-DD with the date after you want to get the log (this will end the log scan on midnight of the previous day)

Example, if I want the log for December 10, 2010, I would put {2010-12-10}:{2010-12-11}

wget -qO - http://ngrams.googlelabs.com/datasets | grep -E href='(.+\.zip)' | sed -r "s/.*href='(.+\.zip)'.*/\1/" | uniq | while read line; do `wget $line`; done
ls | grep '^[A-Z0-9]*$'
2010-12-19 21:45:53
User: b_t
Functions: grep ls
0

Some source package have many 'README' kind of files, among many other regular files/directories. This command could be useful when one wants to list only 'README' kind of files among jungle of other files. (e.g. I came across this situation after downloading source for module-init-tools)

Warning: This command would miss a file like => README.1 (or one with spaces in-between)

Corrections welcome.

aptitude remove $(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)}')
2010-12-11 11:38:15
User: __
Functions: awk sed
7

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).

I'm bummed that this took 228 characters. I'd like to see a simpler version.

pcregrep -r --exclude_dir='.svn' --include='.*jsp$' -A 2 -B 2 --color "pHtmlHome" .
2010-12-09 15:26:07
User: hute37
Tags: perl grep
0

range context (-A -B) search, with exclusion of vcs directories

ifconfig | grep -o "inet [^ ]*" | cut -d: -f2
2010-12-06 10:36:52
User: dooblem
Functions: cut grep ifconfig
Tags: ifconfig grep cut
-2

This is what we use.

You can grep -v 127.0.0.1 if you wish.

grep '\<110/' /etc/services; grep '\b110/' /etc/services
2010-11-25 08:29:42
User: unefunge
Functions: grep
Tags: grep services
0

I used 110 as the port number in examples for clarity.

backslash+lessthan or backslash+b marks 'edge of the word'.

find | egrep "\.(ade|adp|bat|chm|cmd|com|cpl|dll|exe|hta|ins|isp|jse|lib|mde|msc|msp|mst|pif|scr|sct|shb|sys|vb|vbe|vbs|vxd|wsc|wsf|wsh)$"
2010-11-23 16:53:55
User: poulter7
Functions: egrep find
-1

Returns any file in the folder which would be rejected by Gmail, if you were to send zipped version.

(Yes, you could just zip it and knock the extension off and put it back on the other side, but for some people this just isn't a solution)

w3m -no-cookie http://m.espn.go.com/nfl/scoreboard?|sed 's/ Final/ : Final/g'|sed 's/ F\// : F\//g'|sed 's/, / : /g'|grep -i ':'
2010-11-15 01:18:19
User: SQUIIDUX
Functions: grep sed
Tags: sed grep w3m
2

change the nfl in the url to mlb or nba to get those score/times as well

grep 'model\|MHz' /proc/cpuinfo |tail -n 2
pcregrep --color -M -N CRLF "owa_pattern\.\w+\W*\([^\)]*\)" source.sql
sed '/'"<opening tag>"'/,/'"<closing tag>"'/{/'"<closing tag>"'/d;p};d' "<file>"
cat file.txt | sed -e /<opening tag>/d -e /<closing tag>/G | sed -e '/./{H;$!d;}' -e 'x;/<string to search>/!d;'
2010-11-04 10:31:15
User: EBAH
Functions: cat sed
2

Working with log files that contains variable length messages wrapped between open and close tags it may be useful to filter the messages upon a keyword.

This works fine with GNU sed version 4.2 or higher, so pay attention to some unix distros (solaris, hp-ux, etc.).

Linux should be ok.

GREP_OPTIONS='-D skip --binary-files=without-match --ignore-case'
2010-11-03 23:10:09
User: AskApache
7

This is very helpful to place in a shell startup file and will make grep use those options all the time. This example is nice as it won't show those warning messages, skips devices like fifos and pipes, and ignores case by default.

grep -i '[^script$]' 1.txt
curl -s http://urlxray.com/display.php?url=http://tinyurl.com/demo-xray | grep -o '<title>.*</title>' | sed 's/<title>.*--> \(.*\)<\/title>/\1/g'
2010-09-30 10:25:18
User: karpoke
Functions: grep sed
Tags: sed grep curl
-3

We can put this inside a function:

fxray() { curl -s http://urlxray.com/display.php?url="$1" | grep -o '<title>.*</title>' | sed 's/<title>.*--> \(.*\)<\/title>/\1/g'; }; fxray http://tinyurl.com/demo-xray
pgrep -c cat