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,620 results
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
81

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

mplayer -vo caca MovieName.avi
zcat a_big_file.gz | sed -ne "$(zcat a_big_file.gz | tr -d "[:print:]" | cat -n | grep -vP "^ *\d+\t$" | cut -f 1 | sed -e "s/\([0-9]\+\)/\1=;\1p;/" | xargs)" | tr -c "[:print:]\n" "?"
2009-02-24 02:57:37
User: DEinspanjer
Functions: sed tr zcat
1

Scans the file once to build a list of line numbers that contain non-printable characters

Scans the file again, passing those line numbers to sed as two commands to print the line number and the line itself. Also passes the output through a tr to replace the characters with a ?

find / -xdev -print | cpio -pdmuv /mnt/mydisk
2009-02-24 02:03:43
User: afink
Functions: cpio find
2

copies all files from the source disk / (skipping boundaries of mouted -in volumes) to /mnt/mydisk. Logical links are being preserved as well as devices, pipes etc. This can copy a MacOS X or Linux volume and keep it bootable. Note: its not suited to copy files with MacOS 9 style resources.

find . -type d | perl -nle 'print s,/,/,g," $_"' | sort -n | tail
find . -type f -name "*.java" -print0 | xargs -0 -n 1 svn blame | sed -n 's/^[^a-z]*\([a-z]*\).*$/\1/p' | sort | uniq -c | sort -n
sudo route add xxx.xxx.xxx.xxx gw 127.0.0.1 lo
2009-02-23 19:58:09
Functions: route sudo
Tags: Security
2

Someone might attack on your system. You can drop attacker IP using IPtables. However, you can use route command to null route unwanted traffic. A null route (also called as blackhole route) is a network route or kernel routing table entry that goes nowhere. Matching packets are dropped (ignored) rather than forwarded, acting as a kind of very limited firewall. The act of using null routes is often called blackhole filtering.