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 bash from sorted by
Terminal - Commands using bash - 43 results
xkcd() { wget -qO- http://xkcd.com/ | sed -n 's#^<img src="\(http://imgs.[^"]\+\)"\s\+title="\(.\+\?\)"\salt.\+$#eog "\1"\necho '"'\2'#p" | bash ; }
2010-08-25 15:44:31
User: John_W
Functions: bash sed wget
0

This function displays the latest comic from xkcd.com. One of the best things about xkcd is the title text when you hover over the comic, so this function also displays that after you close the comic.

To get a random xkcd comic use the following:

xkcdrandom() { wget -qO- http://dynamic.xkcd.com/comic/random | sed -n 's#^<img src="\(http://imgs.[^"]\+\)"\s\+title="\(.\+\?\)"\salt.\+$#eog "\1"\necho '"'\2'#p" | bash; }

These are just a bit shorter than the ones eigthmillion wrote, however his version didn't work as expected on my laptop for some reason (I got the title-tag first), so these build a command which is executed by bash.

find . -type d -name '*[A-Z]*' -execdir bash -c '! test -f "$(echo "$0" | tr "[:upper:]" "[:lower:]")"' {} \; -execdir bash -c 'mv "$0" "$(echo "$0" | tr "[:upper:]" "[:lower:]")"' {} \;
find /dev/ -name random -exec bash -c '[ -r $0 -a -w $0 ] && dd if=$0 | sort | dd of=$0' {} \;
find /path/to/images -name '*.JPG' -exec bash -c 'mv "$1" "${1/%.JPG/.jpg}"' -- {} \;
2010-01-07 15:41:17
User: sorpigal
Functions: bash find
Tags: bash find mv
7

Recursively rename .JPG to .jpg using standard find and mv. It's generally better to use a standard tool if doing so is not much more difficult.

cd /usr/home && for i in *;do chsh -s bash $i;done
2009-12-31 18:48:53
User: casidiablo
Functions: bash cd chsh
6

This command will set bash as the default shell for all users in a FreeBSD system.

printf "%s\n" !(pattern) ## ksh, or bash with shopt -s extglob
2009-11-26 14:09:56
User: cfajohnson
Functions: bash printf
Tags: ls grep
-1

There's no need for ls or grep; printf is builtin to most modern shells

in bash hit "tab" twice and answer y
seq 4|xargs -n1 -i bash -c "echo -n 164.85.216.{} - ; nslookup 164.85.216.{} |grep name"|tr -s ' ' ' '|awk '{print $1" - "$5}'|sed 's/.$//'
dpkg --get-selections | cut -f1 | while read pkg; do dpkg -L $pkg | xargs -I'{}' bash -c 'if [ ! -d "{}" ]; then echo "{}"; fi' | tr '\n' '\000' | du -c --files0-from - | tail -1 | sed "s/total/$pkg/"; done
2009-10-12 14:57:54
User: pykler
Functions: bash cut du echo read sed tail tr xargs
Tags: Debian wajig
4

Calculates the size on disk for each package installed on the filesystem (or removed but not purged). This is missing the

| sort -rn

which would put the biggest packges on top. That was purposely left out as the command is slightly on the slow side

Also you may need to run this as root as some files can only be checked by du if you can read them ;)

man bash | col -b
2009-09-10 00:45:27
User: suxer
Functions: bash col man
-11

Display man page in plain text

bash -x test.sh 2>&1 | tee out.test
2009-09-03 17:10:44
User: arcege
Functions: bash tee
3

Sends both stdout and stderr to the pipe which captures the data in the file 'out.test' and sends to stdout of tee (likely /dev/tty unless redirected). Works on Bourne, Korn and Bash shells.

bash-hotkey: <CTRL+x+e>
2009-08-25 09:08:54
User: alvinx
Functions: bash
17

* in bash-shell You can capture the current commandline to a text-editor:

* simply press: CTRL+x+e

* Your current commandline will pe put into Your default text-editor (export EDITOR=vim)

find . -name '*'.tiff -exec bash -c "mogrify -format jpg -quality 85 -resize 75% {} && rm {}" \;
2009-08-10 18:27:10
Functions: bash find
4

Simple command to convert a large number of images into jpeg-format. Will delete originals after conversion.

find . -name "*.gz" | xargs -n 1 -I {} bash -c "gunzip -c {} | sort | gzip -c --best > {}.new ; rm {} ; mv {}.new {}"
2009-08-05 14:16:15
User: kennethjor
Functions: bash find xargs
-2

I used this because I needed to sort the content of a bunch of gzipped log files. Replace sort with something else, or simply remove sort to just rezip everything

bash -x script.sh 2> log
bash -x SCRIPT
2009-03-13 00:09:07
User: jcalvert
Functions: bash
3

Display commands and their arguments as they are executed. Useful for debugging shell scripts.

bash -x ./post_to_commandlinefu.sh
^r in bash begins a reverse-search-history with command completion
2009-02-19 18:17:54
User: abcde
Functions: bash command
15

In the sample output, I pressed ctrl+r and typed the letters las. I can't imagine how much typing this has saved me.