left-icon

Linux Succinctly®
by Jason Cannon

Previous
Chapter

of
A
A
A

CHAPTER 3

Command Line Interface

Command Line Interface


A shell is a program that accepts commands and instructs the operating system to execute those commands. When you connect to a Linux system over the network, for example, a shell is started and acts as your interface to the system. The shell in this particular case is a command line interpreter. The command-line interface is often referred to by its abbreviation, CLI.

When you connect to a Linux system directly via an attached keyboard and display, you will either be presented with a textual interface or a graphical interface, depending on how that system is configured. In the case of a textual interface, you will have a very similar experience as if you had connected to that system over the network. When you log in, a command line shell is started and you are presented with a prompt.

If you connect to a system that is in graphical mode, you will be interacting with a graphical user interface (GUI). In order to access the command line while logged into a GUI, you will need to start a terminal emulator application. Common terminal emulators include xterm, GNOME Terminal, Konsole, rxvt, and Eterm. The one you choose depends on personal preference and availability, but they all provide the same basic functionality­—access to the command line.

The following demonstrates logging into an Ubuntu Linux server at the command line.

Ubuntu 14.04 LTS linuxsvr tty1

linuxsvr login: jason

Password:

Welcome to Ubuntu 14.04 LTS

jason@linuxsvr:~$

The line jason@linuxsvr:~$ is the command prompt. The default prompt varies from distribution to distribution and shell to shell. There are a variety of shells with the most common and popular one being Bash. All users can customize their shell prompt to their liking. The information provided in this shell prompt includes the username, the server name, and the current directory.

The tilde represents the home directory of the current user which is /home/jason in this example. You can also specify a username after the tilde, in which case it will expand to the home directory of that user. For example, ~john expands to /home/john. No matter where the user's home directory is, ~username will be translated to that directory. In the case of an application user such as www-data, ~www-data expands to /var/www.

The following are examples of various shell prompts.

[jason@linuxsvr /tmp]$

linuxsvr:/home/jason>

jason@linuxsvr:~>

[12:32:19 linuxsvr ~]$

%

>

$

Shell prompts are not limited to a single line. The following example shell prompts span multiple lines.

linuxsvr:[/home/jason]

$

(jason@linuxsvr)-(09:22am-:-12/15)-]-

(~)

[Mon 14/12/15 09:22 EST][pts/3][x86_64]

<jason@linuxsvr:~>

zsh 26 %

linuxsvr | Mon Dec 15 09:22am

~/

In the remainder of examples in this book, the shell prompt will be abbreviated to just the dollar sign, unless displaying the entire prompt provides additional clarity.

Basic Commands

In Linux, commands are case-sensitive and are typically lowercase. Note that items surrounded by square brackets are optional. Let's start with two commands that will allow you to navigate around the system at the command line. They are pwd and cd.

pwd   The pwd command displays the present working directory to your screen. This command allows you to keep track of where you are in the file system hierarchy.

cd [directory]   The cd command changes the current directory to the supplied directory. If cd is executed without specifying a directory, it changes the current directory to your home directory. This is how you navigate around the system.

The following is an example of how the pwd and cd commands can be used. Remember that case matters.

$ pwd

/home/jason

$ Pwd

Pwd: command not found

$ cd /home

$ pwd

/home

$ cd /var/log

$ pwd

/var/log

$ cd

$ pwd

/home/jason

$

The most common way to see the contents of a directory is to use the ls command. To view the contents of a file, use the cat command.

ls   The ls command lists directory contents. Additionally, ls can be used to display information about files and directories including permissions, size, and type.

cat [file]   The cat command concatenates, or displays, files.

$ pwd

/home/jason

$ ls

Desktop  Documents  Downloads  Music  Pictures  to-do.txt

$ cat to-do.txt

This file contains my to-do list.

* Mow the lawn.

* Take over the world.

$ cd Music

$ ls

JohnColtrane

To end your command line session, type exit, logout, or Ctrl-d.

exit, logout, or Ctrl-d   Exits the shell or your current session.

$ exit

logout

Connection to linuxsvr closed.

Command Line Help

The Linux operating system provides built-in documentation. To access these online manuals, also called man pages, use the man command. Man pages are great for looking up the available options for a given command or even finding a command that will help you accomplish the task at hand.

man [command]   The man command displays the online manual for a given command.

Once you have executed the man command, you can navigate through the man page with the arrow keys, as well as the Page Up and Page Down keys. You can also use Enter to move down a line, the Spacebar to move down a page, g to move to the beginning, and capital G to move to the end of the man page. To quit, type q. To learn about even more commands available while viewing man pages, type h for help.

  1. Navigating Man Pages

Action

Key

Move down one line.

Enter, Down Arrow

Move up one line.

Up Arrow

Move down one page.

Spacebar, Page Down

Go to the start or top.

g

Go to the end or bottom.

G

Quit.

q

$ man

What manual page do you want?

$ man ls

LS(1)            User Commands                LS(1)

NAME

     ls - list directory contents

SYNOPSIS

     ls [OPTION]... [FILE]...

DESCRIPTION

     List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

...

 Manual page ls(1) line 1 (press h for help or q to quit)

$

To search the man pages, supply a keyword to the -k option of the man command. If you are looking for a command that will reboot the system, you could search for "reboot."  Once you have a list of man pages that contain that keyword, you can read the documentation for the most promising ones.

$ man -k reboot

grub-reboot (8) - set the default boot entry for GRUB for the next boot only

halt (8)        - reboot or stop the system

poweroff (8)    - reboot or stop the system

reboot (2)      - reboot or enable/disable Ctrl-Alt-Del

reboot (8)      - reboot or stop the system

$ man reboot

NAME

       reboot, halt, poweroff - reboot or stop the system

...

$

Some commands will print a help message when -h or --help is supplied as an argument. Even the man command follows this convention.

$ man -h

Usage: man [OPTION...] [SECTION] PAGE...

  -C, --config-file=FILE     use this user configuration file

  -d, --debug                emit debugging messages

  -D, --default              reset all options to their default values

      --warnings[=WARNINGS]  enable warnings from groff

...

$ man --help

Usage: man [OPTION...] [SECTION] PAGE...

  -C, --config-file=FILE     use this user configuration file

  -d, --debug                emit debugging messages

  -D, --default              reset all options to their default values

      --warnings[=WARNINGS]  enable warnings from groff

...

$ ls --help

Usage: ls [OPTION]... [FILE]...

List information about the FILEs (the current directory by default).

Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.

  -a, --all                  do not ignore entries starting with .

...

$

Given what you know about the Linux directory structure and the documentation that comes with the Linux operating system, you can start exploring commands on your own. For example, list the directory contents of /bin and /usr/bin. Pick out some commands that grab your attention and use the man command to find out what each one of them does.

$ cd /bin

$ ls

awk diff cal cat cp date du echo grep groups less more

$ man date

NAME

       date - print or set the system date and time

...

$ cd /usr/bin

$ ls

clear crontab cut dos2unix find kill mv pstree pwd sed strings touch ...

$ man clear

The output of the preceding ls commands was truncated. In reality, you will likely find dozens of commands in /bin and hundreds in /usr/bin.

Before we move on, I want to share one last basic command that you might find useful. It is the clear command. If you want to a fresh screen to work with, issue the clear command to clear the contents of your screen.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.