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 by SuperFly from sorted by
Terminal - Commands by SuperFly - 20 results
echo -n '#!'$(which awk)
pkill -KILL -u username
2011-06-16 11:40:57
User: SuperFly
Tags: awk grep who
10

How to force a userid to log out of a Linux host, by killing all processes owned by the user, including login shells:

awk '{gsub("<[^>]*>", "")}1' file
2011-01-06 13:24:34
User: SuperFly
Functions: awk
3

A shorter version of command #3014, using awk instead of sed. Useful when scraping websites with a script.

echo "this is a test" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
2011-01-06 13:09:49
User: SuperFly
Functions: echo sed
8

Converts the first letter of each word to upper case

date +"%V"
2010-11-17 14:59:31
User: SuperFly
Functions: date
4

Not as cool as the python example, but it still works.

/usr/bin/package-cleanup --oldkernels --count=3
2010-09-02 11:18:13
User: SuperFly
2

Install using yum install yum-utils

Options include:

--oldkernels Remove old kernel and kernel-devel packages

--count=KERNELCOUNT Number of kernel packages to keep on the system (default 2)

use package-cleanup --help for a complete list

[ `date --date='next day' +'%B'` == `date +'%B'` ] || echo 'end of month' && echo 'not end of month'
2010-08-11 14:25:04
User: SuperFly
Functions: echo
2

Nice simple example of something we can do in bash.

sed -n '1,/regex/p' filename
2010-07-08 19:25:59
User: SuperFly
Functions: sed
2

Start printing the contents of filename to stdout, until a matching line to regex is found, then stop.

sed -n '/regex/,$p' filename
2010-07-08 19:24:43
User: SuperFly
Functions: sed
1

Search in "filename" for the first line to match regex, and print to stdout from the matching line to the end of the file.

watch -tn1 'date +%r | figlet'
2010-06-22 10:59:16
User: SuperFly
Functions: watch
4

Figlet is easy to find for download on the internet, and works for any text. Quite cool.

/usr/bin/links --source http://weather.noaa.gov/pub/data/forecasts/zone/md/mdz009.txt
echo $VARIABLE | xargs -d'\40' -n 6 echo
2010-01-20 15:12:53
User: SuperFly
Functions: echo xargs
1

Print out the contents of $VARIABLE, six words per line, ignoring any single or double quotes in the text. Useful when $VARIABLE contains a sentence that changes periodically, and may or may not contain quoted text.

awk 'BEGIN{while (a++<50) s=s "-"; print s}'
2010-01-06 16:16:35
User: SuperFly
Functions: awk
0

Change the number 50 to whatever number of characters you want. Change the character inside the double quotes to whatever you want printed.

nl <filename>
2010-01-06 16:14:43
User: SuperFly
Functions: nl
-1

Works in RHEL5 and derivatives.

<your command here> | perl -n -e 'print "$_" if 1 ... /<regex>/;'
2009-12-22 14:06:41
User: SuperFly
Functions: command perl
0

This command line will display the output of , from the first line of output, until the first time it sees a pattern matching .

You could specify the regex pattern /^$/ to look for the first blank line,

or /^foobar/ to look for the first line that starts with the word foobar.

function man2pdf(){ man -t ${1:?Specify man as arg} | ps2pdf -dCompatibility=1.3 - - > ${1}.pdf; }
2009-11-27 12:49:49
User: SuperFly
Functions: as man
14

Tested on Fedora 12. This function will take a man page and convert it to pdf, saving the output to the current working directory. In Gnome, you can then view the output with "gnome-open file.pdf", or your favorite pdf viewer.

shutdown -rF now
2009-10-05 15:02:36
User: SuperFly
Functions: shutdown
2

Force an fsck on reboot. Useful on a system where / has mounted read-only because of file system issues.

dmidecode | awk '/VMware Virtual Platform/ {print $3,$4,$5}'
2009-09-25 14:46:35
User: SuperFly
Functions: awk
6

If you run this command on a VMWare Virtual Machine, it will return the string "VMware Virtual Platform". If you run it on a physical machine, it will return nothing. Useful for having a script determine if it's running on a VM or not. Of course, you must have dmidecode installed for this to work.

Try it this way in a script: ISVM=$(dmidecode | awk '/VMware Virtual Platform/ {print $3,$4,$5}')

Then test if $ISVM has text in it, or is blank.

badblocks -vfw /dev/fd0 10000 ; reboot
2009-09-04 16:57:51
User: SuperFly
Functions: badblocks
0

THIS COMMAND IS DESTRUCTIVE. That said, lets assume you want to render your boot drive unbootable and reboot your machine. Maybe you want it to boot off the network and kickstart from a boot server for a fresh OS install. Replace /dev/fd0 with the device name of your boot drive and this DESTRUCTIVE command will render your drive unbootable. Your BIOS boot priority should be set to boot from HD first, then LAN.

for x in `seq 0 25 $(curl "http://www.commandlinefu.com/commands/browse"|grep "Terminal - All commands" |perl -pe 's/.+(\d+),(\d+).+/$1$2/'|head -n1)`; do curl "http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/$x" ; done > a.txt
2009-08-27 11:02:53
User: SuperFly
Functions: grep head perl
4

'jot' does not come with most *nix distros, so we need to use seq to make it work. This version tested good on Fedora 11.