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

Commands tagged Linux from sorted by
Terminal - Commands tagged Linux - 224 results
wget --server-response --spider http://www.example.com/
2009-03-31 18:49:14
User: penpen
Functions: wget
5

Let me suggest using wget for obtaining the HTTP header only as the last resort because it generates considerable textual overhead. The first ellipsis of the sample output stands for

Spider mode enabled. Check if remote file exists.

--2009-03-31 20:42:46-- http://www.example.com/

Resolving www.example.com... 208.77.188.166

Connecting to www.example.com|208.77.188.166|:80... connected.

HTTP request sent, awaiting response...

and the second one looks for

Length: 438 [text/html]

Remote file exists and could contain further links,

but recursion is disabled -- not retrieving.

lynx -dump -head http://www.example.com/
2009-03-31 18:41:36
User: penpen
-1

Without the -dump option the header is displayed in lynx. You can also use w3m, the command then is

w3m -dump_head http://www.example.com/
rev <<< 'lorem ipsum' | tee /dev/stderr | rev
2009-03-31 13:12:09
User: penpen
Functions: rev tee
Tags: Linux unix
2

In the above example 'muspi merol' (the output of the first rev command) is sent to stderr and 'lorem ipsum' (the output of the second rev command) is sent to stdout. rev reverse lines of a file or files. This use of tee allows testing if a program correctly handles its input without using files that hold the data.

alias rot13="tr '[A-Za-z]' '[N-ZA-Mn-za-m]'"
2009-03-30 19:08:49
User: penpen
Functions: alias
Tags: Linux unix
8

rot13 maps a..mn..z (A..MN..Z) to n..za..m (n..za..m) and so does this alias.

