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.

Universal configuration monitoring and system of record for IT.

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 using grep from sorted by
Terminal - Commands using grep - 1,637 results
fping -r1 -g <subnet> 2> /dev/null | grep unreachable | cut -f1 -d' '
curl -u <username> http://app.boxee.tv/api/get_queue | xml2 | grep /boxeefeed/message/description | awk -F= '{print $2}'
2010-01-20 16:17:19
User: Strawp
Functions: awk grep
Tags: curl xml boxee

Might be able to do it in less steps with xmlstarlet, although whether that would end up being shorter overall I don't know - xmlstarlet syntax confuses the heck out of me.

Prompts for your password, or if you're a bit mental you can add your password into the command itself in the format "-u user:password".

sqlite3 -list /home/$USER/.mozilla/firefox/*.default/places.sqlite 'select url from moz_places ;' | grep http
2010-01-18 15:25:00
User: bubo
Functions: grep

if firefox is running the database is locked, so you need to copy the places.sqlite file temporarily somewhere to be able to query it...

find . -exec grep -l "sample" {} \;
2010-01-16 13:12:52
User: whoami
Functions: find grep

Will find all files containing "sample" in the current directory and in the directories below.

find . -name "*.php" -exec grep -il searchphrase {} \;
2010-01-16 05:09:30
Functions: find grep

This is very similar to the first example except that it employs the 'exec' argument of the find command rather than piping the result to xargs. The second example is nice and tidy but different *NIXs may not have as capable a grep command.

grep -rHi searchphrase *.php
find . -name "*.php" | xargs grep -il searchphrase
2010-01-14 22:42:36
User: refrax
Functions: find grep xargs

This command will find all files recursively containing the phrase entered, represented here by "searchphrase". This particular command searches in all php files, but you could change that to just be html files or just log files etc.

while (ps -ef | grep [r]unning_program_name); do sleep 10; done; command_to_execute
2010-01-14 16:26:34
User: m_a_xim
Functions: grep ps sleep

The '[r]' is to avoid grep from grepping itself. (interchange 'r' by the appropriate letter)

Here is an example that I use a lot (as root or halt will not work):

while (ps -ef | grep [w]get); do sleep 10; done; sleep 60; halt

I add the 'sleep 60' command just in case something went wrong; so that I have time to cancel.

Very useful if you are going to bed while downloading something and do not want your computer running all night.

find /lib/modules/`uname -r`/ -type f -iname '*.o' -or -iname '*.ko' |grep -i -o '[a-z0-9]*[-|_]*[0-9a-z]*\.ko$' |xargs -I {} echo '# {}' >>/etc/modules.autoload.d/kernel-2.6
2010-01-13 02:12:08
User: paragao
Functions: echo find grep xargs

Whenever you compile a new kernel, there are always new modules. The best way to make sure you have the correct modules loaded when you boot is to add all your modules in the modules.autoload file (they will be commented) and uncomment all those modules you need.

Also a good way to keep track of the available modules in your system.

For other distros you may have to change the name of the file to /etc/modprobe.conf

grep -e `date +%Y-%m-%d` /var/log/dpkg.log | awk '/install / {print $4}' | uniq | xargs apt-get -y remove
port=8888;pid=$(lsof -Pan -i tcp -i udp | grep ":$port"|tr -s " " | cut -d" " -f2); ps -Afe|grep "$pid"|grep --invert-match grep | sed "s/^\([^ ]*[ ]*\)\{7\}\(.*\)$/\2/g"
2010-01-11 17:49:22
User: glaudiston
Functions: cut grep ps sed tr

A way not so simple but functional for print the command for the process that's listening a specific port.

I got the pid from lsof because I think it's more portable but can be used netstat

netstat -tlnp
ps -ef | grep user | awk '{print $2}' | while read pid; do echo $pid ; pfiles $pid| grep portnum; done
2010-01-11 12:34:51
User: sharfah
Functions: awk echo grep ps read

My old Solaris server does not have lsof, so I have to use pfiles.

sudo nmap -F -O | grep "Running: " > /tmp/os; echo "$(cat /tmp/os | grep Linux | wc -l) Linux device(s)"; echo "$(cat /tmp/os | grep Windows | wc -l) Window(s) devices"
2010-01-10 03:09:56
User: matthewbauer
Functions: echo grep sudo

Shows how many Windows and Linux devices are on your network.

May add support for others, but that's all that are on my network right now.

grep -n "^" <filename>
2010-01-07 14:54:29
User: JohnGH
Functions: grep

If you don't have nl on your system, this achieves a similar effect, the default behavior in nl is to not number blank lines, but this does.

for x in `ptree | awk '{print $1}'`; do pfiles $x | grep ${PORT} > /dev/null 2>&1; if [ x"$?" == "x0" ]; then ps -ef | grep $x | grep -v grep; fi; done 2> /dev/null
2010-01-05 17:02:23
User: bpfx
Functions: awk grep ps

Can use lsof, but since it's not part of the base OS, it's not always available.

ifconfig | grep -B 1 "inet addr:" | head -1 | cut -d" " -f1
2010-01-04 23:02:57
User: jasonwalsh
Functions: cut grep head ifconfig

Get the line containing "inet addr:" and the line before that, get down to only the first line, and then get the first word on that line, which should be the interface.

flight_status() { if [[ $# -eq 3 ]];then offset=$3; else offset=0; fi; curl "http://mobile.flightview.com/TrackByRoute.aspx?view=detail&al="$1"&fn="$2"&dpdat=$(date +%Y%m%d -d ${offset}day)" 2>/dev/null |html2text |grep ":"; }
2010-01-04 15:49:09
User: suhasgupta
Functions: grep

Usage: flight_status airline_code flight_number (optional)_offset_of_departure_date_from_today

So for instance, to track a flight which departed yesterday, the optional 3rd parameter should have a value of -1.


flight_status ua 3655 -1



Status: Arrived

Departure: San Francisco, CA (SFO)

Scheduled: 6:30 AM, Jan 3

Takeoff: 7:18 AM, Jan 3

Term-Gate: Term 1 - 32A

Arrival: Newark, NJ (EWR)

Scheduled: 2:55 PM, Jan 3

At Gate: 3:42 PM, Jan 3

Term-Gate: Term C - C131


html2text needs to be installed for this command. only tested on ubuntu 9.10

(bzcat BZIP2_FILES && cat TEXT_FILES) | grep -E "Invalid user|PAM" | grep -o -E "from .+" | awk '{print $2}' | sort | uniq >> /etc/hosts.deny
2010-01-03 04:41:51
User: jayhawkbabe
Functions: awk cat grep sort uniq

Searches all log files (including archived bzip2 files) for invalid user and PAM authentication errors, both of which are indicative of brute force attempts at logging into computer. A list of all unique IP addresses and domain names is appended to hosts.deny. The command (and grep error messages) will work on Mac OS X 10.6, small adjustments may be needed for other OSs.

dpkg --get-selections | grep linux-image
find . -exec grep $foo {} \; -print
2009-12-30 17:41:44
User: linuxgeek
Functions: find grep

The command will help to print the location of the pattern. Above command will print all the files which contain variable "$foo" along with line containing that pattern.

Specify pattern after "grep"

stop () { ps -ec | grep $@ | kill -SIGSTOP `awk '{print $1}'`; }
2009-12-27 19:40:09
User: iridium172
Functions: grep kill ps

Add that and "cont () { ps -ec | grep $@ | kill -SIGCONT `awk '{print $1}'`; }" (without the quotes) to you bash profile and then use it to pause and resume processes safely

$class=ExampleClass; $path=src; for constant in `grep ' const ' $class.php | awk '{print $2;}'`; do grep -r "$class::$constant" $path; done
find . -name '*png' -printf '%h\0' | xargs -0 ls -l --hide=*.png | grep -ZB1 ' 0$'
php -i | grep php.ini
2009-12-23 15:52:20
User: jemmille
Functions: grep
Tags: bash grep PHP

Quick and easy way to find out which php.ini file is being used. Especially useful if you just need to find the location of the file for editing purposes.

DIR=. ; FSTYPE=$(df -TP ${DIR} | grep -v Type | awk '{ print $2 }') ; echo "${FSTYPE}"
2009-12-22 14:01:50
User: unixhome
Functions: awk df echo grep

Exclude 400 client hosts with NFS auto-mounted home directories.

Easily modified for inclusion in your scripts.