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 egrep from sorted by
Terminal - Commands using egrep - 197 results
shorty () { curl -s https://www.googleapis.com/urlshortener/v1/url\?key\=API_KEY -H 'Content-Type: application/json' -d '{"longUrl": "'"$1"'"}' | egrep -o 'http://goo.gl/[^"]*' }
2012-04-26 18:30:50
User: cybersiddhu
Functions: egrep

First get a api key for google url shortner from here https://developers.google.com/url-shortener/

Then replace the API_KEY in the command

find . -name unit-test -o -name '*.c' -o -name '*.cpp' | egrep -v "unit-test|android"
false; while [ $? != 0 ]; do apg -c /dev/urandom -n1 -E oOlL10 | egrep '^[[:alnum:]]+$'; done
lynx -dump http://www.cooks4arab.com | awk '/http/{print $2}' | egrep "^https{0,1}"
lynx -dump http://www.domain.com | awk '/http/{print $2}' | egrep "^https{0,1}"
cal | egrep -e '^ [0-9]|^[0-9]' | tr '\n' ' ' | awk '{print $NF}'
2012-04-16 11:58:09
User: gouverney
Functions: awk cal egrep tr

Returns last day of current month. Useful to implement a bash script backup based on a GFS strategy.

egrep "([_a-zA-Z][_a-zA-Z0-9]*) *= *\1 *[*/+-] *[0-9]+ *;"
2012-04-09 12:42:07
User: aandre
Functions: egrep

Search for java explicit incrementation in order to replace it with postfix or assignment operator

find /some/path -type f -and -printf "%f\n" | egrep -io '\.[^.]*$' | sort | uniq -c | sort -rn
2012-04-02 19:25:35
User: kyle0r
Functions: egrep find sort uniq
Tags: uniq ls grep


find -printf "%f\n" prints just the file name from the given path. This means directory paths which contain extensions will not be considered.
pmset -g batt | egrep "([0-9]+\%).*" -o --colour=auto | cut -f1 -d';'
2012-03-23 23:23:46
User: deshawnbw
Functions: cut egrep

adjusting the field "f1" will give you additional information such as

f1 = 98%

f2 = discharging

f3 = 2:02 remaining

lsgrp() { read GID USERS <<< "$(grep "^$1:" /etc/group | cut -d: -f3,4 | tr ':,' ' ')" ; echo -e "${USERS// /\n}" | egrep -v "^($1)?$" ; egrep :[0-9]+:$GID: /etc/passwd | cut -d: -f1 ; }
2012-03-16 09:57:33
User: livibetter
Functions: cut echo egrep read

I can't find the lid command on my system, there is also another complied program: http://xyne.archlinux.ca/projects/lsgrp/

curl "http://api.twitter.com/1/statuses/user_timeline.xml?count=1&screen_name=barackobama" | egrep -w "<text>(.*)</text>" | sed -E "s/<\/?text>//g" | say
2012-02-27 18:46:33
User: beerdeaap
Functions: egrep sed
Tags: twitter osx say

speaks out last twitter update using 'say'

wineserver -k; killall -9 wine wineserver; for i in `ps ax|egrep "*\.exe"|grep -v 'egrep'|awk '{print $1 }'`;do kill -9 $i;done
2011-12-30 01:38:15
User: godmachine81
Functions: awk egrep grep kill killall
Tags: kill wine exe

The other 2 commands that are listed will also kill the egrep process and any libexec processes because the .exe isn't escaped so it is really using . meaning anything containing exe. The command i posted escapes the (dot) in .exe and then filters the actual egrep process so that it doesn't get killed before the other processes being killed. Also added the -9 switch for kill to send sigterm to the processes, in case people are wondering why processes aren't getting killed after running just kill . This should work better for people :)

mysql -u root -p -N -e"show processlist\G;" | egrep "Host\:" | awk -F: '{ print $2 }' | sort | uniq -c
for i in `netstat -rn|egrep -v "Interface|Routing"|awk '{print $5}'`;do ifconfig $i;done
egrep -v "^\s*(#|$)" myfile.cfg
2011-12-08 15:25:17
User: dan
Functions: egrep
Tags: egrep comment

Does not print any line that either:

- is empty

- contains only spaces or tabs

- starts with #

- starts with spaces/tabs followed by a #

resolve(){ curl -Is $1 | egrep "Location" | sed "s/Location: \(.*\)/\1/g"; }
2011-12-06 12:37:46
User: hoodie
Functions: egrep sed

since the most url shorteners respond with a header containing the Location: ... this works with most common shorteners

nocomments () { cat $1 | egrep -v '^[[:space:]]*#|^[[:space:]]*$|^[[:space:]]*;' | sed '/<!--.*-->/d' | sed '/<!--/,/-->/d'; }
2011-11-04 12:47:39
User: RuizTapiador
Functions: cat egrep sed
Tags: sed grep comment

Hide comments and empty lines, included XML comments,

function geoip() { curl -s "http://www.geoiptool.com/en/?IP=$1" | html2text | egrep --color "IP Address:|Country:|City:|Longitude:|Latitude:|Host Name:" }
2011-10-25 04:49:25
User: JohnQUnknown
Functions: egrep

Provides a cleaner output plus some more details about the IP address. Also, a flaw was corrected where the URL provided the results in Spanish by default.

portnum() { egrep "[[:space:]]$*/" /etc/services; }
lynx -dump http://www.anti-abuse.org/multi-rbl-check/ | grep ']' | awk -F\] '{ print $2 }' | sed '/^\[/d' | egrep -v ^[A-Z]
sudo ngrep -lqi -p -W none ^get\|^post tcp dst port 80 -d eth0 | egrep '(flv|mp4|m4v|mov|mp3|wmv)'
2011-09-18 06:33:36
User: marste
Functions: egrep sudo

Discover host and url of media files (ex. flv, mp4, m4v..).

It locate the urls of audio and video files so that they can be recorded.

lynx --dump "http://www.google.com.br" | egrep -o "http:.*"
2011-09-05 01:12:15
Functions: egrep

Get all URLs from website via Regular Expression... You must have lynx installed in your computer to execute the command.

--> lynx --dump "" | egrep -o ""

- Must substitute it for the website path that you want to extract the URLs

- Regular Expression that you wanna filter the website

while IFS= read -r -u3 -d $'\0' file; do file "$file" | egrep -q 'executable|ELF' && chmod +x "$file"; done 3< <(find . -type f -print0)
2011-08-18 15:37:23
User: keymon
Functions: chmod egrep file find read

If you make a mess (like I did) and you removed all the executable permissions of a directory (or you set executable permissions to everything) this can help.

It supports spaces and other special characters in the file paths, but it will work only in bash, GNU find and GNU egrep.

You can complement it with these two commands:

1. add executable permission to directories:

find . type d -print0 | xargs -0 chmod +x

2. and remove to files:

find . type d -print0 | xargs -0 chmod -x

Or, in the same loop:

while IFS= read -r -u3 -d $'\0' file; do case $(file "$file" | cut -f 2- -d :) in :*executable*|*ELF*|*directory*) chmod +x "$file" ;; *) chmod -x "$file" ;; esac || break done 3< <(find . -print0)

Ideas stolen from Greg's wiki: http://mywiki.wooledge.org/BashFAQ/020

rm -R `ls | egrep -v 'dir1|dir2|file1'`
whichpkg () { dpkg -S $1 | egrep -w $(readlink -f "$(which $1)")$; }
2011-07-17 13:39:56
User: b_t
Functions: egrep readlink
Tags: dpkg readlink

Advanced revision to the command 8776 . This revision follows symbolic links.

The quotation-marks surrounding $(which $1) allows for graceful handling of errors ( ie. readlink does not complain incase 'which' command generates (null) output)