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

All commands from sorted by
Terminal - All commands - 11,618 results
ls -1 . dir2 dir3|sort|uniq -d
2009-02-17 16:29:03
User: tlknv
Functions: ls
1

cat file1 file2 file3|sort|uniq -d

finds the same lines in several files, especially in files with lists of files.

ssh -CNL 3306:localhost:3306 user@site.com
ssh -D 8888 user@site.com
2009-02-17 15:34:27
User: wesrog
Functions: ssh
5

Simply change your web browser's proxy settings to point to a SOCKS proxy at port 8888 and you're good to go.

kill -9 $$
2009-02-17 15:10:29
User: chanux
Functions: kill
-2

Kill -9 immediately kills the given process number. $$ is the process ID of the process you are in.

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.

mkdir -p work/{d1,d2}/{src,bin,bak}
perl -pe 's/\d+/++$n/e' file.txt
cat /proc/cpuinfo
2009-02-17 14:50:29
User: stinger
Functions: cat
-4

how to get full cpu info of your (linux) box

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.

for FILE in $(ls); do [COMMAND]; done
2009-02-17 14:42:46
User: stinger
-4

# Small for loop, that can list files in dir, and after that executes

# [COMMAND] of your choice, usefull for example rename, move, tar etc..

# change cmd's for different results :)

echo $RANDOM
nc -v -l 80 < file.ext
2009-02-17 14:39:52
User: moz667
77

From the other machine open a web navigator and go to ip from the machine who launch netcat, http://ip-address/

If you have some web server listening at 80 port then you would need stop them or select another port before launch net cat ;-)

* You need netcat tool installed

history | awk '{print $2}' | sort | uniq -c | sort -rn | head
grep -c -e '^cpu[0-9]\+' /proc/stat
egrep -o '\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b' access.log | sort -u
^J tput sgr0 ^J
2009-02-17 09:57:22
User: berta
Functions: tput
1

when your terminal session seems unrensponsive (this normally happen after outputting some binary data directly on your standard output) it may me saned by hitting:

CTRL+J tput sgr0 CTRL+J

Note: don't press the Enter key, just ctrl+j

curl http://root:PASSWORD@ROUTER_DYN_DNS/bwm/tomato_rstatsa001839ceb1d4.gz?_http_id=HTTPID > $HOME/Dropbox/Backups/tomato_stats.gz
2009-02-17 09:45:34
User: moritz
0

You must get your Backup Url from: http://ROUTER_DYN_DNS/admin-bwm.asp under "Backup".

I set it up in a curl

find . -type f|while read f; do mv $f `echo $f |tr '[:upper:]' '[ :lower:]'`; done
2009-02-17 09:44:38
User: berta
Functions: find mv read
9

or, to process a single directory:

for f in *; do mv $f `echo $f |tr '[:upper:]' '[:lower:]'`; done
find . -type f -name \*.c | while read f; do mv $f "`basename $f .c`".C; done
2009-02-17 09:30:43
User: berta
Functions: find mv read
0

or, for a single directory:

for f in *.c; do mv $f "`basename $f .c`".C; done
echo foo bar foobar barfoo && echo !$ !^ !:3 !* && echo /usr/bin/foobar&& echo !$:h !$:t
2009-02-17 09:10:17
User: lhb
Functions: echo
11

When expanding, bash output the command, so don't be affraid if you type the command.

Here is the details:

First examples:

echo foo bar foobar barfoo

First argument:

echo !$

echo barfoo

barfoo

(Note that typing echo foo bar foobar barfoo && echo !$, bash substitute !$ with $:1)

Last argument:

echo foo bar foobar barfoo && echo !^

echo foo bar foobar barfoo && echo barfoo

foo bar foobar barfoo

barfoo

All the arguments:

echo !*

echo foo bar foobar barfoo

foo bar foobar barfoo

The third argument:

echo foo bar foobar barfoo && echo !:3

echo foo bar foobar barfoo && echo foobar

foo bar foobar barfoo

foobar

You may want to add {} for large numbers: echo !:{11} for example

Now with path:

echo /usr/bin/foobar

/usr/bin/foobar

For the head:

echo !$:h

echo /usr/bin

/usr/bin

And the tail:

echo !$:t

echo foobar

foobar

You also may want to try !:h and !:t or !!3-4 for the third and the fourth (so !!:* == !!:1-$)

find ~/bin/ -name "*sh" -print0 | xargs -0t tar -zcvf foofile.tar.gz
2009-02-17 08:48:34
User: lhb
Functions: find tar xargs
5

tar options may change ;)

c to compress into a tar file, z for gzip (j for bzip) man tar

-print0 and -0t are usefull for names with spaces, \, etc.

for i in `seq -f %03g 5 50 111`; do echo $i ; done
2009-02-17 08:41:44
User: lhb
Functions: echo
4

seq allows you to format the output thanks to the -f option. This is very useful if you want to rename your files to the same format in order to be able to easily sort for example:

for i in `seq 1 3 10`; do touch foo$i ;done

And

ls foo* | sort -n

foo1

foo10

foo4

foo7

But:

for i in `seq -f %02g 1 3 10`; do touch foo$i ;done

So

ls foo* | sort -n

foo01

foo04

foo07

foo10

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.

dd if=/dev/random of=bigfile bs=1024 count=102400
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.