0% found this document useful (0 votes)
36 views43 pages

Learning The Shell

The document discusses the shell and terminal programs in Linux. It introduces the pwd, cd, and ls commands, which print the working directory, change directories, and list files. It covers basic file system navigation and pathnames.

Uploaded by

Robel Dekebo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views43 pages

Learning The Shell

The document discusses the shell and terminal programs in Linux. It introduces the pwd, cd, and ls commands, which print the working directory, change directories, and list files. It covers basic file system navigation and pathnames.

Uploaded by

Robel Dekebo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Previous | Contents | Next

What is "the Shell"?


Simply put, the shell is a program that takes commands from the keyboard and gives
them to the operating system to perform. In the old days, it was the only user
interface available on a Unix-like system such as Linux. Nowadays, we have graphical
user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell.

On most Linux systems a program called bash (which stands for Bourne Again SHell,
an enhanced version of the original Unix shell program, sh, written by Steve Bourne)
acts as the shell program. Besides bash, there are other shell programs available for
Linux systems. These include: ksh, tcsh and zsh.

What's a "Terminal?"
It's a program called a terminal emulator. This is a program that opens a window and
lets you interact with the shell. There are a bunch of different terminal emulators we
can use. Some Linux distributions install several. These might include gnome-
terminal, konsole, xterm, rxvt, kvt, nxterm, and eterm.

Starting a Terminal
Window managers usually have a way to launch a terminal from the menu. Look
through the list of programs to see if anything looks like a terminal emulator. While
there are a number of different terminal emulators, they all do the same thing. They
give us access to a shell session. You will probably develop a preference for one,
based on the different bells and whistles it provides.

Testing the Keyboard


OK, let's try some typing. Bring up a terminal window. The first thing we should see is
a shell prompt that contains our user name and the name of the machine followed by
a dollar sign. Something like this:

[me@linuxbox me]$

Excellent! Now type some nonsense characters and press the enter key.

[me@linuxbox me]$ kdkjflajfks

If all went well, we should have gotten an error message complaining that it cannot
understand the command:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ kdkjflajfks
bash: kdkjflajfks: command not found

Wonderful! Now press the up-arrow key. Watch how our previous command
"kdkjflajfks" returns. Yes, we have command history. Press the down-arrow and we
get the blank line again.

Recall the "kdkjflajfks" command using the up-arrow key if needed. Now, try the left
and right-arrow keys. We can position the text cursor anywhere in the command line.
This allows us to easily correct mistakes.

You're not operating as root, are you?


If the last character of your shell prompt is # rather than $, you are operating as
the superuser. This means that you have administrative privileges. This can be
dangerous, since you are able to delete or overwrite any file on the system.
Unless you absolutely need administrative privileges, do not operate as the
superuser.

Using the Mouse


Even though the shell is a command line interface, the mouse is still handy.

Besides using the mouse to scroll the contents of the terminal window, we can can use
it to copy text. Drag the mouse over some text (for example, "kdkjflajfks" right here
on the browser window) while holding down the left button. The text should highlight.
Release the left button and move the mouse pointer to the terminal window and press
the middle mouse button (alternately, press both the left and right buttons at the
same time when working on a touch pad). The text we highlighted in the browser
window should be copied into the command line.

Further Reading
The Wikipedia entry for Steve Bourne, developer of the original Bourne shell
The Wikipedia article on the Unix shell, the place where all this fun got started
The "Power Terminals" Adventure

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next

Navigation
In this lesson, we will introduce our first three commands: pwd (print working
directory), cd (change directory), and ls (list files and directories).

Those new to the command line will need to pay close attention to this lesson since
the concepts will take some getting used to.

File System Organization


Like Windows, the files on a Linux system are arranged in what is called a hierarchical
directory structure. This means that they are organized in a tree-like pattern of
directories (called folders in other systems), which may contain files and
subdirectories. The first directory in the file system is called the root directory. The
root directory contains files and subdirectories, which contain more files and
subdirectories and so on and so on.

Most graphical environments include a file manager program used to view and
manipulate the contents of the file system. Often we will see the file system

represented like this:

One important difference between Windows and Unix-like operating systems such as
Linux is that Linux does not employ the concept of drive letters. While Windows drive
letters split the file system into a series of different trees (one for each device), Linux
always has a single tree. Different storage devices may be different branches of the
tree, but there is always just a single tree.

pwd
Since the command line interface cannot provide graphic pictures of the file system
structure, we must have a different way of representing it. To do this, think of the file

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
system tree as a maze, and that we are standing in it. At any given moment, we are
located in a single directory. Inside that directory, we can see its files and the pathway
to its parent directory and the pathways to the subdirectories of the directory in which
we are standing.

The directory we are standing in is called the working directory. To see the name of
the working directory, we use the pwd command.

[me@linuxbox me]$ pwd


/home/me

When we first log on to our Linux system, the working directory is set to our home
directory. This is where we put our files. On most systems, the home directory will be
called /home/user_name, but it can be anything according to the whims of the system
administrator.

To list the files in the working directory, we use the ls command.

[me@linuxbox me]$ ls
Desktop Downloads foo.txt Pictures Templates
Documents examples.desktop Music Public Videos

We will come back to ls in the next lesson. There are a lot of fun things you can do
with it, but we have to talk about pathnames and directories a bit first.

cd
To change the working directory (where we are standing in the maze) we use the cd
command. To do this, we type cd followed by the pathname of the desired working
directory. A pathname is the route we take along the branches of the tree to get to the
directory we want. Pathnames can be specified two different ways; absolute
pathnames or relative pathnames. Let's look with absolute pathnames first.

An absolute pathname begins with the root directory and follows the tree branch by
branch until the path to the desired directory or file is completed. For example, there
is a directory on your system in which most programs are installed. The pathname of
the directory is /usr/bin. This means from the root directory (represented by the
leading slash in the pathname) there is a directory called "usr" which contains a
directory called "bin".

Let's try this out:

me@linuxbox me]$ cd /usr/bin


me@linuxbox bin]$ pwd
/usr/bin
me@linuxbox bin]$ ls
'[' mshortname
2to3-2.7 mshowfat
411toppm mtools
a2ps mtoolstest
a2ps-lpr-wrapper mtr

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
aa-enabled mtrace
aa-exec mtr-packet
aclocal mtvtoppm
aclocal-1.15 mtype
aconnect mutter
acpi_listen mxtar
add-apt-repository mzip
addpart namei

and many more...

Now we can see that we have changed the current working directory to /usr/bin and
that it is full of files. Notice how the shell prompt has changed? As a convenience, it is
usually set up to display the name of the working directory.

Where an absolute pathname starts from the root directory and leads to its
destination, a relative pathname starts from the working directory. To do this, it uses a
couple of special notations to represent relative positions in the file system tree. These
special notations are "." (dot) and ".." (dot dot).

The "." notation refers to the working directory itself and the ".." notation refers to the
working directory's parent directory. Here is how it works. Let's change the working
directory to /usr/bin again:

me@linuxbox me]$ cd /usr/bin


me@linuxbox bin]$ pwd
/usr/bin

O.K., now let's say that we wanted to change the working directory to the parent of
/usr/bin which is /usr. We could do that two different ways. First, with an absolute
pathname:

me@linuxbox bin]$ cd /usr


