Command Line Basics

«« Previous
Next »»

The Power of the Command Line


For this LPI Linux Essentials objective we take a look at the Linux command line basics. Beginning our journey in becoming accustomed to using the CLI command line interface that Linux supplies. It has to be said, that many Windows users never use the command line, and in the same way we some users of Linux never knowing the command line. However, even within Windows we are seeing the evolution from GUI to command line management and it is is important and powerful. This is seen clearly with Microsoft Windows Server 2012 and PowerShell 3. It does not matter which OS you use, the importance of being able to do things repeatedly correct from the command line is always going to have its importance. If you would like to work with Linux servers the command line has to be mastered and we can begin right here, right now!

Linux Command Line Basics


Looking just at the basics we will see how we can open a shell and exit with the exit command or Ctrl + d. These shells can be physical terminals on the server or remote terminals using SSH Connections. If we are using the GUI we can open Pseudo-Terminals in the GUI. Wishing this GUI Terminals we can easily adjust font size with Ctrl + Shift + + and reduce with Ctrl + – . To clear as screen we have the clear command to just ctrl + l.

Note: Ctrl + l is a short-cut key sequence within the bash shell but not all shells, so check the shell that you are using. The output from the command: echo $SHELL will let you know your current shell.

Command Line Basics, LPI

History


We can see how to use commands and space out the arguments and options from commands. All of out commands we type are stored in a history file, .bash_history so they persist even after reboots.

The command history itself can be used to display and clear the command line history for a given user, each user has their own unique .bash_history file which is created in the user’s home directory.

history
history 3
history -c

From the command line examples:

◉ history without any arguments displays the users’ history
◉ history 3 show the last 3 lines of the users’ history
◉ history -c will clear the users’ history

A simple way to get used to the history is just using the up arrow key to scroll through last used commands.



Of course, variables come into play to store information about our session, when reading variables they are prefixed with a $ symbol. Typing $ followed by TAB TAB in quick succession will list all variables as will the env (/usr/bin/env) command; additionally the command env displays the variables and their values.

The $ symbol identifies variables that need to be expanded in the command line. $$ is a special variable that identifies the current process id. The ID of the currently running process. This can be used to find out what when is currently running.

Shell Quotes


As we have seen, spacing out options will make them seem as two options. This means that we can create two directories with the one command:

mkdir dir1 dir2;

Should we want a space in the directory name we can use quotes or the (backslash) character:

mkdir “dir1 dir1”

The above command will create a single directory named dir1[space]dir2. We can start to see the importance that the space has in the steel and we can protect characters with special meaning, such as the space, using quotes.

The supporting video will demonstrate the usage.

Finally we will look at ANDing and ORing command together to allow for some simple command line flow control. For example:

mkdir dir1 && cd dir1

The above command will make the directory and if the command succeeds will enter the directory with the command cd.

id bob || useradd bob

The above example using the double vertical bar or pipe, Ors the two commands together. In this way id the first commands, in the case of there not being a user bob; then, the useradd command will run and we create the user named bob

Command Types


Issuing command at the command line can be easy but often there is more going on than we think. For example using the ls command, more often than not, we run an alias rather than the actual command. We can check this with the command:

type ls

or

type -a ls

As we enter a command we search first for

◉ aliases
◉ functions
◉ keywords
◉ builtin commands
◉ commands

Two more video follow. The first on the basics  of the shell and the second on using shell quotes.




«« Previous
Next »»

0 comments:

Post a Comment