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 - 8,690 results
tasksel list-tasks
2012-02-03 16:13:17
User: 0disse0
Tags: Ubuntu install
0

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).

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%"
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).

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 -i <file> -re '<start>,<end>d'
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

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.

exec -a "/sbin/getty 38400 tty7" your_cmd -erase_all_files
2012-02-01 10:54:03
User: mhs
Functions: exec
4
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`)

diff -y <(ssh user@host find /boot|sort) <(find /boot|sort)
2012-01-31 15:04:30
User: knoppix5
Functions: diff find ssh
2

You can compare directories on two different remote hosts as well:

diff -y <(ssh user1@host1 find /boot|sort) <(ssh user2@host2 find /boot|sort)

To avoid password-prompt on remote host just generate the rsa key locally and copy it to remote host:

ssh-keygen -t rsa

then

ssh you@server1 "mkdir .ssh"

then

scp .ssh/id_rsa.pub you@server1:; .ssh/authorized_keys2
VBoxManage closemedium dvd "/sicuramente/mipaghi/tutto.iso
VBoxManage storageattach "volpedimongibello" --storagectl "fighetto" --port 1 --device 0 --type dvddrive --medium "/tanto/mipaghi/tutto.iso
2012-01-31 14:42:54
User: 0disse0
0

was inspired by http://www.commandlinefu.com/commands/view/8936/boot-from-a-block-device-without-giving-root-privilege-to-virtual-box

volpedimongibello= virtual machine name

fighetto= controller name

tutto.iso= DVD iso image

How to remove the DVD:

VBoxManage storageattach "volpedimongibello" --storagectl "fighetto" --port 1 --device 0 --type dvddrive --medium none

diff <(exiftool img_1.jpg) <(exiftool img_2.jpg)
2012-01-30 22:01:17
User: fpunktk
Functions: diff
5

I've been looking for this for a long time. Does anybody know how to do this in dash (POSIX shell)?

An alternative version might be:

exiftool img_1.jpg | diff - <(exiftool img_2.jpg)
svn diff <FILE>
2012-01-30 16:47:48
User: bbbco
Functions: diff
Tags: svn diff
0

If you have ever edited a locally checked out version of a file to tweak it for testing purposes, and came back to it over a weekend, you might have forgotten what you exactly changed. This command helps you see the differences between the the checked in SVN version, and the one you tweaked.

sed 's!<[Aa] *href*=*"\([^"]*\)"*>\([^<>]*\)</[Aa]>!\1,\2!g' links.html
PID=`ps | grep process_name | grep -v grep | head -n 1 | awk '{print $1}'`; cat /proc/$PID/smaps | grep heap -A 2
pv -petrs $(stat -c %s file.iso) file.iso | dd bs=1M oflag=sync of=/dev/sdX
time (pv file.iso | dd bs=1M oflag=sync of=/dev/sdX 2>/dev/null)
perl -i -ne 'print if $. == 3..5' <filename>
vi +'<start>,<end>d' +wq <filename>
dd if=<device> | pv | nc <target> <port>
2012-01-27 18:37:36
Functions: dd
Tags: dd nc pv 7z
5

Create an image of "device" and send it to another machine through the network ("target" and "port" sets the ip and port the stream will be sent to), outputting a progress bar

On the machine that will receive, compress and store the file, use:

nc -l -p <port> | 7z a <filename> -si -m0=lzma2 -mx=9 -ms=on

Optionally, add the -v4g switch at the end of the line in order to split the file every 4 gigabytes (or set another size: accepted suffixes are k, m and g).

The file will be compressed using 7z format, lzma2 algorithm, with maximum compression level and solid file activated.

The compression stage will be executed on the machine which will store the image. It was planned this way because the processor on that machine was faster, and being on a gigabit network, transfering the uncompressed image wasn't much of a problem.

zc () { for exp in $argv; do print "$exp = $(( exp ))"; done; }
vi +{<end>..<start>}d +wq <filename>
2012-01-26 20:36:04
User: javidjamae
Functions: vi
Tags: bash vi
0

Deletes lines to of a file. You must put the end line first in the range for the curly brace expansion, otherwise it will not work properly.

showkey -a
vi +<lineNumber>d +wq <filename>
sed -e '/^#/d' -e 's/#.*$//' in
2012-01-25 15:05:57
User: agambier
Functions: sed
6

Use sed to remove comments from a file.

In this example the comments begin with #.

The command '/^#/d' remove line starting with #.

The command 's/#.*$//' remove comments at end of lines.