me@linuxbox usr]$ pwd
/usr

Or, with a relative pathname:

me@linuxbox bin]$ cd ..
me@linuxbox usr]$ pwd
/usr

Two different methods with identical results. Which one should we use? The one that
requires the least typing!

Likewise, we can change the working directory from /usr to /usr/bin in two different
ways. First using an absolute pathname:

me@linuxbox usr]$ cd /usr/bin


me@linuxbox bin]$ pwd
/usr/bin

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Or, with a relative pathname:

me@linuxbox usr]$ cd ./bin


me@linuxbox bin]$ pwd
/usr/bin

Now, there is something important that we must point out here. In most cases, we
can omit the "./". It is implied. Typing:

me@linuxbox usr]$ cd bin

would do the same thing. In general, if we do not specify a pathname to something,


the working directory will be assumed. There is one important exception to this, but
we won't get to that for a while.

A Few Shortcuts
If we type cd followed by nothing, cd will change the working directory to our home
directory.

A related shortcut is to type cd ~user_name. In this case, cd will change the working
directory to the home directory of the specified user.

Typing cd - changes the working directory to the previous one.

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next

Looking Around
Now that we know how to move from working directory to working directory, we're
going to take a tour of our Linux system and, along the way, learn some things about
what makes it tick. But before we begin, we have to learn about some tools that will
come in handy during our journey. These are:

ls (list files and directories)


less (view text files)
file (classify a file's contents)

ls
The ls command is used to list the contents of a directory. It is probably the most
commonly used Linux command. It can be used in a number of different ways. Here
are some examples:

Command Result

ls List the files in the working directory

ls /bin List the files in the /bin directory (or any other directory we
care to specify)

ls -l List the files in the working directory in long format

ls -l /etc /bin
List the files in the /bin directory and the /etc directory in
long format

ls -la .. List all files (even ones with names beginning with a period
character, which are normally hidden) in the parent of the
working directory in long format
Examples of the ls command

These examples also point out an important concept about commands. Most
commands operate like this:

command -options arguments

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
where command is the name of the command, -options is one or more adjustments to
the command's behavior, and arguments is one or more "things" upon which the
command operates.

In the case of ls, we see that ls is the name of the command, and that it can have
one or more options, such as -a and -l, and it can operate on one or more files or
directories.

A Closer Look at Long Format

If we use the -l option with ls, you will get a file listing that contains a wealth of
information about the files being listed. Here's an example:

-rw------- 1 me me 576 Apr 17 2019 weather.txt


drwxr-xr-x 6 me me 1024 Oct 9 2019 web_page
-rw-rw-r-- 1 me me 276480 Feb 11 20:41 web_site.tar
-rw------- 1 me me 5743 Dec 16 2018 xmas_file.txt

---------- ------- ------- -------- ------------ -------------


| | | | | |
| | | | | File Name
| | | | |
| | | | +--- Modification Time
| | | |
| | | +------------- Size (in bytes)
| | |
| | +----------------------- Group
| |
| +-------------------------------- Owner
|
+---------------------------------------------- File Permissions

File Name
The name of the file or directory.
Modification Time
The last time the file was modified. If the last modification occurred more than
six months in the past, the date and year are displayed. Otherwise, the time of
day is shown.
Size
The size of the file in bytes.
Group
The name of the group that has file permissions in addition to the file's owner.
Owner
The name of the user who owns the file.
File Permissions

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
A representation of the file's access permissions. The first character is the type of
file. A "-" indicates a regular (ordinary) file. A "d" indicates a directory. The
second set of three characters represent the read, write, and execution rights of
the file's owner. The next three represent the rights of the file's group, and the
final three represent the rights granted to everybody else. We'll discuss this in
more detail in a later lesson.

less
less is a program that lets us view text files. This is very handy since many of the
files used to control and configure Linux are human readable.

The less program is invoked by simply typing:

less text_file

This will display the file.

Controlling less

Once started, less will display the text file one page at a time. We can use the Page
Up and Page Down keys to move through the text file. To exit less, we type "q". Here
are some commands that less will accept:

Command Action

Page Up or b Scroll back one page

Page Down or Scroll forward one page


space

G Go to the end of the text file

1G Go to the beginning of the text file

/characters Search forward in the text file for an occurrence of the specified
characters

n Repeat the previous search

h Display a complete list less commands and options

q Quit
Keyboard commands for the less program

file

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
As we wander around our Linux system, it is helpful to determine what kind of data a
file contains before we try to view it. This is where the file command comes in. file
will examine a file and tell us what kind of file it is.

To use the file program, we just type:

file name_of_file

The file program can recognize most types of files, such as:

File Type Description Viewable as text?

ASCII text The name says it all yes

Bourne-Again shell A bash script yes


script text

ELF 64-bit LSB An executable binary program no


executable

ELF 64-bit LSB A shared library no


shared object

GNU tar archive A tape archive file. A common way of no, use tar tvf to
storing groups of files. view listing.

gzip compressed An archive compressed with gzip no


data

HTML document A web page yes


text

JPEG image data A compressed JPEG image no

PostScript A PostScript file yes


document text

Zip archive data An archive compressed with zip no


Various kinds of files

While it may seem that most files cannot be viewed as text, a surprising number can
be. This is especially true of the important configuration files. During our adventure
we will see that many features of the operating system are controlled by text
configuration files and shell scripts. In Linux, there are no secrets!

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next

A Guided Tour
It's time to take our tour. The table below lists some interesting places to explore. This
is by no means a complete list, but it should prove to be an interesting adventure. For
each of the directories listed below, do the following:

cd into each directory.


Use ls to list the contents of the directory.
If there is an interesting file, use the file command to determine its contents.
For text files, use less to view them.

Directory Description

/ The root directory where the file system begins. The root
directory will probably contain only subdirectories.

/boot This is where the Linux kernel and boot loader files are kept.
The kernel is a file called vmlinuz.

/etc The /etc directory contains the configuration files for the
system. All of the files in /etc should be text files. Some
points of interest are:

/etc/passwd
The passwd file contains the essential information for
each user. This is where user accounts are defined.
/etc/fstab
The fstab file contains a table of devices that get
mounted when the system boots. This file defines the
system's disk drives.
/etc/hosts
This file lists the network host names and IP addresses
that are intrinsically known to the system.
/etc/init.d
This directory contains the scripts that start various
system services at boot time.

/bin, /usr/bin These two directories contain most of the programs for the
system. The /bin directory has the essential programs that
the system requires to operate, while /usr/bin contains
applications for the system's users.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
/sbin, /usr/sbin The sbin directories contain programs for system
administration, mostly for use by the superuser.

/usr The /usr directory contains a variety of things that support


user applications. Some highlights:

/usr/share/X11
Support files for the X Window system
/usr/share/dict
Dictionaries for the spelling checker. Yes, Linux comes
with a spelling checker. See look and aspell.
/usr/share/doc
Various documentation files in a variety of formats.
/usr/share/man
The man pages are kept here.

/usr/local /usr/local and its subdirectories are used for the installation
of software and other files for use on the local machine. What
this really means is that software that is not part of the
official distribution (which usually goes in /usr/bin) goes
here.

When you find interesting programs to install on your system,


they should be installed in one of the /usr/local directories.
Most often, the directory of choice is /usr/local/bin.

/var The /var directory contains files that change as the system is
running. This includes:

/var/log
Directory that contains log files. These are updated as
the system runs. It's a good idea to view the files in this
directory from time to time, to monitor the health of
your system.
/var/spool
This directory is used to hold files that are queued for
some process, such as mail messages and print jobs.
When a user's mail first arrives on the local system
(assuming it has local mail, a rare occurrence on modern
machines that are not mail servers), the messages are
first stored in /var/spool/mail

/lib The shared libraries (similar to DLLs in that other operating


system) are kept here.

/home /home is where users keep their personal work. In general,


this is the only place users are allowed to write files. This
keeps things nice and clean :-)

