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,531 results
Q="YOURSEARCH"; GOOG_URL="http://www.google.com/search?q="; AGENT="Mozilla/4.0"; stream=$(curl -A "$AGENT" -skLm 10 "${GOOG_URL}\"${Q/\ /+}\"" | grep -oP '\/url\?q=.+?&amp' | sed 's/\/url?q=//;s/&amp//'); echo -e "${stream//\%/\x}"
2013-04-03 09:56:41
User: techie
Functions: echo grep sed
Tags: google
8

I found this command on a different site and thought you guy might enjoy it. Just change "YOURSEARCH" to what ever you want to search. Example, "Linux Commands"

ps aux | grep [process] | awk '{print $2}' | xargs -I % ls /proc/%/fd | wc -l
lsof -i -n | grep ESTABLISHED
2013-04-03 09:14:09
User: techie
Functions: grep
2

Fast and easy way to find all established tcp connections without using the netstat command.

for a in $(seq 5 8); do cat twit.txt | cut -d " " -f$a | grep "^@" | sort -u; done > followlst.txt
2013-03-29 21:07:09
User: xmuda
Functions: cat cut grep seq sort
-6

Go to "https://twitter.com/search/realtime?q=%23TeamFollowBack&src=hash" and then copy al the text on the page. If you scroll down the page will be bigger. Then put al the text in a text file called twit.txt

If you follow the user there is a high probability the users give you follow back.

To follow all the users you can use an iMacros script.

grep -qIm1 . $file
2013-03-28 14:11:51
User: anon1251
Functions: grep
0

This command produces no output, but its exit status is 0 ("true") if $file is text, non-0 ("false") if $file is binary (or is not accessible).

Explanation:

-q suppresses all the output of grep

-I is the trick: if a binary file is found, it is considered a non-match

-m 1: limit "output" to first match (speed up for big files)

.: the match string, "." stands for any character

Usage: e.g. run editor only on text files

grep -qIm 1 . $file && vi $file
git log | grep Date | awk '{print " : "$4" "$3" "$6}' | uniq -c
grep -Fvxf $(file1) $(file2) | wc -l
dpkg -l |grep i386 | awk '{ print "apt-get -y remove --purge "$2 }' | sh
if wget https://twitter.com/users/username_available?username=xmuda -q -O - | grep -q "\"reason\":\"taken\""; then echo "Username taken"; else echo "Free / Banned Name"; fi
2013-03-23 17:39:15
User: Joschasa
Functions: echo grep wget
0

Reason can be: taken, available, contains_banned_word

if lynx --dump http://twitter.com/xmuda | grep -q "Sorry, that page does"; then echo "Dont Exist"; else echo "Exist"; fi
2013-03-23 16:12:24
User: xmuda
Functions: echo grep
-4

I use these command to validate twitter accounts, we can use a "for a in $(cat list.txt)" to validate a complete list of twitter accounts.

lsof -i -P +c 0 +M | grep -i "$1"
netstat -an | grep --color -i -E 'listen|listening'
tcpdump -l -s0 -w - tcp dst port 25 | strings | grep -i 'MAIL FROM\|RCPT TO'
2013-03-18 18:55:20
User: ene2002
Functions: grep strings tcpdump
3

This works just as well for SMTP. You could run this on your mail server to watch e-mail senders and recipients:

tcpdump -l -s0 -w - tcp dst port 25 | strings | grep -i 'MAIL FROM\|RCPT TO'

find /some/path -type f -printf '%f\n' | grep -o '\..\+$' | sort | uniq -c | sort -rn
2013-03-18 14:42:29
User: skkzsh
Functions: find grep sort uniq
2

Get the longest match of file extension (Ex. For 'foo.tar.gz', you get '.tar.gz' instead of '.gz')

grep -E '([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})' -o tmp
lsmod | grep -io MODULENAME| xargs modinfo | grep -iw version
2013-03-18 07:52:14
User: adriano
Functions: grep lsmod modinfo xargs
Tags: kernel modules
1

Returns the version of the kernel module specified as "MODULENAME", when available.

wget -O - "[PICASA ALBUM RSS LINK]" |sed 's/</\n</g' | grep media:content |sed 's/.*url='"'"'\([^'"'"']*\)'"'"'.*$/\1/' |awk -F'/' '{gsub($NF,"d/"$NF); print $0}'|wget -i -
dpkg --list | grep '^rc\b' | awk '{ print $2 }' | xargs sudo dpkg -P
grep -hor ofx[a-zA-Z]*.h src/ | grep -o ofx[^\.]* >> addons.make
pwd|grep -o '/'|perl -ne '$x.="./.";print`readlink -f $x`'|xargs -tn1 chmod 755
2013-03-14 12:03:44
Functions: chmod grep perl pwd xargs
0

`pwd` returns the current path

`grep -o` prints each slash on new line

perl generates the paths sequence: './.', './../.', ...

`readlink` canonicalizes paths (it makes the things more transparent)

`xargs -tn1` applies chmod for each of them. Each command applied is getting printed to STDERR.

lynx --dump http://en.trending-topic.com/countries/Mexico/ | grep "62]#" | sed 's/\[62\]//g'
2013-03-12 16:25:14
User: xmuda
Functions: grep sed
-1

In these command i use lynx to get the top trend topic of Mexico, if you replace Mexico with other country, you will get the #1 Trending topic

cat /var/lib/dpkg/info/*.md5sums|grep usr/sbin/sshd|sed 's,usr,/usr,'|md5sum -c
2013-03-12 11:20:48
User: Ztyx
Functions: cat grep md5sum sed
0

Replace "user/sbin/sshd" with the file you would like to check. If you are doing this due to intrusion, you obviously would want to check size, last modification date and md5 of the md5sum application itself. Also, note that "/var/lib/dpkg/info/*.md5sums" files might have been tampered with themselves. Neither to say, this is a useful command.

file -i `find . -name '*.jpg' -print` | grep "application/msword"
2013-03-10 16:53:23
User: genghisdani
Functions: file grep
0

Created to deal with an overzealous batch rename on our server that renamed all files to .jpg files.

ls -l | grep ^d
find ./ -type f -name "*.php" | xargs grep -n "name" -r {}