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,857 results
cd <YOUR_DIRECTORY>; for i in `ls ./`; do tar czvf "$i".tar.gz "$i" ; done
2009-06-11 18:33:27
User: ElAlecs
Functions: cd tar
-3

Very simple and useful, you need to change the word "directory" for your directory

ldapsearch -H ldap://localhost:389 -D cn=username,ou=users,dc=domain -x -W -b ou=groups,dc=domain '(member=cn=username,ou=users,dc=domain)' | grep ^dn | sed "s/dn\: cn=\([^,]*\),ou=\([^,]*\),.*/\2 \1/"
2009-06-11 14:50:11
User: nitehawk
Functions: grep sed
-2

This expression looks for groups inside of a GroupOfNames class element, that is itself inside one (or many) Organizational Unit (ou) nodes in the ldap tree. Give you a quick dump of all the groups the user belongs to. Handy for displaying on a webpage.

ldapsearch -LLL -H ldap://activedirectory.example.com:389 -b 'dc=example,dc=com' -D 'DOMAIN\Joe.Bloggs' -w 'p@ssw0rd' '(sAMAccountName=joe.bloggs)'
2009-06-11 13:07:11
User: greppo
Tags: ldap
9

These are the parameters to ldapsearch (from ldap-utils in Ubuntu), for searching for the record for Joe Blogg's user. sAMAccountName is the LDAP field that ActiveDirectory uses to store the user name. 'DOMAIN\Joe.Bloggs' where "DOMAIN" is the the active directory domain.

Othewise you could use "CN=Joe.Bloggs,DC=example,DC=com" instead of "DOMAIN\Joe.Bloggs"

perl -e 'map { $on=$_; s/\]/_/; rename($on, $_) or warn $!; } <*>;'
mecp () { scp "$@" ${SSH_CLIENT%% *}:Desktop/; }
2009-06-11 00:43:08
User: braino
Functions: scp
Tags: scp
9

Place in .bashrc and invoke like this: "mecp /path/to/file", and it will copy the specified file(s) back to the desktop of the host you're ssh'ing in from. To easily upload a file from the host you're ssh'ing in from use this:

ucp (){ scp ${SSH_CLIENT%% *}:Desktop/upload/* .; }

alias findstring="find . -type f -print | xargs grep $1"
$ i=(*);echo ${i[RANDOM%(${#i[@]}+1)]]}
2009-06-10 18:47:03
User: sputnick
2

An other way to run it ( playing a random file ending with avi, flv or mpeg ) from a specified dir and a specified type of extension :

making MOVIE array with a glob :

MOVIE=( /PATH/TO/MY/FAVORITE/MOVIES/*.{avi,flv,mpeg} )

playing the random file from a random key from the array

mplayer ${MOVIE[ RANDOM % ( ${#i[@]} + 1 ) ]]}

I use only globs and a bash array.

I use GNU bash, version 3.2.48

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