/root This is the superuser's home directory.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
/tmp /tmp is a directory in which programs can write their
temporary files.

/dev The /dev directory is a special directory, since it does not


really contain files in the usual sense. Rather, it contains
devices that are available to the system. In Linux (like Unix),
devices are treated like files. You can read and write devices
as though they were files. For example /dev/fd0 is the first
floppy disk drive, /dev/sda is the first hard drive. All the
devices that the kernel understands are represented here.

/proc The /proc directory is also special. This directory does not
contain files. In fact, this directory does not really exist at all.
It is entirely virtual. The /proc directory contains little peep
holes into the kernel itself. There are a group of numbered
entries in this directory that correspond to all the processes
running on the system. In addition, there are a number of
named entries that permit access to the current configuration
of the system. Many of these entries can be viewed. Try
viewing /proc/cpuinfo. This entry will tell you what the
kernel thinks of the system's CPU.

/media Finally, we come to /media, a normal directory which is used


in a special way. The /media directory is used for mount
points. As we learned in the second lesson, the different
physical storage devices (like hard disk drives) are attached
to the file system tree in various places. This process of
attaching a device to the tree is called mounting. For a device
to be available, it must first be mounted.

When your system boots, it reads a list of mounting


instructions in the /etc/fstab file, which describes which
device is mounted at which mount point in the directory tree.
This takes care of the hard drives, but we may also have
devices that are considered temporary, such as optical disks
and USB storage devices. Since these are removable, they do
not stay mounted all the time. The /media directory is used
by the automatic device mounting mechanisms found in
modern desktop oriented Linux distributions. To see what
devices and mount points are used, type mount.
Interesting directories and their contents

Further Reading
To learn more about the organization of the Linux filesystem, consult the
Filesystem Hierarchy Standard

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next

Manipulating Files
This lesson will introduce the following commands:

cp - copy files and directories


mv - move or rename files and directories
rm - remove files and directories
mkdir - create directories

These four commands are among the most frequently used Linux commands. They
are the basic commands for manipulating both files and directories.

Now, to be frank, some of the tasks performed by these commands are more easily
done with a graphical file manager. With a file manager, you can drag and drop a file
from one directory to another, cut and paste files, delete files, etc. So why use these
old command line programs?

The answer is power and flexibility. While it is easy to perform simple file
manipulations with a graphical file manager, complicated tasks can be easier with the
command line programs. For example, how would you copy all the HTML files from
one directory to another, but only copy files that did not exist in the destination
directory or were newer than the versions in the destination directory? Pretty hard
with with a file manager. Pretty easy with the command line:

[me@linuxbox me]$ cp -u *.html destination

Wildcards
Before we begin with our commands, we'll first look at a shell feature that makes
these commands so powerful. Since the shell uses filenames so much, it provides
special characters to help you rapidly specify groups of filenames. These special
characters are called wildcards. Wildcards allow you to select filenames based on
patterns of characters. The table below lists the wildcards and what they select:

Wildcard Meaning

* Matches any characters

? Matches any single character

[characters] Matches any character that is a member of the set characters. The
set of characters may also be expressed as a POSIX character

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
class such as one of the following:
[:alnum:] Alphanumeric characters

[:alpha:] Alphabetic characters

[:digit:] Numerals

[:upper:] Uppercase alphabetic characters

[:lower:] Lowercase alphabetic characters


POSIX Character Classes

[!characters] Matches any character that is not a member of the set characters
Summary of wildcards and their meanings

Using wildcards, it is possible to construct very sophisticated selection criteria for


filenames. Here are some examples of patterns and what they match:

Pattern Matches

*
All filenames

g*
All filenames that begin with the character "g"

b*.txt
All filenames that begin with the character "b" and end with the
characters ".txt"

Data???
Any filename that begins with the characters "Data" followed by
exactly 3 more characters

[abc]*
Any filename that begins with "a" or "b" or "c" followed by any
other characters

[[:upper:]]*
Any filename that begins with an uppercase letter. This is an
example of a character class.

BACKUP.
[[:digit:]] Another example of character classes. This pattern matches any

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[[:digit:]] filename that begins with the characters "BACKUP." followed by
exactly two numerals.

*[!
[:lower:]] Any filename that does not end with a lowercase letter.

Examples of wildcard matching

We can use wildcards with any command that accepts filename arguments.

cp
The cp program copies files and directories. In its simplest form, it copies a single file:

[me@linuxbox me]$ cp file1 file2

It can also be used to copy multiple files (and/or directories) to a different directory:

[me@linuxbox me]$ cp file... directory

A note on notation: ... signifies that an item can be repeated one or more times.

Other useful examples of cp and its options include:

Command Results

cp file1 file2 Copies the contents of file1 into file2. If file2 does not exist,
it is created; otherwise, file2 is silently overwritten
with the contents of file1.

cp -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, if file2 exists, the user is prompted before it is
overwritten with the contents of file1.

cp file1 dir1 Copy the contents of file1 (into a file named file1) inside of
directory dir1.

cp -R dir1 dir2 Copy the contents of the directory dir1. If directory dir2 does
not exist, it is created. Otherwise, it creates a directory
named dir1 within directory dir2.
Examples of the cp command

mv
The mv command moves or renames files and directories depending on how it is used.
It will either move one or more files to a different directory, or it will rename a file or
directory. To rename a file, it is used like this:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ mv filename1 filename2

To move files (and/or directories) to a different directory:

[me@linuxbox me]$ mv file... directory

Examples of mv and its options include:

Command Results

mv file1 file2 If file2 does not exist, then file1 is renamed file2. If file2
exists, its contents are silently replaced with the
contents of file1.

mv -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, if file2 exists, the user is prompted before it is
overwritten with the contents of file1.

mv file1 file2 dir1 The files file1 and file2 are moved to directory dir1. If dir1
does not exist, mv will exit with an error.

mv dir1 dir2 If dir2 does not exist, then dir1 is renamed dir2. If dir2
exists, the directory dir1 is moved within directory dir2.
Examples of the mv command

rm
The rm command removes (deletes) files and directories.

[me@linuxbox me]$ rm file...

Using the recursive option (-r), rm can also be used to delete directories:

[me@linuxbox me]$ rm -r directory...

Examples of rm and its options include:

Command Results

rm file1 file2 Delete file1 and file2.

rm -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, the user is prompted before each file is deleted.

rm -r dir1 dir2 Directories dir1 and dir2 are deleted along with all of their
contents.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Examples of the rm command

Be careful with rm!


Linux does not have an undelete command. Once you delete something with rm,
it's gone. You can inflict terrific damage on your system with rm if you are not
careful, particularly with wildcards.

