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 sputnick from sorted by
Terminal - Commands by sputnick - 42 results
netstat -nptl
2009-12-11 10:23:43
User: sputnick
Functions: netstat
2

Alternative of OJM snippet :

This one show the IP too, where ports bind. It's very important, because if there's only 127.0.0.1 instead of 0.0.0.0, connections from internet are rejected.

od -c <FILE> | grep --color '\\.'
2009-12-11 02:15:48
User: sputnick
Functions: grep od
Tags: od
6

For fancier and cleaner output, try the following snippet :

showendlines(){ while read i; do od --address-radix=n --width=$(wc -c <<< "$i") -c <<< "$i" | perl -pe 's/.\K\s{2,3}//g'; done < $1 | grep --color '\\.'; }

Now you can run that with :

showendlines <FILE>

Thanks to prince_jammys to "debug" my English ;)

grep -oP '"url":"\K[^"]+' $(ls -t ~/.mozilla/firefox/*/sessionstore.js | sed q)
2009-12-09 20:34:32
User: sputnick
Functions: grep ls sed
0

Require "grep -P" ( pcre ).

If you don't have grep -P, use that :

grep -Eo '"url":"[^"]+' $(ls -t ~/.mozilla/firefox/*/sessionstore.js | sed q) | cut -d'"' -f4
curl -s http://sputnick-area.net/ip
perl -e 'use strict; use warnings; my $c; my $file = $ARGV[0]; open my $handle, "<", $file or die "$0: $file: $!\n"; while (<$handle>) { print $c++, " " x 5, $_; } close($handle);' <FILE>
2009-12-09 16:07:14
User: sputnick
Functions: perl
0

This is a joke for @putnamhill and @glaudiston

I'm pretty sure we can write longer if we want ;)

traceroute 2>/dev/null -n google.com | awk '/^ *1/{print $2;exit}'
find . -iname '*.mp3' -type f -print0 | xargs -I{} -0 cp {} </path>
2009-12-08 20:50:48
User: sputnick
Functions: cp find xargs
5

No problem with word splitting. That should works on many Unix likes.

perl -pe 'print "$. "' <file>
alias :q='tput setaf 1; echo >&2 "this is NOT vi(m) :/"; tput sgr0'
2009-12-08 12:59:44
User: sputnick
Functions: alias echo tput
Tags: vim alias vi tput
-1

For vi(m) users :

Add it in your ~/.bashrc

Add an "exit" @ the end if you are masochist ;)

nc -zw2 www.example.com 80 && echo open
2009-12-07 21:35:25
User: sputnick
Functions: echo
Tags: netcat nc
6

@putnamhill, no need if statement in that case.

&& is a AND and || is a OR

MyIps(){ echo -e "local:\n$(ifconfig $1 | grep -oP 'inet (add?r:)?\K(\d{1,3}\.){3}\d{1,3}')\n\npublic:\n$(curl -s sputnick-area.net/ip)"; }
2009-12-06 22:52:31
User: sputnick
Functions: echo
1

Like the tiltle said, you can use an argument too ( the interface )

MyIps eth0

will show only the IP of this interface and the public IP

( tested with Linux )

You can add that function in ~/.bashrc, then

. ~/.bashrc

Now you are ready to call this function in all your terms...

straceprocessname(){ x=( $(pgrep "$@") ); [[ ${x[@]} ]] || return 1; strace -vf ${x[@]/#/-p }; }
2009-12-03 00:04:39
User: sputnick
Functions: return strace
3

Especially for sysadmins when they don't want to waste time to add -p flag on the N processes of a processname.

In the old school, you did ;

pgrep processname

and typing strace -f -p 456 -p 678 -p 974...

You can add -f argument to the function. That way, the function will deal with pgrep to match the command-line.

Example :

processname -f jrockit
modinfo $(cut -d' ' -f1 /proc/modules) | sed '/^dep/s/$/\n/; /^file\|^desc\|^dep/!d'
2009-11-18 23:40:46
User: sputnick
Functions: cut modinfo sed
6

I modify 4077 and marssi commandline to simplify it and skip an error when parsing the first line of lsmod (4077). Also, it's more concise and small now. I skip using xargs ( not required here ). This is only for GNU sed.

For thoses without GNU sed, use that :

modinfo $(lsmod | awk 'NR>1 {print $1}') | sed -e '/^dep/s/$/\n/g' -e '/^file/b' -e '/^desc/b' -e '/^dep/b' -e d
slice(){ cut -c$((${#1}+1))-; }; ls -l | slice "-rw-r--r--"
GeoipLookUp(){ curl -A "Mozilla/5.0" -s "http://www.geody.com/geoip.php?ip=$1" | grep "^IP.*$1" | html2text; }
2009-11-06 00:32:27
User: sputnick
Functions: grep
Tags: sed curl
3

That makes a function you can put in your ~/.bashrc to run it when you need in any term with an IP as argument

$ i=(*);echo ${i[RANDOM%(${#i[@]}+1)]]}
2009-06-10 18:47:03
User: sputnick
2

An other way to run it ( playing a random file ending with avi, flv or mpeg ) from a specified dir and a specified type of extension :

making MOVIE array with a glob :

MOVIE=( /PATH/TO/MY/FAVORITE/MOVIES/*.{avi,flv,mpeg} )

playing the random file from a random key from the array

mplayer ${MOVIE[ RANDOM % ( ${#i[@]} + 1 ) ]]}

I use only globs and a bash array.

I use GNU bash, version 3.2.48

dd if=/dev/sda5 bs=2048 conv=noerror,sync | gzip -fc | lftp -u user,passwd domain.tld -e "put /dev/stdin -o backup-$(date +%Y%m%d%H%M).gz; quit"