Thursday 16 March 2017

Linux Tutorial : Basic Command Line Skills on Linux Part 1

1 Introduction

If you are like most people, you are probably most familiar with using a Graphical User Interface (GUI) to control your computer. Introduced to the masses by Apple on the Macintosh computer and popularized by Microsoft, a GUI provides an easy, discoverable way to manage your system. Without a GUI, some tools for graphics and video would not be practical.
Prior to the popularity of the GUI, the Command Line Interface (CLI) was the preferred way to control a computer. The CLI relies solely on keyboard input. Everything you want the computer to do is relayed by typing commands rather than clicking on icons.
If you have never used a CLI, at first it may prove challenging because it requires memorizing commands and their options. However, a CLI provides more precise control, greater speed and the ability to easily automate tasks through scripting (see sidebar). Although Linux does have many GUI environments, you will be able to control Linux much more effectively by using the Command Line Interface.

2 Command Line Interface (CLI)

The Command Line Interface (CLI), is a text-based interface to the computer, where the user types in a command and the computer then executes it. The CLI environment is provided by an application on the computer known as a terminal.
The terminal accepts what the user types and passes to a shell. The shell interprets what the user has typed into instructions that can be executed by the operating system. If output is produced by the command, then this text is displayed in the terminal. If problems with the command are encountered, then an error message is displayed.

3 Accessing a Terminal

There are many ways to access a terminal window. Some systems will boot directly to a terminal. This is often the case with servers, as a Graphical User Interface (GUI) can be resource intensive and may not be needed to perform server-based operations.
A good example of a server that doesn't necessarily require a GUI is a web server. Web servers need to run as quickly as possible and a GUI would just slow the system down.
On systems that boot to a GUI, there are commonly two ways to access a terminal, a GUI-based terminal and a virtual terminal:
  • A GUI terminal is a program within the GUI environment that emulates a terminal window. GUI terminals can be accessed through the menu system. For example, on a CentOS machine, you could click on Applications on the menu bar, then System Tools > and, finally, Terminal:
  • A virtual terminal can be run at the same time as a GUI, but requires the user to log in via the virtual terminal before they can execute commands (as they would before accessing the GUI interface). Most systems have multiple virtual terminals that can be accessed by pressing a combination of keys, for example: Ctrl-Alt-F1

Prompt

A terminal window displays a prompt; the prompt appears when no commands are being run and when all command output has been printed to the screen. The prompt is designed to tell the user to enter a command.
The structure of the prompt may vary between distributions, but will typically contain information about the user and the system. Below is a common prompt structure:
sysadmin@localhost:~$
The previous prompt provides the name of the user that is logged in (sysadmin), the name of the system (localhost) and the current directory (~). The ~ symbol is used as shorthand for the user's home directory (typically the home directory for the user is under the /home directory and named after the user account name, for example: /home/sysadmin).

Shell

A shell is the interpreter that translates commands entered by a user into actions to be performed by the operating system. The Linux environment provides many different types of shells, some of which have been around for many years.
The most commonly used shell for Linux distributions is called the BASH shell. It is a shell that provides many advanced features, such as command history, which allows you to easily re-execute previously executed commands.
The BASH shell also has other popular features:
  • Scripting: The ability to place commands in a file and execute the file, resulting in all of the commands being executed. This feature also has some programming features, such as conditional statements and the ability to create functions (AKA, subroutines).
  • Aliases: The ability to create short "nicknames" for longer commands.
  • Variables: Variables are used to store information for the BASH shell. These variables can be used to modify how commands and features work as well as provide vital system information.

Formatting commands