Before you use rm with wildcards, try this helpful trick: construct your
command using ls instead. By doing this, you can see the effect of your wildcards
before you delete files. After you have tested your command with ls, recall the
command with the up-arrow key and then substitute rm for ls in the command.

mkdir
The mkdir command is used to create directories. To use it, you simply type:

[me@linuxbox me]$ mkdir directory...

Using Commands with Wildcards


Since the commands we have covered here accept multiple file and directories names
as arguments, you can use wildcards to specify them. Here are a few examples:

Command Results

cp *.txt text_files Copy all files in the current working directory with
names ending with the characters ".txt" to an existing
directory named text_files.

mv dir1 ../*.bak dir2 Move the subdirectory dir1 and all the files ending in
".bak" in the current working directory's parent
directory to an existing directory named dir2.

rm *~ Delete all files in the current working directory that end


with the character "~". Some applications create
backup files using this naming scheme. Using this
command will clean them out of a directory.
Command examples using wildcards

Further Reading
Chapter 4 of The Linux Command Line covers this topic in more detail

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next

Working with Commands


Up until now, we have seen a number of commands and their mysterious options and
arguments. In this lesson, we will try to remove some of that mystery. We will
introduce the following commands.

type - Display information about command type


which - Locate a command
help - Display reference page for shell builtin
man - Display an on-line command reference

What are "Commands?"


Commands can be one of 4 different kinds:

1. An executable program like all those files we saw in /usr/bin. Within this
category, programs can be compiled binaries such as programs written in C and
C++, or programs written in scripting languages such as the shell, Perl, Python,
Ruby, etc.
2. A command built into the shell itself. bash provides a number of commands
internally called shell builtins. The cd command, for example, is a shell builtin.
3. A shell function. These are miniature shell scripts incorporated into the
environment. We will cover configuring the environment and writing shell
functions in later lessons, but for now, just be aware that they exist.
4. An alias. Commands that we can define ourselves, built from other commands.
This will be covered in a later lesson.

Identifying Commands
It is often useful to know exactly which of the four kinds of commands is being used
and Linux provides a couple of ways to find out.

type

The type command is a shell builtin that displays the kind of command the shell will
execute, given a particular command name. It works like this:

type command

where “command” is the name of the command we want to examine. Here are some
examples:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ type type
type is a shell builtin
[me@linuxbox me]$ type ls
s is aliased to `ls --color=auto'
[me@linuxbox me]$ type cp
cp is /bin/cp

Here we see the results for three different commands. Notice that the one for ls and
how the ls command is actually an alias for the ls command with the “-- color=auto”
option added. Now we know why the output from ls is displayed in color!

which

Sometimes there is more than one version of an executable program installed on a


system. While this is not very common on desktop systems, it's not unusual on large
servers. To determine the exact location of a given executable, the which command is
used:

[me@linuxbox me]$ which ls


/bin/ls

which only works for executable programs, not builtins nor aliases that are substitutes
for actual executable programs.

Getting Command Documentation


With this knowledge of what a command is, we can now search for the documentation
available for each kind of command.

help

bash has a built-in help facility available for each of the shell builtins. To use it, type
“help” followed by the name of the shell builtin. Optionally, we can add the -m option
to change the format of the output. For example:

[me@linuxbox me]$ help -m cd


NAME
cd - Change the shell working directory.

SYNOPSIS
cd [-L|-P] [dir]

DESCRIPTION
Change the shell working directory.

Change the current directory to DIR. The default DIR is the value o
HOME shell variable.

The variable CDPATH defines the search path for the directory contai
DIR. Alternative directory names in CDPATH are separated by a colon
A null directory name is the same as the current directory. If DIR

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
with a slash (/), then CDPATH is not used.

If the directory is not found, and the shell option `cdable_vars' is


the word is assumed to be a variable name. If that variable has a
its value is used for DIR.

Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbo
links

The default is to follow symbolic links, as if `-L' were specified.

Exit Status:
Returns 0 if the directory is changed; non-zero otherwise.

SEE ALSO
bash(1)

IMPLEMENTATION
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.

A note on notation: When square brackets appear in the description of a command's


syntax, they indicate optional items. A vertical bar character indicates mutually
exclusive items. In the case of the cd command above:

cd [-L|-P] [dir]

This notation says that the command cd may be followed optionally by either a “-L” or
a “-P” and further, optionally followed by the argument “dir”.

--help

Many executable programs support a “--help” option that displays a description of the
command's supported syntax and options. For example:

[me@linuxbox me]$ mkdir --help


Usage: mkdir [OPTION] DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options
too.

-Z, --context=CONTEXT (SELinux) set security context to CONTEXT


-m, --mode=MODE set file mode (as in chmod), not a=rwx – umask
-p, --parents no error if existing, make parent directories as
needed
-v, --verbose print a message for each created directory
--help display this help and exit
--version output version information and exit

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Some programs don't support the “--help” option, but try it anyway. Often it results in
an error message that will reveal similar usage information.

man

Most executable programs intended for command line use provide a formal piece of
documentation called a manual or man page. A special paging program called man is
used to view them. It is used like this:

man program

where “program” is the name of the command to view. Man pages vary somewhat in
format but generally contain a title, a synopsis of the command's syntax, a description
of the command's purpose, and a listing and description of each of the command's
options. Man pages, however, do not usually include examples, and are intended as a
reference, not a tutorial. Let's try viewing the man page for the ls command:

[me@linuxbox me]$ man ls

On most Linux systems, man uses less to display the manual page, so all of the
familiar less commands work while displaying the page.

README and Other Documentation Files

Many software packages installed on your system have documentation files residing in
the /usr/share/doc directory. Most of these are stored in plain text format and can
be viewed with less. Some of the files are in HTML format and can be viewed with a
web browser. We may encounter some files ending with a “.gz” extension. This
indicates that they have been compressed with the gzip compression program. The
gzip package includes a special version of less called zless that will display the
contents of gzip-compressed text files.

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next

I/O Redirection
In this lesson, we will explore a powerful feature used by command line programs
called input/output redirection. As we have seen, many commands such as ls print
their output on the display. This does not have to be the case, however. By using
some special notations we can redirect the output of many commands to files,
devices, and even to the input of other commands.

Standard Output
Most command line programs that display their results do so by sending their results
to a facility called standard output. By default, standard output directs its contents to
the display. To redirect standard output to a file, the ">" character is used like this:

[me@linuxbox me]$ ls > file_list.txt

In this example, the ls command is executed and the results are written in a file
named file_list.txt. Since the output of ls was redirected to the file, no results
appear on the display.

Each time the command above is repeated, file_list.txt is overwritten from the
beginning with the output of the command ls. To have the new results appended to
the file instead, we use ">>" like this:

[me@linuxbox me]$ls >> file_list.txt

When the results are appended, the new results are added to the end of the file, thus
making the file longer each time the command is repeated. If the file does not exist
when we attempt to append the redirected output, the file will be created.

Standard Input
Many commands can accept input from a facility called standard input. By default,
standard input gets its contents from the keyboard, but like standard output, it can be
redirected. To redirect standard input from a file instead of the keyboard, the "<"
character is used like this:

[me@linuxbox me]$ sort < file_list.txt

In the example above, we used the sort command to process the contents of
file_list.txt. The results are output on the display since the standard output was
not redirected. We could redirect standard output to another file like this:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ sort < file_list.txt > sorted_file_list.txt

As we can see, a command can have both its input and output redirected. Be aware
that the order of the redirection does not matter. The only requirement is that the
redirection operators (the "<" and ">") must appear after the other options and
arguments in the command.

Pipelines
The most useful and powerful thing we can do with I/O redirection is to connect
multiple commands together to form what are called pipelines. With pipelines, the
standard output of one command is fed into the standard input of another. Here is a
very useful example:

[me@linuxbox me]$ ls -l | less

In this example, the output of the ls command is fed into less. By using this "|
less" trick, we can make any command have scrolling output.

By connecting commands together, we can accomplish amazing feats. Here are some
examples to try:

Command What it does

ls -lt | head Displays the 10 newest files in the current


directory.

du | sort -nr Displays a list of directories and how much


space they consume, sorted from the largest
to the smallest.

find . -type f -print | wc -l Displays the total number of files in the


current working directory and all of its
subdirectories.
Examples of commands used together with pipelines

Filters
One kind of program frequently used in pipelines is called a filter. Filters take standard
input and perform an operation upon it and send the results to standard output. In
this way, they can be combined to process information in powerful ways. Here are
some of the common programs that can act as filters:

Program What it does

sort Sorts standard input then outputs the sorted result on standard output.

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
uniq Given a sorted stream of data from standard input, it removes
duplicate lines of data (i.e., it makes sure that every line is unique).

grep Examines each line of data it receives from standard input and outputs
every line that contains a specified pattern of characters.

fmt Reads text from standard input, then outputs formatted text on
standard output.

pr Takes text input from standard input and splits the data into pages with
page breaks, headers and footers in preparation for printing.

head Outputs the first few lines of its input. Useful for getting the header of
a file.

tail Outputs the last few lines of its input. Useful for things like getting the
most recent entries from a log file.

tr Translates characters. Can be used to perform tasks such as


upper/lowercase conversions or changing line termination characters
from one type to another (for example, converting DOS text files into
Unix style text files).

sed Stream editor. Can perform more sophisticated text translations than
tr.

awk An entire programming language designed for constructing filters.


Extremely powerful.
Common filter commands

Further Reading
Chapter 6 of The Linux Command Line covers this topic in more detail.
Chapters 19 through 21 of The Linux Command Line provide an in-depth look at
the text processing tools available in Linux.
To learn more about the AWK programming language, consider the AWK
adventure.

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next

Expansion
Each time we type a command line and press the enter key, bash performs several
processes upon the text before it carries out our command. We have seen a couple of
cases of how a simple character sequence, for example “*”, can have a lot of meaning
to the shell. The process that makes this happen is called expansion. With expansion,
we type something and it is expanded into something else before the shell acts upon
it. To demonstrate what we mean by this, let's take a look at the echo command. echo
is a shell builtin that performs a very simple task. It prints out its text arguments on
standard output:

[me@linuxbox me]$ echo this is a test


this is a test

That's pretty straightforward. Any argument passed to echo gets displayed. Let's try
another example:

[me@linuxbox me]$ echo *


Desktop Documents ls-output.txt Music Pictures Public Templates Videos

So what just happened? Why didn't echo print “*”? As we recall from our work with
wildcards, the “*” character means match any characters in a filename, but what we
didn't see in our original discussion was how the shell does that. The simple answer is
that the shell expands the “*” into something else (in this instance, the names of the
files in the current working directory) before the echo command is executed. When
the enter key is pressed, the shell automatically expands any qualifying characters on
the command line before the command is carried out, so the echo command never
saw the “*”, only its expanded result. Knowing this, we can see that echo behaved as
expected.

Pathname Expansion
The mechanism by which wildcards work is called pathname expansion. If we try some
of the techniques that we employed in our earlier lessons, we will see that they are
really expansions. Given a home directory that looks like this:

[me@linuxbox me]$ ls
Desktop
ls-output.txt
Documents Music
Pictures
Public

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Templates
Videos

we could carry out the following expansions:

[me@linuxbox me]$ echo D*


Desktop Documents

and:

[me@linuxbox me]$ echo *s


Documents Pictures Templates Videos

or even:

[me@linuxbox me]$ echo [[:upper:]]*


Desktop Documents Music Pictures Public Templates Videos

and looking beyond our home directory:

[me@linuxbox me]$ echo /usr/*/share


