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,677 results
perl -i -ne 'print if $. == 3..5'
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
1

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
3

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.

sed '${/^$/d}' file
2012-01-25 14:07:55
User: moogmusic
Functions: sed
3

Use sed to remove the last line of a file only if it is empty.

awk -F":" '!list[$3]++{print $3}' /etc/passwd
pscp -h hosts.txt -l username /etc/hosts /tmp/hosts
2012-01-24 12:19:30
User: zlemini
8

You can push files to up to 32 servers at once assuming ssh keys are in place.

Great tool, it is part of the pssh suite.

dd if=/dev/urandom of=/dev/somedisk
2012-01-24 09:13:15
User: joedhon
Functions: dd
-3

Same game as #10096 . loop as many times as you like.

shred --iterations=N /dev/sdaX
2012-01-23 20:40:36
User: bibe
Functions: shred
0

Instead of zeroing the filesystem, this command overwrites N times (default is 3) the disk content, making data recovery much harder.

The command accepts many more options

RX1=`cat /sys/class/net/${INTERFACE}/statistics/rx_bytes` (see script below)
2012-01-23 18:22:44
User: jp
Functions: script
0

Need output in mbps (bits)

# ./bytes-second.sh eth0

eth0 interface maximum Speed: 1000Mb/s

RX:12883212 TX:17402002 B/s | RX:98 TX:132 Mb/s

RX:12371647 TX:17830111 B/s | RX:94 TX:136 Mb/s

RX:12502750 TX:17860915 B/s | RX:95 TX:136 Mb/s

dd if=/dev/zero of=/dev/hda
genRandomText() { perl -e '$n=shift; print chr(int(rand(26)) + 97) for 1..$n; print "\n"' $1;}
2012-01-21 00:21:20
User: putnamhill
Functions: perl
-1

If you don't have seq, you can use perl.

fdupes -r -1 Neu | while read line; do j="0"; buf=""; for file in ${line[*]}; do if [ "$j" == "0" ]; then j="1"; buf=$file; else ln -f $buf $file; fi; done; done
netstat -a --numeric-ports | grep 8321
2012-01-20 22:00:56
User: peter4512
Functions: grep netstat
Tags: net tcp
0

if you don't do --numeric-ports, netstat will try to resolve them to names

tail -f to.log | colorize.pl +l10:".*" &
2012-01-20 21:46:47
User: peter4512
Functions: tail
Tags: perl
-3

(follow with next command)

tail -f from.log | colorize.pl +l20:".*" &

Use with http://www.commandlinefu.com/commands/view/10031/intercept-monitor-and-manipulate-a-tcp-connection. - can use to view output of tees that send traffic to files - output will be interwoven with red for sent traffic and green for received.

genRandomText() { a=( a b c d e f g h i j k l m n o p q r s t u v w x y z );f=0;for i in $(seq 1 $(($1-1))); do r=$(($RANDOM%26)); if [ "$f" -eq 1 -a $(($r%$i)) -eq 0 ]; then echo -n " ";f=0;continue; else f=1;fi;echo -n ${a[$r]};done;echo"";}
2012-01-20 21:18:16
User: bbbco
Functions: echo seq
0

Ever need to get some text that is a specific number of characters long? Use this function to easily generate it! Doesn't look pretty, but sure does work for testing purposes!

for HOSTTOREMOVE in $(dig +short host.domain.tld); do ssh-keygen -qR $HOSTTOREMOVE; done
ssh-keygen -R $(dig +short host.domain.tld)
2012-01-19 15:08:50
User: atoponce
Functions: dig ssh ssh-keygen
1

Quick shortcut if you know the hostname and want to save yourself one step for looking up the IP address separately.

xdotool getmouselocation
wmctrl -o 100,0
2012-01-19 14:36:20
User: totti
Tags: switc
0

For a demo try

wmctrl -o 100,0; sleep 3; wmctrl -o 1500,0; sleep 3; wmctrl -o 3000,0; sleep 3; wmctrl -o 4500,0;

Actually

wmctrl -o <width x Number>,0;

to switch to that workspace

kde-open -v | grep Platform | cut -d' ' -f4-
2012-01-19 13:05:26
User: funollet
Functions: cut grep
-1

Sample input:

kde-open -v

Qt: 4.7.4

KDE Development Platform: 4.7.3 (4.7.3)

KIO Client: 2.0