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 xeor from sorted by
Terminal - Commands by xeor - 15 results
ls -l /etc/**/*killall
2011-08-30 05:57:49
User: xeor
Functions: ls
9

This command will give you the same list of files as "find /etc/ -name '*killall' | xargs ls -l".

In a simpler format just do 'ls /etc/**/file'.

It uses shell globbing, so it will also work with other commands, like "cp /etc/**/sshd sshd_backup".

sed -i '1s/^/text to prepend\n/' file1
2011-06-25 12:02:11
User: xeor
Functions: sed
Tags: sed replace
2

Using the sed -i (inline), you can replace the beginning of the first line of a file without redirecting the output to a temporary location.

diff rpm_output_from_other_computer <(rpm -qa|sort)
2011-06-25 11:45:15
User: xeor
Functions: diff rpm
0

Description is moved to "Sample output" because the html sanitizer for commandlinefu breaks the examples..

[enter]~?
2011-06-17 18:29:59
User: xeor
Tags: ssh
30

SSH can be controlled trough an ~ escape sequence. Example, to terminate the current ssh connection, type a newline, then the ~ character, and last a . character.

This is useful eg when an ssh connection hangs after you reboot a machine and the connection hangs.

mydir=$(cd $(dirname ${BASH_SOURCE:-$0});pwd)
2011-04-27 16:33:38
User: xeor
Functions: cd dirname
Tags: cd script pwd
0

I submitted a command like this without $0 if $BASH_SOURCE is unset. Therefor, it did only work when using ./script, not using 'sh script'. This version handles both, and will set $mydir in a script to the current working directory. It also works on linux, osx and probably bsd.

current_dir=$(cd $(dirname $BASH_SOURCE);pwd)
2011-04-18 09:24:14
User: xeor
Functions: cd dirname
-2

I think this is the cleanest way of getting the current working directory of a script. It also works on osx, Linux, and probably bsd as well..

shopt -s autocd
2010-12-12 20:53:33
User: xeor
Tags: bash cd bashrc
1

Makes bash-4.x like zsh. Automatic cd into a directory if a command with that name doesnt exists. Ready for your ~/.bashrc file

~.
2010-09-04 15:13:10
User: xeor
Tags: ssh
22

~ is the ssh control character. If you type it after a newline, you can example force close the ssh session.

, ~, then .

If you want more info, type ~? when you are connecting using ssh.

Very useful to kill of any hanging ssh sessions when nothing else works.

cd() { if [ -z "$1" ]; then command cd; else if [ -f "$1" ]; then command cd $(dirname "$1"); else command cd "$1"; fi; fi; }
2010-04-23 19:17:43
User: xeor
Functions: cd command dirname
Tags: cd command
9

This little function will smarten 'cd'. If you try to cd into a file (which I guess we all have done), it cd's into the directory of that file instead.

I had to use nesten if's, to get cd to still work with 'cd' (to get to $HOME), 'cd -' (to get to last directory), and 'cd foo\ bar'.

if cat /proc/cpuinfo | grep " lm " &> /dev/null; then echo "Got 64bit" ; fi
2010-04-10 15:31:58
User: xeor
Functions: cat echo grep
Tags: cpuinfo
4

Check if you have 64bit by looking for "lm" in cpuinfo. lm stands for "long mem". This can also be used without being root.

prefix="10.0.0" && for i in `seq 25`; do ping -c 1 $prefix.$i &> /dev/null && echo "Answer from: $prefix.$i" ; done
2010-04-07 17:17:21
User: xeor
Functions: echo ping
Tags: ping
-2

Not really an easier solution. But an example using && for (if last command returned 0). You can use || for (if last command returned other than 0)..

telnet somehost 1234, <ctrl+5> close
2010-04-05 12:10:21
User: xeor
Functions: telnet
2

You are stuck with testing a tcp port using telnet, and must kill the telnet session from another terminal... Or, press ctrl+5 and write close/quit to force the current connection to close..

lynx --dump http://ip.boa.nu|sed -e 's/^[[:space:]]*//' -e 's/*[[:space:]]$//'|grep -v ^$
2009-11-04 22:23:45
User: xeor
Functions: grep sed
-5

For those of us that still uses lynx :)

wget -O - -q ip.boa.nu
check_dns_no() { for i in $* ; do if `wget -O - -q http://www.norid.no/domenenavnbaser/whois/?query=$i.no | grep "no match" &>/dev/null` ; then echo $i.no "available" ; fi ; sleep 1 ;done }
2009-09-30 21:17:33
User: xeor
Functions: echo grep sleep
Tags: wget dig dns
0

Mostly for Norwegians, but easily adoptable to others. Very handy if you are brainstorming for a new domainname.

Will only display the available ones..

You can usually do this better with dig, but if you dont have dig, or the TLD only have an online service to check with, this will be usefull..