/usr/kerberos/share /usr/local/share

Tilde Expansion
As we recall from our introduction to the cd command, the tilde character (“~”) has a
special meaning. When used at the beginning of a word, it expands into the name of
the home directory of the named user, or if no user is named, the home directory of
the current user:

[me@linuxbox me]$ echo ~


/home/me

If user “foo” has an account, then:

[me@linuxbox me]$ echo ~foo


/home/foo

Arithmetic Expansion
The shell allows arithmetic to be performed by expansion. This allow us to use the
shell prompt as a calculator:

[me@linuxbox me]$ echo $((2 + 2))


4

Arithmetic expansion uses the form:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
$((expression))

where expression is an arithmetic expression consisting of values and arithmetic


operators.

Arithmetic expansion only supports integers (whole numbers, no decimals), but can
perform quite a number of different operations.

Spaces are not significant in arithmetic expressions and expressions may be nested.
For example, to multiply five squared by three:

[me@linuxbox me]$ echo $(($((5**2)) * 3))


75

Single parentheses may be used to group multiple subexpressions. With this


technique, we can rewrite the example above and get the same result using a single
expansion instead of two:

[me@linuxbox me]$ echo $(((5**2) * 3))


75

Here is an example using the division and remainder operators. Notice the effect of
integer division:

[me@linuxbox me]$ echo Five divided by two equals $((5/2))


Five divided by two equals 2
[me@linuxbox me]$ echo with $((5%2)) left over.
with 1 left over.

Brace Expansion
Perhaps the strangest expansion is called brace expansion. With it, we can create
multiple text strings from a pattern containing braces. Here's an example:

[me@linuxbox me]$ echo Front-{A,B,C}-Back


Front-A-Back Front-B-Back Front-C-Back

Patterns to be brace expanded may contain a leading portion called a preamble and a
trailing portion called a postscript. The brace expression itself may contain either a
comma-separated list of strings, or a range of integers or single characters. The
pattern may not contain embedded whitespace. Here is an example using a range of
integers:

[me@linuxbox me]$ echo Number_{1..5}


Number_1 Number_2 Number_3 Number_4 Number_5

A range of letters in reverse order:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ echo {Z..A}
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A

Brace expansions may be nested:

[me@linuxbox me]$ echo a{A{1,2},B{3,4}}b


aA1b aA2b aB3b aB4b

So what is this good for? The most common application is to make lists of files or
directories to be created. For example, if we were a photographer and had a large
collection of images we wanted to organize into years and months, the first thing we
might do is create a series of directories named in numeric “Year-Month” format. This
way, the directory names will sort in chronological order. we could type out a complete
list of directories, but that's a lot of work and it's error-prone too. Instead, we could
do this:

[me@linuxbox me]$ mkdir Photos


