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 using ifconfig from sorted by
Terminal - Commands using ifconfig - 89 results
ifconfig | grep addr:192 | sed s/Bcast.*// | sed 's/^.*inet addr://'
2010-03-26 07:37:21
User: hasenj
Functions: grep ifconfig sed
-5

This assumes your local ip starts with 192.something (e.g. 192.168), it greps ifconfig output for an ip that starts with 192, then strips the extra garbage (besides the ip)

Maybe `ifconfig | grep addr | grep Bcast` would also do it

ifconfig | awk '/inet addr/ {print $2 }' | sed 's/.*://g'
2010-03-04 08:15:08
User: Guyverix
Functions: awk ifconfig sed
-2

Easy way to grab the IP address of a machine for easy script use. If needed a "| grep -v 127.0.0.1" at the end will suppress localhost.

ifconfig | awk '/HWaddr/ { print $NF }'
ifconfig -a| grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
2010-02-27 14:30:43
User: evenme
Functions: grep ifconfig
Tags: ifconfig grep
3

Get mac address listed for all interfaces.

for i in `lanscan -i | awk '{print $1}'` ; do ifconfig $i ; done
for i in `netstat -rn |grep lan |cut -c55-60 |sort |uniq`; do ifconfig $i; done
2010-01-28 17:35:20
User: Kaio
Functions: cut grep ifconfig sort
-4

HP UX doesn't have a -a switch in the ifconfig command.

This line emulates the same result shown in Solaris, AIX or Linux

ifconfig eth0:1 192.168.0.1/24
2010-01-12 11:57:11
User: glaudiston
Functions: ifconfig
1

the below command create a alias for share your internet connection with another.

ifconfig eth0:1 192.168.0.1/24

Its obviously necessary too activate the iptables post-routing and ip forwarding, as root:

modprobe iptable_nat iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE echo 1 > /proc/sys/net/ipv4/ip_forward

Be sure that the alias 192.168.0.0/24 is not your active real ip range

ifconfig | grep -B 1 "inet addr:" | head -1 | cut -d" " -f1
2010-01-04 23:02:57
User: jasonwalsh
Functions: cut grep head ifconfig
-2

Get the line containing "inet addr:" and the line before that, get down to only the first line, and then get the first word on that line, which should be the interface.

ifconfig -l | xargs -n1 ipconfig getifaddr 2> /dev/null
ipcalc $(ifconfig eth0 | grep "inet addr:" | cut -d':' -f2,4 | sed 's/.+Bcast:/\//g') | awk '/Network/ { print $2 } '
awk '/inet end/ {print $3}' <(ifconfig eth0)
ifconfig eth0 | grep "inet:" | cut -d ":" -f2 | cut -d " " -f1
ifconfig -a| awk 'BEGIN{FS="[ :]+"} /Bcast/{print $4}'
ifconfig -a|grep Bcast:|cut -d\: -f2|awk '{print $1}'
ifconfig | awk '/HW/ {print $5}'
2009-11-05 18:00:50
User: Cont3mpo
Functions: awk ifconfig
0

Simple MAC adrress, thanks to ifconfig.

sudo ifconfig -a | grep eth | grep HW | cut -d' ' -f11
ifconfig eth1 | grep inet\ addr | awk '{print $2}' | cut -d: -f2 | sed s/^/eth1:\ /g
2009-11-03 19:26:40
User: TuxOtaku
Functions: awk cut grep ifconfig sed
2

Sometimes, you don't really care about all the other information that ifconfig spits at you (however useful it may otherwise be). You just want an IP. This strips out all the crap and gives you exactly what you want.

ips(){ for if in ${1:-$(ip link list|grep '^.: '|cut -d\ -f2|cut -d: -f1)};do cur=$(ifconfig $if|grep "inet addr"|sed 's/.*inet addr:\([0-9\.]*\).*/\1/g');printf '%-5s%-15s%-15s\n' $if $cur $(nc -s $cur sine.cluenet.org 128 2>/dev/null||echo $cur);done;}
2009-08-07 10:04:46
User: frozenfire
Functions: cut echo grep ifconfig link sed
-3

Gets the internal and external IP addresses of all your interfaces, or the ones given as arguments

ifconfig $devices | grep "inet addr" | sed 's/.*inet addr:\([0-9\.]*\).*/\1/g'
ifconfig | grep "0xffffffff" | awk '{ print $2 }' | xargs -n 1 ifconfig em0 delete
2009-08-04 05:18:36
User: vwal
Functions: awk grep ifconfig xargs
3

The example command deletes all aliases for network interface 'em0' assuming that the aliases have netmask of 255.255.255.255 and the master IP has some other netmask (such as 255.255.255.0). See here -> http://my.galagzee.com/2009/07/22/deleting-all-network-interface-aliases/ for more on the rationale of this command.

ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'
2009-07-31 09:49:17
User: sneaker
Functions: ifconfig perl
12

works on Linux and Solaris. I think it will work on nearly all *nix-es

ifconfig | awk '/ddr:[0-9]/ {sub(/addr:/, ""); print $2}'
2009-07-31 09:30:54
User: danny_b85
Functions: awk ifconfig
Tags: Linux ifconfig
1

The initial version of this command also outputted extra empty lines, so it went like this:

192.168.166.48

127.0.0.1

This happened on Ubuntu, i haven't tested on anything else.

ifconfig | awk -F':| +' '/ddr:/{print $4}'
2009-07-25 22:51:08
User: 0x89
Functions: awk ifconfig
Tags: Linux ifconfig
1

and, a lot uglier, with sed:

ifconfig | sed -n '/inet addr:/s/[^:]\+:\(\S\+\).*/\1/p'

Edit:

Wanted to be shorter than the perl version. Still think that the perl version is the best..

ifconfig | grep "inet [[:alpha:]]\+" | cut -d: -f2 | cut -d' ' -f2
2009-07-24 13:16:37
User: feraf
Functions: cut grep ifconfig
Tags: Linux ifconfig
-6

Get the IP address of all your network cards.

ifconfig | grep "inet addr" | cut -d: -f2 | cut -d' ' -f1