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,624 results
netstat -alpn | grep :80 | awk '{print $4}' |awk -F: '{print $(NF-1)}' |sort | uniq -c | sort -n
psg () { ps auxwww | egrep "$1|PID" | grep -v grep }
2009-02-18 23:37:35
User: mulad
Functions: egrep grep ps

Yet another ps grep function, but this one includes the column headings.

psg() { if [ -z "$2" ]; then psargs="aux"; greparg="$1"; else psargs="$1"; greparg="$2"; fi; ps $psargs | grep -i "$(echo $greparg | sed -e 's/^\(.\)/[\1]/')\|^$(ps $psargs | head -1)" ; }
2009-02-18 20:57:17
User: DEinspanjer
Functions: grep ps

My variant on this common function. Some highlights:

Allows you to override the default ps args of "aux"

Uses bracket trick to omit the grep process itself without having to use a second grep

Always prints the correct header row of ps output

Limitations: Ugly ps error output if you forget to quote your multi word grep argument

last | grep -v "^$" | awk '{ print $1 }' | sort -nr | uniq -c
2009-02-18 16:38:59
User: hkyeakley
Functions: awk grep last sort uniq

This command takes the output of the 'last' command, removes empty lines, gets just the first field ($USERNAME), sort the $USERNAMES in reverse order and then gives a summary count of unique matches.

grep -iR find_me ./
2009-02-18 15:38:59
User: lfamorim
Functions: grep

Searching for a String in Multiple Files With Grep

sudo dmidecode | grep Serial\ Number | head -n1
2009-02-18 14:54:28
User: nlinux
Functions: grep head sudo

This will give you the Dell Service tag number associated with your machine. Incredibly useful when you need that number for tech support or downloads.

find . | grep deleteme | while read line; do rm $line; done
2009-02-18 00:55:57
User: tec
Functions: find grep read rm

This command deletes all files in all subfolders if their name or path contains "deleteme".

To dry-run the command without actually deleting files run:

find . | grep deleteme | while read line; do echo rm $line; done
ps -ef | grep $USERNAME | awk {'print $2'} | xargs kill [-9]
2009-02-17 20:35:35
User: TheNomad
Functions: awk grep kill ps xargs

This is a 'killall' command equivalent where it is not available.

Prior to executing it, set the environment variable USERNAME to the username, whose processes you want to kill or replace the username with the $USERNAME on the command above.

Side effect: If any processes from other users, are running with a parameter of $USERNAME, they will be killed as well (assuming you are running this as root user)

[-9] in square brackets at the end of the command is optional and strongly suggested to be your last resort. I do not like to use it as the killed process leaves a lot of mess behind.

find apps/ -name "*.svn-base" -prune -o -print -name "*.php" | xargs grep -E 'new .+Form\('
2009-02-17 14:56:01
User: ubermuda
Functions: find grep xargs

finds all forms instanciated into a symfony project, pruning svn files.

ls -1 /bin | xargs -l1 whatis 2>/dev/null | grep -v "nothing appropriate"
2009-02-17 14:46:01
User: stinger
Functions: grep ls whatis xargs

Get simple description on each file from /bin dir, in list form, usefull for newbies.

grep -c -e '^cpu[0-9]\+' /proc/stat
find /directory/to/search/ -type f -print0 | xargs -0 grep "findtext"
2009-02-17 07:16:32
User: dingobytes
Functions: find grep xargs

this will find text in the directory you specify and give you line where it appears.

grep "processor" /proc/cpuinfo | wc -l
2009-02-17 05:39:49
User: jbcurtis
Functions: grep wc

/proc/cpuinfo contains information about the CPU.

Search for "processor" in the /proc/cpuinfo file

wc -l, counts the number of lines.

grep -sq "" /etc/lsb-release && lsb_release -rd
2009-02-17 05:21:20
User: int19h
Functions: grep

grep -sq "" filename && command

grep can be used in combination with && to run a command if a file exists.

ps aux | grep "[s]ome_text"
2009-02-17 02:10:50
User: SiegeX
Functions: grep ps

The trick here is to use the brackets [ ] around any one of the characters of the grep string. This uses the fact that [?] is a character class of one letter and will be removed when parsed by the shell. This is useful when you want to parse the output of grep or use the return value in an if-statement without having its own process causing it to erroneously return TRUE.

grep -v "^\($\|#\)" <filenames>
2009-02-16 21:40:09
User: raphink
Functions: grep

Filter comments and empty lines in files. I find this very useful when trying to find what values are actually set in a very long example config file.

I often set an alias for it, like :

alias nocomment='grep -v "^\($\|#\)"'

cd /usr/ports; grep -F "`for o in \`pkg_info -qao\` ; \ do echo "|/usr/ports/${o}|" ; done`" `make -V INDEXFILE` | \ grep -i \|ports@freebsd.org\| | cut -f 2 -d \|
2009-02-16 21:07:35
User: grep
Functions: cd cut grep

only works for freeBSD where ports are installed in /usr/ports

credit to http://wiki.freebsd.org/PortsTasks

newhostname=$(hostname | awk -F. '{print $1 "." $2}'); ipaddress=$(nslookup `hostname` | grep -i address | awk -F" " '{print $2}' | awk -F. '{print $3 "." $4}' | grep -v 64.142);PS1="[`id -un`.$newhostname.$ipaddress]"' (${PWD}): '; export PS1
2009-02-16 20:11:53
User: simardd

changes the PS1 to something better than default.

[username.hostname.last-2-digits-of-ip] (current directory)

df / | awk '{print $1}' | grep dev | xargs tune2fs -l | grep create
2009-02-16 18:45:03
User: Kaio
Functions: awk df grep tune2fs xargs

Very useful set of commands to know when your file system was created.

find . -name "*.jar" | xargs -tn1 jar tvf | grep --color "SearchTerm"
2009-02-16 17:18:36
Functions: find grep xargs

Great for finding which jar some pesky class is hiding in!

for I in `echo "show databases;" | mysql | grep -v Database`; do mysqldump $I > "$I.sql"; done
2009-02-16 16:41:05
User: ruedu
Functions: grep

Backups all MySQL databases to individual files. Can be put into a script that grabs current date so you have per day backups.

ip route | grep default | awk '{print $3}'
2009-02-16 16:29:03
User: ruedu
Functions: awk grep route

This gets you your default route programatically, useful for scripts

LC_ALL=C svn info | grep Revision | awk '{print $2}'
2009-02-16 14:53:52
Functions: awk grep info

This is the simple revision number on stdout, that can be fed to any useful/fun script of yours. Setting LC_ALL is useful if you use another locale, in which case "Revision" is translated and cannot be found. I use this with doxygen to insert my source files revisions into the doc. An example in Doxyfile:

FILE_VERSION_FILTER = "function svn_filter { LC_ALL=C svn info $1 | grep Revision | awk '{print $2}'; }; svn_filter"

Share your ideas about what to do with the revision number !

iptables -L -n --line-numbers | grep xx.xx.xx.xx
2009-02-16 13:38:05
User: nullrouter
Functions: grep iptables

This is just one method of checking to see if an IP is blocked via IP tables or CSF. Simple and to the point. Replace xx.xx.xx.xx with the IP you wish to check.

for i in $(svn st | grep "?" | awk '{print $2}'); do svn add $i; done;