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/
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.
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
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:
above line in .bash_profile will give you window title in putty or terminal client when you login to your remote server
The $[...] block in bash and zsh will let you do math.
echo $[6*7]
This is the same as using $((...)), which also works in ksh. Of course, this is a simple, dumb wrapper and doesn't allow floating-point.
unsets variables used by the one-liner
sets up the IFS bash variable to not be affected by whitespace and disables extra glob expansion
uses read to slurp the results of the find command into an array
selects an element of the array at random to be passed as an argument to mplayer
This is the solution to the common mistake made by sudo newbies, since
sudo echo "foo bar" >> /path/to/some/file
does NOT add to the file as root.
Alternatively,
sudo echo "foo bar" > /path/to/some/file
should be replaced by
echo "foo bar" | sudo tee /path/to/some/file
And you can add a >/dev/null in the end if you're not interested in the tee stdout :
echo "foo bar" | sudo tee -a /path/to/some/file >/dev/null
If you are using an xterm emulation capable terminal emulator, such as PuTTY or xterm on Linux desktop, this command will replace the title of that terminal window. I know it is not nice to have seventeen terminals on your desktop with title PuTTY, you can not tell which one is connected to which server and doing what.
Even though the string between the quotes is typed as literals, it needs a little more finesse to make it work. Here is how it is done key-by-key:
echo "( ctrl-v then ctrl-[ )0;Enter_Title_String_Here( ctrl-v then ctrl-g )"( enter )
ctrl-v : means hold down ctrl key and hit v at the same time like you are pasting in windoze ; also please don't type the parentheses, i.e., ( and )
When expanding, bash output the command, so don't be affraid if you type the command.
Here is the details:
First examples:
echo foo bar foobar barfoo
First argument:
echo !$
echo barfoo
barfoo
(Note that typing echo foo bar foobar barfoo && echo !$, bash substitute !$ with $:1)
Last argument:
echo foo bar foobar barfoo && echo !^
echo foo bar foobar barfoo && echo barfoo
foo bar foobar barfoo
barfoo
All the arguments:
echo !*
echo foo bar foobar barfoo
foo bar foobar barfoo
The third argument:
echo foo bar foobar barfoo && echo !:3
echo foo bar foobar barfoo && echo foobar
foo bar foobar barfoo
foobar
You may want to add {} for large numbers: echo !:{11} for example
Now with path:
echo /usr/bin/foobar
/usr/bin/foobar
For the head:
echo !$:h
echo /usr/bin
/usr/bin
And the tail:
echo !$:t
echo foobar
foobar
You also may want to try !:h and !:t or !!3-4 for the third and the fourth (so !!:* == !!:1-$)
seq allows you to format the output thanks to the -f option. This is very useful if you want to rename your files to the same format in order to be able to easily sort for example:
for i in `seq 1 3 10`; do touch foo$i ;done
And
ls foo* | sort -n
foo1
foo10
foo4
foo7
But:
for i in `seq -f %02g 1 3 10`; do touch foo$i ;done
So
ls foo* | sort -n
foo01
foo04
foo07
foo10
This commands lets you generate a random number between the range [$START; $END].
I know its not much but is very useful in time consuming scripts (cron, rc.d, etc).
This command converts a MySQL query directly into a .csv (Comma Seperated Value)-file.
Replace sed regular expressions with perl patterns on the command line.
The sed equivalent is: echo "sed -e"|sed -e 's/sed -e/perl -pe/'
Modify the script for your username and password, and save it as a script. Run the script, and enjoy ./tweet
This works in bash and zsh.
You may also want to alias it, if you need to look at it often...
alias lpath="echo \$PATH | tr : \\\\n"
"\$PATH" to make sure to look at your current $PATH
this is very useful when there is a different network host to determine which are turned on or not
Return the current shell. It is better than print $SHELL which can sometimes return a false value.