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,926 results
lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\'
2009-02-25 17:16:56
User: leftyfb
Tags: bash
24

I save this to bin/iptrace and run "iptrace ipaddress" to get the Country, City and State of an ip address using the http://ipadress.com service.

I add the following to my script to get a tinyurl of the map as well:

URL=`lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep details|awk '{print $2}'`

lynx -dump http://tinyurl.com/create.php?url=$URL|grep tinyurl|grep "19. http"|awk '{print $2}'

:r !command
2009-02-25 15:49:08
User: sud0er
Tags: vim
13

This will append the output of "command" to whatever file you're currently editing in vim. Who else has good vim tricks? :)

split -b 19m file Nameforpart
2009-02-25 15:24:06
User: vranx
Functions: file split
10

Split File in 19 MB big parts, putting parts together again via

cat Nameforpartaa Nameforpartab Nameforpartac >> File

command !$
tail -1000 /some/file | vim -
2009-02-25 11:43:27
User: root
Functions: tail vim
17

The hyphen tells vim to open from STDOUT - saves having to create temporary files.

svn st | grep ^? | xargs svn add 2> /dev/null
ps -ef | grep [t]clsh
on="off"; off="on"; now=$(amixer get Master | tr -d '[]' | grep "Playback.*%" |head -n1 |awk '{print $7}'); amixer sset Master ${!now}
tr -dc "a-zA-Z0-9-_\$\?" < /dev/urandom | head -c 10 | gpg -e -r medha@nerdish.de > password.gpg
2009-02-25 08:48:26
User: hans
Functions: gpg head tr
2

Adjust the

head -c

part for password length.

I use filenames like "hans@commandlinefu.com.gpg" and a vim which automatically decrypts files with .gpg suffixes.

for x in `find . -name '*.html'` ; do iconv -f ISO-8859-1 -t UTF-8 $x > "$x.utf8"; rm $x; mv "$x.utf8" $x; done
2009-02-25 08:16:01
Functions: iconv mv rm
7

This is my first attempt at converting all HTML files to UTF-8 file encoding, including all subfolders.

Theres probably a much more compact way to do it, but I'm quite proud of it with my windows background ;)

echo COMMAND | xargs -ixxx ps -C xxx -o pid= | xargs -ixxx ls -l /proc/xxx/cwd
function miso () { mkdir ~/ISO_CD && sudo mount -o loop "$@" ~/ISO_CD && cd ~/ISO_CD && ls; } function uiso () { cd ~ && sudo umount ~/ISO_CD && rm -r ~/ISO_CD; }
2009-02-25 03:41:35
User: vududevil
Functions: cd mkdir mount rm sudo umount
4

Add the functions to the .bashrc to make it work

Example: First go to the iso file directory and type:

----------------------------------------------------------------------------------------------------

user@box:~$ miso file.iso

----------------------------------------------------------------------------------------------------

It will put you into a temporary mounting point directory (ISO_CD) and will show the files

You can umount the iso file whatever the directory you are

----------------------------------------------------------------------------------------------------

user@box:~/ISO_CD$ uiso

----------------------------------------------------------------------------------------------------

It wil umount the iso file and remove the temporary directory in your home

cat /dev/tty > FILE
2009-02-25 01:43:47
User: Jo
Functions: cat
1

Takes input from the connected terminal and dumps it to the specified file. Stop writing and close file with control + D or the end of line character. Useful for copying+pasting large blobs of text over SSH to a new machine.

nc -v -n -z -w 1 127.0.0.1 22-1000
sudo !!
2009-02-25 00:52:28
User: silbermm
Functions: sudo
-13

I often forget to type sudo before a command that needs it. This is the quickest way to rerun the command prefixed by sudo.

echo "!!" > foo.sh
2009-02-25 00:37:25
User: dnavarre
Functions: echo
82

Sometimes commands are long, but useful, so it's helpful to be able to make them permanent without having to retype them. An alternative could use the history command, and a cut/sed line that works on your platform.

history -1 | cut -c 7- > foo.sh
declare -i aa ; aa=3*8 ; echo $aa
mdfind "tag:data" > /tmp/data.txt ; zip -r9@ ~/Desktop/data.zip < /tmp/data.txt
2009-02-24 14:49:50
User: vranx
Tags: Os X
-1

finding all files with the metadata tag "data" using the metadata find command mdfind , writing the list to a temporary file, reading the input for the .zip file from this temporary file.

find . -name '*.txt' -exec sed -ir 's/this/that/g' {} \;
sips -s format jpeg Bild.pdf --out Bild.jpg
2009-02-24 13:41:12
User: vranx
Tags: Os X
1

Converts a .pdf to .jpg . should work with jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga

man -t UNIX_COMMAND | open -f -a preview
du --max-depth=1 | sort -r -n | awk '{split("k m g",v); s=1; while($1>1024){$1/=1024; s++} print int($1)" "v[s]"\t"$2}'
2009-02-24 11:03:08
User: hans
Functions: awk du sort
16

I use this on debian testing, works like the other sorted du variants, but i like small numbers and suffixes :)

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6
2009-02-24 09:43:40
User: Blackbit
Functions: head tr
11

If you want a password length longer than 6, changing the -c6 to read -c8 will give you 8 random characters instead of 6. To end up with a line-feed, use this with echo:

# echo `< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6`

TMPROOT=/tmp; TMPDIR=$(mktemp -d $TMPROOT/somedir.XXXXXX); TMPFILE=$(mktemp $TMPROOT/somefile.XXXXXX); trap "rm -rf $TMPDIR $TMPFILE; exit" INT TERM EXIT; some treatment using $TMPDIR and $TMPFILE; exit 0
2009-02-24 09:35:22
User: raphink
Functions: exit mktemp trap
2

Cleanly create tempfiles using mktemp and remove them using traps instead of removing them in the end of the script. This way, you make sure the tempfiles are removed properly even if the script is killed or interrupted.

For a user script in KDE4, you can set TMPROOT using :

TMPROOT=$(kde4-config --path tmp)
RETURN~.
2009-02-24 08:25:00
User: n30
17

A key sequence for terminating a frozen session. Full sequence on a swedish keyboard: [ENTER] [ALTGR] tilde [SPACE] dot