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.

Universal configuration monitoring and system of record for IT.
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 - 12,034 results
$ hash="foo:bar"; key=${hash%:*}; value=${hash#*:}; echo "Key: $key Value: $value"
2011-02-21 14:47:28
User: bartonski
Functions: echo
-1

This is a quick and dirty way of creating key/value pairs in bash. Not as flexible as an associative array, but often powerful enough to be useful. For example, if I want to rename sets of files in a way that I can't do with wildcards, I'll create something like 'rename-me.txt'

filename_with_no_pattern:aa.txt\n AnotherPieceOfText:ab.txt\n /deeply/nested/file/with/no/pattern.txt:ac.txt\n

Then I'll traverse rename-me.txt with a read loop

cat rename-me.txt | while read line; do old=${line%:*}; new=${line#*:}; mv $old $new; done
find . -type f -name '*.c' -exec wc -l {} \; | awk '{sum+=$1} END {print sum}'
2009-09-04 15:51:30
User: arcege
Functions: awk find wc
Tags: awk find wc
-1

Have wc work on each file then add up the total with awk; get a 43% speed increase on RHEL over using "-exec cat|wc -l" and a 67% increase on my Ubuntu laptop (this is with 10MB of data in 767 files).

ykpersonalize -1 -ostatic-ticket -ostrong-pw1 -ostrong-pw2
2011-05-13 04:00:52
-1

By default, slot 1 is used for OTP purposes and validates against the Yubikey servers. Running the command above will replace that. If you have a Yubikey 2 or above, you can change -1 to -2 and program the second slot with a strong static key.

for IP in `cat ip.txt|awk -F: '{print $1}'`; do geoiplookup -f /usr/local/share/GeoIP/GeoIP.dat $IP|awk -F, '{print $2}'>>out.txt; done; paste -d ":" ip.txt out.txt>zoom.txt
2011-08-05 09:26:17
User: woben2
Functions: awk paste
Tags: geoip
-1

take a list of IP:PORT and output IP:PORT:COUNTRY

ethtool -S eth0
defaults write com.macromates.textmate OakDefaultLanguage 17994EC8-6B1D-11D9-AC3A-000D93589AF6
2009-10-20 07:10:34
User: vigo
Functions: write
-1

When you press "cmd+n" in TextMate, you can have "HTML" language as default document format... You can also define other languages too. You need to know the UUID of your language bundle.

grep -hIr -m 1 :name ~/.mozilla/firefox/*.$USER/extensions | tr '<>=' '"""' | cut -f3 -d'"' | sort -u
2010-05-18 14:49:44
User: new_user
Functions: grep sort tr
-1

1.) my profile ends with $USER not with .default

2.) only grep for the first occurrence because some extensions have the translated name also inside the install.rdf

pvscan | awk '/name_of_vg/ {print $2}' | sed 's/[-|/|]/ /g' | cut -d " " -f7
2010-06-22 16:34:42
User: slashdot
Functions: awk cut sed
-1

This is an easy way to quickly get a status for a device in multipath on SLES systems, as long as the server is configured based on Novell's standards, where multipathed disks are referred to by /dev/disk/by-... tree. Make sure to replace name_of_vg with your Volume Group name.

gethostip -d hostname
jkhgkjh; until [[ $? -eq 0 ]]; do YOURCOMMAND; done
2014-04-11 08:19:15
User: moiefu
-1

You want bash to keep running the command until it is successful (until the exit code is 0). Give a dummy command, which sets the exit code to 1 then keep running your command until it exits cleanly

In MySQL client, \P less -S
2011-01-19 09:44:43
User: maxmanders
Functions: less
-1

Instead of dumping the entire query to the screen, it will use 'less -S' to handle pagination. This means you can move back and forward through the result set, and instead of wrapping rows at screen boundaries, the table will extend off the screen, and can be scrolled left and right with arrow keys.

alias viaco='task="$(basename "$(pwd)")"; if [ -f "$task.c" ]; then vi -c "set mouse=n" -c "set autoread" -c "vsplit $task.out" -c "split $task.in" -c "wincmd l" -c "wincmd H" $task.c; fi'
2011-05-13 13:35:02
User: yolila
Functions: alias vi
-1

1. Get name of task by task=$(basename "$(pwd)")

2. Check whether "$task.c" exists as a file

3. open "$task.c", "$task.in", "task.out" in vim with such layout.

-------------------------------

|            | $task.in    |

|            |                |

|$task.c  |-----------------|

|            | $task.out  |

|            |                |

-------------------------------

echo -e "n\np\n1\n\n\nt\n8e\nw" | fdisk /dev/sdX
2009-10-20 16:21:54
User: sud0er
Functions: echo fdisk
Tags: Linux fdisk
-1

This is a bit hacky, but if you're setting up a bunch of new LUNs, it can save a bunch of time. Also check out sfdisk. The fdisk will fail if, for example, a partition table already exists.

mknod replypipe p; nc -k -lp 1234 < replypipe| nc -U /var/run/mysocket.sock > replypipe
2014-07-19 18:17:35
User: adimania
Functions: mknod
-1

* Make a FIFO file named replypipe

* listen on 1234

* pass the request to unix socket

* unix socket will reply to replypipe

* replypipe will write reply to the client

gsed -e :a -e 's/\(<\/[^>]*>\)/\1\n/g;s/\(<br>\)/\1\n/g' page2.txt | sed -n '/<cite>/p;s/<cite>\(.*\)<\/cite>/\1/g' >> output
2009-02-06 12:45:20
User: shrimphead
Functions: sed
-1

From a saved page of google search results, split out all of the links for the results. Useful for creating apache rewrite rules from.

java -jar /path/to/filename.jar
2011-04-01 05:44:56
User: Bonster
-1

Use this to make a launcher for Java apps ( .jar files ) such as with jdownloader

ls -1 | xargs ruby -e'puts ARGV.shuffle'
perl -le 'print join ", ", map { chomp; $_ } <>'
2009-02-06 12:50:43
User: jozef
Functions: join perl
-1

joins multiple lines to create single line with comma separated values. for example if we have an email addresses one per line (copy&paste from spreadsheet) it will oputput one line with comman separated addresses to put it to email client.

dhclient wlan0 && sbnt=$(ifconfig wlan0 |grep "inet addr" |cut -d ":" -f 2 | cut -d "." -f 1-3) && nmap $sbnt.0/24 -sP
2010-06-22 21:00:29
User: wltj
Functions: cut grep ifconfig
-1

This is helpful if you connect to several networks with different subnets such as 192 networks, 10 networks, etc. Cuts first three octets of ip from ifconfig command and runs nmap ping scan on that subnet.

Replace wlan0 with your interface. Assumes class c network, if class b use: cut -d "." -f 1-2 and change nmap command accordingly.

lynx --dump http://www.seeon.tv/channels| grep "/channels"|awk '{print $2}'|sort -u|while read links; do lynx --dump "$links"|awk '/view/ {print $2}'|sort -u; done
2011-04-01 05:58:20
User: Bonster
Functions: awk grep read sort
-1

This shows a list of channels from seeon.tv website to watch shows and movies

du -ks * | sort -n
2009-02-16 14:18:52
Functions: du sort
-1

Very useful for finding the largest files and subdirectories at any given point. Any user can run it from current location just when need to know their largest files and subtdirectories from a certain point down as well.

sudo chown -R user2:user2 /../../somedirectory
2009-09-23 21:27:45
User: bkn390
Functions: chown sudo
-1

This will change the ownership of /../../somedirectory as well as all its subdirectories so they will be be owned by user2 - typically used when a directory is owned by root:root

< <infile> tr ' \t' '\n' | tr -s '\n' > <outfile>
2009-07-07 01:17:47
User: qubyte
Functions: tr
Tags: shell tr
-1

Puts words on new lines, removing additional newlines.

for x in *.ogg; do ffmpeg -i "$x" "`basename "$x" .ogg`.mp3"; done
2009-09-04 20:23:09
User: Superhuman
Tags: ffmpeg ogg mp3 for
-1

Converts all ogg files to mp3 files in the current directory.