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 infinull from sorted by
Terminal - Commands by infinull - 19 results
curl -s 'http://www.discogs.com/search?q=724349691704' | sed -n '\#/release/#{s/^<div>.*>\(.*\)<\/a><\/div>/\1/p}'
2011-01-30 23:49:22
User: infinull
Functions: sed
Tags: sed discogs UPC
1

I like curl better than wget, I just think that curl -s is a lot simpler than wget ... see I forget what you even have to do to get wget to pipe it's output

Anyway, all in one sed command as "requested"

echo "Terrorist threat level: $(curl -s 'http://www.dhs.gov/dhspublic/getAdvisoryCondition' | awk -F\" 'NR==2{ print $2 }')"
2011-01-28 00:23:13
User: infinull
Functions: echo
0

Used curl instead of wget (minor change)

Used the full-url instead of the shortened one (in case is.gd goes down/empties their cache)

turned the tail | awk bit into a single awk command.

echo $SSH_CLIENT | cut -f 1 -d ' '
2011-01-11 10:20:45
User: infinull
Functions: cut echo
0
host -i `echo $SSH_CLIENT | cut -f 1 -d \ ` | sed 's/.* domain name pointer \(.*\)\./\1/'

to reverse lookup and get the hostname.

echo <command>; !#:0-$
2010-12-15 08:28:50
User: infinull
Functions: echo
2

!# is the currennt line so far, by using !#:- you can go from the second arg (it's zero indexed) to the last ($)

(did I understand the problem correctly?)

lspci -v -s `lspci | awk '/VGA/{print $1}'`
2010-10-26 17:45:14
User: infinull
Functions: awk lspci
Tags: Linux hardware
1

probably only works if you have one graphics card.

used this: http://www.cyberciti.biz/faq/howto-find-linux-vga-video-card-ram/

as reference

can be expanded, for example:

lspci -v -s `lspci | awk '/VGA/{print $1}'` | sed -n '/Memory.*, prefetchable/s/.*\[size=\([^]]\+\)\]/\1/p'

will just get the amount of prefetchable memory

compare to:

lshw -C display

which does not give the size (it does give byte ranges and you could calculate the size from that, but that's a pain)

Also uses a command which is not standard on linux; wheras lspci is a core utility provided by most systems

pkgfile -lb `pkgfile <command>`
2010-10-01 04:54:49
User: infinull
Tags: archlinux
1

uses the pkgfile command (part of the community repository), highly suggested.

awk '{print $1}' < three-column.txt > first-column.txt
while read col1 col23; do echo $col1; done < three-column.txt > first-column.txt
sed -n '4p'
head -n X | tail -n 1
2010-07-08 22:06:39
User: infinull
Functions: head tail
-1

using tail first won't do it because tail counts from the bottom of the file. You could do it this way but I don't suggest it

xdg-open [path]
2010-06-30 07:32:53
User: infinull
Tags: KDE gnome
16

open [path] in the default program, regardless of which Desktop Environment you use (KDE, GNOME, etc.)

Works on all "freedesktop.org" compatible desktop environments

file -L <library> | grep -q '64-bit' && echo 'library is 64 bit' || echo 'library is 32 bit'
2010-03-07 06:31:35
User: infinull
Functions: echo file grep
Tags: bash
-3

file displays a files type

the -L flag means follow sym-links (as libraries are often sym-linked to another this behavior is likely preferred)

more complex behavior (*two* grep commands!) could be used to determine if the file is or is not a shared library.

uri_escape(){ echo -E "$@" | sed 's/\\/\\\\/g;s/./&\n/g' | while read -r i; do echo $i | grep -q '[a-zA-Z0-9/.:?&=]' && echo -n "$i" || printf %%%x \'"$i" done }
2010-02-13 01:39:51
User: infinull
Functions: echo grep printf read sed
1

This one uses hex conversion to do the converting and is in shell/sed only (should probably still use the python/perl version).

ps aux | sed -n '/USER/!s/\([^ ]\) .*/\1/p' | sort -u
2010-02-10 05:56:26
User: infinull
Functions: ps sed sort
1

This is different that `who` in that who only cares about logged-in users running shells, this command will show all daemon users and what not; also users logged in remotely via SSH but are running SFTP/SCP only and not a shell.

printf $(echo -n $1 | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')
2009-11-25 04:27:39
User: infinull
Functions: echo printf sed
2

My version uses printf and command substitution ($()) instead of echo -e and xargs, this is a few less chars, but not real substantive difference.

Also supports lowercase hex letters and a backslash (\) will make it through unescaped

chr () { echo -en "\0$(printf %x $1)"}
ord () { seq 1 127 | while read i; do echo `chr $i` $i; done | grep "^$1 " | cut -c '3-' }
2009-10-16 21:54:01
User: infinull
Functions: cut echo grep read seq
0

uses the previous "chr" function and uses it to create the inverse function "ord" by brute force.

It's slow, It's inelegant, but it works.

I thought I needed ord/chr to do a cartesian cipher in shell script a whie ago, but eventualy I realized I could get fancy with tr and do the same thing...

smartypants | php -r "echo mb_decode_numericentity(file_get_contents('php://stdin'),array(0x0000,0xFFFF,0x0000,0xFFFF),'UTF-8');"
2009-08-14 04:02:38
User: infinull
-2

Turns regular quotes into curly quotes, also converts hyphens to dashes using a heuristic and outputs the result as UTF-8, suitable to copy/paste into wordprocessor.

requires: http://daringfireball.net/projects/smartypants/ (which does most of the work)

(I renamed smartypants.pl to smartypants before adding it to my $PATH)

Also requrires PHP with the multibyte module installed (its installed by default with PHP, but the sysadmin on one server I use disabled it... presumadly to increase performance or something).

find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33 | cut -c 35-
2009-08-04 07:05:12
User: infinull
Functions: cut find md5sum sort uniq
18

Calculates md5 sum of files. sort (required for uniq to work). uniq based on only the hash. use cut ro remove the hash from the result.