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 dbbolton from sorted by
Terminal - Commands by dbbolton - 28 results
zc () { for exp in $argv; do print "$exp = $(( exp ))"; done; }
aptitude search ~i -F %p
2011-10-15 00:31:10
User: dbbolton
0

This will print the name of every installed package on a Debian system.

/usr/sbin/dmidecode | perl -lne 'print $1 if /Current\s+Speed:\s+(\d+\s+MHz)/'
wget -q -O - http://www.perl.org/get.html | grep -m1 '\.tar\.gz' | sed 's/.*perl-//; s/\.tar\.gz.*//'
curl -s http://www.perl.org/get.html | grep -m1 '\.tar\.gz' | sed 's/.*perl-//; s/\.tar\.gz.*//'
!<number>
2011-08-18 01:08:57
User: dbbolton
Tags: history bash zsh
0

You can find a command's history event number via the `history` command.

You can also put the history event number in your prompt: \! for bash, or %h for zsh.

Finally, I would like to point out that by "number", I mean POSITIVE INTEGER. Not, say, a letter, such as 'm'. Examples:

!1

or

!975
isgd () { curl 'http://is.gd/create.php?format=simple&url='"$1" ; printf "\n" }
2011-08-14 23:31:39
User: dbbolton
Functions: printf
Tags: curl shorturl url
1

Check the API. You shouldn't need sed. The print-newline at the end is to prevent zsh from inserting a % after the end-of-output.

Also works with http://v.gd

prep () { perl -nle 'print if '"$1"';' $2 }
2011-08-13 14:29:26
User: dbbolton
Functions: perl
Tags: perl grep regex
2

If you've ever tried "grep -P" you know how terrible it is. Even the man page describes it as "highly experimental". This function will let you 'grep' pipes and files using Perl syntax for regular expressions.

The first argument is the pattern, e.g. '/foo/'. The second argument is a filename (optional).

find . -maxdepth 1 -type d | sort
aptitude search \~ilinux-image
perl -m'AptPkg::Cache' -le '$c=AptPkg::Cache->new; for (keys %$c){ push @a, $_ if $c->{$_}->{'CurrentState'} eq 'Installed';} print for sort @a;'
2011-03-14 23:56:43
User: dbbolton
Functions: perl sort
-2

A space-padded version:

perl -m'AptPkg::Cache' -e '$c=AptPkg::Cache->new; for (keys %$c){ push @a, $_ if $c->{$_}->{'CurrentState'} eq 'Installed';} print "$_ " for sort @a;'
rm ^'name with spaces'
2010-08-21 02:24:17
User: dbbolton
Functions: rm
Tags: rm zsh glob
1

This is for zsh with extended globbing.

convert example.png -resize 100x100 output.png
2010-08-08 16:40:21
User: dbbolton
-1

You can also specify width and height of the resized image (in pixels), as an alternative to using a percentage.

find "$1" -iname "*$2*"
perl -le 'chomp($w=`which $ARGV[0]`);$_=`file $w`;while(/link\b/){chomp($_=(split/`/,$_)[1]);chop$_;$w.=" -> $_";$_=`file $_`;}print "\n$w";' COMMAND_NAME
2010-07-30 19:26:35
User: dbbolton
Functions: perl
0

This will show you any links that a command follows (unlike 'file -L'), as well as the ultimate binary or script.

Put the name of the command at the very end; this will be passed to perl as the first argument.

For obvious reasons, this doesn't work with aliases or functions.

rm $( ls | egrep -v 'abc|\s' )
2010-07-18 10:59:15
User: dbbolton
Functions: egrep ls rm
Tags: grep rm
-1

Really, you deserve whatever happens if you have a whitespace character in a file name, but this has a small safety net. The truly paranoid will use '-i'.

grep '^MemFree:' /proc/meminfo | awk '{ mem=($2)/(1024) ; printf "%0.0f MB\n", mem }'
2010-06-30 18:33:29
User: dbbolton
Functions: awk grep printf
4

This will show the amount of physical RAM that is left unused by the system.

perl -MNet::Twitter -e '$nt = Net::Twitter->new(traits => [qw/API::REST/], username => "YOUR USERNAME", password => "YOUR PASSWORD"); $ud = $nt->update("YOUR TWEET");'
2010-06-16 19:46:05
User: dbbolton
Functions: perl
2

Requires Net::Twitter. Just replace the double quoted strings with the appropriate info.

quickfind () { find . -maxdepth 2 -iname "*$1*" }
2010-06-12 03:04:50
User: dbbolton
Functions: find
Tags: find function
1

A function that allows you to perform a case-insensitive search in the current directory, and directories in the current directory (but no further), for files containing the first argument anywhere in their names.

!:1-3
!:n
2010-06-12 02:48:27
User: dbbolton
Tags: history bash zsh
8

'n' is a non-negative integer. Using 0 will expand to the name of the previous command.

du -hs /path/to/target
aptitude remove ?and(~i~nlinux-(im|he) ?not(~n`uname -r`))
2010-06-11 22:57:09
User: dbbolton
2

A little aptitude magic. Note: this will remove images AND headers. If you just want to remove images: aptitude remove ?and(~i~nlinux-im ?not(~n`uname -r`))

I used this in zsh without any problems. I'm not sure how other shells will interpret some of the special characters used in the aptitude search terms. Use -s to simulate.

tw translate.google.com.de-en `xsel`
2010-06-11 22:28:28
User: dbbolton
-1

Translate the X selection from German to English. The tw program is available from Savannah: http://mirror.its.uidaho.edu/pub/savannah/twandgtw/

I'm posting this because the base Debian system also does not include curl.

acpi -b | sed 's/,//g' | awk '{print $3}'
2010-06-11 05:32:57
User: dbbolton
Functions: acpi awk sed
Tags: battery acpi
0

This prints "Charging" or "Discharging". Obviously, this will indicate the status of the AC adapter. The awk part could be from 1-6. I removed the comma because it is useless when only looking at one element of the output array. See acpi(1) for more info.