[me@linuxbox me]$ cd Photos
[me@linuxbox Photos]$ mkdir {2017..2019}-{01..12}
[me@linuxbox Photos]$ ls
2017-01 2017-07 2018-01 2018-07 2019-01 2019-07
2017-02 2017-08 2018-02 2018-08 2019-02 2019-08
2017-03 2017-09 2018-03 2018-09 2019-03 2019-09
2017-04 2017-10 2018-04 2018-10 2019-04 2019-10
2017-05 2017-11 2018-05 2018-11 2019-05 2019-11
2017-06 2017-12 2018-06 2018-12 2019-06 2019-12

Pretty slick!

Parameter Expansion
We're only going to touch briefly on parameter expansion in this lesson, but we'll be
covering it more later. It's a feature that is more useful in shell scripts than directly on
the command line. Many of its capabilities have to do with the system's ability to store
small chunks of data and to give each chunk a name. Many such chunks, more
properly called variables, are available for our examination. For example, the variable
named “USER” contains our user name. To invoke parameter expansion and reveal the
contents of USER we would do this:

[me@linuxbox me]$ echo $USER


me

To see a list of available variables, try this:

[me@linuxbox me]$ printenv | less

With other types of expansion, if we mistype a pattern, the expansion will not take
place and the echo command will simply display the mistyped pattern. With parameter
expansion, if we misspell the name of a variable, the expansion will still take place,
but will result in an empty string:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ echo $SUER
[me@linuxbox ~]$

Command Substitution
Command substitution allows us to use the output of a command as an expansion:

[me@linuxbox me]$ echo $(ls)


Desktop Documents ls-output.txt Music Pictures Public Templates Videos

A clever one goes something like this:

[me@linuxbox me]$ ls -l $(which cp)


-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 /bin/cp

Here we passed the results of which cp as an argument to the ls command, thereby


getting the listing of of the cp program without having to know its full pathname. We
are not limited to just simple commands. Entire pipelines can be used (only partial
output shown):

