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

All commands from sorted by
Terminal - All commands - 31 results
exec -a "/sbin/getty 38400 tty7" your_cmd -erase_all_files
2012-02-01 10:54:03
User: mhs
Functions: exec
11
exec -a $NAME $COMMAND $ARGS

`your_cmd -erase_all_files` is the real process, but harmless-looking getty appears in the process table.

Never actually had a need to do this, but interesting nonetheless... Tested in bash, dash.

-a $NAME

"pass NAME as the zeroth argument to COMMAND", i.e. customise the name of the process (as commonly seen with `ps`)

sed -i <file> -re '<start>,<end>d'
mkdir rotated; for v in *.3gp; do ffmpeg -i $v -vf transpose=2 -vcodec ffv1 rotated/${v/3gp/avi} ; done
2012-02-04 18:20:04
User: keturn
Functions: mkdir
2

Takes all the .3gp files in the directory, rotates them by 90 degrees, and saves them in the lossless ffv1 encoding.

If this rotates in the wrong direction, you may want transponse=1

Re-encoding to ffv1 may result in a significant increase in file size, as it is a lossless format. Other applications may not recognize ffv1 if they don't use ffmpeg code. "huffyuv" might be another option for lossless saving of your transformations.

The audio may be re-encoded as well, if the encoding used by your 3gp file doesn't work in a avi container.

sudo hal-get-property --udi /org/freedesktop/Hal/devices/computer --key 'system.hardware.product'
ls -l | gawk -v FIELDWIDTHS='1 3 3 3' '{print $2}'
mysqldump --opt --where="true LIMIT 5000" dbinproduzione > miodbditest.sql
2012-02-02 11:50:35
User: 0disse0
Tags: mysqldump
1

How to extract data from one table:

mysqldump --opt --where="true LIMIT 5000" dbinproduzione tabella > miodbditest_tabella.sql

tasksel list-tasks
2012-02-03 16:13:17
User: 0disse0
Tags: Ubuntu install
1

The command tasksel allows the choice of packages from the command line to get predefined configurations for specific services (usually this option is offered during installation).

find /protocollo/paflow -type f -mtime +5 | xargs tar -cvf /var/dump-protocollo/`date '+%d%m%Y'_archive.tar`
2012-02-03 16:24:08
User: 0disse0
Functions: find tar xargs
1

Finally, we can make the file "unchangeable"

sudo chattr +i

10,30,50 * * * * ping -c1 -w3 192.168.0.14 >/dev/null
2012-02-06 14:54:15
User: bashrc
Functions: ping
1

Cleaner with a mailto assignment in crontab (if the command fails you get an email):

MAILTO=admin@example.com

10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 >/dev/null

sed -re '/^#/d ; s/#.*$//'
2012-02-01 20:39:23
User: Zulu
Functions: sed
Tags: sed
0

