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 tagged grep from sorted by
Terminal - Commands tagged grep - 346 results
netstat -Aan | grep .80 | grep -v 127.0.0.1 | grep EST | awk '{print $6}' | cut -d "." -f1,2,3,4 | sort | uniq
2012-02-03 13:54:11
Functions: awk cut grep netstat sort
0

See who is using a specific port. Especially when you're using AIX. In Ubuntu, for example, this can easily be seen with the netstat command.

ps -fea | grep PATTERN | awk {'print $2'} | xargs kill -9
grep . "$f" | head -n1
2012-01-27 02:58:07
User: captaincomic
Functions: grep head
Tags: grep
0

Use this command if your file may contain empty lines and you need to optain the first non-empty line.

find . | grep -E "(\||\\|\?|\*|<|\"|:|>)"
2012-01-21 17:59:20
User: dj_bushido
Functions: find grep
0

Find all files under "." that are invalid NTFS filenames. Find locates all files, and grep shows the invalid ones.

find . | grep -E "(\||\\|\?|\*|<|\"|:|>|\+|\[|\])"
2012-01-21 17:54:58
User: dj_bushido
Functions: find grep
0

Find is used to "find" all filenames - grep shows those that are invalid.

grep "cpu " /proc/stat | awk -F ' ' '{total = $2 + $3 + $4 + $5} END {print "idle \t used\n" $5*100/total "% " $2*100/total "%"}'
2012-01-21 04:12:50
User: Goez
Functions: awk grep
0

This command displays the CPU idle + used time using stats from /proc/stat.

pcregrep --color -M -N CRLF -e "SQLEngine\.\w+\W*\([^\)]*\)" -r --include='\.java$'
find /etc/ /pentest/ -type f -iname "*sql*" | grep map
2012-01-15 13:30:32
User: celalerdik
Functions: find grep
Tags: find grep
0

you can find a special things(with defined -iname "*sql*") from in most of one direcroty(for example from both /etc/ and /pentest/) and then you can want to grep only include "map" word

ps -ef | grep [j]ava | awk -F ' ' ' { print $1," ",$2,"\t",$(NF-2),"\t",$(NF-1),"\t",$NF } ' | sort -k4
2012-01-05 16:05:48
User: drockney
Functions: awk grep ps sort
Tags: sort awk grep ps
0

Tested in bash on AIX & Linux, used for WAS versions 6.0 & up. Sorts by node name.

Useful when you have vertically-stacked instances of WAS/Portal. Cuts out all the classpath/optional parameter clutter that makes a simple "ps -ef | grep java" so difficult to sort through.

find `pwd` -type f \( -iname thumb.php -or -iname timthumb.php \) -exec grep -HP 'define ?\(.VERSION' {} \;
2011-12-27 11:33:54
User: djkee
Functions: find grep
-3

Good for finding outdated timthumb.php scripts which need to be updated, anything over 2.0 should be secure, below that timthimb is vulnerable and can be used to compromise your website.

find . -type f -exec awk '/linux/ { printf "%s %s: %s\n",FILENAME,NR,$0; }' {} \;
ps -p pid -o logname |tail -1
ps aux | grep PID | grep -v 'grep' | awk '{ print $1 }'
2011-11-05 04:53:29
User: nssy
Functions: awk grep ps
Tags: bash awk grep ps
0

Gets the current system user running a process with the specified pid

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
1

Hide comments and empty lines, included XML comments,

for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k" --`\\t"$k";done|sort
2011-11-02 06:16:22
User: ryan
Functions: echo sed
1

This fixes a bug found in the other scripts which fail when a branch has the same name as a file or directory in the current directory.

grep -Ilr "<?php" .
grep -n log4j MainPm.java | sed -e 's/^\([^:]*\):\(.*\)/\2 \1/'
2011-10-21 12:50:30
User: bash_vi
Functions: grep sed
Tags: sed grep regex
0

Uses sed with a regex to move the linenumbers to the line end. The plain regex (w/o escapes) looks like that:

^([^:]*):(.*)

#!/bin/bash find | grep -P -v "(class)|(zip)|(png)|(gz)|(gif)|(jpeg)|(jpg)" | xargs -I @ grep -H $1 @
2011-10-14 13:28:49
User: kirdie
Functions: find grep xargs
Tags: find grep
-1

calls grep on all non-binary files returned by find on its current working directory

apt-get remove $(dpkg -l | awk "/^ii linux-(image|headers)/ && ! /`uname -r`/ {print \$2}")
2011-10-09 13:58:47
User: _john
Functions: apt awk
0

since awk was already there one can use it instead of the 2 greps. might not be faster, but fast enough

svn status|grep -iR '^!'|sed 's/!/ /g'|xargs -i svn rm '{}'
2011-10-06 08:11:25
User: erdeszt
Functions: grep rm sed xargs
Tags: svn sed xargs grep
0

Helps if you accidentally deleted files from an svn repo with plain rm and you would like to mark them for svn to delete too.

comm -13 <(sort file1) <(sort file2) > file-new
2011-10-01 18:07:54
User: daa
Functions: comm sort
-2

If both file1 and file2 are already sorted:

comm -13 file1 file2 > file-new

tail -n +<N> <file> | head -n 1
2011-09-30 08:30:30
User: qweqq
Functions: head tail
-5

Tail is much faster than sed, awk because it doesn't check for regular expressions.

grep -Fvf file1 file2 > file-new
2011-09-29 06:37:13
User: wibbel4
Functions: grep
Tags: grep textfile
2

This command compares file2 with file1 and removes the lines that are in file1 from file2. Handy if you have a file where file1 was the origional and you want to remove the origional data from your file2.

grep -rnA 10 TODO *
2011-09-28 16:26:14
User: blockcipher
Functions: grep
Tags: grep
-1

This will recursively go through every file under the current directory showing all lines containing "TODO" as well as 10 lines after it. The output will be marked with line numbers to make it easier to find where the TODO is in the actual file.