[me@linuxbox me]$ file $(ls /usr/bin/* | grep bin/zip)


/usr/bin/bunzip2:
/usr/bin/zip: ELF 32-bit LSB executable, Intel 80386, version 1
(SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, str
/usr/bin/zipcloak: ELF 32-bit LSB executable, Intel 80386, version 1
(SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, str
/usr/bin/zipgrep: POSIX shell script text executable
/usr/bin/zipinfo: ELF 32-bit LSB executable, Intel 80386, version 1
(SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, str
/usr/bin/zipnote: ELF 32-bit LSB executable, Intel 80386, version 1
(SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, str
/usr/bin/zipsplit: ELF 32-bit LSB executable, Intel 80386, version 1
(SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, str

In this example, the results of the pipeline became the argument list of the file
command. There is an alternate syntax for command substitution in older shell
programs which is also supported in bash. It uses back-quotes instead of the dollar
sign and parentheses:

[me@linuxbox me]$ ls -l `which cp`


-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 /bin/cp

Quoting
Now that we've seen how many ways the shell can perform expansions, it's time to
learn how we can control it. Take for example:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ echo this is a     test
this is a test

or:

[me@linuxbox me]$ [me@linuxbox ~]$ echo The total is $100.00


The total is 00.00

In the first example, word-splitting by the shell removed extra whitespace from the
echo command's list of arguments. In the second example, parameter expansion
substituted an empty string for the value of “$1” because it was an undefined variable.
The shell provides a mechanism called quoting to selectively suppress unwanted
expansions.

Double Quotes
The first type of quoting we will look at is double quotes. If we place text inside double
quotes, all the special characters used by the shell lose their special meaning and are
treated as ordinary characters. The exceptions are “$”, “\” (backslash), and “`” (back-
quote). This means that word-splitting, pathname expansion, tilde expansion, and
brace expansion are suppressed, but parameter expansion, arithmetic expansion, and
command substitution are still carried out. Using double quotes, we can cope with
filenames containing embedded spaces. Imagine we were the unfortunate victim of a
file called two words.txt. If we tried to use this on the command line, word-splitting
would cause this to be treated as two separate arguments rather than the desired
single argument:

[me@linuxbox me]$ ls -l two words.txt


ls: cannot access two: No such file or directory
ls: cannot access words.txt: No such file or directory

By using double quotes, we can stop the word-splitting and get the desired result;
further, we can even repair the damage:

[me@linuxbox me]$ ls -l "two words.txt"


-rw-rw-r-- 1 me me 18 2020-02-20 13:03 two words.txt
[me@linuxbox me]$ mv "two words.txt" two_words.txt

There! Now we don't have to keep typing those pesky double quotes. Remember,
parameter expansion, arithmetic expansion, and command substitution still take place
within double quotes:

[me@linuxbox me]$ echo "$USER $((2+2)) $(cal)"


me 4
February 2020
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
17 18 19 20 21 22 23
24 25 26 27 28 29

We should take a moment to look at the effect of double quotes on command


substitution. First let's look a little deeper at how word splitting works. In our earlier
example, we saw how word-splitting appears to remove extra spaces in our text:

[me@linuxbox me]$ echo this is a     test


this is a test

By default, word-splitting looks for the presence of spaces, tabs, and newlines
(linefeed characters) and treats them as delimiters between words. This means that
unquoted spaces, tabs, and newlines are not considered to be part of the text. They
only serve as separators. Since they separate the words into different arguments, our
example command line contains a command followed by four distinct arguments. If we
add double quotes:

[me@linuxbox me]$ echo "this is a     test"


this is a     test

word-splitting is suppressed and the embedded spaces are not treated as delimiters,
rather they become part of the argument. Once the double quotes are added, our
command line contains a command followed by a single argument. The fact that
newlines are considered delimiters by the word-splitting mechanism causes an
interesting, albeit subtle, effect on command substitution. Consider the following:

[me@linuxbox me]$ echo $(cal)


February 2020 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
[me@linuxbox me]$ echo "$(cal)"
February 2020
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29

In the first instance, the unquoted command substitution resulted in a command line
containing thirty-eight arguments. In the second, a command line with one argument
that includes the embedded spaces and newlines.

Single Quotes
When we need to suppress all expansions, we use single quotes. Here is a comparison
of unquoted, double quotes, and single quotes:

[me@linuxbox me]$ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
text /home/me/ls-output.txt a b foo 4 me
[me@linuxbox me]$ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
text ~/*.txt {a,b} foo 4 me
[me@linuxbox me]$ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER

As we can see, with each succeeding level of quoting, more and more of the
expansions are suppressed.

Escaping Characters
Sometimes we only want to quote a single character. To do this, we can precede a
character with a backslash, which in this context is called the escape character. Often
this is done inside double quotes to selectively prevent an expansion:

[me@linuxbox me]$ echo "The balance for user $USER is: \$5.00"
The balance for user me is: $5.00

It is also common to use escaping to eliminate the special meaning of a character in a


filename. For example, it is possible to use characters in filenames that normally have
special meaning to the shell. These would include “$”, “!”, “&”, “ “, and others. To
include a special character in a filename we can to this:

[me@linuxbox me]$ mv bad\&filename good_filename

To allow a backslash character to appear, escape it by typing “\\”. Note that within
single quotes, the backslash loses its special meaning and is treated as an ordinary
character.

More Backslash Tricks


If we look at the man pages for any program written by the GNU project, we will see
that in addition to command line options consisting of a dash and a single letter, there
are also long option names that begin with two dashes. For example, the following are
equivalent:

ls -r
ls --reverse

Why do they support both? The short form is for lazy typists on the command line and
the long form is mostly for scripts though some options may only be available in long
form. Sometimes it is better to use a long option when the option is obscure or we
want to document more clearly what an option is. This is especially useful when
writing scripts where maximum readability is desired, and besides, anytime we can
save ourselves a trip to the man page is a good thing.

As we might suspect, using the long form options can make a single command line
very long. To combat this problem, we can use a backslash to get the shell to ignore a
newline character like this:

ls -l \
--reverse \

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
--human-readable \
--full-time

Using the backslash in this way allows us to embed newlines in our command. Note
that for this trick to work, the newline must be typed immediately after the backslash.
If we put a space after the backslash, the space will be ignored, not the newline.
Backslashes are also used to insert special characters into our text. These are called
backslash escape characters. Here are the common ones:

Escape Character Name Possible Uses

\n newline Adding blank lines to text

\t tab Inserting horizontal tabs to text

\a alert Makes our terminal beep

\\ backslash Inserts a backslash

\f formfeed Sending this to our printer ejects the page

The use of the backslash escape characters is very common. This idea first appeared
in the C programming language. Today, the shell, C++, Perl, python, awk, tcl, and
many other programming languages use this concept. Using the echo command with
the -e option will allow us to demonstrate:

[me@linuxbox me]$ echo -e "Inserting several blank lines\n\n\n"


Inserting several blank lines

[me@linuxbox me]$ echo -e "Words\tseparated\tby\thorizontal\ttabs."


Words separated   by  horizontal  tabs
[me@linuxbox me]$ echo -e "\aMy computer went \"beep\"."
My computer went "beep".
[me@linuxbox me]$ echo -e "DEL C:\\WIN2K\\LEGACY_OS.EXE"
DEL C:\WIN2K\LEGACY_OS.EXE

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next

Permissions
The Unix-like operating systems, such as Linux differ from other computing systems in
that they are not only multitasking but also multi-user.

What exactly does this mean? It means that more than one user can be operating the
computer at the same time. While a desktop or laptop computer only has one
keyboard and monitor, it can still be used by more than one user. For example, if the
computer is attached to a network, or the Internet, remote users can log in via ssh
(secure shell) and operate the computer. In fact, remote users can execute graphical
applications and have the output displayed on a remote computer. The X Window
system supports this.

The multi-user capability of Unix-like systems is a feature that is deeply ingrained into
the design of the operating system. If we remember the environment in which Unix
was created, this makes perfect sense. Years ago before computers were "personal,"
they were large, expensive, and centralized. A typical university computer system
consisted of a large mainframe computer located in some building on campus and
terminals were located throughout the campus, each connected to the large central
computer. The computer would support many users at the same time.

In order to make this practical, a method had to be devised to protect the users from
each other. After all, we wouldn't want the actions of one user to crash the computer,
nor would we allow one user to interfere with the files belonging to another user.

This lesson will cover the following commands:

chmod - modify file access rights


su - temporarily become the superuser
sudo - temporarily become the superuser
chown - change file ownership
chgrp - change a file's group ownership

File Permissions
On a Linux system, each file and directory is assigned access rights for the owner of
the file, the members of a group of related users, and everybody else. Rights can be
assigned to read a file, to write a file, and to execute a file (i.e., run the file as a
program).

To see the permission settings for a file, we can use the ls command. As an example,
we will look at the bash program which is located in the /bin directory:

[me@linuxbox me]$ ls -l /bin/bash


-rwxr-xr-x 1 root root 1113504 Jun 6 2019 /bin/bash

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Here we can see:

The file "/bin/bash" is owned by user "root"


The superuser has the right to read, write, and execute this file
The file is owned by the group "root"
Members of the group "root" can also read and execute this file
Everybody else can read and execute this file

In the diagram below, we see how the first portion of the listing is interpreted. It
consists of a character indicating the file type, followed by three sets of three
characters that convey the reading, writing and execution permission for the owner,
group, and everybody else.

chmod
The chmod command is used to change the permissions of a file or directory. To use it,
we specify the desired permission settings and the file or files that we wish to modify.
There are two ways to specify the permissions. In this lesson we will focus on one of
these, called the octal notation method.

It is easy to think of the permission settings as a series of bits (which is how the
computer thinks about them). Here's how it works:

rwx rwx rwx = 111 111 111


rw- rw- rw- = 110 110 110
rwx --- --- = 111 000 000

and so on...

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
rwx = 111 in binary = 7
rw- = 110 in binary = 6
r-x = 101 in binary = 5
r-- = 100 in binary = 4

Now, if we represent each of the three sets of permissions (owner, group, and other)
as a single digit, we have a pretty convenient way of expressing the possible
permissions settings. For example, if we wanted to set some_file to have read and
write permission for the owner, but wanted to keep the file private from others, we
would:

[me@linuxbox me]$ chmod 600 some_file

Here is a table of numbers that covers all the common settings. The ones beginning
with "7" are used with programs (since they enable execution) and the rest are for
other kinds of files.

Value Meaning

777 (rwxrwxrwx) No restrictions on permissions. Anybody may do anything.


Generally not a desirable setting.

755 (rwxr-xr-x) The file's owner may read, write, and execute the file. All
others may read and execute the file. This setting is common for programs
that are used by all users.

700 (rwx------) The file's owner may read, write, and execute the file. Nobody
else has any rights. This setting is useful for programs that only the owner
may use and must be kept private from others.

666 (rw-rw-rw-) All users may read and write the file.

644 (rw-r--r--) The owner may read and write a file, while all others may only
read the file. A common setting for data files that everybody may read,
but only the owner may change.

600 (rw-------) The owner may read and write a file. All others have no rights.
A common setting for data files that the owner wants to keep private.

Directory Permissions
The chmod command can also be used to control the access permissions for
directories. Again, we can use the octal notation to set permissions, but the meaning
of the r, w, and x attributes is different:

r - Allows the contents of the directory to be listed if the x attribute is also set.
w - Allows files within the directory to be created, deleted, or renamed if the x
attribute is also set.
x - Allows a directory to be entered (i.e. cd dir).

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Here are some useful settings for directories:

Value Meaning

777 (rwxrwxrwx) No restrictions on permissions. Anybody may list files,


create new files in the directory and delete files in the directory. Generally
not a good setting.

755 (rwxr-xr-x) The directory owner has full access. All others may list the
directory, but cannot create files nor delete them. This setting is common
for directories that you wish to share with other users.

700 (rwx------) The directory owner has full access. Nobody else has any
rights. This setting is useful for directories that only the owner may use
and must be kept private from others.

Becoming the Superuser for a Short While


It is often necessary to become the superuser to perform important system
administration tasks, but as we know, we should not stay logged in as the superuser.
In most distributions, there is a program that can give you temporary access to the
superuser's privileges. This program is called su (short for substitute user) and can be
used in those cases when you need to be the superuser for a small number of tasks.
To become the superuser, simply type the su command. You will be prompted for the
superuser's password:

[me@linuxbox me]$ su
Password:
[root@linuxbox me]#

After executing the su command, we have a new shell session as the superuser. To
exit the superuser session, type exit and we will return to your previous session.

In most modern distributions, an alternate method is used. Rather than using su,
these systems employ the sudo command instead. With sudo, one or more users are
granted superuser privileges on an as needed basis. To execute a command as the
superuser, the desired command is simply preceded with the sudo command. After the
command is entered, the user is prompted for the their own password rather than the
superuser's:

[me@linuxbox me]$ sudo some_command


Password for me:
[me@linuxbox me]$

In fact, modern distributions don't even set the root account password thus making it
impossible to log in as the root user. A root shell is still possible with sudo by using the
"-i" option:

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ sudo -i
Password for me:
root@linuxbox:~#

Changing File Ownership


We can change the owner of a file by using the chown command. Here's an example:
Suppose we wanted to change the owner of some_file from "me" to "you". We could:

[me@linuxbox me]$ sudo chown you some_file

Notice that in order to change the owner of a file, we must have superuser privileges.
To do this, our example employed the sudo command to execute chown.

chown works the same way on directories as it does on files.

Changing Group Ownership


The group ownership of a file or directory may be changed with chgrp. This command
is used like this:

[me@linuxbox me]$ chgrp new_group some_file

In the example above, we changed the group ownership of some_file from its
previous group to "new_group". We must be the owner of the file or directory to
perform a chgrp.

Further Reading
Chapter 9 of The Linux Command Line covers this topic in much more detail.

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next

Job Control
In the previous lesson, we looked at some of the implications of Linux being a multi-
user operating system. In this lesson, we will examine the multitasking nature of
Linux, and how it is controlled with the command line interface.

As with any multitasking operating system, Linux executes multiple, simultaneous


processes. Well, they appear simultaneous, anyway. Actually, a single processor core
can only execute one process at a time but the Linux kernel manages to give each
process its turn at the processor and each appears to be running at the same time.

There are several commands that are used to control processes. They are:

ps - list the processes running on the system


kill - send a signal to one or more processes (usually to "kill" a process)
jobs - an alternate way of listing your own processes
bg - put a process in the background
fg - put a process in the foreground

A Practical Example
While it may seem that this subject is rather obscure, it can be very practical for the
average user who mostly works with the graphical user interface. Though it might not
be apparent, most (if not all) graphical programs can be launched from the command
line. Here's an example: there is a small program supplied with the X Window system
called xload which displays a graph representing system load. We can execute this
program by typing the following:

[me@linuxbox me]$ xload

Notice that the small xload window appears and begins to display the system load
graph. On systems where xload is not available, try gedit instead. Notice also that
our prompt did not reappear after the program launched. The shell is waiting for the
program to finish before control returns. If we close the xload window, the xload
program terminates and the prompt returns.

Putting a Program into the Background


Now, in order to make life a little easier, we are going to launch the xload program
again, but this time we will put it in the background so that the prompt will return. To
do this, we execute xload like this:

[me@linuxbox me]$ xload &


[1] 1223

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$

In this case, the prompt returned because the process was put in the background.

Now imagine that we forgot to use the "&" symbol to put the program into the
background. There is still hope. We can type Ctrl-z and the process will be
suspended. We can verify this by seeing that the program's window is frozen. The
process still exists, but is idle. To resume the process in the background, type the bg
command (short for background). Here is an example:

[me@linuxbox me]$ xload


[2]+ Stopped xload

[me@linuxbox me]$ bg
[2]+ xload &

Listing Running Processes


Now that we have a process in the background, it would be helpful to display a list of
the processes we have launched. To do this, we can use either the jobs command or
the more powerful ps command.

[me@linuxbox me]$ jobs


[1]+ Running xload&

[me@linuxbox me]$ ps
PID TTY TIME CMD
1211 pts/4 00:00:00 bash
1246 pts/4 00:00:00 xload
1247 pts/4 00:00:00 ps

[me@linuxbox me]$

Killing a Process
Suppose that we have a program that becomes unresponsive; how do we get rid of it?
We use the kill command, of course. Let's try this out on xload. First, we need to
identify the process we want to kill. We can use either jobs or ps, to do this. If we use
jobs we will get back a job number. With ps, we are given a process id (PID). We will
do it both ways:

[me@linuxbox me]$ xload &


[1] 1292

[me@linuxbox me]$ jobs


[1]+ Running xload&

[me@linuxbox me]$ kill %1

[me@linuxbox me]$ xload &

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[2] 1293
[1] Terminated xload

[me@linuxbox me]$ ps
PID TTY TIME CMD
1280 pts/5 00:00:00 bash
1293 pts/5 00:00:00 xload
1294 pts/5 00:00:00 ps

[me@linuxbox me]$ kill 1293


[2]+ Terminated xload

[me@linuxbox me]$

A Little More About kill


While the kill command is used to "kill" processes, its real purpose is to send signals
to processes. Most of the time the signal is intended to tell the process to go away,
but there is more to it than that. Programs (if they are properly written) listen for
signals from the operating system and respond to them, most often to allow some
graceful method of terminating. For example, a text editor might listen for any signal
that indicates that the user is logging off, or that the computer is shutting down. When
it receives this signal, it could save the work in progress before it exits. The kill
command can send a variety of signals to processes. Typing:

kill -l

will print a list of the signals it supports. Many are rather obscure, but several are
handy to know:

Signal # Name Description

1 SIGHUP Hang up signal. Programs can listen for this signal and act
upon it. This signal is sent to processes running in a
terminal when you close the terminal.

2 SIGINT Interrupt signal. This signal is given to processes to


interrupt them. Programs can process this signal and act
upon it. We can also issue this signal directly by typing
Ctrl-c in the terminal window where the program is
running.

15 SIGTERM Termination signal. This signal is given to processes to


terminate them. Again, programs can process this signal
and act upon it. This is the default signal sent by the kill
command if no signal is specified.

9 SIGKILL Kill signal. This signal causes the immediate termination of


the process by the Linux kernel. Programs cannot listen for

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
this signal.

Now let's suppose that we have a program that is hopelessly hung and we want to get
rid of it. Here's what we do:

1. Use the ps command to get the process id (PID) of the process we want to
terminate.
2. Issue a kill command for that PID.
3. If the process refuses to terminate (i.e., it is ignoring the signal), send
increasingly harsh signals until it does terminate.

[me@linuxbox me]$ ps x | grep bad_program


PID TTY STAT TIME COMMAND
2931 pts/5 SN 0:00 bad_program

[me@linuxbox me]$ kill -SIGTERM 2931

[me@linuxbox me]$ kill -SIGKILL 2931

In the example above we used the ps command with the x option to list all of our
processes (even those not launched from the current terminal). In addition, we piped
the output of the ps command into grep to list only list the program we are interested
in. Next, we used kill to issue a SIGTERM signal to the troublesome program. In
actual practice, it is more common to do it in the following way since the default signal
sent by kill is SIGTERM and kill can also use the signal number instead of the
signal name:

[me@linuxbox me]$ kill 2931

Then, if the process does not terminate, force it with the SIGKILL signal:

[me@linuxbox me]$ kill -9 2931

That's It!
This concludes the "Learning the Shell" series of lessons. In the next series, "Writing
Shell Scripts," we will look at how to automate tasks with the shell.

Further Reading
For a more in-depth treatment of the topic, see Chapter 10 in The Linux
Command Line.
1963 Timesharing: A Solution to Computer Bottlenecks, a fascinating YouTube
video from the Computer History Museum describing the first timesharing
operating system and how it works. It's basically the same method used by all
modern computers.

Top | Previous | Contents | Next

Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

You might also like