watch -tn1 'bc<<<"`date -d'\''friday 21:00'\'' +%s`-`date +%s`"|perl -ne'\''@p=gmtime($_);printf("%dd %02d:%02d:%02d\n",@p[7,2,1,0]);'\'
2009-03-29 19:53:36
User: penpen
Functions: perl watch
Tags: Linux unix date
-2

An improved version of http://www.commandlinefu.com/commands/view/1772/simple-countdown-from-a-given-date that uses Perl to pretty-print the output. Note that the GNU-style '--no-title' option has been replaced by its one-letter counterpart '-t'.

iptables -F && iptables -X && iptables -P INPUT ACCEPT && iptables -OUTPUT ACCEPT
2009-03-27 15:03:54
User: lme
Functions: iptables
Tags: Linux iptables
0

This turns your iptables packet filter to a "Allow any from any to any" filter, so you can rule out any filtering issues when you have a problem to enable a connection from or to your host.

To re-enable it, run /etc/init.d/iptables restart

while true; do X=$Y; sleep 1; Y=$(ifconfig eth0|grep RX\ bytes|awk '{ print $2 }'|cut -d : -f 2); echo "$(( Y-X )) bps"; done
scrot -e 'mv $f \$HOME/shots/; sitecopy -u shots; echo "\$BASE/$f" | xsel -i; feh `xsel -o`'
2009-03-26 12:08:39
User: penpen
Functions: echo
-1

Here $HOME/shots must exist and have appropriate access rights and sitecopy must be correctly set up to upload new screen shots to the remote site.

Example .sitecopyrc (for illustration purposes only)

site shots

server ftp.example.com

username user

password antabakadesuka

local /home/penpen/shots

remote public_html/shots

permissions ignore

The command uses scrot to create a screen shot, moves it to the screen shot directory, uploads it using screen uses xsel to copy the URL to the paste buffer (so that you can paste it with a middle click) and finally uses feh to display a preview of the screen shot.

Note that $BASE stands for the base URL for the screen shots on the remote server, replace it by the actual location; in the example http://www.example.com/~user/shots would be fitting.

Assign this command to a key combination or an icon in whatever panel you use.

scrot -e 'mv $f \$HOME/public_html/shots/; echo "http://\$HOSTNAME/~\$USER/shots/$f" | xsel -i; feh `xsel -o`'
2009-03-26 11:32:09
User: penpen
Functions: echo
0

A web server using $HOME/public_html as user directory is required, $HOME/public_html/shots must exist and have appropriate access rights and $HOSTNAME must be known to and accessible from the outside world.

The command uses scrot to create a screen shot, moves it to the screen shot directory, uses xsel to copy the URL to the paste buffer (so that you can paste it with a middle click) and finally uses feh to display a preview of the screen shot.

Assign this command to a key combination or an icon in whatever panel you use.

cat /dev/cdrom > ~/mydisk.iso
2009-03-26 05:54:41
User: sata
Functions: cat
Tags: Linux
-10

Generate the iso from the disk, easily.

same as "dd if=/dev/cdrom of=~/mydisk.iso"

nc -l 8000
2009-03-25 23:09:38
User: penpen
Tags: Linux unix WWW
2

Have netcat listen on port 8000, point browser to http://localhost:8000/ and you see the information sent. netcat terminates as soon as your browser disconnects.

I tested this command on my Fedora box but linuxrawkstar pointed out that he needs to use

nc -l -p 8000

instead. This depends on the netcat version you use. The additional '-p' is required by GNU netcat that for example is used by Debian but not by the OpenBSD netcat port used by my Fedora system.

watch -n 1 :
2009-03-25 23:00:28
User: penpen
Functions: watch
Tags: Linux unix
-2

'watch' repeatedly (default every 2 seconds, -n 1 => every second) runs a command (here ':', a shorthand for 'true'), displays the output (here nothing) and the date and time of the last run.

I thought it to be obvious but it seemingly is not: to exit use Ctrl-C.

for i in $(pgrep -v -u root);do kill -9 $i;done
2009-03-24 02:54:52
User: lostnhell
Functions: kill
1

explanation:

grep -- displays process ids

-v -- negates the matching, displays all but what is specified in the other options

-u -- specifies the user to display, or in this case negate

The process loops through all PIDs that are found by pgrep, then orders a forced kill to the processes in numerical order, effectively killing the parent processes first including the shells in use which will force the users to logout.

Tested on Slackware Linux 12.2 and Slackware-current

echo 'wget url' | at 01:00
2009-03-21 06:19:40
User: Vlad003
Functions: at echo
16

Replace "url" with the correct address of what your're downloading. Replace 01:00 with what time you want. (24-hour clock).

alias pst='pstree -Alpha'
2009-03-20 10:53:37
User: Alexander
Functions: alias
Tags: Linux
0

By 'pst' you can print out process tree with all details about all processes (including a command line, PID, and the current process you are running in).

By 'pst username' you can get an information about processes belonging to the particular user 'username'.

sed ':a;N;$!ba;s/\n/ /g'
2009-03-17 20:54:04
User: cidiom
Functions: sed
Tags: Linux
0

Ever had a file with a list of numbers you wanted to add, use:

cat file | sed ':a;N;$!ba;s/\n/+/g' | bc
sort file1.txt | uniq > file2.txt
convert image123.png -colors 14 -resize 640x480 grubimg.xpm
2009-03-12 22:55:10
User: starchox
Tags: Linux grub boot
3

* size must be 640?480 pixels

* only has 14 colors

* save it in XPM format

Edit /boot/grub/menu.lst and add

splashimage=(hd0,0)/boot/grub/grubimg.xpm

make sure for your path name and hard disk

find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
2009-03-12 22:25:26
Functions: find sed
-1

NOT MINE! Taken from hackzine.com blog.

It creates a tree-style output of all the (sub)folders and (sub)files from the current folder and down(deeper)

Quoting some of hackzine's words

"Murphy Mac sent us a link to a handy find/sed command that simulates the DOS tree command that you might be missing on your Mac or Linux box. [..split...] Like most things I've seen sed do, it does quite a bit in a single line of code and is completely impossible to read. Sure it's just a couple of substitutions, but like a jack in the box, it remains a surprise every time I run it."

mv file_name.extension ..
2009-03-09 15:35:58
User: takealeft
Functions: mv
-3

cd into the directory that contains the file.

this is just the usual move command but shortcut'd.

say you wanted to move a photo img1.png from ~/photos/holidayphotos into the parent directory which is ~/photos

command would be:

~/photos/holidayphotos$ mv img1.png ..

I use Ubuntu so this'll work in debian but not sure what else.

INFILE=/path/to/your/backup.img; MOUNTPT=/mnt/foo; PARTITION=1; mount "$INFILE" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "$INFILE" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ]
6

Suppose you made a backup of your hard disk with dd:

dd if=/dev/sda of=/mnt/disk/backup.img

This command enables you to mount a partition from inside this image, so you can access your files directly.

Substitute PARTITION=1 with the number of the partition you want to mount (returned from sfdisk -d yourfile.img).

some_cronjobed_script.sh 2>&1 | tee -a output.log | grep -C 1000 ERROR
2009-03-06 17:51:13
User: DEinspanjer
Functions: grep tee
Tags: Linux
-1

The large context number (-C 1000) is a bit of a hack, but in most of my use cases, it makes sure I'll see the whole log output.

taskset -c 0 your_command
2009-02-28 22:38:02
User: Alanceil
Functions: taskset
19

This is useful if you have a program which doesn't work well with multicore CPUs. With taskset you can set its CPU affinity to run on only one core.

for vm in $(vmware-cmd -l);do echo -n "${vm} ";vmware-cmd ${vm} getstate|awk '{print $2 " " $3}';done
2009-02-26 16:45:04
User: dbart
Functions: echo
3

I use this command on my machines running VMware Server to print out the state of all registered Virtual machines.