Navigating the Command Line

From docwiki
Jump to: navigation, search


Navigating the Command Line

Before you can use commands on the command line you need to know how to even get there and then it helps to know some keyboard shortcuts for basic editing.



Where to find the Command Line

When you are logged in the graphical environment on a Linux desktop you can open a Terminal window. There are many different versions of it: gnome-terminal, Konsole, rxvt, lxterminal, xterm, st, ... and many more. Basically all do the same: They present you with a terminal window where you text-characters can be displayed and you can enter text.

There are tools where you can connect to remote computer via network and get a text console there.

On a naked Linux system without a graphical environment you are presented with a login prompt and when you log in with username and password you can enter commands there. Linux gives you multiple virtual text consoles there: You can switch between them by Pressing Ctrl-Alt-F1 to Ctrl-Alt-F10. (Ctrl is "Strg" on German Keyboard). (This will often also work when you have a graphical environment.)

Raspberry Pi connecting to Pi Zero via Serial Line

Often your computer also has a serial port. E.g. On your Raspberry Pi, you can use 3 wires (Ground, Rx (receive-data) and Tx (transmit-data) ) to run a terminal connection over that and work without keyboard or monitor connected. This is useful if your network is not working because you have e.g. changed your WiFi password.

By the way: Did you know you also have a Unix shell on your Mac?

The Shell

Now when you have your terminal ready you will be greeted by a so called prompt.

anna@tinbox:~$

Your prompt will look different, as it is configurable and will likely show your own username and the name of your computer. The ~ means that you are in your home directory and you can enter commands after the $

The program that presents you the prompt and that also processes your input and executes your commands is called a shell.

The default shell on a Linux system is the GNU bash. There are other shells you can use. E.g. zsh has many fans.

You can now enter commands. E.g. date:

$ date
Sat 21 Mar 2020 02:39:09 PM CET

You do not type the $ sign. That is part of the promot. When I write a $ sign it indicates that what follows is the command to type, the lines withouth $ are then the output of the command.

Keyboard Shortcuts in the Shell

In order to conveniently enter commands it makes sense to learn some useful keyboard shortcuts that help you edit your commands. The shortcuts can be changed, but here you will learn the default EMACS-Like shortcuts. There are many resources online that offer cheat-sheets. [1][2][3] Here are only the most important ones.


Ctrl + a   Go to the beginning of the line (Home)
Ctrl + e   Go to the End of the line (End)
Ctrl + p   Previous command (Up arrow)
Ctrl + n   Next command (Down arrow)

Ctrl + l   Clear the Screen
Ctrl + k   Deletes from cursor position till end of line

Ctrl + r   Reverse Incremental Search - search in the history by typing
           extra characters that are part of the commands
  
Ctrl + c   Interrupt the currently running process
Ctrl + z   Stop the currently running process (can be continued
           with fg or bg )

Ctrl + d   Logout or End-Of-File

Ctrl + s   Stop - Stops the output
Ctrl + q   Continue the stopped output.
TAB        The Tabulator key is used for command completion.

The TAB key is especially useful for quickly entering commands. If you type the TAB key on a place where you are supposed to enter a command (e.g. on the beginning of the line) then the TAB will complete your command, if it is unique. If it is not unique it will BEEP and then you can press TAB a second time and it will show you a list of all possible completions. (So if you would use the TAB after e.g. typing 'da then you will see date dash and other commands that start with da. It also completes file names, directory names and, if the right modules are installed, even some arguments and options of commands. Getting used to using the TAB key is "key" to feeling at home on the CLI.

Basic Commands

So in order to play with the keyboard shortcuts above here are some basic commands: Here are the commands (after the $) and what they do (after the #)

$ date              # shows the current date
$ echo hello world  # echo the text
$ who               # who is logged in
$ sleep 10          # sleep (do nothing) for 10 seconds
$ history           # shows all the commands you typed so far
$ hostname          # prints the name of your machine

Exercises

Try out a few of the commands above and also try to navigate using the keyboard shortcuts mentioned above. Especially try the TAB key.