Many commands can be used by themselves with no further input. Some commands require additional input to run properly. This additional input comes in two forms: options and arguments.
The typical format for a command is as follows:
command [options] [arguments]
Options are used to modify the core behavior of a command while arguments are used to provide additional information (such as a filename or a username). Each option and argument is normally separated by a space, although options can often be combined together.
Keep in mind that Linux is case sensitive. Commands, options, arguments, variables and filenames must be entered exactly as shown.
The ls command will provide useful examples. By itself, the ls command will list the files and directories contained in your current working directory:
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
sysadmin@localhost:~$
The ls command will be covered in complete detail in a later chapter. The purpose of introducing this command now is to demonstrate how arguments and options work. At this point you shouldn't worry about what the output of the command is, but rather focus on understanding what an argument and an option is.
An argument can also be passed to the ls command to specify which directory to list the contents of. For example, the command ls /etc/pppwill list the contents of the /etc/pppdirectory instead of the current directory:
sysadmin@localhost:~$ ls /etc/ppp
ip-down.d ip-up.d
sysadmin@localhost:~$
Since the ls command will accept multiple arguments, you can list the contents of multiple directories at once by typing the ls /etc/ppp /etc/sshcommand:
sysadmin@localhost:~$ ls /etc/ppp /etc/ssh
/etc/ppp:
ip-down.d ip-up.d
/etc/ssh:
moduli ssh_host_dsa_key.pub ssh_host_rsa_key sshd_configssh_config
ssh_host_ecdsa_key ssh_host_rsa_key.pub
sysadmin@localhost:~$
ssh_host_dsa_key ssh_host_ecdsa_key.pub ssh_import_id

Working with Options

Options can be used with commands to expand or modify the way a command behaves. Options are often single letters; however, they sometimes will be "words" as well. Typically, older commands use single letters while newer commands use complete words for options. Single-letter options are preceded by a single dash -. Full-word options are preceded by two dashes --.
For example, you can use the -loption with the ls command to display more information about the files that are listed. The ls -l command will list the files contained within the current directory and provide additional information, such as the permissions, the size of the file and other information:
sysadmin@localhost:~$ ls -l
total 0
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Desktop
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Documents
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Downloads
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Music
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Pictures
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Public
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Templates
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Videos
sysadmin@localhost:~$
In most cases, options can be used in conjunction with other options. For example, the ls -l -h or ls -lhcommand will list files with details, but will display the file sizes in human-readable format instead of the default value (bytes):
sysadmin@localhost:~$ ls -l /usr/bin/perl
-rwxr-xr-x 2 root root 10376 Feb 4 2014 /usr/bin/perl
sysadmin@localhost:~$ ls -lh /usr/bin/perl
-rwxr-xr-x 2 root root 11K Feb 4 2014 /usr/bin/perl
sysadmin@localhost:~$
Note that the previous example also demonstrated how you can combine single letter options: -lh . The order of the combined options isn't important.
The -h option also has a full-word form: --human-readable.
Options can often be used with an argument. In fact, some options require their own arguments. You can use options and arguments with the lscommand to list the contents of another directory by executing the ls -l /etc/ppp command:
sysadmin@localhost:~$ ls -l /etc/ppp
total 0
drwxr-xr-x 1 root root 10 Jan 29 2015 ip-down.d
drwxr-xr-x 1 root root 10 Jan 29 2015 ip-up.d
sysadmin@localhost:~$

4 Command history

When you execute a command in a terminal, the command is stored in a "history list". This is designed to make it easy for you to execute the same command later since you won't need to retype the entire command.
To view the history list of a terminal, use the history command:
sysadmin@localhost:~$ date
Sun Nov 1 00:40:28 UTC 2015
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates
Videos
sysadmin@localhost:~$ cal 5 2015
May 2015
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9 10 11 12 13 14 15 16
31
17 18 19 20 21 22 23 24 25 26 27 28 29 30
sysadmin@localhost:~$ history
1 date
2 ls
4 history
3 cal 5 2015
sysadmin@localhost:~$
Pressing the Up Arrow ↑ key will display the previous command on your prompt line. You can press up repeatedly to move back through the history of commands you have run. Pressing the Enter key will run the displayed command again.
When you find the command that you want to execute, you can use the Left arrow ← keys and Right arrow → keys to position the cursor for editing. Other useful keys for editing include the HomeEnd, Backspace and Delete keys.
If you see a command you wish to run in the list that the history command generates, you can execute this command by typing an exclamation point and then the number next to the command, for example:
!3
sysadmin@localhost:~$ history
1 date
2 ls
3 cal 5 2015
sysadmin@localhost:~$ !3
4 history cal 5 2015
Su Mo Tu We Th Fr Sa
May 2015 1 2
17 18 19 20 21 22 23
3 4 5 6 7 8 9 10 11 12 13 14 15 16
sysadmin@localhost:~$
24 25 26 27 28 29 30
31
Some additional history examples:
ExampleMeaning
history 5Show the last five commands from the history list
!!Execute the last command again
!-5Execute the fifth command from the bottom of the history list
!lsExecute the most recent ls command

Continued on next page : Part II
Load disqus comments

0 comments