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,581 results
find . -type d -execdir du -sh '{}' ';' | grep -E "[0-9]+K" | sed 's/^[0-9\.]\+K[\t ]\+//' | tr "\n" "\0" | xargs -0 rm -rf
grep lm /proc/cpuinfo
2009-02-19 22:48:47
User: bobbydale
Functions: grep
-2

If the 'lm' flag is present, then the CPU is 64-bit.

If no output, then CPU is 32-bit.

netstat -an | grep -i listen
2009-02-19 19:27:49
User: scubacuda
Functions: grep netstat
-2

From 'man netstat'

"netstat -i | -I interface [-abdnt] [-f address_family] [-M core] [-N system]

Show the state of all network interfaces or a single interface

which have been auto-configured (interfaces statically configured

into a system, but not located at boot time are not shown). An

asterisk (``*'') after an interface name indicates that the

interface is ``down''. If -a is also present, multicast

addresses currently in use are shown for each Ethernet interface

and for each IP interface address. Multicast addresses are shown

on separate lines following the interface address with which they

are associated. If -b is also present, show the number of bytes

in and out. If -d is also present, show the number of dropped

packets. If -t is also present, show the contents of watchdog

timers."

ps aux | grep -i firefox | grep -v grep | awk '{print $2}' | xargs -t -i kill -9 {}
2009-02-19 18:50:00
User: blackdude
Functions: awk grep kill ps xargs
-7

This is a nice way to kill processes.. the example here is for firefox!!! substitute firefox for whatever the process name is...

grep -v "^$" file
2009-02-19 18:11:58
User: hkyeakley
Functions: grep
0

To save the result, redirect the output to another file.

grep -v "^$" file1 > file2
lsof | grep "/var/spool/mail/"
svn st | grep ^\? | awk '{print $2}' | xargs svn add
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
1

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
0

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
15

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
-4

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
14

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
-3

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
3

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
-1

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
4

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
2

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
4

/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
-3

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
11

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
2

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
-1

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
-4

changes the PS1 to something better than default.

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