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 route from sorted by
Terminal - Commands using route - 15 results
ip route list 0/0
ip route get 8.8.8.8 2>/dev/null|grep -Eo 'src [0-9.]+'|grep -Eo '[0-9.]+'
2012-10-14 02:06:13
User: intangible
Functions: get grep route
2

Instead of hard-coding in a check to scrape info from ifconfig based on a specific interface, do it in a more portable way.

This works really well if you switch between wired, wireless, bluetooth or even VPN connections. You can get your current IP in a script (since it'll be something like tun0 instead of eth0 or wlan1).

This uses a well known public ip address 8.8.8.8, but it doesn't actually connect to it, it just shows you the route it would take.

ip route get dest_ip
2012-08-08 17:19:16
User: miguelcnf
Functions: get route
1

Useful to determine the source_ip of outgoing packages to a certain destination

route -n | perl -ne '$ANY="0.0.0.0"; /^$ANY/ and split /\s+/ and print "Gateway to the World: ",($_[1]!=$ANY)?$_[1]:(`ip address show $_[$#_]`=~/peer ([0-9\.]+)/ and $1),", via $_[$#_].\n"'
for net in $(ip route show | cut -f1 -d\ | grep -v default); do ipcalc $net | grep Broadcast | cut -d\ -f 2; done
2010-09-28 13:21:31
User: svg
Functions: cut grep route
0

Can be used in a firewall script, e.g. to avoid logging broadcast packtes.

ip route | awk '/default/{print $3}'
route -n | grep "^0\." | awk '{print "Gateway to the World: "$2", via "$NF""}'
ip route show | awk '$3 ~ /^[1-9]+/ {print $3;}'
2010-08-08 23:57:23
User: kerim
Functions: awk route
-1

Find your default gateway and print it directly output.

http://www.bayner.com/

kerim@bayner.com

route -n | awk '$2 ~/[1-9]+/ {print $2;}'
2010-08-08 23:51:07
User: kerim
Functions: awk route
-2

Find your default gateway and print it directly output

http://www.bilgisayarmatematik.com/

kerim@bayner.com

ip route list match 0.0.0.0/0 | cut -d " " -f 3
ip route show dev eth0 | awk '{print $7}'
2009-05-26 20:29:54
User: P17
Functions: awk route
Tags: IP
2
ip address show | grep eth0 | sed '1d' | awk '{print $2}'

does the same, but shows network-prefix.

ip route show dev ppp0 | awk '{ print $7 }'
arp -s $(route -n | awk '/^0.0.0.0/ {print $2}') \ $(arp -n | grep `route -n | awk '/^0.0.0.0/ {print $2}'`| awk '{print $3}')
sudo route add xxx.xxx.xxx.xxx gw 127.0.0.1 lo
2009-02-23 19:58:09
Functions: route sudo
Tags: Security
2

Someone might attack on your system. You can drop attacker IP using IPtables. However, you can use route command to null route unwanted traffic. A null route (also called as blackhole route) is a network route or kernel routing table entry that goes nowhere. Matching packets are dropped (ignored) rather than forwarded, acting as a kind of very limited firewall. The act of using null routes is often called blackhole filtering.

ip route | grep default | awk '{print $3}'
2009-02-16 16:29:03
User: ruedu
Functions: awk grep route
1

This gets you your default route programatically, useful for scripts