The Command Line (CLI) is the primary user inferface of many Unix-like operating systems. Over the years it has evolved into a powerful thing which will make your life a lot easier if you invest some time to learn it.
This post is based on one of my internal presentations which I created for my teammates at Netsafe. It assumes that you are already familiar with the CLI.
I'll try to summarize stuff that I find most useful and that I wish I had known back when I started using Linux. It will certainly not cover everything as I think that's not even possible. The journey to mastering the CLI is endless anyways. :)
I'm using BASH as my shell, so some things might differ if you use something else.
In order to use the CLI efficiently it's good to know some basic keys to move around and edit text. By default BASH uses Emacs-like keyboard shortcuts. There is also a VI-like mode but I'm not going to cover that.
Please note that most of these key bindings also work in other programs (e.g. mysql) which use the readline library to handle user input. Some of them even work in OSX GUI applications.
Ctrl-aMove the cursor to the start of the line
Ctrl-eMove the cursor to the end of the line
Alt-fMove forward a word
Alt-bMove backward a word
Alt-BackspaceKill the previous word
Alt-dKill to the end of the word
Ctrl-uKill to the beginning of the line
Ctrl-kKill to the end of the line
Ctrl-yPaste the previously killed text
Alt-.Paste the last argument of the previous command (hit again to get older items)
Personally I find
Alt-. super useful. I'm using it several times a
day instead of manually doing copy&paste using mouse or retyping the
Ctrl-rIncrementally search the line history (hit again to get older items)
Ctrl-gAbort an incremental search and restore the original line
Ctrl-pPrevious item in history
Ctrl-nNext item in history
Ctrl-lClear the terminal
In CLI one uses the
cd command to change the current directory. Plain
without any argument switches the current directory to the user's home. It's
the same as typing
There are a few tricks which make jumping around easier.
cd -switches to the previous directory (can be used multiple times to jump back and forth)
CDPATH- By default
cd some-dirchanges to
some-dirlocated in the current directory. If you have a bunch of directories which you visit often, you can make your life easier with
CDPATH. For example, if you keep all your projects in
~/Projects, you can append this path to
cd project-nameto jump to the given project. It doesn't matter what your current directory is at the time you run
If you have the
bash-completion package installed and enabled you
will be able to complete almost anything using the
TAB key. This
includes directory names, commands, command switches, git branches,
server names, environment variables and so on. If you hit
multiple times it will show possible completions.
Thanks kotfic and pkkm (of #emacs fame) for the grammar check. :)