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,925 results
FILENAME=${FILE##*/};FILEPATH=${FILE%/*};NOEXT=${FILENAME%\.*};EXT=${FILE##*.}
open .
tar czv Pictures | split -d -a 3 -b 16M - pics.tar.gz.
2009-06-09 19:48:01
User: asmoore82
Functions: split tar
11

Leave it to a proprietary software vendor to turn a cheap and easy parlor trick into a selling point. "Hey guys, why don't we turn our _collection of multiple files_ into a *collection of multiple files*!!" Extract the ^above with this:

cat pics.tar.gz.??? | tar xzv

^extract on any Unix - no need to install junkware!

(If you must make proprietary software, at least make it do something *new*)

if [ -e windows ]; then use 7-Zip

ffmpeg -i [source].avi -f mp4 -vcodec mpeg4 -b 250000 -s 480?320 -acodec aac -ar 24000 -ab 64 -ac 2 [destination].mp4
stty cbreak -echo; KEY=$(dd bs=1 count=1 2>/dev/null); stty -cbreak echo
2009-06-09 13:15:49
User: inof
Functions: dd stty
5

This shell snippet reads a single keypress from stdin and stores it in the $KEY variable.

You do NOT have to press the enter key!

The key is NOT echoed to stdout!

This is useful for implementing simple text menus in scripts and similar things.

x2vnc {-west|-east|-north|-south} computer-ip:display-number
2009-06-09 12:09:43
User: kellsens
0

This command use vnc to connect to another computer and emulate an dual-screen enviroment.

Example of use:

x2vnc -west 192.168.0.100:0

After this, if vnc server needs an password, will be prompt in the shell.

p.s.: sorry my english

watch -d -n 3 "iw dev wlan0 station dump; iwconfig wlan0"
$command 3>&1 1>&2 2>&3
2009-06-09 08:48:08
User: svg
6

Possible use, to filter something in stderr:

(/usr/bin/$COMMAND $PARAM 3>&1 1>&2 2>&3 | grep -v $uninteresting_error ) 3>&1 1>&2 2>&3

ncdu directory_name
2009-06-09 00:02:48
User: bwoodacre
4

ncdu is a text-mode ncurses-based disk usage analyzer. Useful for when you want to see where all your space is going. For a single flat directory it isn't more elaborate than an du|sort or some such thing, but this analyzes all directories below the one you specify so space consumed by files inside subdirectories is taken into account. This way you get the full picture. Features: file deletion, file size or size on disk and refresh as contents change. Homepage: http://dev.yorhel.nl/ncdu

echo "$(( 0x10 )) - $(( 010 )) = $(( 0x10 - 010 ))"
2009-06-08 19:35:53
User: asmoore82
Functions: echo
10

^Hexadecimal Ten minus Octal Ten is Eight(in Decimal).

echo "$(( 0xaf )) = $(( 0257 ))"

^Hexadecimal AF and Octal 257 are both Decimal 175.

bash -x script.sh 2> log
diff -urp /originaldirectory /modifieddirectory
find . -iname '*.jar' | xargs du -ks | cut -f1 | xargs echo | sed "s/ /+/g" | bc
w3m -dump_head www.fiat.com | awk '/Date+/{print $6, $7}'
2009-06-08 00:06:01
User: m33600
Functions: awk
-1

w3m is a commanline web browser, full of options, I used -dump_head for less unnecessary page download.

With awk, I can retrieve dynamic changes in webpages in this very econnomical fashion

wget $URL | htmldoc --webpage -f "$URL".pdf - ; xpdf "$URL".pdf &
watch '/home/mm/bash/keypress.sh |/home/mm/bash/evento.sh'
2009-06-07 20:57:49
User: m33600
Functions: watch
-7

evento.sh needed for awk syntax

the aplay command makes a camera sound. It takes a picture of who looked at the display

#!/bin/bash

# evento.sh: deteta evento e fala

awk '{print}' | espeak -v pt -stdin

awk '/e/{print "emergencia"}' | espeak -v pt -stdin

aplay -q /home/mm/bash/camera.wav # -q inibe verbose do comand aplay

exit 0

awk '{print}' | espeak -v pt -stdin
2009-06-07 20:45:44
User: m33600
Functions: awk
0

Speaks whatever comes in via stdin (-v pt = portuguese, default = englisg)

..speech part of keyboard event talker. Made to accomplish a simple alarm central based on a cheap keyboard circuit. This way I have one zone per direct keypress. Depends on keypress.sh. It speaks out loud wich zone (key) has been pressed ( faulted ). Here is keypress.sh :

#!/bin/bash

# keypress.sh: Detect a user keypress ("hot keys").

echo

old_tty_settings=$(stty -g) # Save old settings (why?).

stty -icanon

Keypress=$(head -c1) # or $(dd bs=1 count=1 2> /dev/null)

# on non-GNU systems

aplay -q /home/mm/bash/beep-1.wav

echo

echo "Chamada quarto \""$Keypress"\"."

echo

stty "$old_tty_settings" # Restore old settings.

# Thanks, Stephane Chazelas.

exit 0

( nw=192.168.0 ; h=1; while [ $h -lt 255 ] ; do ( ping -c2 -i 0.2 -W 0.5 -n $nw.$h & ); h=$[ $h + 1 ] ; done ) | awk '/^64 bytes.*/ { gsub( ":","" ); print $4 }' | sort -u
2009-06-07 15:14:46
Functions: awk ping sort
3

What do you do when nmap is not available and you want to see the hosts responding to an icmp echo request ? This one-liner will print all hosts responding with their ipv4 address.

ls -la | sort -k 5bn
2009-06-07 14:35:17
Functions: ls sort
6

Sort ls output of all files in current directory in ascending order

Just the 20 biggest ones:

ls -la | sort -k 5bn | tail -n 20

A variant for the current directory tree with subdirectories and pretty columns is:

find . -type f -print0 | xargs -0 ls -la | sort -k 5bn | column -t

And finding the subdirectories consuming the most space with displayed block size 1k:

du -sk ./* | sort -k 1bn | column -t
dd if=/dev/zero of=/dev/sdb bs=446 count=1
2009-06-07 10:29:49
User: dcabanis
Functions: dd
Tags: dd grub boot usb
0

If you don't want your computer to try to boot form a USB stick that used to be used as a boot device (maybe for a live linux distro), you will have to remove the boot loader from your stick other wise the boot will fail each time the device is attached to your PC.

for f in *.jpg; do exif --ifd=0 --tag=0x0110 --set-value="LOMO LC-A" --output=$f $f; exif --ifd=0 --tag=0x010f --set-value="LOMO" --output=$f $f; done }
2009-06-07 09:05:14
User: beanie
6

this takes every jpg in the current directory and changes the exif data for manufactur and model. in my case i change it to LOMO LC-A because my scanner puts his data in there :]

find . -type d -exec chmod 755 {} \;
2009-06-07 08:17:06
User: marssi
Functions: chmod find
Tags: find chmod
1

"find . -type d -print0 | xargs -0 chmod 755"

thanks masterofdisaster

find . -type f -print0|xargs -0 md5sum|sort|perl -ne 'chomp;$ph=$h;($h,$f)=split(/\s+/,$_,2);print "$f"."\x00" if ($h eq $ph)'|xargs -0 rm -v --
2009-06-07 03:14:06
Functions: find perl rm xargs
19

This one-liner will the *delete* without any further confirmation all 100% duplicates but one based on their md5 hash in the current directory tree (i.e including files in its subdirectories).

Good for cleaning up collections of mp3 files or pictures of your dog|cat|kids|wife being present in gazillion incarnations on hd.

md5sum can be substituted with sha1sum without problems.

The actual filename is not taken into account-just the hash is used.

Whatever sort thinks is the first filename is kept.

It is assumed that the filename does not contain 0x00.

As per the good suggestion in the first comment, this one does a hard link instead:

find . -xdev -type f -print0 | xargs -0 md5sum | sort | perl -ne 'chomp; $ph=$h; ($h,$f)=split(/\s+/,$_,2); if ($h ne $ph) { $k = $f; } else { unlink($f); link($k, $f); }'
find -L /path/to/check -type l -delete
2009-06-06 16:07:04
Functions: find
14

If you don't want to delete them, but just want to list them, do

find -L /path -type l

If you want to delete them with confirmation first, do

find -L /path -type l -exec rm -i {} +

Using the -L flag follows symlinks, so the -type l test only returns true if the link can't be followed, or is a symlink to another broken symlink.