Delete all comments (#) on text :

It deletes the entire comment line and remove comments form end of others.

sed -re '/^#/d ; s/#.*$// ; /^\s*$/d'
2012-02-02 18:12:52
User: Zulu
Functions: sed
Tags: sed
0

Use it with cat and '|' for know what is used in a conf file.

For example cat /etc/squid/squid.conf | sed -re '/^#/d ; s/#.*$// ; /^\s$/d' :

Show you what you use in your file conf.

It removes all comments and empty lines.

Empty lines are lines with nothing, a tab, or a space.

sed -r 's/(\[|])//g' | awk ' { $1=strftime("%D %T",$1); print }'
2012-02-03 13:07:37
User: Zulu
Functions: awk sed
Tags: sed awk timestamp
0

It remove the square bracket and convert UNIX time to human readable time for all line of a stream (or file).

TOTAL_RAM=`free | head -n 2 | tail -n 1 | awk '{ print $2 }'`; PROC_RSS=`ps axo rss,comm | grep [h]ttpd | awk '{ TOTAL += $1 } END { print TOTAL }'`; PROC_PCT=`echo "scale=4; ( $PROC_RSS/$TOTAL_RAM ) * 100" | bc`; echo "RAM Used by HTTP: $PROC_PCT%"
svn diff --diff-cmd='meld' -r 100:BASE FILE
sudo svn co svn+ ciccio_diverso://root@192.160.150.151/svn-repo/progettino
2012-02-03 18:16:02
User: 0disse0
Functions: sudo
Tags: subversion
0

first need to Edit the configuration file

/home/cicciobomba/.subversion

and under the [tunnels] add this line

ciccio_diverso = /usr/bin/ssh -p 12345

DISPLAY=":0.0" import -window root screenshot.png
2012-02-05 11:52:33
User: 0disse0
Tags: display
0

But if you use a screen saver, you must first disable your screen saver

kill $(ps ax | grep screensaver | grep -v grep | awk '{ print $1 }')

pv -i 1 -p -t -e /path/to/sql/dump | mysql -u USERNAME -p DATABASE_NAME
2012-02-06 08:16:55
User: wr8cr8
0

Print out the progress of MySQL import using the pv command. Updates every second.

netsh firewall add programmaautorizzato C:\nltest.exe mltest enable
2012-02-06 19:33:57
User: 0disse0
0

To allow a program ("programmaautorizzato" in example) to listen through the firewall

useradd -m -p $(perl -e'print crypt("passwordscelta", "stigghiola")') user
2012-02-06 19:53:01
User: 0disse0
Functions: perl useradd
0

The crypt function takes a password, key, as a string, and a salt character array which is described below, and returns a printable ASCII string which starts with another salt. It is believed that, given the output of the function, the best way to find a key that will produce that output is to guess values of key until the original value of key is found.

from http://en.wikipedia.org/wiki/Crypt_(Unix)

tail -f LogFile | while read line; do echo $(date '+%F %T:%N'|sed 's/......$//') $line ; done
2012-02-07 14:01:09
User: zlemini
Functions: date echo read sed tail
0

Works by trimming digits from the GNU version of Date's nano-second output.

From Date man page:

( %N nanoseconds (000000000..999999999)

python -c "DEV = '/dev/input/event4' fo = open(DEV) def inn(k,s): if s == 0: print '%i up'%k if s == 1: print '%i down'%k while 1: l = fo.read(16) if ord(l[10]) != 0: k,s = l[10],l[12] inn(ord(k),ord(s)) "
2012-02-08 12:11:23
User: totti
Functions: python
0
python -c "DEV = '/dev/input/event4'

#if event0 doesn't work, try event1 event2 etc

fo = open(DEV)

def interpret(keycode,state):

if state == 0:

print '%i up'%keycode

if state == 1:

print '%i down'%keycode

if state == 2:

print '%i repeat'%keycode

while 1:

line = fo.read(16)

if ord(line[10]) != 0:

keycode,state = line[10],line[12]

interpret(ord(keycode),ord(state))

"

tag() { local t="$HOME/tags/$1"; [ -d $t ] || mkdir -p $t; shift; ln -s $* $t;}
2012-02-08 12:40:45
User: bartonski
Functions: ln mkdir
0

The tag function takes a tag name as its first argument, then a list of files which take that tag. The directory $HOME/tags/tagname will then hold symbolic links to each of the tagged files. This function was inspired by tmsu (found at https://bitbucket.org/oniony/tmsu/wiki/Home).

Example:

tag dog airedale.txt .shizturc weimeraner.pl

This will create $HOME/tags/dog which contains symbolic links to airedale.txt .shizturc and weimeraner.pl

VBoxManage modifyvm "vm-name" --vrdp on --vrdpport 3389 --vrdpauthtype external
2012-02-03 18:36:04
User: 0disse0
-1

--vrdp on enables VirtualBox RDP server for the VM

--vrdpport 3389 ndicates the TCP port that the server will accept RDP connections direct to the VM (for each VM is assigned a different port)

--vrdpauthtype external RDP console gives access to the VM Host physical users via authentication

In fact, in the 3.1.x version of VirtualBox, the external value for the parameter --vrdpauthtype allows access via RDP only to the user who started the VM.

The workaround is to add the user that runs the VM to shadow group, using the command

sudo hal-get-property --udi /org/freedesktop/Hal/devices/computer --key 'system.formfactor'
sudo dmidecode | grep Product