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 grep from sorted by
Terminal - Commands using grep - 1,569 results
net=DTAG-DIAL ; for (( i=1; i<30; i++ )); do whois -h whois.ripe.net $net$i | grep '^inetnum:' | sed "s;^.*:;$net$i;" ; done
2009-08-01 05:28:19
User: drizzt
Functions: grep sed whois
1

Useful if you f.i. want to block/allow all connections from a certain provider which uses successive netnames for his ip blocks. In this example I used the german Deutsche Telekom which has DTAG-DIAL followed by a number as netname for the dial in pools.

There are - as always ;) - different ways to do this. If you have seq available you can use

net=DTAG-DIAL ; for i in `seq 1 30`; do whois -h whois.ripe.net $net$i | grep '^inetnum:' | sed "s;^.*:;$net$i;" ; done

or without seq you can use bash brace expansion

net=DTAG-DIAL ; for i in {1..30}; do whois -h whois.ripe.net $net$i | grep '^inetnum:' | sed "s;^.*:;$net$i;" ; done

or if you like while better than for use something like

net=DTAG-DIAL ; i=1 ; while true ; do whois -h whois.ripe.net $net$i | grep '^inetnum:' | sed "s;^.*:;$net$i;" ; test $i = 30 && break ; i=$(expr $i + 1) ; done

and so on.

netstat -ant | grep :80 | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -n
grep -E ":(`cat bnd-ips.txt | sed 's/\./\\./g' | tr '\n' '|'`)" access.log
2009-07-30 12:54:55
User: Cowboy
Functions: grep
0

bnd-ips.txt is a list of IP adresses that is concenated for use as regular expression. Yes, this is still very speedy ;=)

cat /proc/net/ip_conntrack | grep ESTABLISHED | grep -c -v ^#
dpkg --get-selections | grep -v deinstall | cut -f 1
2009-07-29 14:37:36
User: odoepner
Functions: cut grep
1

Should work on all systems that use dpkg and APT package management.

ipconfig getpacket en0 | grep yi| sed s."yiaddr = "."en0: ". ipconfig getpacket en1 | grep yi| sed s."yiaddr = "."en1: ".
find . -iname '*filename*.doc' | { while read line; do antiword "$line"; done; } | grep -C4 search_term;
2009-07-28 15:49:58
User: Ben
Functions: find grep read
3

Find Word docs by filename in the current directory, convert each of them to plain text using antiword (taking care of spaces in filenames), then grep for a search term in the particular file.

(Of course, it's better to save your data as plain text to make for easier grepping, but that's not always possible.)

Requires antiword. Or you can modify it to use catdoc instead.

find . *oldname* | grep oldname | perl -p -e 's/^(.*)(oldname)(.*$)/mv $1$2$3 $1newname$3/' | sh
DD=`cat /etc/my.cnf | sed "s/#.*//g;" | grep datadir | tr '=' ' ' | gawk '{print $2;}'` && ( cd $DD ; find . -mindepth 2 | grep -v db\.opt | sed 's/\.\///g; s/\....$//g; s/\//./;' | sort | uniq | tr '/' '.' | gawk '{print "CHECK TABLE","`"$1"`",";";}' )
2009-07-25 03:42:31
User: atcroft
Functions: cd find gawk grep sed sort tr uniq
-1

This command will generate "CHECK TABLE `db_name.table_name` ;" statements for all tables present in databases on a MySQL server, which can be piped into the mysql command. (Can also be altered to perform OPTIMIZE and REPAIR functions.)

Tested on MySQL 4.x and 5.x systems in a Linux environment under bash.

ifconfig | grep "inet [[:alpha:]]\+" | cut -d: -f2 | cut -d' ' -f2
2009-07-24 13:16:37
User: feraf
Functions: cut grep ifconfig
Tags: Linux ifconfig
-6

Get the IP address of all your network cards.

ifconfig | grep "inet addr" | cut -d: -f2 | cut -d' ' -f1
ip addr|grep "inet "
2009-07-22 07:38:06
User: RickDeckardt
Functions: grep
-3

Shows a single line per interface (device), with its IPv4 settings.

Shorter command, better readability in output.

curl -u 'username' https://api.del.icio.us/v1/posts/all | sed 's/^.*href=//g;s/>.*$//g;s/"//g' | awk '{print $1}' | grep 'http'
2009-07-22 07:32:59
User: bubo
Functions: awk grep sed
2

a variation of avi4now's command - thanks by the way!

/sbin/ifconfig | grep inet | cut -f 2 -d ":" | cut -f 1 -d " " |egrep -v "^$"
while true ; do IFS="" read i; echo "$i"; sleep .01; done < <(tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]")
2009-07-22 03:59:07
Functions: dd echo grep read sleep tr true
Tags: color
2

Same as above but slooooow it down

/sbin/ifconfig | grep inet | cut -f 2 -d ":" | cut -f 1 -d " "
2009-07-21 21:42:04
User: random_bob
Functions: cut grep
-1

another way to output the IP address' of the system

for i in `git log --all --oneline --format=%h`; do git grep SOME_STRING $i; done
/sbin/ifconfig eth0 | grep "inet addr" | sed -e 's/.*inet addr:\(.*\) B.*/\1/g'
cat `whereis mysqlbug | awk '{print $2}'` | grep 'CONFIGURE_LINE='
ps -u $USER |grep $1 | awk '{ print $1}'| xargs kill
2009-07-20 10:06:32
User: buffer
Functions: awk grep ps xargs
-4

Well this can come handy , when you don't feel like playing with pid rather if you know

the process name say "firefox",it would kill it.The script given below would kill the process with its name given as first parameter , though not robust enough to notify that process doesn't exist , well if you know what you are doing that's wouldn't be a problem.:)

----

killhim.sh

----

#!/bin/bash

ps -u $USER |grep $1 | awk '{ print $1}'| xargs kill

----

lsof -i -n -P | grep :80
grep -Ir foo *
2009-07-17 15:30:10
User: libdave
Functions: grep
0

short command to find a string in all text files in all subdirectories, excluding all files grep does not deem text files.

$ grep -or string path/ | wc -l
while true; do netstat -p |grep "tcp"|grep --color=always "/[a-z]*";sleep 1;done
2009-07-16 04:52:49
User: buffer
Functions: grep netstat
-4

The -p parameter tell the netstat to display the PID and name of the program to which each socket belongs or in digestible terms list the program using the net.Hope you know what pipe symbol means!

Presently we wish to only moniter tcp connections so we ask grep to scan for string tcp, now from the op of grep tcp we further scan for regular expression /[a-z]*.

Wonder what that means ?

If we look at the op of netstat -p we can see that the name of the application is preceded by a / ( try netstat -p ) so,now i assume application name contains only characters a to z (usually this is the case) hope now it makes some sense.Regular expression /[a-z]* means to scan a string that start with a / and contains zero or more characters from the range a-z !!. Foof .. is t

grep -rc logged_in app/ | cut -d : -f 2 | awk '{sum+=$1} END {print sum}'
2009-07-15 14:16:44
User: terceiro
Functions: awk cut grep
-2

grep's -c outputs how may matches there are for a given file as "file:N", cut takes the N's and awk does the sum.