0% found this document useful (0 votes)
13 views72 pages

Edx Linux

The document provides an overview of using the GNOME desktop environment in Linux, including commands for file management, text editing, and package management. It covers essential utilities like the file manager, terminal commands, and methods for managing the GUI. Additionally, it discusses the use of various applications available in Linux for development, internet tasks, and multimedia, along with command-line utilities for file searching and redirection.

Uploaded by

iqbalhussain2024
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)
13 views72 pages

Edx Linux

The document provides an overview of using the GNOME desktop environment in Linux, including commands for file management, text editing, and package management. It covers essential utilities like the file manager, terminal commands, and methods for managing the GUI. Additionally, it discusses the use of various applications available in Linux for development, internet tasks, and multimedia, along with command-line utilities for file searching and redirection.

Uploaded by

iqbalhussain2024
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

EdX course

Session manager plus display manager plus utilities create the desktop experience in GUI. Can start
GUI in CLI by command startx

To open the File Manager from the command line, on most systems simply type nautilus.

The shortcut key to get to the search text box is CTRL-F. You can exit the search text box view by
clicking the Search button or CTRL-F again.
Another quick way to access a specific directory is to press CTRL-L, which will give you a Location text
box to type in a path to a directory.

1. To add multiple search criteria, click the + button and select Additional Search
Criteria.

The default text editor in GNOME is gedit.

Deleting a file in Nautilus will automatically move the deleted files to the .local/share/Trash/files/
directory (a trash can of sorts) under the user's home directory. There are several ways to delete
files and directories using Nautilus.
1. Select all the files and directories that you want to delete.
2. Press CTRL-Delete on your keyboard, or right-click the file.
3. Select Move to Trash

Alternatively, select the file or directory you want to permanently delete and press Shift-Delete.
As a precaution, you should never delete your Home directory, as doing so will most likely erase all
your GNOME configuration files and possibly prevent you from logging in. Many personal system
and program configurations are stored under your home directory.

Open the file manager and navigate to your home directory. Once your user configuration is set up
for it this is as simple as right clicking in the directory, and selecting Create New->Text File and giving
it a name. (You may see something other than Text File.)
With the GNOME file manager, there is no such option in the default setup. You must create a file
called new in your Templates directory first. The easiest way to do this is to type Alt-F2 and then in
the little window that opens up
student:/tmp> touch ~/Templates/new

Once you have done that, you can either right click in the directory, or somewhere on the right side
of the title bar to open up a dialog where there will be an option to create a new file of some type.

• The default display manager for GNOME is called gdm.


• The gdm display manager presents the user with the login screen, which prompts for
the login username and password.

The X server, which actually provides the GUI, uses /etc/X11/[Link] as its configuration file if it
exists; In modern Linux distributions, this file is usually present only in unusual circumstances, such
as when certain less common graphic drivers are in use. Changing this configuration file directly is
usually for more advanced users.

NOTE: You can also ascertain your current resolution by typing at the command line:
student:/tmp> $ xdpyinfo | grep dim dimensions: 3200x1080 pixels (847x286 millimeters)
Network Manager sets the actual network settings via Dynamic Host Configuration Protocol (DHCP).
For static configurations that do not use DHCP, manual setup can also be done easily through
Network Manager. You can also change the Ethernet Media Access Control (MAC) address if your
hardware supports it. The MAC address is a unique hexadecimal number of your network card.

dpkg is the underlying package manager for these systems. It can install, remove, and build
packages. Unlike higher-level package management systems, it does not automatically download
and install packages and satisfy their dependencies.

Red Hat Package Manager (RPM) is the other package management system popular on Linux
distributions. It was developed by Red Hat, and adopted by a number of other distributions,
including SUSE/openSUSE, Mageia, CentOS, Oracle Linux, and others.

On any recent GNOME-based system you can run the gnome-software utility as we have described.
You can either run it from some menu location (happy hunting!) or from a command line or by
typing it after hitting ALT-F2.
On Ubuntu, use either the Ubuntu Software Center or synaptic. On any other Debian-based system
you can use synaptic.
On a Red Hat 7-based systems including CentOS 7, Click on Applications in the top menu bar, then
System Tools->Software.
On SUSE based systems, you can click on the System Settings icon in the upper right corner (the
wrench and screw driver) and then System->YaST.
On any of these systems you can then just type dump in the search box and proceed from there.

Linux distributions come with a complete set of applications and tools that are needed by those
developing or maintaining both user applications and the kernel itself.
These tools are tightly integrated and include:
• Advanced editors customized for programmers' needs, such as vi and emacs.
• Compilers (such as gcc and clang for programs in C and C++) for every computer
language that has ever existed, including very popular new ones such as Golang and Rust.
• Debuggers such as gdb and various graphical front ends to it and many other
debugging tools (such as Valgrind).
• Performance measuring and monitoring programs, some with easy to use graphical
interfaces, others more arcane and meant to be used only by serious experienced development
engineers.
• Complete Integrated Development Environments (IDE's) such as Eclipse and Visual
Studio Code that put all these tools together.

• Linux offers a wide variety of Internet applications, such as web browsers, email
clients, online media applications, and others.
• Web browsers supported by Linux can be either graphical or text-based, such as
Firefox, Google Chrome, Epiphany, w3m, lynx, and others.
• Linux supports graphical email clients, such as Thunderbird, Evolution, and Claws
Mail, and text mode email clients, such as Mutt and mail.
• Linux systems provide many other applications for performing Internet-related
tasks, such as Filezilla, XChat, Pidgin, and others.
• Most Linux distributions offer LibreOffice to create and edit different kinds of
documents.
• Linux systems offer entire suites of development applications and tools, including
compilers and debuggers.
• Linux systems offer a number of sound players including Amarok, Audacity, and
Rhythmbox.
• Linux systems offer a number of movie players, including VLC, MPlayer, Xine, and
Totem.
• Linux systems offer a number of movie editors, including Kino, Cinepaint, Blender
among others.
• The GIMP (GNU Image Manipulation Program) utility is a feature-rich image
retouching and editing tool available on all Linux distributions.
• Other graphics utilities that help perform various image-related tasks are eog,
Inkscape, convert, and Scribus.

The GNU Image Manipulation Program (GIMP)

CLI

• cat: used to type out a file (or combine files).


• head: used to show the first few lines of a file.
• tail: used to show the last few lines of a file.
• man: used to view documentation.

If your system does not already have sudo set up and enabled, you need to do the following steps:
1. You will need to make modifications as the administrative or superuser, root. While
sudo will become the preferred method of doing this, we do not have it set up yet, so we will
use su (which we will discuss later in detail) instead. At the command line prompt, type su and
press Enter. You will then be prompted for the root password, so enter it and press Enter. You will
notice that nothing is printed; this is so others cannot see the password on the screen. You should
end up with a different looking prompt, often ending with ‘#’. For example: $ su Password: #
2. Now, you need to create a configuration file to enable your user account to use
sudo. Typically, this file is created in the /etc/sudoers.d/ directory with the name of the file the same
as your username. For example, for this demo, let’s say your username is student. After doing step 1,
you would then create the configuration file for student by doing this: # echo "student ALL=(ALL)
ALL" > /etc/sudoers.d/student
3. Finally, some Linux distributions will complain if you do not also change permissions
on the file by doing: # chmod 440 /etc/sudoers.d/student

To switch between VTs, press CTRL-ALT-function key for the VT. For example, press CTRL-ALT-F6 for
VT 6. Actually, you only have to press the ALT-F6 key combination if you are in a VT and want to
switch to another VT.

Methods of bringing down the GUI:


student:/tmp> sudo systemctl stop gdm
student:/tmp> sudo systemctl stop lightdm
student:/tmp> sudo telinit 3

Methods of bringing the GUI back up:


student:/tmp> sudo systemctl start gdm
student:/tmp> sudo systemctl start lightdm
student:/tmp> sudo telinit 5

One way to locate programs is to employ the which utility. For example, to find out exactly where
the diff program resides on the filesystem:
$ which diff /usr/bin/diff
If which does not find the program, whereis is a good alternative because it looks for packages in a
broader range of system directories:
$ whereis diff diff: /usr/bin/diff /usr/share/man/man1/[Link] /usr/share/man/man1p/[Link]
as well as locating source and man files packaged with the program

When you first log into a system or open a terminal, the default directory should be your home
directory. You can print the exact path of this by typing echo $HOME

Command Result
pwd Displays the present working directory
cd ~ or cd Change to your home directory (shortcut name is ~ (tilde))
cd .. Change to parent directory (..)
cd - Change to previous directory (- (minus)

Command Usage
cd / Changes your current directory to the root (/) directory (or path you supply)
ls List the contents of the present working directory
ls –a List all files, including hidden files and directories (those whose name start with . )
tree Displays a tree view of the filesyste

The cd command remembers where you were last, and lets you get back there with cd -. For
remembering more than just the last directory visited, use pushd to change the directory instead of
cd; this pushes your starting directory onto a list. Using popd will then send you back to those
directories, walking in reverse order (the most recent directory will be the first one retrieved
with popd). The list of directories is displayed with the dirs command.

Command Usage
cat Used for viewing files that are not very long; it does not provide any scroll-back.
tac Used to look at a file backwards, starting with the last line.
less Used to view larger files because it is a paging program. It pauses at each screen full of text,
provides scroll-back capabilities, and lets you search and navigate within the file.

NOTE: Use / to search for a pattern in the forward direction and ? for a pattern in the backward
direction. An older program named more is still used, but has fewer capabilities: "less is more".
tail Used to print the last 10 lines of a file by default. You can change the number of lines by
doing -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default.
head The opposite of tail; by default, it prints the first 10 lines of a file.
Command Usage
mv Rename a file
rm Remove a file
rm –f Forcefully remove a file
rm –i Interactively remove a file

Command Usage
mv Rename a directory
rmdir Remove an empty directory
rm -rf Forcefully remove a directory recursivel

The locate utility program performs a search taking advantage of a previously constructed database
of files and directories on your system, matching all entries that contain a specified character string.
This can sometimes result in a very long list.
To get a shorter (and possibly more relevant) list, we can use the grep program as a filter. grep will
print only the lines that contain one or more specified strings, as in:
$ locate zip | grep bin
which will list all the files and directories with both zip and bin in their name. We will cover grep in
much more detail later. Notice the use of | to pipe the two commands together.
locate utilizes a database created by a related utility, updatedb. Most Linux systems run this
automatically once a day. However, you can update it at any time by just running updatedb from the
command line as the root user.

Through the command shell, we can redirect the three standard file streams so that we can get input
from either a file or another command, instead of from our keyboard, and we can write output and
errors to files or use them to provide input for subsequent commands.
For example, if we have a program called do_something that reads from stdin and writes to stdout
and stderr, we can change its input source by using the less-than sign (<) followed by the name of
the file to be consumed for input data:
$ do_something < input-file
If you want to send the output to a file, use the greater-than sign (>) as in: $ do_something >
output-file
Because stderr is not the same as stdout, error messages will still be seen on the terminal windows
in the above example.
If you want to redirect stderr to a separate file, you use stderr’s file descriptor number (2), the
greater-than sign (>), followed by the name of the file you want to hold everything the running
command writes to stderr: $ do_something 2> error-file
NOTE: By the same logic, do_something 1> output-file is the same as do_something > output-file.
A special shorthand notation can send anything written to file descriptor 2 (stderr) to the same place
as file descriptor 1 (stdout): 2>&1.
$ do_something > all-output-file 2>&1
bash permits an easier syntax for the above:
$ do_something >& all-output-file

Wildcard Result
? Matches any single character
* Matches any string of characters
[set] Matches any character in the set of characters, for example [adf] will match any occurrence
of a, d, or f
[!set] Matches any character not in the set of characters

Searching for files and directories named gcc: $ find /usr -name gcc
Searching only for directories named gcc: $ find /usr -type d -name gcc
Searching only for regular files named gcc: $ find /usr -type f -name gcc

To find files based on time: $ find / -ctime 3


Here, -ctime is when the inode metadata (i.e. file ownership, permissions, etc.) last changed; it is
often, but not necessarily, when the file was first created. You can also search for accessed/last read
(-atime) or modified/last written (-mtime) times.

To find files based on sizes:


$ find / -size 0

For example, to find files greater than 10 MB in size and running a command on those files: $ find /
-size +10M -exec command {} ’;’

student:/tmp> apt-get install dump


student:/tmp> apt-get remove dump

or
student:/tmp> dnf install dump
student:/tmp> dnf remove dump

or
student:/tmp> yum install dump
student:/tmp> yum remove dump

or
student:/tmp> zypper install dump
student:/tmp> zypper remove dump

[Link] To access help regarding Linux

• man –f generates the same result as typing whatis.


• man –k generates the same result as typing apropos.

1. student:/tmp> man man


2. Use the Page Down key or search in less with the '/' key for the EXAMPLES
section.
3. student:/tmp> man -k compress
4. or
5. student:/tmp> apropos compress
6. will bring up a long list of programs and references, including gzip, bzip2 and xz,
and a number of file utilities that work with compressed files, such as zless, zgrep, bzcat, and xzdiff.
7. student:/tmp> man 3 printf
8. (man printf will bring up the command-line utility (section 1) of the same
name.)

The topic which you view in an info page is called a node. The table lists the basic keystrokes for
moving between nodes.

Items function like browser links and are identified by an asterisk (*) at the beginning of the item
name. Named items (outside a menu) are identified with double-colons (::) at the end of the item
name. Items can refer to other nodes within the file or to other files.

Key Function
n Go to the next node
p Go to the previous node
u Move one node up in the index

student:/tmp> mkdir --help

or
student:/tmp>$ man mkdir

If you do not want to spend time hunting for the right icon or menu item to launch the help
application, you can also start the graphical help system from a terminal window or command
prompt by using one of the following utility programs:
• GNOME: gnome-help or yelp
• KDE: khelpcenter

There are many places to access online Linux documentation, and a little bit of searching will get you
buried in it.
The following book has been well-reviewed by other users of this course. It is a free, downloadable
command line compendium under a Creative Commons license: "The Linux Command Line" by
William Shotts.
You can also find very helpful documentation for each distribution. Each distribution has its own
user-generated forums and wiki sections. Here are just a few links to such sources:
• Ubuntu Documentation
• CentoS Documentation
• openSUSE Documentation
• Gentoo Documentation
• Fedora Documentation.
Moreover, you can use online search sites to locate helpful resources from all over the Internet,
including blog posts, forum and mailing list posts, news articles, and so on.
However, if at the command line you type something like:
student:/tmp> yelp man:cat
it will indeed bring up the man page for cat. However, you cannot type something like man:ls in the
location bar and have it work, unless you hit CTRL-l first! It is not exactly clear whether this is a bug
or a feature, but a bug seems more likely. Once you are in the page, clicking on links to get other
man pages works just fine.
The same mechanism works to get info pages as well, as in:
student:/tmp> yelp info:cpio

Process Type Description Example


Interactive Processes Need to be started by a user, either at a command line or through a
graphical interface such as an icon or a menu selection. bash, firefox, top
Batch ProcessesAutomatic processes which are scheduled from and then disconnected from the
terminal. These tasks are queued and work on a FIFO (First-In, First-Out) basis. updatedb, ldconfig
Daemons Server processes that run continuously. Many are launched during system startup
and then wait for a user or system request indicating that their service is required. httpd, sshd,
libvirtd
Threads Lightweight processes. These are tasks that run under the umbrella of a main process,
sharing memory and other resources, but are scheduled and run by the system on an individual
basis. An individual thread can end without terminating the whole process and a process can create
new threads at any time. Many non-trivial programs are multi-threaded. firefox, gnome-terminal-
server
Kernel Threads Kernel tasks that users neither start nor terminate and have little control over. These
may perform actions like moving a thread from one CPU to another, or making sure input/output
operations to disk are completed. kthreadd, migration, ksoftirq

To terminate a process, you can type kill -SIGKILL <pid> or kill -9 <pid>.

To look at processes command ps lf


Priority of processes is managed by nice number. The higher the number the lower the priority.
Niceness can be changed by command renice (nice number) (process number) nice number can be
from -19 to +20. This can be done using graphical interface command gnome-system-monitor. Right
click the process and change nice of process by choosing high,low etc.

jobs -l provides the same information as jobs, and adds the PID of the background jobs.

Getting Uptime and Load Averages


A very simple method is just to use the uptime utility:
student:/tmp> uptime
10:26:40 up 3:19, 5 users, load average: 1.46, 1.40, 1.19
A second method is to look at the first line of output from top (to be discussed in detail shortly):
student:/tmp> top | head
top - 10:28:11 up 3:20, 5 users, load average: 1.93, 1.52, 1.25
Tasks: 313 total, 1 running, 312 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.0 us, 0.3 sy, 0.0 ni, 98.2 id, 0.5 wa, 0.0 hi, 0.0 si, 0.0
KiB Mem : 16284472 total, 6556792 free, 1029760 used, 8697920 buff/cache
KiB Swap: 8290300 total, 8290300 free, 0 used. 10364220 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND


2615 coop 20 0 504536 186312 65488 S 6.7 1.1 6:28.30 skype-b+
18248 coop 20 0 655804 50816 30884 S 6.7 0.3 0:20.11 emacs
1 root 20 0 204912 6508 3956 S 0.0 0.0 0:00.92 systemd
A third method is to use w:
student:/tmp> w
10:30:51 up 3:23, 5 users, load average: 0.55, 1.11, 1.14
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
coop :0 :0 07:08 ?xdm? 16:51 0.19s gdm-session-
coop pts/0 :0 07:09 2:22m 0.12s 0.12s bash
coop pts/1 :0 07:09 1:37m 0.42s 0.42s bash
coop pts/2 :0 07:09 0.00s 51.09s 0.00s w
coop pts/3 :0 07:09 27:08 0.25s 0.25s bash

Lab: Background and Foreground Jobs


We are going to launch a graphical program from a terminal window, so that one can no longer type
in the window. gedit is an easy choice but you can substitute any other program that does this.
1. Open gedit on a new file as in $ gedit somefile
2. While you can still type in the terminal window, the shell will not pay attention
to what you input.
3. While your pointer is over the terminal window, hit CTRL-Z. ^Z
4. [3]+ Stopped gedit somefile
5. You can no longer type in the gedit window.
6. With jobs -l, see what processes have been launched from this terminal window: $
jobs -l
7. [1] 17705 Running evince *pdf &
8. [2]- 18248 Running emacs /tmp/[Link] &
9. [3]+ 19827 Stopped gedit somefile
10.
11. Now put the most recent job (gedit somefile) in background: $ bg
12. [3]+ gedit somefile &
13. Now you should be able to type in the gedit window.
14. Put the process in foreground again: $ fg
15. gedit somefile
16. Note you once again input to the terminal window. has no effect
17. To clean up, suspend the process again and then use kill to terminate it: ^Z
18. [3]+ Stopped gedit somefile
19. $ jobs -l
20. [1] 17705 Running evince *pdf &
21. [2]- 18248 Running emacs /tmp/[Link] &
22. [3]+ 19827 Stopped gedit somefile
23. $ kill -9 19827
24. $ jobs -l
25. [1] 17705 Running evince *pdf &
26. [2]- 18248 Running emacs /tmp/[Link] &
27. [3]+ 19827 Killed gedit somefile
28. $ jobs -l
29. [1]- 17705 Running evince *pdf &
30. [2]- 18248 Running emacs /tmp/[Link] &

ps provides information about currently running processes keyed by PID. If you want a repetitive
update of this status, you can use top or other commonly installed variants (such as htop
or atop) from the command line, or invoke your distribution's graphical system monitor application.

Without options, ps will display all processes running under the current shell. You can use the -u
option to display information of processes for a specified username. The command ps -ef displays all
the processes in the system in full detail. The command ps -eLf goes one step further and displays
one line of information for every thread (remember, a process can contain multiple threads).other
options can be found with man option

pstree displays the processes running on the system in the form of a tree diagram showing the
relationship between a process and its parent process and any other processes that it created.
Repeated entries of a process are not displayed, and threads are displayed in curly braces.

A better alternative is to use top to get constant real-time updates (every two seconds by default),
until you exit by typing [Link] clearly highlights which processes are consuming the most CPU cycles
and memory (using appropriate commands from within top).

The first line of the top output displays a quick summary of what is happening in the system,
including:
• How long the system has been up
• How many users are logged on
• What is the load average
The load average determines how busy the system is. A load average of 1.00 per CPU indicates a
fully subscribed, but not overloaded, system. If the load average goes above this value, it indicates
that processes are competing for CPU time. If the load average is very high, it might indicate that the
system is having a problem, such as a runaway process (a process in a non-responding state).

The second line of the top output displays the total number of processes, the number of running,
sleeping, stopped, and zombie processes. Comparing the number of running processes with the load
average helps determine if the system has reached its capacity or perhaps a particular user is
running too many processes. The stopped processes should be examined to see if everything is
running correctly.

The third line of the top output indicates how the CPU time is being divided between the users (us)
and the kernel (sy) by displaying the percentage of CPU time used for each.
The percentage of user jobs running at a lower priority (niceness - ni) is then listed. Idle mode (id)
should be low if the load average is high, and vice versa. The percentage of jobs waiting (wa) for I/O
is listed. Interrupts include the percentage of hardware (hi) vs. software interrupts (si). Steal time (st)
is generally used with virtual machines, which has some of its idle CPU time taken for other uses.
The fourth and fifth lines of the top output indicate memory usage, which is divided in two
categories:
• Physical memory (RAM) – displayed on line 4.
• Swap space – displayed on line 5.
Both categories display total memory, used memory, and free space.
You need to monitor memory usage very carefully to ensure good system performance. Once the
physical memory is exhausted, the system starts using swap space (temporary storage space on the
hard drive) as an extended memory pool, and since accessing disk is much slower than accessing
memory, this will negatively affect system performance.
If the system starts using swap often, you can add more swap space. However, adding more physical
memory should also be considered.

Each line in the process list of the top output displays information about a process. By default,
processes are ordered by highest CPU usage. The following information about each process is
displayed:
• Process Identification Number (PID)
• Process owner (USER)
• Priority (PR) and nice values (NI)
• Virtual (VIRT), physical (RES), and shared memory (SHR)
• Status (S)
• Percentage of CPU (%CPU) and memory (%MEM) used
• Execution time (TIME+)
• Command (COMMAND).

The table lists what happens when pressing various keys when running top:

Command Output
t Display or hide summary information (rows 2 and 3)
m Display or hide memory information (rows 4 and 5)
A Sort the process list by top resource consumers
r Renice (change the priority of) a specific processes
k Kill a specific process
f Enter the top configuration screen
o Interactively select a new sort order in the process list

Suppose you need to perform a task on a specific day sometime in the future. However, you know
you will be away from the machine on that day. How will you perform the task? You can use the
at utility program to execute any non-interactive command at a specified time, as illustrated in the
screenshot below:

cron is a time-based scheduling utility program. It can launch routine background jobs at specific
times and/or days on an on-going basis. cron is driven by a configuration file called /etc/crontab
(cron table), which contains the various shell commands that need to be run at the properly
scheduled times. There are both system-wide crontab files and individual user-based ones. Each line
of a crontab file represents a job, and is composed of a so-called CRON expression, followed by a
shell command to execute.
Typing crontab -e will open the crontab editor to edit existing jobs or to create new jobs. Each line of
the crontab file will contain 6 fields:

Field Description Values


MIN Minutes 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6 (0 = Sunday)
CMD Command Any command to be executed

Examples:
• The entry * * * * * /usr/local/bin/execute/this/[Link] will schedule a job to
execute [Link] every minute of every hour of every day of the month, and every month and every
day in the week.
• The entry 30 08 10 06 * /home/sysadmin/full-backup will schedule a full-backup at
8.30 a.m., 10-June, irrespective of the day of the week.

sleep suspends execution for at least the specified period of time, which can be given as the number
of seconds (the default), minutes, hours, or days. After that time has passed (or an interrupting
signal has been received), execution will resume.
The syntax is: sleep NUMBER[SUFFIX]... where SUFFIX may be:
• s for seconds (the default)
• m for minutes
• h for hours
• d for days.
sleep and at are quite different; sleep delays execution for a specific period, while at starts execution
at a later time.

Lab: Using at for Batch Processing in the Future


1. Create the file [Link] containing: #!/bin/bash
2. date > /tmp/datestamp
3. and then make it executable and queue it up with at: $ chmod +x [Link]
4. $ at now + 1 minute -f [Link]
5. You can see if the job is queued up to run with atq: $ atq
6. 17 Wed Apr 22 08:55:00 2015 a student
7. Make sure the job actually ran: $ cat /tmp/datestamp
8. Wed Apr 22 08:55:00 CDT 2015
9. What happens if you take the /tmp/datestamp out of the command? (Hint:
type mail if not prompted to do so!)
10. Interactively it is basically the same procedure. Just queue up the job with: $ at
now + 1 minute
11. at> date > /tmp/datestamp
12. CTRL-D
13. $ atq

Lab: Scheduling a Periodic Task with cron


Set up a cron job to do some simple task every day at 10 AM. Create a file named mycrontab with
the following content:
0 10 * * * /tmp/[Link]

and then create /tmp/[Link] containing:


#!/bin/bash
echo Hello I am running $0 at $(date)

and make it executable:


$ chmod +x /tmp/[Link]

Put it in the crontab system with:


$ crontab mycrontab

and verify it was loaded with:


$ crontab -l
0 10 * * * /tmp/[Link]
$ sudo ls -l /var/spool/cron/student
-rw------- 1 student student 25 Apr 22 09:59 /var/spool/cron/student
$ sudo cat /var/spool/cron/student
0 10 * * * /tmp/[Link]

Note if you don't really want this running every day, printing out messages like:
Hello I am running /tmp/[Link] at Wed Apr 22 10:03:48 CDT 2015

and mailing them to you, you can remove it with:


$ crontab -r

If the machine is not up at 10 AM on a given day, anacron will run the job at a suitable time.

The mount command is used to attach a filesystem (which can be local to the computer or on a
network) somewhere within the filesystem tree. The basic arguments are the device node and
mount point. For example,
$ sudo mount /dev/sda5 /home

To unmount the partition, the command would be:


$ sudo umount /home

On the server machine, NFS uses daemons (built-in networking and service processes in Linux) and
other system servers are started at the command line by typing:
$ sudo systemctl start nfs
NOTE: On RHEL/CentOS 8, the service is called nfs-server, not nfs.
The text file /etc/exports contains the directories and permissions that a host is willing to share with
other systems over NFS. A very simple entry in this file may look like the following:
/projects *.[Link](rw)
This entry allows the directory /projects to be mounted using NFS with read and
write (rw) permissions and shared with other hosts in the [Link] domain. As we will detail in
the next chapter, every file in Linux has three possible permissions: read (r), write (w) and execute
(x).
After modifying the /etc/exports file, you can type exportfs -av to notify Linux about the directories
you are allowing to be remotely mounted using NFS. You can also restart NFS with sudo systemctl
restart nfs, but this is heavier, as it halts NFS for a short while before starting it up again. To make
sure the NFS service starts whenever the system is booted, issue sudo systemctl enable nfs.

On the client machine, if it is desired to have the remote filesystem mounted automatically upon
system boot, /etc/fstab is modified to accomplish this. For example, an entry in the client's
/etc/fstab might look like the following:
servername:/projects /mnt/nfs/projects nfs defaults 0 0
You can also mount the remote filesystem without a reboot or as a one-time mount by directly using
the mount command:
$ sudo mount servername:/projects /mnt/nfs/projects
Remember, if /etc/fstab is not modified, this remote mount will not be present the next time the
system is restarted. Furthermore, you may want to use the nofail option in fstab in case the NFS
server is not live at boot.

Another way to show mounted filesystems is to type:


sudent:/tmp> cat /proc/mounts

Issue the command:


student:/tmp> cat /etc/fstab
Now type:
student:/tmp> mount
Compare the results. What are the differences?
Find another way to see a list of the mounted filesystems, by examining the /proc pseudo-
filesystem.

Each user has a home directory, usually placed under /home. The /root ("slash-root") directory on
modern Linux systems is no more than the home directory of the root user (or superuser, or system
administrator account).
On multi-user systems, the /home directory infrastructure is often mounted as a separate filesystem
on its own partition, or even exported (shared) remotely on a network through NFS.
The /bin directory contains executable binaries, essential commands used to boot the system or in
single-user mode, and essential commands required by all system users, such as cat, cp, ls, mv,
ps, and rm.

The /proc filesystem contains virtual files (files that exist only in memory) that permit viewing
constantly changing kernel data. /proc contains files and directories that mimic kernel structures and
configuration information.

The /dev directory contains device nodes, a type of pseudo-file used by most hardware and software
devices, except for network devices.

The /var directory contains files that are expected to change in size and content as the system is
running (var stands for variable),

The /etc directory is the home for system configuration files. It contains no binary programs,
although there are some executable scripts. Note that /etc is for system-wide configuration files and
only the superuser can modify files there. User-specific configuration files are always found under
their home directory.

The /boot directory contains the few essential files needed to boot the system. For every alternative
kernel installed on the system there are four files:
1. vmlinuz The compressed Linux kernel, required for booting.
2. initramfs The initial ram filesystem, required for booting, sometimes called initrd,
not initramfs.
3. config The kernel configuration file, only used for debugging and bookkeeping.
4. [Link] Kernel symbol table, only used for debugging.
Each of these files has a kernel version appended to its name.
The Grand Unified Bootloader (GRUB) files such as /boot/grub/[Link] or
/boot/grub2/[Link] are also found under the /boot directory.

/lib contains libraries (common code shared by applications and needed for them to run) for the
essential programs in /bin and /sbin. These library filenames either start with ld or lib. For example,
/lib/[Link].5.9. Kernel modules (kernel code, often device drivers, that can be loaded and
unloaded without re-starting the system) are located in /lib/modules/<kernel-version-number>.

While historically this was done under the /media directory, modern Linux distributions place these
mount points under the /run directory. For example, a USB pen drive with a label myusbdrive for a
user named student would be mounted at /run/media/student/myusbdrive. The /mnt directory has
been used since the early days of UNIX for temporarily mounting filesystems. These can be those on
removable media, but more often might be network filesystems, which are not normally mounted.
Or these can be temporary partitions, or so-called loopback filesystems, which are files which
pretend to be partitions.

There are some additional directories to be found under the root directory:

Directory Name Usage


/opt Optional application software packages
/sys Virtual pseudo-filesystem giving information about the system and the hardware
Can be used to alter system parameters and for debugging purposes
/srv Site-specific data served up by the system
Seldom used
/tmp Temporary files; on some distributions erased across a reboot and/or may actually be a
ramdisk in memory
/usr Multi-user applications, utilities and data

The /usr directory tree contains theoretically non-essential programs and scripts (in the sense that
they should not be needed to initially boot the system) and has at least the following sub-
directories:

Directory Name Usage


/usr/include Header files used to compile applications
/usr/lib Libraries for programs in /usr/bin and /usr/sbin
/usr/lib64 64-bit libraries for 64-bit programs in /usr/bin and /usr/sbin
/usr/sbin Non-essential system binaries, such as system daemons
/usr/share Shared data used by applications, generally architecture-independent
/usr/src Source code, usually for the Linux kernel
/usr/local Data and programs specific to the local machine; subdirectories
include bin, sbin, lib, share, include, etc.
/usr/bin This is the primary directory of executable commands on the system

Now that you know about the filesystem and its structure, let’s learn how to manage files and
directories.
diff is used to compare files and directories. This often-used utility program has many useful options
(see: man diff) including:

diff Option Usage


-c Provides a listing of differences that include three lines of context before and after the lines
differing in content
-r Used to recursively compare subdirectories, as well as the current directory
-i Ignore the case of letters
-w Ignore differences in spaces and tabs (white space)
-q Be quiet: only report if files are different without listing the differences

To compare two files, at the command prompt, type diff [options] <filename1> <filename2>. diff is
meant to be used for text files; for binary files, one can use cmp.
In this section, you will learn additional methods for comparing files and how to apply patches to
files.

You can compare three files at once using diff3, which uses one file as the reference basis for the
other two. For example, suppose you and a co-worker both have made modifications to the same
file working at the same time independently. diff3 can show the differences based on the common
file you both started with. The syntax for diff3 is as follows:
$ diff3 MY-FILE COMMON-FILE YOUR-FILE


Many modifications to source code and configuration files are distributed utilizing patches, which
are applied, not surprisingly, with the patch program. A patch file contains the deltas (changes)
required to update an older version of a file to the new one. The patch files are actually produced by
running diff with the correct options, as in:
$ diff -Nur originalfile newfile > patchfile
Distributing just the patch is more concise and efficient than distributing the entire file. For example,
if only one line needs to change in a file that contains 1000 lines, the patch file will be just a few lines
long.


Using patch

To apply a patch, you can just do either of the two methods below:
$ patch -p1 < patchfile $ patch originalfile patchfile
The first usage is more common, as it is often used to apply changes to an entire directory tree,
rather than just one file, as in the second example. To understand the use of the -p1 option and
many others, see the man page for patch.

The real nature of a file can be ascertained by using the file utility. For the file names given as
arguments, it examines the contents and certain characteristics to determine whether the files are
plain text, shared libraries, executable programs, scripts, or something else.

For this exercise, you could use any text file, but we will use /etc/group as described.
1. student:/tmp> cd /tmp
2.
3. student:/tmp> cp /etc/group /tmp
4.
5. student:/tmp> dd if=/tmp/group of=/tmp/GROUP conv=ucase
6. 1+1 records in
7. 1+1 records out
8. 963 bytes (963 B) copied, 0.000456456 s, 2.1 MB/s
9.
10. student:/tmp> diff -Nur group GROUP > patchfile
11. student:/tmp> cat patchfile
12. --- group 2015-04-17 11:03:26.710813740 -0500
13. +++ GROUP 2015-04-17 11:15:14.602813740 -0500
14. @@ -1,68 +1,68 @@
15. -root:x:0:
16. -daemon:x:1:
17. -bin:x:2:
18. -sys:x:3:
19. ....
20. -libvirtd:x:127:student
21. -vboxsf:x:999:
22. +ROOT:X:0:
23. +DAEMON:X:1:
24. +BIN:X:2:
25. +SYS:X:3:
26. .....
27.
28. student:/tmp> patch --dry-run group patchfile
29. checking file group
30. student:/tmp> patch group patchfile
31. patching file group
32.
33. Note you could have also done either of these two commands: student:/tmp> patch
group < patchfile
34. student:/tmp> patch < patchfile
35.
36. student:/tmp> diff group GROUP
37. student:/tmp>

Backing up data

cp can only copy files to and from destinations on the local machine (unless you are copying to or
from a filesystem mounted using NFS), but rsync can also be used to copy files from one machine to
another. Locations are designated in the target:path form, where target can be in the form of
someone@host. The someone@ part is optional and used if the remote user is different from the
local user.
rsync is very efficient when recursively copying one directory tree to another, because only the
differences are transmitted over the network. One often synchronizes the destination directory tree
with the origin, using the -r option to recursively walk down the directory tree copying all files and
directories below the one listed as the source.

rsync is a very powerful utility. For example, a very useful way to back up a project directory might
be to use the following command:
$ rsync -r project-X archive-machine:archives/project-X. It is highly recommended that you first test
your rsync command using the -dry-run option to ensure that it provides the results that you want.

To use rsync at the command prompt, type rsync sourcefile destinationfile, where either file can be
on the local machine or on a networked machine; The contents of sourcefile will be copied to
destinationfile.
A good combination of options is shown in:
$ rsync --progress -avrxH --delete sourcedir destdir

Compressing data

File data is often compressed to save disk space and reduce the time it takes to transmit files over
networks.
Linux uses a number of methods to perform this compression, including:

Command Usage
gzip The most frequently used Linux compression utility
bzip2 Produces files significantly smaller than those produced by gzip
xz The most space-efficient compression utility used in Linux
zip Is often required to examine and decompress archives from other operating systems

These techniques vary in the efficiency of the compression (how much space is saved) and in how
long they take to compress; generally, the more efficient techniques take longer. Decompression
time does not vary as much across different methods.
In addition, the tar utility is often used to group files in an archive and then compress the whole
archive at once.

gzip is the most often used Linux compression utility. It compresses very well and is very fast. The
following table provides some usage examples:

Command Usage
gzip * Compresses all files in the current directory; each file is compressed and renamed with a .gz
extension.
gzip -r projectX Compresses all files in the projectX directory, along with all files in all of the
directories under projectX.
gunzip foo De-compresses foo found in the file [Link]. Under the hood, the gunzip command is
actually the same as gzip –d.

xz is the most space efficient compression utility used in Linux and is used to store archives of the
Linux kernel. Once again, it trades a slower compression speed for an even higher compression ratio.
Some usage examples:

Command Usage
xz * Compresses all of the files in the current directory and replaces each file with one with a .xz
extension.
xz foo Compresses foo into [Link] using the default compression level (-6), and removes foo if
compression succeeds.
xz -dk [Link] Decompresses [Link] into bar and does not remove [Link] even if decompression is
successful.
xz -dcf [Link] [Link] > [Link] Decompresses a mix of compressed and uncompressed files to
standard output, using a single command.
xz -d *.xz Decompresses the files compressed using xz.

The zip program is not often used to compress files in Linux, but is often required to examine and
decompress archives from other operating systems. It is only used in Linux when you get a zipped
file from a Windows user. It is a legacy program.

Command Usage
zip backup * Compresses all files in the current directory and places them in the [Link].
zip -r [Link] ~ Archives your login directory (~) and all files and directories under it in
[Link].
unzip [Link] Extracts all files in [Link] and places them in the current directory
Historically, tar stood for "tape archive" and was used to archive files to a magnetic tape. It allows
you to create or extract files from an archive file, often called a tarball. At the same time, you can
optionally compress while creating the archive, and decompress while extracting its contents.
Here are some examples of the use of tar:

Command Usage
tar xvf [Link] Extract all the files in [Link] into the mydir directory.
tar zcvf [Link] mydir Create the archive and compress with gzip.
tar jcvf [Link].bz2 mydir Create the archive and compress with bz2.
tar Jcvf [Link] mydir Create the archive and compress with xz.
tar xvf [Link] Extract all the files in [Link] into the mydir directory.
NOTE: You do not have to tell tar it is in gzip format

The dd program is very useful for making copies of raw disk space. For example, to back up your
Master Boot Record (MBR) (the first 512-byte sector on the disk that contains a table describing the
partitions on that disk), you might type:
dd if=/dev/sda of=[Link] bs=512 count=1
WARNING!
Typing:
dd if=/dev/sda of=/dev/sdb
to make a copy of one disk onto another, will delete everything that previously existed on the
second disk.
An exact copy of the first disk device is created on the second disk device.
Do not experiment with this command as written above, as it can erase a hard disk!

Archiving (Backing Up) the Home Directory


To construct a tarball archive of your home directory you can do:
student:/tmp> tar -cvf /tmp/[Link] ~

or equivalently
student:/tmp> tar -cvf /tmp/[Link] /home/student

Note you can have omitted the - in the options with no change. In the following we will not bother
using the -v option for verbose. To create archives with all three compression utilities:
student:/tmp> tar zcf /tmp/[Link] ~
student:/tmp> tar jcf /tmp/[Link].bz2 ~
student:/tmp> tar Jcf /tmp/[Link] ~

Comparing the sizes (first using the -h option to ls to make it human-readable):


student@ubuntu:~student:/tmp> ls -lh /tmp/backup*
-rw-rw-r-- 1 student student 8.3M Apr 17 10:14 /tmp/[Link]
-rw-rw-r-- 1 student student 12M Apr 17 10:13 /tmp/[Link]
-rw-rw-r-- 1 student student 8.4M Apr 17 10:15 /tmp/[Link].bz2
-rw-rw-r-- 1 student student 8.3M Apr 17 10:14 /tmp/[Link]
-rw-rw-r-- 1 student student 8.2M Apr 17 10:15 /tmp/[Link]

and then without it:


student@ubuntu:~student:/tmp> ls -l /tmp/backup*
-rw-rw-r-- 1 student student 8686942 Apr 17 10:14 /tmp/[Link]
-rw-rw-r-- 1 student student 12226560 Apr 17 10:13 /tmp/[Link]
-rw-rw-r-- 1 student student 8720491 Apr 17 10:15 /tmp/[Link].bz2
-rw-rw-r-- 1 student student 8686929 Apr 17 10:14 /tmp/[Link]
-rw-rw-r-- 1 student student 8551064 Apr 17 10:15 /tmp/[Link]

Note in this case there is not much difference in the different archiving methods, but this particular
directory was a bad choice because it already contained a lot of compressed files. A somewhat
better example involving more text files:
student:/tmp> tar cf /tmp/[Link] /usr/share/doc
student:/tmp> tar zcf /tmp/[Link] /usr/share/doc
student:/tmp> tar jcf /tmp/[Link].bz2 /usr/share/doc
student:/tmp> tar Jcf /tmp/[Link] /usr/share/doc

student:/tmp> ls -lh /tmp/[Link]*


-rw-rw-r-- 1 student student 85M Apr 17 10:34 /tmp/[Link]
-rw-rw-r-- 1 student student 31M Apr 17 10:35 /tmp/[Link].bz2
-rw-rw-r-- 1 student student 34M Apr 17 10:34 /tmp/[Link]
-rw-rw-r-- 1 student student 28M Apr 17 10:36 /tmp/[Link]

which shows xz did best, followed by bz2 and then gz. You may have noticed, however, the inverse
relationship between the size reduction of the compression and how long it took!

Text editing

Creating Files Without Using an Editor

As a graphical editor, gedit is part of the GNOME desktop system (kwrite is associated with KDE). The
gedit and kwrite editors are very easy to use and are extremely capable. They are also very
configurable. They look a lot like Notepad in Windows. Other variants such as kate are also
supported by KDE.

nano is easy to use, and requires very little effort to learn. To open a file, type nano <filename> and
press Enter. If the file does not exist, it will be created.
nano provides a two line shortcut bar at the bottom of the screen that lists the available commands.

gedit (pronounced 'g-edit') is a simple-to-use graphical editor that can only be run within a Graphical
Desktop environment. It is visually quite similar to the Notepad text editor in Windows, but is
actually far more capable and very configurable and has a wealth of plugins available to extend its
capabilities further.
To open a new file find the program in your desktop's menu system, or from the command line type
gedit <filename>. If the file does not exist, it will be created.
GNOME extends vi with a very graphical interface known as gvim and KDE offers kvim. Either of
these may be easier to use at first.

VI

Vimtutor

Mode Feature
Command • By default, vi starts in Command mode.
• Each key is an editor command.
• Keyboard strokes are interpreted as commands that can modify file contents.
Insert • Type i to switch to Insert mode from Command mode.
• Insert mode is used to enter (insert) text into a file.
• Insert mode is indicated by an “? INSERT ?” indicator at the bottom of the screen.
• Press Esc to exit Insert mode and return to Command mode.
Line • Type : to switch to the Line mode from Command mode. Each key is an external
command, including operations such as writing the file contents to disk or exiting.
• Uses line editing commands inherited from older line editors. Most of these
commands are actually no longer used. Some line editing commands are very powerful.
• Press Esc to exit Line mode and return to Command mode

The table describes the most important commands used to start, exit, read, and write files in vi. The
ENTER key needs to be pressed after all of these commands.

Command Usage
vi myfile Start the editor and edit myfile
vi -r myfile Start and edit myfile in recovery mode from a system crash
:r file2 Read in file2 and insert at current position
:w Write to the file
:w myfile Write out to myfile
:w! file2 Overwrite file2
:x or :wq Exit and write out modified file
:q Quit
:q! Quit even though modifications have not been saved

The table describes the most important keystrokes used when changing cursor position in vi. Line
mode commands (those following colon : ) require the ENTER key to be pressed after the command
is typed.

Key Usage
arrow keys To move up, down, left and right
j or <ret> To move one line down
k To move one line up
h or Backspace To move one character left
l or Space To move one character right
0 To move to beginning of line
$ To move to end of line
w To move to beginning of next word
:0 or 1G To move to beginning of file
:n or nG To move to line n
:$ or G To move to last line in file
CTRL-F or Page Down To move forward one page
CTRL-B or Page Up To move backward one page
^l To refresh and center scree

The table describes the most important commands used when searching for text in vi. The ENTER
key should be pressed after typing the search pattern.

Command Usage
/pattern Search forward for pattern
?pattern Search backward for pattern

The table describes the most important keystrokes used when searching for text in vi.

Key Usage
n Move to next occurrence of search pattern
N Move to previous occurrence of search patter

The table describes the most important keystrokes used when changing, adding, and deleting text in
vi.

Key Usage
a Append text after cursor; stop upon Escape key
A Append text at end of current line; stop upon Escape key
i Insert text before cursor; stop upon Escape key
I Insert text at beginning of current line; stop upon Escape key
o Start a new line below current line, insert text there; stop upon Escape key
O Start a new line above current line, insert text there; stop upon Escape key
r Replace character at current position
R Replace text starting with current position; stop upon Escape key
x Delete character at current position
Nx Delete N characters, starting at current position
dw Delete the word at the current position
D Delete the rest of the current line
dd Delete the current line
Ndd or dNd Delete N lines
u Undo the previous operation
yy Yank (copy) the current line and put it in buffer
Nyy or yNy Yank (copy) N lines and put it in buffer
p Paste at the current position the yanked line or lines from the buffer

Typing sh command opens an external command shell. When you exit the shell, you will resume
your editing session.
Typing ! executes a command from within vi. The command follows the exclamation point. This
technique is best suited for non-interactive commands, such as : ! wc %. Typing this will run the
wc (word count) command on the file; the character % represents the file currently being edited.

vi external Commands

The table lists some of the most important key combinations that are used when starting, exiting,
reading, and writing files in emacs.

Key Usage
emacs myfile Start emacs and edit myfile
CTRL-x i Insert prompted for file at current position
CTRL-x s Save all files
CTRL-x CTRL-w Write to the file giving a new name when prompted
CTRL-x CTRL-s Saves the current file
CTRL-x CTRL-c Exit after being prompted to save any modified files

The emacs tutorial is a good place to start learning basic commands. It is available any time when in
emacs by simply typing CTRL-h (for help) and then the letter t for tutorial.

The table lists some of the keys and key combinations that are used for changing cursor positions in
emacs.

Key Usage
arrow keys Use the arrow keys for up, down, left and right
CTRL-n One line down
CTRL-p One line up
CTRL-f One character forward/right
CTRL-b One character back/left
CTRL-a Move to beginning of line
CTRL-e Move to end of line
Meta-f Move to beginning of next word
Meta-b Move back to beginning of preceding word
Meta-< Move to beginning of file
Meta-g-g-n Move to line n (can also use 'Esc-x Goto-line n')
Meta-> Move to end of file
CTRL-v or Page Down Move forward one page
Meta-v or Page Up Move backward one page
CTRL-l Refresh and center screen

The table lists the key combinations that are used for searching for text in emacs.

Key Usage
CTRL-s Search forward for prompted pattern, or for next pattern
CTRL-r Search backwards for prompted pattern, or for next patter

The table lists some of the key combinations used for changing, adding, and deleting text in emacs:
Key Usage
CTRL-o Insert a blank line
CTRL-d Delete character at current position
CTRL-k Delete the rest of the current line
CTRL-_ Undo the previous operation
CTRL- (space or CTRL-@) Mark the beginning of the selected region. The end will be at the
cursor position
CTRL-w Delete the current marked text and write it to the buffer
CTRL-y Insert at current cursor location whatever was most recently deleted

There is no shortage of online tutorials on the use of the basic editors available. One of the examples
includes an interactive vi tutorial.
For emacs, it is as simple as starting the program (you do not even have to give a file name) and then
typing Ctl-h and then t. This will launch the built-in emacs tutorial.
You should take some time to familiarize yourself with these tutorials.

Identifying a user

• To identify the current user, type whoami.


• To list the currently logged-on users, type who.
Giving who the -a option will give more detailed information.

In Linux, the command shell program (generally bash) uses one or more startup files to configure the
user environment. Files in the /etc directory define global settings for all users, while initialization
files in the user's home directory can include and/or override the global settings.

You can create customized commands or modify the behavior of already existing ones by creating
aliases. Most often, these aliases are placed in your ~/.bashrc file so they are available to any
command shells you create. unalias removes an alias.
Typing alias with no arguments will list currently defined aliases.
Please note there should not be any spaces on either side of the equal sign and the alias definition
needs to be placed within either single or double quotes if it contains any spaces.

Adding a new user is done with useradd and removing an existing user is done with userdel. In the
simplest form, an account for the new user bjmoose would be done with:
$ sudo useradd bjmoose
which, by default, sets the home directory to /home/bjmoose, populates it with some basic files
(copied from /etc/skel) and adds a line to /etc/passwd such as:
bjmoose:x:1002:1002::/home/bjmoose:/bin/bash
and sets the default shell to /bin/bash. Removing a user account is as easy as typing userdel
bjmoose. However, this will leave the /home/bjmoose directory intact. This might be useful if it is a
temporary inactivation. To remove the home directory while removing the account one needs to use
the -r option to userdel.
Typing id with no argument gives information about the current user, as in:
$ id uid=1002(bjmoose) gid=1002(bjmoose) groups=106(fuse),1002(bjmoose)
If given the name of another user as an argument, id will report information about that other user.

Adding a new group is done with groupadd:


$ sudo /usr/sbin/groupadd anewgroup
The group can be removed with:
$ sudo /usr/sbin/groupdel anewgroup
Adding a user to an already existing group is done with usermod. For example, you would first look
at what groups the user already belongs to:
$ groups rjsquirrel bjmoose : rjsquirrel
and then add the new group:
$ sudo /usr/sbin/usermod -a -G anewgroup rjsquirrel
$ groups rjsquirrel rjsquirrel: rjsquirrel anewgroup
These utilities update /etc/group as necessary. Make sure to use the -a option, for append, so as to
avoid removing already existing groups. groupmod can be used to change group properties, such as
the Group ID (gid) with the -g option or its name with then -n option.
Removing a user from the group is somewhat trickier. The -G option to usermod must give a
complete list of groups. Thus, if you do:
$ sudo /usr/sbin/usermod -G rjsquirrel rjsquirrel
$ groups rjsquirrel rjsquirrel : rjsquirrel
only the rjsquirrel group will be left.

sudo configuration files are stored in the /etc/sudoers file and in the /etc/sudoers.d/ directory. By
default, the sudoers.d directory is empty.

Lab Solution: Deploying aliases


The alias line would look like this:
student:/tmp> alias projx='cd /home/staff/RandD/projects/projectX/src'
Note you can use double quotes instead of single quotes, or use no quotes at all since there is no
white space in your defined alias. All you have to do now to change to the directory is
student:/tmp> projx
To make the alias persistent, just place it in your $HOME/.bashrc file.

Environment variables are quantities that have specific values which may be utilized by the
command shell, such as bash, or other utilities and applications. Some environment variables are
given preset values by the system (which can usually be overridden), while others are set directly by
the user, either at the command line or within startup and other scripts.
An environment variable is actually just a character string that contains information used by one or
more applications. There are a number of ways to view the values of currently set environment
variables; one can type set, env, or export. Depending on the state of your system, set may print out
many more lines than the other two methods.

By default, variables created within a script are only available to the current shell; child processes
(sub-shells) will not have access to values that have been set or modified. Allowing child processes to
see the values requires use of the export command.
Task Command
Show the value of a specific variable echo $SHELL
Export a new variable value export VARIABLE=value (or VARIABLE=value; export VARIABLE)
Add a variable permanently Edit ~/.bashrc and add the line export VARIABLE=value
Type source ~/.bashrc or just . ~/.bashrc (dot ~/.bashrc); or just start a new shell by typing bash

You can also set environment variables to be fed as a one shot to a command as in:
$ SDIRS=s_0* KROOT=/lib/modules/$(uname -r)/build make modules_install
which feeds the values of the SDIRS and KROOT environment variables to the command make
modules_install.

HOME is an environment variable that represents the home (or login) directory of the user. cd
without arguments will change the current working directory to the value of HOME. Note the tilde
character (~) is often used as an abbreviation for $HOME. Thus, cd $HOME and cd ~ are completely
equivalent statements.

Command Explanation
$ echo $HOME /home/me $ cd /bin Show the value of the HOME environment variable, then
change directory (cd) to /bin.
$ pwd /bin Where are we? Use print (or present) working directory (pwd) to find out. As
expected, /bin.
$ cd Change directory without an argument...
$ pwd /home/me ...takes us back to HOME, as you can now see.

The screenshot demonstrates this.

• path1::path2
In the example :path1:path2, there is a null directory before the first colon (:). Similarly, for
path1::path2 there is a null directory between path1 and path2.
To prefix a private bin directory to your path:
$ export PATH=$HOME/bin:$PATH $ echo
$PATH /home/student/bin:/usr/local/bin:/usr/bin:/bin/usr


The environment variable SHELL points to the user's default command shell (the program that is
handling whatever you type in a command window, usually bash) and contains the full pathname to
the shell:
$ echo $SHELL /bin/bash $

Prompt Statement (PS) is used to customize your prompt string in your terminal windows to display
the information you want.
PS1 is the primary prompt variable which controls what your command line prompt looks like. The
following special characters can be included in PS1:
\u - User name \h - Host name \w - Current working directory \! - History number of this
command \d - Date
They must be surrounded in single quotes when they are used, as in the following example:
$ echo $PS1 $ $ export PS1='\u@\h:\w$ ' student@[Link]:~$ # new prompt
To revert the changes:
student@[Link]:~$ export PS1='$ ' $
An even better practice would be to save the old prompt first and then restore, as in:
$ OLD_PS1=$PS1
change the prompt, and eventually change it back with:
$ PS1=$OLD_PS1 $

Adding /tmp to Your Path


First create the phony ls program, using an editor, or just simply doing:
student:/tmp>echo "echo HELLO, this is the phony ls program." > /tmp/ls
student:/tmp>chmod +x /tmp/ls
For the next two steps it is a good idea to work in another terminal window, or just start a new shell,
so the changes do not persist on later issued commands. You can start a new shell by just typing
bash.
1. student:/tmp>bash
2. student:/tmp>PATH=$PATH:/tmp
3. student:/tmp>ls /usr
4. bin etc games include lib lib64 libexec local sbin share src tmp
5. student:/tmp>exit
6.
7. student:/tmp>bash
8. student:/tmp>PATH=/tmp:$PATH
9. student:/tmp>ls /usr
10. HELLO, this is the phony ls program.
11. student:/tmp>exit
12.
Note the second form is a very dangerous thing to do, and is a trivial way to insert a Trojan Horse
program; if someone can put a malicious program in /tmp, they can trick you into running it
accidentally.

Changing the Command Line Prompt


1. $ echo $PWD
2. /tmp
3. $ PS1='\w>'
4. /tmp>
5.
6. PS1='\h>'
7. student>
8.
9. PS1='\h:\w>'
10. student:/tmp>

To view the list of previously executed commands, you can just type history at the command line.
The list of commands is displayed with the most recent command appearing last in the list. This
information is stored in ~/.bash_history.

Several associated environment variables can be used to get information about the history file.
• HISTFILE The location of the history file.
• HISTFILESIZE The maximum number of lines in the history file (default 500).
• HISTSIZE The maximum number of commands in the history file.
• HISTCONTROL How commands are stored.
• HISTIGNORE Which command lines can be unsaved.
For a complete description of the use of these environment variables, see man bash.

The following is an example of how you can use the CTRL-R command to search through the
command history:
$ ^R (This all happens on 1 line) (reverse-i-search)'s': sleep
1000 (Searched for 's'; matched "sleep") $ sleep
1000 (Pressed Enter to execute the searched command) $

The following utility programs involve user and group ownership and permission setting:

Command Usage
chown Used to change user ownership of a file or directory
chgrp Used to change group ownership
chmod Used to change the permissions on the file, which can be done separately for owner, group
and the rest of the world (often named as other)

Files have three kinds of permissions: read (r), write (w), execute (x). These are generally
represented as in rwx. These permissions affect three groups of owners: user/owner (u), group (g),
and others (o).
As a result, you have the following three groups of three permissions:
rwx: rwx: rwx u: g: o
There are a number of different ways to use chmod. For instance, to give the owner and others
execute permission and remove the group write permission:
$ ls -l somefile -rw-rw-r-- 1 student student 1601 Mar 9 15:04 somefile $ chmod uo+x,g-w
somefile $ ls -l somefile -rwxr--r-x 1 student student 1601 Mar 9 15:04 somefile
where u stands for user (owner), o stands for other (world), and g stands for group.
This kind of syntax can be difficult to type and remember, so one often uses a shorthand which lets
you set all the permissions in one step. This is done with a simple algorithm, and a single digit
suffices to specify all three permission bits for each entity. This digit is the sum of:
• 4 if read permission is desired
• 2 if write permission is desired
• 1 if execute permission is desired.
Thus, 7 means read/write/execute, 6 means read/write, and 5 means read/execute.
When you apply this to the chmod command, you have to give three digits for each degree of
freedom, such as in:
$ chmod 755 somefile $ ls -l somefile -rwxr-xr-x 1 student student 1601 Mar 9 15:04 somefile

Let's see an example of changing file ownership using chown, as shown in the screenshot to the
right. First, we create two empty files using touch.
Notice it requires sudo to change the owner of file2 to root. The second chown command changes
both owner and group at the same time!
Finally, only the superuser can remove the files.

Now, let’s see an example of changing the group ownership using chgrp:

For example, cat [Link] will display the contents of [Link] on the terminal. However, the
main purpose of cat is often to combine (concatenate) multiple files together. You can perform the
actions listed in the table using cat.
The tac command (cat spelled backwards) prints the lines of a file in reverse order. Each line remains
the same, but the order of lines is inverted. The syntax of tac is exactly the same as for cat, as in:
$ tac file $ tac file1 file2 > newfile

Command Usage
cat file1 file2 Concatenate multiple files and display the output; i.e. the entire content of the first
file is followed by that of the second file
cat file1 file2 > newfile Combine multiple files and save the output into a new file
cat file >> existingfile Append a file to the end of an existing file
cat > file Any subsequent lines typed will go into the file, until CTRL-D is typed
cat >> file Any subsequent lines are appended to the file, until CTRL-D is type

cat can be used to read from standard input (such as the terminal window) if no files are specified.
You can use the > operator to create and add lines into a new file, and the >> operator to append
lines (or files) to an existing file. We mentioned this when talking about how to create files without
an editor.
To create a new file, at the command prompt type cat > <filename> and press the Enter key.
This command creates a new file and waits for the user to edit/enter the text. After you finish typing
the required text, press CTRL-D at the beginning of the next line to save and exit the editing.
Another way to create a file at the terminal is cat > <filename> << EOF. A new file is created and you
can type the required input. To exit, enter EOF at the beginning of a line.
Note that EOF is case sensitive. One can also use another word, such as STOP.

echo can be used to display a string on standard output (i.e. the terminal) or to place in a new file
(using the > operator) or append to an already existing file (using the >> operator).
The –e option, along with the following switches, is used to enable special character sequences, such
as the newline character or horizontal tab:
• \n represents newline
• \t represents horizontal tab.
echo is particularly useful for viewing the values of environment variables (built-in shell variables).
For example, echo $USERNAME will print the name of the user who has logged into the current
terminal.
The following table lists echo commands and their usage:

Command Usage
echo string > newfile The specified string is placed in a new file
echo string >> existingfile The specified string is appended to the end of an already existing file
echo $variable The contents of the specified environment variable are displaye

head reads the first few lines of each named file (10 by default) and displays it on standard output.
You can give a different number of lines in an option.
For example, if you want to print the first 5 lines from /etc/default/grub, use the following
command:
$ head –n 5 /etc/default/grub
You can also just say:
head -5 /etc/default/grub

tail prints the last few lines of each named file and displays it on standard output. By default, it
displays the last 10 lines. You can give a different number of lines as an option. tail is especially
useful when you are troubleshooting any issue using log files, as you probably want to see the most
recent lines of output.
For example, to display the last 15 lines of [Link], use the following command:
$ tail -n 15 [Link]
You can also just say:
tail -15 [Link]
To continually monitor new output in a growing log file:
$ tail -f [Link]
This command will continuously display any new lines of output in [Link] as soon as they
appear. Thus, it enables you to monitor any current activity that is being reported and recorded.

When working with compressed files, many standard commands cannot be used directly. For many
commonly-used file and text manipulation programs, there is also a version especially designed to
work directly with compressed files. These associated utilities have the letter "z" prefixed to their
name. For example, we have utility programs such as zcat, zless, zdiff and zgrep.
Here is a table listing some z family commands:

Command Description
$ zcat [Link] To view a compressed file
$ zless [Link]
or
$ zmore [Link] To page through a compressed file
$ zgrep -i less [Link] To search inside a compressed file
$ zdiff [Link] [Link] To compare two compressed files

Note that if you run zless on an uncompressed file, it will still work and ignore the decompression
stage. There are also equivalent utility programs for other compression methods besides gzip, for
example, we have bzcat and bzless associated with bzip2, and xzcat and xzless associated with xz.

sed is a powerful text processing tool and is one of the oldest, earliest and most popular UNIX
utilities. It is used to modify the contents of a file or input stream, usually placing the contents into a
new file or output stream. Its name is an abbreviation for stream editor.


sed

sed can filter text, as well as perform substitutions in data streams.


Data from an input source/file (or stream) is taken and moved to a working space. The entire list of
operations/modifications is applied over the data in the working space and the final contents are
moved to the standard output space (or stream).

You can invoke sed using commands like those listed in the accompanying table.

Command Usage
sed -e command <filename> Specify editing commands at the command line, operate on file and
put the output on standard out (e.g. the terminal)
sed -f scriptfile <filename> Specify a scriptfile containing sed commands, operate on file and
put output on standard out
echo "I hate you" | sed s/hate/love/ Use sed to filter standard input, putting output on standard
out

The -e option allows you to specify multiple editing commands simultaneously at the command line.
It is unnecessary if you only have one operation invoked.

Now that you know that you can perform multiple editing and filtering operations with sed, let’s
explain some of them in more detail. The table explains some basic operations, where pattern is the
current string and replace_string is the new string:
Command Usage
sed s/pattern/replace_string/ file Substitute first string occurrence in every line
sed s/pattern/replace_string/g file Substitute all string occurrences in every line
sed 1,3s/pattern/replace_string/g file Substitute all string occurrences in a range of lines
sed -i s/pattern/replace_string/g file Save changes for string substitution in the same file

You must use the -i option with care, because the action is not reversible. It is always safer to use
sed without the –i option and then replace the file yourself, as shown in the following example:
$ sed s/pattern/replace_string/g file1 > file2
The above command will replace all occurrences of pattern with replace_string in file1 and move the
contents to file2. The contents of file2 can be viewed with cat file2. If you approve, you can then
overwrite the original file with mv file2 file1.
Example: To convert 01/02/… to JAN/FEB/…
sed -e 's/01/JAN/' -e 's/02/FEB/' -e 's/03/MAR/' -e 's/04/APR/' -e 's/05/MAY/' \ -e 's/06/JUN/' -e
's/07/JUL/' -e 's/08/AUG/' -e 's/09/SEP/' -e 's/10/OCT/' \ -e 's/11/NOV/' -e 's/12/DEC/'

The table explains the basic tasks that can be performed using awk. The input file is read one line at
a time, and, for each line, awk matches the given pattern in the given order and performs the
requested action. The -F option allows you to specify a particular field separator character. For
example, the /etc/passwd file uses ":" to separate the fields, so the -F: option is used with the
/etc/passwd file.
The command/action in awk needs to be surrounded with apostrophes (or single-quote (')). awk can
be used as follows:

Command Usage
awk '{ print $0 }' /etc/passwd Print entire file
awk -F: '{ print $1 }' /etc/passwd Print first field (column) of every line, separated by a space
awk -F: '{ print $1 $7 }' /etc/passwd Print first and seventh field of every line

sort is used to rearrange the lines of a text file, in either ascending or descending order according to
a sort key. You can also sort with respect to particular fields (columns) in a file. The default sort key
is the order of the ASCII characters (i.e. essentially alphabetically).
sort can be used as follows:

Syntax Usage
sort <filename> Sort the lines in the specified file, according to the characters at the beginning of
each line
cat file1 file2 | sort Combine the two files, then sort the lines and display the output on the
terminal
sort -r <filename> Sort the lines in reverse order
sort -k 3 <filename> Sort the lines by the 3rd field on each line instead of the beginning

When used with the -u option, sort checks for unique values after sorting the records (lines). It is
equivalent to running uniq (which we shall discuss) on the output of sort.

uniq removes duplicate consecutive lines in a text file and is useful for simplifying the text display.
Because uniq requires that the duplicate entries must be consecutive, one often runs sort first and
then pipes the output into uniq; if sort is used with the -u option, it can do all this in one step.
To remove duplicate entries from multiple files at once, use the following command:
sort file1 file2 | uniq > file3
or
sort -u file1 file2 > file3
To count the number of duplicate entries, use the following command:
uniq -c filename

paste can be used to combine fields (such as name or phone number) from different files, as well as
combine lines from multiple files. For example, line one from file1 can be combined with line one of
file2, line two from file1 can be combined with line two of file2, and so on.
To paste contents from two files one can do:
$ paste file1 file2
The syntax to use a different delimiter is as follows:
$ paste -d, file1 file2
Common delimiters are 'space', 'tab', '|', 'comma', etc.

To combine two files on a common field, at the command prompt type join file1 file2 and press the
Enter key.
For example, the common field (i.e. it contains the same values) among the phonebook and
cities files is the phone number, and the result of joining these two files is shown in the screen
capture.

where we have used wc (word count, soon to be discussed) to report on the number of lines in the
file. Then, typing:
$ split american-english dictionary
will split the American-English file into 100 equal-sized segments named dictionaryxx. The last one
will of course be somewhat smaller.

Regular expressions are text strings used for matching a specific pattern, or to search for a specific
location, such as the start or end of a line or a word. Regular expressions can contain both normal
characters or so-called meta-characters, such as * and $.

Search Patterns Usage


.(dot) Match any single character
a|z Match a or z
$ Match end of a line
^ Match beginning of a line
* Match preceding item 0 or more times
For example, consider the following sentence: the quick brown fox jumped over the lazy dog.
Some of the patterns that can be applied to this sentence are as follows:

Command Usage
a.. matches azy
b.|j. matches both br and ju
..$ matches og
l.* matches lazy dog
l.*y matches lazy
the.* matches the whole sentence

Generate a column containing a unique list of all the shells used for users in /etc/passwd.
You may need to consult the manual page for /etc/passwd as in:
student:/tmp> man 5 passwd
Which field in /etc/passwd holds the account’s default shell (user command interpreter)?
How do you make a list of unique entries (with no repeats)?
Click the link below to view a solution to the Lab exercise.
Lab Solution

Parsing Files with awk (and sort and uniq)


The field in /etc/passwd that holds the shell is number 7. To display the field holding the shell in
/etc/passwd using awk and produce a unique list:
$ awk -F: '{print $7}' /etc/passwd | sort -u
or
$ awk -F: '{print $7}' /etc/passwd | sort | uniq
For example:
$ awk -F: '{print $7}' /etc/passwd | sort -u
/bin/bash
/bin/sync
/sbin/halt
/sbin/nologin
/sbin/shutdown

grep is extensively used as a primary text searching tool. It scans files for specified patterns and can
be used with regular expressions, as well as simple strings, as shown in the table:

Command Usage
grep [pattern] <filename> Search for a pattern in a file and print all matching lines
grep -v [pattern] <filename> Print all lines that do not match the pattern
grep [0-9] <filename> Print the lines that contain the numbers 0 through 9
grep -C 3 [pattern] <filename> Print context of lines (specified number of lines above and below
the pattern) for matching the pattern. Here, the number of lines is specified as

strings is used to extract all printable character strings found in the file or files given as arguments. It
is useful in locating human-readable content embedded in binary files; for text files one can just
use grep.
For example, to search for the string my_string in a spreadsheet: $ strings [Link] | grep
my_string
The screenshot shows a search of a number of programs to see which ones have GPL licenses of
various versions.

In the following we give some examples of things you can do with the grep command; your task is to
experiment with these examples and extend them.
1. Search for your username in file /etc/passwd .
2. Find all entries in /etc/services that include the string ftp:
3. Restrict to those that use the tcp protocol.
4. Now restrict to those that do not use the tcp protocol, while printing out the line
number
5. Get all strings that start with ts or end with st.
Click the link below to view a solution to the Lab exercise.
Lab Solution

Lab Solution: Using grep


1. student:/tmp> grep student /etc/passwd
2.
3. student:/tmp> grep ftp /etc/services
4.
5. student:/tmp> grep ftp /etc/services | grep tcp
6.
7. student:/tmp> grep -n ftp /etc/services | grep -v tcp
8.
9. student:/tmp> grep -e ^ts -e st$ /etc/services

The tr utility is used to translate specified characters into other characters or to delete them. The
general syntax is as follows:
$ tr [options] set1 [set2]
The items in the square brackets are optional. tr requires at least one argument and accepts a
maximum of two. The first, designated set1 in the example, lists the characters in the text to be
replaced or removed. The second, set2, lists the characters that are to be substituted for the
characters listed in the first argument. Sometimes these sets need to be surrounded by apostrophes
(or single-quotes (')) in order to have the shell ignore that they mean something special to the shell.
It is usually safe (and may be required) to use the single-quotes around each of the sets as you will
see in the examples below.
For example, suppose you have a file named city containing several lines of text in mixed case. To
translate all lower case characters to upper case, at the command prompt type cat city | tr a-z A-Z
and press the Enter key.

Command Usage
tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ Convert lower case to upper
case
tr '{}' '()' < inputfile > outputfile Translate braces into parenthesis
echo "This is for testing" | tr [:space:] '\t' Translate white-space to tabs
echo "This is for testing" | tr -s [:space:] Squeeze repetition of characters using -s
echo "the geek stuff" | tr -d 't' Delete specified characters using -d option
echo "my username is 432234" | tr -cd [:digit:] Complement the sets using -c option
tr -cd [:print:] < [Link] Remove all non-printable character from a file
tr -s '\n' ' ' < [Link] Join all the lines in a file into a single line

tee takes the output from any command, and, while sending it to standard output, it also saves it to
a file. In other words, it tees the output stream from the command: one stream is displayed on the
standard output and the other is saved to a file.
For example, to list the contents of a directory on the screen and save the output to a file, at the
command prompt type ls -l | tee newfile and press the Enter key.
Typing cat newfile will then display the output of ls –l.

wc (word count) counts the number of lines, words, and characters in a file or list of files. Options
are given in the table below.

Option Description
–l Displays the number of lines
-c Displays the number of bytes
-w Displays the number of words

By default, all three of these options are active.


For example, to print only the number of lines contained in a file, type wc -l filename and press the
Enter key.

cut is used for manipulating column-based files and is designed to extract specific columns. The
default column separator is the tab character. A different delimiter can be given as a command
option.
For example, to display the third column delimited by a blank space, at the command prompt type ls
-l | cut -d" " -f3 and press the Enter key.

The tee utility is very useful for saving a copy of your output while you are watching it being
generated.
Execute a command such as doing a directory listing of the /etc directory:
student:/tmp> ls -l /etc
while both saving the output in a file and displaying it at your terminal.
Click the link below to view a solution to the Lab exercise.
Lab Solution

Lab Solution: Using tee


student:/tmp> ls -l /etc | tee /tmp/ls-output
student:/tmp> less /tmp/ls-output
total 2948
drwxr-xr-x. 3 root root 4096 Nov 3 07:27 abrt
-rw-r--r--. 1 root root 16 Jan 15 2015 adjtime
-rw-r--r-- 1 root root 1518 Jun 7 2013 aliases
-rw-r--r--. 1 root root 12288 Nov 3 07:49 [Link]
drwxr-xr-x. 2 root root 4096 Nov 3 07:26 alsa
drwxr-xr-x. 2 root root 4096 Jan 20 07:28 alternatives
-rw------- 1 root root 541 Feb 23 2016 anacrontab
-rw-r--r-- 1 root root 55 Jun 6 2016 [Link]
-rw-r--r-- 1 root root 1 May 23 2016 [Link]
drwxr-xr-x. 2 root root 4096 Nov 3 07:26 at-spi2
drwxr-x---. 3 root root 4096 Nov 3 07:26 audisp
drwxr-x---. 3 root root 4096 Nov 3 07:26 audit
drwxr-xr-x. 4 root root 4096 Nov 3 07:32 avahi
drwxr-xr-x. 2 root root 4096 Jan 18 06:59 bash_completion.d
-rw-r--r-- 1 root root 2853 May 4 2016 bashrc
drwxr-xr-x. 2 root root 4096 Nov 7 10:20 binfmt.d
drwxr-xr-x 2 root root 4096 Nov 3 07:26 bluetooth
drwxr-xr-x. 2 root root 4096 Apr 9 2015 bonobo-activation
drwxr-xr-x 2 root root 12288 Nov 3 07:26 brltty
-rw-r--r-- 1 root root 21929 May 6 2016 [Link]
-rw-r--r-- 1 root root 676 Jun 23 2016 [Link]

Summary of above chapter

You have completed Chapter 13. Let’s summarize the key concepts covered:
• The command line often allows the users to perform tasks more efficiently than the
GUI.
• cat, short for concatenate, is used to read, print, and combine files.
• echo displays a line of text either on standard output or to place in a file.
• sed is a popular stream editor often used to filter and perform substitutions on files
and text data streams.
• awk is an interpreted programming language, typically used as a data extraction and
reporting tool.
• sort is used to sort text files and output streams in either ascending or descending
order.
• uniq eliminates duplicate entries in a text file.
• paste combines fields from different files. It can also extract and combine lines from
multiple sources.
• join combines lines from two files based on a common field. It works only if files
share a common field.
• split breaks up a large file into equal-sized segments.
• Regular expressions are text strings used for pattern matching. The pattern can be
used to search for a specific location, such as the start or end of a line or a word.
• grep searches text files and data streams for patterns and can be used with regular
expressions.
• tr translates characters, copies standard input to standard output, and handles
special characters.
• tee saves a copy of standard output to a file while still displaying at the terminal.
• wc (word count) displays the number of lines, words, and characters in a file or
group of files.
• cut extracts columns from a file.
• less views files a page at a time and allows scrolling in both directions.
• head displays the first few lines of a file or data stream on standard output. By
default, it displays 10 lines.
• tail displays the last few lines of a file or data stream on standard output. By default,
it displays 10 lines.
• strings extracts printable character strings from binary files.
• The z command family is used to read and work with compressed files.

One reason IPv4 has not disappeared is there are ways to effectively make many more addresses
available by methods such as NAT (Network Address Translation). NAT enables sharing one IP
address among many locally connected computers, each of which has a unique address only seen on
the local network. While this is used in organizational settings, it also used in simple home networks.
For example, if you have a router hooked up to your Internet Provider (such as a cable system) it
gives you one externally visible address, but issues each device in your home an individual local
address.

A 32-bit IPv4 address is divided into four 8-bit sections called octets.
Example: IP address → 172 . 16 . 31 . 46 Bit format
→ 10101100.00010000.00011111.00101110
NOTE: Octet is just another word for byte.
Network addresses are divided into five classes: A, B, C, D and E. Classes A, B and C are classified into
two parts: Network addresses (Net ID) and Host address (Host ID). The Net ID is used to identify the
network, while the Host ID is used to identify a host in the network. Class D is used for special
multicast applications (information is broadcast to multiple computers simultaneously) and Class E is
reserved for future use. In this section you will learn about classes A, B and C.

Class A addresses use the first octet of an IP address as their Net ID and use the other three octets as
the Host ID. The first bit of the first octet is always set to zero. So you can use only 7-bits for unique
network numbers. As a result, there are a maximum of 126 Class A networks available (the
addresses 0000000 and 1111111 are reserved). Not surprisingly, this was only feasible when there
were very few unique networks with large numbers of hosts. As the use of the Internet expanded,
Classes B and C were added in order to accommodate the growing demand for independent
networks.
Each Class A network can have up to 16.7 million unique hosts on its network. The range of host
address is from [Link] to [Link].
NOTE: The value of an octet, or 8-bits, can range from 0 to 255.

Class B addresses use the first two octets of the IP address as their Net ID and the last two octets as
the Host ID. The first two bits of the first octet are always set to binary 10, so there are a maximum
of 16,384 (14-bits) Class B networks. The first octet of a Class B address has values from 128 to 191.
The introduction of Class B networks expanded the number of networks but it soon became clear
that a further level would be needed.
Each Class B network can support a maximum of 65,536 unique hosts on its network. The range of
host addresses is from [Link] to [Link].

Class C addresses use the first three octets of the IP address as their Net ID and the last octet as their
Host ID. The first three bits of the first octet are set to binary 110, so almost 2.1 million (21-bits)
Class C networks are available. The first octet of a Class C address has values from 192 to 223. These
are most common for smaller networks which don't have many unique hosts.
Each Class C network can support up to 256 (8-bits) unique hosts. The range of host addresses is
from [Link] to [Link].

Host Configuration Protocol (DHCP) is used to assign IP addresses.

Name Resolution is used to convert numerical IP address values into a human-readable format
known as the hostname. For example, [Link] is the numerical IP address that refers to the
hostname [Link]. Hostnames are much easier to remember!
Given an IP address, you can obtain its corresponding hostname. Accessing the machine over the
network becomes easier when you can type the hostname instead of the IP address.
You can view your system’s hostname simply by typing hostname with no argument.

Network configuration files are essential to ensure that interfaces function correctly. They are
located in the /etc directory tree. However, the exact files used have historically been dependent on
the particular Linux distribution and version being used.
For Debian family configurations, the basic network configuration files could be found
under /etc/network/, while for Red Hat and SUSE family systems one needed to inspect
/etc/sysconfig/network.
Modern systems emphasize the use of Network Manager, which we briefly discussed when we
considered graphical system administration, rather than try to keep up with the vagaries of the files
in /etc. While the graphical versions of Network Manager do look somewhat different in different
distributions, the nmtui utility (shown in the screenshot) varies almost not at all, as does the even
more sparse nmcli (command line interface) utility

Information about a particular network interface or all network interfaces can be reported by the ip
and ifconfig utilities, which you may have to run as the superuser, or at least, give the full path,
i.e. /sbin/ifconfig, on some distributions. ip is newer than ifconfig and has far more capabilities, but
its output is uglier to the human eye. Some new Linux distributions do not install the older net-tools
package to which ifconfig belongs, and so you would have to install it if you want to use it.

To view the IP address:


$ /sbin/ip addr show
To view the routing information:
$ /sbin/ip route show

ping is used to check whether or not a machine attached to the network can receive and send data;
i.e. it confirms that the remote host is online and is responding.
To check the status of the remote host, at the command prompt, type ping <hostname>.

A network requires the connection of many nodes. Data moves from source to destination by
passing through a series of routers and potentially across multiple networks. Servers
maintain routing tables containing the addresses of each node in the network. The IP routing
protocols enable routers to build up a forwarding table that correlates final destinations with the
next hop addresses.

One can use the route utility or the newer ip route command to view or change the IP routing table
to add, delete, or modify specific (static) routes to specific hosts or networks. The table explains
some commands that can be used to manage IP routing:

Task Command
Show current routing table $ route –n or ip route
Add static route $ route add -net address or ip route add
Delete static route $ route del -net address or ip route de

To print the route taken by the packet to reach the network host, at the command prompt,
type traceroute <address>.

Now, let’s learn about some additional networking tools. Networking tools are very useful for
monitoring and debugging network problems, such as network connectivity and network traffic.

Networking Tools Description


ethtool Queries network interfaces and can also set various parameters such as the speed
netstat Displays all active connections and routing tables; useful for monitoring performance and
troubleshooting
nmap Scans open ports on a network; important for security analysis
tcpdump Dumps network traffic for analysis
iptraf Monitors network traffic in text mode
mtr Combines functionality of ping and traceroute and gives a continuously updated display
dig Tests DNS workings; a good replacement for host and nslookup

Sometimes, you either do not have a graphical environment to work in (or have reasons not to use
it) but still need to access web resources. In such a case, you can use non-graphical browsers, such as
the following:

Non-Graphical Browsers Description


lynx Configurable text-based web browser; the earliest such browser and still in use
elinks Based on Lynx; it can display tables and frames
w3m Another text-based web browser with many feature

To download a web page, you can simply type wget <url>, and then you can read the downloaded
page as a local file using a graphical or non-graphical browser.
Besides downloading, you may want to obtain information about a URL, such as the source code
being used. curl can be used from the command line or a script to read such information. curl also
allows you to save the contents of a web page to a file, as does wget.
You can read a URL using curl <URL>. For example, if you want to read
[Link] type curl [Link]
To get the contents of a web page and store it to a file, type curl -o [Link]
[Link] The contents of the main index file at the website will be saved
in [Link].

If you want to run as another user, you can do either ssh -l someone some_system or ssh
someone@some_system. To run a command on a remote system via SSH, at the command prompt,
you can type ssh some_system my_command.

We can also move files securely using Secure Copy (scp) between two networked hosts. scp uses the
SSH protocol for transferring data.
To copy a local file to a remote system, at the command prompt, type scp <localfile>
<user@remotesystem>:/home/user/ and press Enter

In order to access another machine use command ssh usernameofthatmachine@ip address of that
machine.

If you want to copy from one machine to another, use command scp -r ( recursive meaning directory
and all its contents) directory or path if directory to be copied ip address of target machine: /where
directory is to be copied

Lab: Network Troubleshooting


Troubleshooting network problems is something that you will often encounter if you haven't
already. We are going to practice some of the previously discussed tools, that can help you isolate,
troubleshoot and fix problems in your network.
Suppose you need to perform an Internet search, but your web browser can not find [Link],
saying the host is unknown. Let's proceed step by step to fix this.
1. First make certain your network is properly configured. If your Ethernet device is up
and running, running ifconfig should display something like: student:/tmp>
/sbin/ifconfig eno167777 Link encap:Ethernet HWaddr 00:0C:29:BB:92:C2
2. inet addr:[Link] Bcast:[Link] Mask:[Link]
3. inet6 addr: fe80::20c:29ff:febb:92c2/64 Scope:Link
4. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
5. RX packets:3244 errors:0 dropped:0 overruns:0 frame:0
6. TX packets:2006 errors:0 dropped:0 overruns:0 carrier:0
7. collisions:0 txqueuelen:1000
8. RX bytes:4343606 (4.1 Mb) TX bytes:169082 (165.1 Kb)
9.
10. lo Link encap:Local Loopback
11. inet addr:[Link] Mask:[Link]
12. inet6 addr: ::1/128 Scope:Host
13. UP LOOPBACK RUNNING MTU:65536 Metric:1
14. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
15. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
16. collisions:0 txqueuelen:0
17. RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) On older systems you probably will see a
less cryptic name than eno167777, like eth0, or for a wireless connection, you might see something
like wlan0 or wlp3s0. You can also show your IP address with: student:/tmp> ip addr show 1: lo:
mtu 65536 qdisc noqueue state UNKNOWN group default
18. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
19. inet [Link]/8 scope host lo
20. valid_lft forever preferred_lft forever
21. inet6 ::1/128 scope host
22. valid_lft forever preferred_lft forever
23. 2: eno16777736: mtu 1500 qdisc pfifo_fast state \
24. UP group default qlen 1000
25. link/ether 00:0c:29:bb:92:c2 brd ff:ff:ff:ff:ff:ff
26. p inet [Link]/24 brd [Link] scope global dynamic eno16777736
27. valid_lft 84941sec preferred_lft 84941sec
28. inet [Link]/24 brd [Link] scope global secondary dynamic
eno16777736
29. valid_lft 85909sec preferred_lft 85909sec
30. inet6 fe80::20c:29ff:febb:92c2/64 scope link
31. valid_lft forever preferred_lft forever
32. Does the IP address look valid? Depending on where you are using this from, it
is most likely a Class C IP address; in the above this is [Link] If it does not show a device with
an IP address, you may need to start or restart the network and/or NetworkManager. Exactly how
you do this depends on your system. For most distributions one of these commands will accomplish
this: student:/tmp> sudo systemctl restart NetworkManager
33. student:/tmp> sudo systemctl restart network
34. student:/tmp> sudo service NetworkManager restart
35. student:/tmp> sudo service network restart
36. If your device was up but had no IP address, the above should have helped fix it,
but you can try to get a fresh address with: student:/tmp> sudo dhclient eth0
37. substituting the right name for the Ethernet device.
38. If your interface is up and running with an assigned IP address and you still can not
reach [Link], we should make sure you have a valid hostname assigned to your machine, with
hostname: student:/tmp> hostname openSUSE
39. It is rare you would have a problem here, as there is probably always at least a
default hostname, such as localhost.
40. When you type in a name of a site such as [Link], that name needs to be
connected to a known IP address. This is usually done employing the DNS sever (Domain Name
System) First, see if the site is up and reachable with ping: student:/tmp> sudo ping -c 3
[Link] PING [Link] ([Link]) 56(84) bytes of data.
41. 64 bytes from [Link] ([Link]): icmp_seq=1 ttl=51
time=21.7 ms
42. 64 bytes from [Link] ([Link]): icmp_seq=2 ttl=51
time=23.8 ms
43. 64 bytes from [Link] ([Link]): icmp_seq=3 ttl=51
time=21.3 ms
44.
45. --- [Link] ping statistics ---
46. 3 packets transmitted, 3 received, 0% packet loss, time 2002ms
47. rtt min/avg/max/mdev = 21.388/22.331/23.813/1.074 ms
48. Note:
• We have used sudo for ping; recent Linux distributions have required this to avoid
clueless or malicious users from flooding systems with such queries.
• We have used -c 3 to limit to 3 packets; otherwise ping would run forever until
forcibly terminated, say with CTRL-C.
49. If the result was: ping: unknown host [Link]
50. It is likely that something is wrong with your DNS set-up. (Note on some
systems you will never see the unknown host message, but you will get a suspicious result like:
student:/tmp> sudo ping [Link] PING [Link] ([Link]) 56(84) bytes of
data.
51. 64 bytes from [Link]: icmp_seq=1 ttl=64 time=0.016 ms
52. ...
53. where the 127.0.x.x address is a loop feeding back to the host machine you are
on. You can eliminate this as being a valid address by doing: student:/tmp> host [Link]
54. Host [Link] not found: 3(NXDOMAIN)
55. whereas a correct result would look like: student:/tmp> host
[Link] [Link] has address [Link]
56. [Link] has IPv6 address 2607:f8b0:4009:80b::200e
57. [Link] mail is handled by 20 [Link].
58. [Link] mail is handled by 10 [Link].
59. [Link] mail is handled by 30 [Link].
60. [Link] mail is handled by 40 [Link].
61. [Link] mail is handled by 50 [Link].
62. The above command utilizes the DNS server configured in /etc/[Link] on
your machine. If you wanted to override that you could do: host [Link] [Link].[Link] domain
name pointer [Link].
63. student@linux:~> host [Link] [Link]
64. Using domain server:
65. Name: [Link]
66. Address: [Link]#53
67. Aliases:
68.
69. [Link] has address [Link]
70. [Link] has IPv6 address 2607:f8b0:4009:804::1002
71. ...\
72. where we have used the publicly available DNS server provided by Google itself.
(Using this or another public server can be a good trick sometimes if your network is up but DNS is
ill; in that case you can also enter it in [Link].) Note that there is another file, /etc/hosts, where
you can associate names with IP addresses, which is used before the DNS server is consulted. This is
most useful for specifying nodes on your local network. You could also use the dig utility if you
prefer: student:/tmp> dig [Link] ; <<>> DiG 9.9.5-rpz2+rl.14038.05-P1 <<>> [Link]
73. ;; global options: +cmd
74. ;; Got answer:
75. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29613
76. ;; flags: qr rd ra; QUERY: 1, ANSWER: 11, AUTHORITY: 0, ADDITIONAL: 1
77.
78. ;; OPT PSEUDOSECTION:
79. ; EDNS: version: 0, flags:; MBZ: 1c20 , udp: 1280
80. ;; QUESTION SECTION:
81. ;[Link]. IN A
82.
83. ;; ANSWER SECTION:
84. [Link]. 244 IN A [Link]
85. [Link]. 244 IN A [Link]
86. [Link]. 244 IN A [Link]
87. [Link]. 244 IN A [Link]
88. [Link]. 244 IN A [Link]
89. [Link]. 244 IN A [Link]
90. [Link]. 244 IN A [Link]
91. [Link]. 244 IN A [Link]
92. [Link]. 244 IN A [Link]
93. [Link]. 244 IN A [Link]
94. [Link]. 244 IN A [Link]
95.
96. ;; Query time: 22 msec
97. ;; SERVER: [Link]#53([Link])
98. ;; WHEN: Mon Apr 20 08:58:58 CDT 2015
99. ;; MSG SIZE rcvd: 215
100.
101. Suppose host or dig fail to connect the name to an IP address. There are many
reasons DNS can fail, some of which are:
• The DNS server is down. In this case try pinging it to see if it is alive (you should have
the IP address in /etc/[Link].
• The server can be up and running, but DNS may not be currently available on the
machine.
• Your route to the DNS server may not be correct.
102. How can we test the route? Tracing the route to one of the public name server we
mentioned before: student@linux:~> sudo traceroute [Link] traceroute to [Link] ([Link]), 30
hops max, 60 byte packets
103. 1 [Link] ([Link]) 0.405 ms 0.494 ms 0.556 ms
104. 2 [Link] ([Link]) 15.127 ms 15.107 ms 15.185 ms
105. 3 [Link] ([Link])
106. 15.243 ms 15.327 ms 17.878 ms
107. 4 [Link] ([Link]) 17.667 ms 17.734 ms
20.016 ms
108. 5 [Link] ([Link]) 22.017 ms 22.359 ms
22.052 ms
109. 6 [Link] ([Link]) 29.430 ms 22.705 ms
22.076 ms
110. 7 [Link] ([Link]) 17.795 ms 25.542 ms 25.600
ms
111. 8 [Link] ([Link]) 28.227 ms 28.270 ms 28.303
ms
112. 9 [Link] ([Link]) 33.114 ms 33.072 ms 33.175
ms
113. 10 [Link] ([Link]) 36.882 ms 36.794 ms 36.895
ms
114. 11 [Link] ([Link]) 42.585 ms 42.326 ms
42.401 ms
115. 12 [Link] ([Link]) 28.737 ms [Link] ([Link])
116. 24.558 ms 23.941 ms
117. 13 [Link] ([Link]) 24.269 ms [Link] ([Link])
118. 25.758 ms [Link] ([Link]) 25.433 ms
119. 14 [Link] ([Link]) 25.239 ms 24.003 ms 23.795 ms
120. Again, this should likely work for you, but what if you only got the first line in
the traceroute output? If this happened, most likely your default route is wrong. Try:
student:/tmp> ip route show efault via [Link] dev eno16777736 proto static metric 1024
121. [Link]/24 dev eno16777736 proto kernel scope link src [Link]
122. Most likely this is set to your network interface and the IP address of your
router, DSL, or Cable Modem. Let's say that it is blank or simply points to your own machine. Here's
your problem! At this point, you would need to add a proper default route and run some of the
same tests we just did. Note, an enhanced version of traceroute is supplied by mtr, which runs
continuously (like top). Running it with the --report-cycles option to limit how long it runs:
student:/tmp> sudo mtr --report-cycles 3 [Link] My traceroute [v0.85]
123. c7 ([Link]) Mon Apr 20 09:30:41 2015
124. Unable to allocate IPv6 socket for nameserver communication: Address family not
supported
125. by protocol Packets Pings
126. Host Loss% Snt Last Avg Best Wrst StDev
127. 0.0% 3 0.3 0.3 0.2 0.3 0.0
128. 2. [Link] 0.0% 3 6.3 7.1 6.3 8.4 0.7
129. 3. [Link]. 0.0% 3 6.2 7.5 6.2 10.0 2.1
130. 4. [Link] 0.0% 3 8.9 8.5 6.2 10.4 2.0
131. 5. [Link] 0.0% 3 8.9 9.7 8.9 10.4 0.0
132. 6. [Link] 0.0% 3 16.5 17.4 14.2 21.5 3.7
133. 7. [Link] 0.0% 3 23.5 22.0 18.2 24.2 3.2
134. 8. [Link] 0.0% 3 18.9 22.7 18.1 31.1 7.2
135. 9. [Link] 0.0% 3 22.9 23.0 22.9 23.1 0.0
136. 10. [Link] 0.0% 3 21.4 24.1 20.8 30.2 5.2
137. 11. [Link] 0.0% 3 22.6 21.9 20.0 23.3 1.6
138. 12. [Link] 0.0% 3 21.2 21.7 21.2 22.0 0.0
139. 13. [Link] 0.0% 3 21.2 21.0 19.8 21.9 1.0
140. 14. [Link] 0.0% 3 26.7 23.0 21.0 26.7 3.2
141.
Hopefully, running through some of these commands helped. It actually helps to see what the
correct output for your system looks like. Practice using these commands; it is very likely that you
will need them someday.

Lab: Non-graphical browsers


We have discussed non-graphical browsers:
• lynx
• links and elinks
• w3m
There are times when you will not have a graphical window interface running on your Linux machine
and you need to look something up on the web or download a driver (like a graphics driver in order
to bring up a graphical window interface). So, it is a good idea to practice using a non-graphical web
browser to do some work.
With links, you can use your mouse to click on the top line of the screen to get a menu. In this case,
we want to go to [Link] (or your favorite search engine), so you can just type g to go to a
typed-in URL.
Pressing the TAB key will move your cursor to the OK button. You can then press the ENTER key.
You should now be at [Link] (or your favorite search engine). Use the down-arrow key to move
through the choices until you reach the blank line used to enter your search query. Now type Intel
Linux graphics drivers in the search box. Use the down-arrow key to move you to the Google Search
button. With that highlighted, press the ENTER key.
Use your down-arrow key to move to the entry: Intel(R) Graphics Drivers for Linux - Download
Center. It may take several presses of the down-arrow key. You can press the space-bar to move
down the page or the 'b' key to move back up the page if needed. Once this line is highlighted, press
the ENTER key. You will now go to the Intel Graphics Driver for Linux page. If you want, you can read
the page. Remember, the space-bar will page you down the page while the 'b' key will move you
back up the page. The Page Down and Page Up keys will do the same thing if you prefer. Find the
URL under the line
URL Location:

Position your cursor at this line using the up-arrow or down-arrow key. Press the ENTER key to go to
this location.
Page down this page until you see the line:
Latest Releases

If you move your cursor with the arrow keys, find the latest version (with the most recent release
date) under this section. If using your arrow-keys, you should highlight Release Notes. Press the
ENTER key.
This has installers for versions of Ubuntu and Fedora, along with the source code. You will need to
page down a page or two depending on the size of your screen.
Select one of the installers, perhaps for the version of Linux that you are running, or just a random
one, and press the ENTER key.
You should see a text dialog box with choices of what to do. Save the package wherever you want to.
You can now quit your non-graphical browser. If you used links, then click on the top line of the
screen, select the File drop-down menu item, and click on Exit. Confirm that you really want to exit
Links. You should now see your shell prompt.

Linux provides a wide choice of shells; exactly what is available on the system is listed in /etc/shells.
Typical choices are:
/bin/sh /bin/bash /bin/tcsh /bin/csh /bin/ksh /bin/zsh
Most Linux users use the default bash shell, but those with long UNIX backgrounds with other shells
may want to override the default.

Let's write a simple bash script that displays a one line message on the screen. Either type:
$ cat > [Link] #!/bin/bash echo "Hello Linux Foundation Student"
and press ENTER and CTRL-D to save the file, or just create [Link] in your favorite text editor. Then,
type chmod +x [Link] to make the file executable by all users.
You can then run the script by typing ./[Link] or by doing:
$ bash [Link] Hello Linux Foundation Student
NOTE: If you use the second form, you do not have to make the file executable.


Now, let's see how to create a more interactive example using a bash script. The user will be
prompted to enter a value, which is then displayed on the screen. The value is stored in a temporary
variable, name. We can reference the value of a shell variable by using a $ in front of the variable
name, such as $name. To create this script, you need to create a file named [Link] in your
favorite editor with the following content:
#!/bin/bash # Interactive reading of a variable echo "ENTER YOUR NAME" read name # Display
variable input echo The name given was :$name
Once again, make it executable by doing chmod +x [Link].
In the above example, when the user types ./[Link] and the script is executed, the user is
prompted with the string ENTER YOUR NAME. The user then needs to enter a value and press the
Enter key. The value will then be printed out.
NOTE: The hash-tag/pound-sign/number-sign (#) is used to start comments in the script and can be
placed anywhere in the line (the rest of the line is considered a comment). However, note the
special magic combination of #!, used on the first line, is a unique exception to this rule.

As a script executes, one can check for a specific value or condition and return success or failure as
the result. By convention, success is returned as 0, and failure is returned as a non-zero value. An
easy way to demonstrate success and failure completion is to execute ls on a file that exists as well
as one that does not, the return value is stored in the environment variable represented by $?:
$ ls /etc/[Link] /etc/[Link]
$ echo $? 0
In this example, the system is able to locate the file /etc/[Link] and ls returns a value of 0 to
indicate success. When run on a non-existing file, it returns 2. Applications often translate these
return values into meaningful messages easily understood by the user.

Lab Solution: Exit Status Codes


Create a file named [Link], with the content below.
#!/bin/bash
#
# check for non-existent file, exit status will be 2
#
ls [Link]
echo "status: $?"

# create file, and do again, exit status will be 0


touch [Link]
ls [Link]
echo "status: $?"

# remove the file to clean up


rm [Link]
Make it executable and run it:
student:/tmp> chmod +x [Link]
student:/tmp> ./[Link]
ls: cannot access [Link]: No such file or directory
status: 2
[Link]
status: 0

Scripts require you to follow a standard language syntax. Rules delineate how to define variables and
how to construct and format allowed statements, etc. The table lists some special character usages
within bash scripts:

Character Description
# Used to add a comment, except when used as \#, or as #! when starting a script
\ Used at the end of a line to indicate continuation on to the next line
; Used to interpret what follows as a new command to be executed next
$ Indicates what follows is an environment variable
> Redirect output
>> Append output
< Redirect input
| Used to pipe the result into the next command

Sometimes, commands are too long to either easily type on one line, or to grasp and understand
(even though there is no real practical limit to the length of a command line).
In this case, the concatenation operator (\), the backslash character, is used to continue long
commands over several lines.

Users sometimes need to combine several commands and statements and even conditionally
execute them based on the behavior of operators used in between them. This method is
called chaining of commands.
There are several different ways to do this, depending on what you want to do. The ; (semicolon)
character is used to separate these commands and execute them sequentially, as if they had been
typed on separate lines. Each ensuing command is executed whether or not the preceding one
succeeded.
Thus, the three commands in the following example will all execute, even if the ones preceding them
fail:
$ make ; make install ; make clean
However, you may want to abort subsequent commands when an earlier one fails. You can do this
using the && (and) operator as in:
$ make && make install && make clean
If the first command fails, the second one will never be executed. A final refinement is to use the ||
(or) operator, as in:
$ cat file1 || cat file2 || cat file3
In this case, you proceed until something succeeds and then you stop executing any further steps.
Chaining commands is not the same as piping them; in the later case succeeding commands begin
operating on data streams produced by earlier ones before they complete, while in chaining each
step exits before the next one starts.

Most operating systems accept input from the keyboard and display the output on the terminal.
However, in shell scripting you can send the output to a file. The process of diverting the output to a
file is called output redirection. We have already used this facility in our earlier sections on how to
use the command line.
The > character is used to write output to a file. For example, the following command sends the
output of free to /tmp/[Link]:
$ free > /tmp/[Link]
To check the contents of /tmp/[Link], at the command prompt type cat /tmp/[Link].
Two > characters (>>) will append output to a file if it exists, and act just like > if the file does not
already exist.

Just as the output can be redirected to a file, the input of a command can be read from a file. The
process of reading input from a file is called input redirection and uses the < character.
The following three commands (using wc to count the number of lines, words and characters in a
file) are entirely equivalent and involve input redirection, and a command operating on the contents
of a file:
$ wc < /etc/passwd 49 105 2678 /etc/passwd
$ wc /etc/passwd 49 105 2678 /etcpasswd
$ cat /etc/passwd | wc 49 105 2678

Users often need to pass parameter values to a script, such as a filename, date, etc. Scripts will take
different paths or arrive at different values according to the parameters (command arguments) that
are passed to them. These values can be text or numbers as in:
$ ./[Link] /tmp $ ./[Link] 100 200 Within a script, the parameter or an argument is
represented with a $ and a number or special character. The table lists some of these parameters.

Parameter Meaning
$0 Script name
$1 First parameter
$2, $3, etc. Second, third parameter, etc.
$* All parameters
$# Number of arguments

If you type in the script shown in the figure, make the script executable with chmod +x [Link].
Then, run the script giving it several arguments, as shown. The script is processed as follows:
$0 prints the script name: [Link]
$1 prints the first parameter: one
$2 prints the second parameter: two
$3 prints the third parameter: three
$* prints all parameters: one two three four five
The final statement becomes: All done with [Link]

Most scripts use variables containing a value, which can be used anywhere in the script. These
variables can either be user or system-defined. Many applications use such environment variables
(already covered in some detail in Chapter 12: User Environment) for supplying inputs, validation,
and controlling behavior.
As we discussed earlier, some examples of standard environment variables are HOME, PATH, and
HOST. When referenced, environment variables must be prefixed with the $ symbol, as in $HOME.
You can view and set the value of environment variables. For example, the following command
displays the value stored in the PATH variable:
$ echo $PATH
However, no prefix is required when setting or modifying the variable value. For example, the
following command sets the value of the MYCOLOR variable to blue:
$ MYCOLOR=blue
You can get a list of environment variables with the env, set, or printenv commands.

While we discussed the export of environment variables in the section on the "User Environment", it
is worth reviewing this topic in the context of writing bash scripts.
By default, the variables created within a script are available only to the subsequent steps of that
script. Any child processes (sub-shells) do not have automatic access to the values of these variables.
To make them available to child processes, they must be promoted to environment variables using
the export statement, as in:
export VAR=value
or
VAR=value ; export VAR
While child processes are allowed to modify the value of exported variables, the parent will not see
any changes; exported variables are not shared, they are only copied and inherited.
Typing export with no arguments will give a list of all currently exported environment variables.

A function is a code block that implements a set of operations. Functions are useful for executing
procedures multiple times, perhaps with varying input variables. Functions are also often
called subroutines. Using functions in scripts requires two steps:
1. Declaring a function
2. Calling a function
The function declaration requires a name which is used to invoke it. The proper syntax is:
function_name () { command... }
For example, the following function is named display:
display () { echo "This is a sample function" }
The function can be as long as desired and have many statements. Once defined, the function can be
called later as many times as necessary. In the full example shown in the figure, we are also showing
an often-used refinement: how to pass an argument to the function. The first argument can be
referred to as $1, the second as $2, etc.

Lab Solution: Working with Files and Directories in a Script


Create a file named [Link], with the content below.
#!/bin/bash
# Prompts the user for a directory name and then creates it with mkdir.

echo "Give a directory name to create:"


read NEW_DIR

# Save original directory so we can return to it (could also just use pushd, popd)

ORIG_DIR=$(pwd)

# check to make sure it doesn't already exist!

[[ -d $NEW_DIR ]] && echo $NEW_DIR already exists, aborting && exit


mkdir $NEW_DIR

# Changes to the new directory and prints out where it is using pwd.

cd $NEW_DIR
pwd

# Using touch, creates several empty files and runs ls on them to verify they are empty.

for n in 1 2 3 4
do
touch file$n
done

ls file?

# (Could have just done touch file1 file2 file3 file4, just want to show do loop!)

# Puts some content in them using echo and redirection.

for names in file?


do
echo This file is named $names > $names
done

# Displays their content using cat

cat file?

# Says goodbye to the user and cleans up after itself

cd $ORIG_DIR
rm -rf $NEW_DIR
echo "Goodbye My Friend!"
Make it executable and run it:
$ chmod +x [Link]
./[Link]
Give a directory name to create:
/tmp/SOME_DIR
/tmp/SOME_DIR
file1 file2 file3 file4
This file is named file1
This file is named file2
This file is named file3
This file is named file4
Goodbye My Friend

Lab Solution: Passing Arguments


Create a file named [Link], with the content below.
#!/bin/bash
#
# check for an argument, print a usage message if not supplied.
#
if [ $# -eq 0 ] ; then
echo "Usage: $0 argument"
exit 1
fi
echo $1
exit 0
Make it executable and run it:
student:/tmp> chmod +x [Link]
student:/tmp> ./[Link] Hello
Hello
student:/tmp>./[Link]
Usage: ./[Link] argument
student:/tmp>

Lab Solution: Environment Variables


Create a file named [Link], with the content below.
#!/bin/bash

echo "Enter 1 or 2, to set the environmental variable EVAR to Yes or No"


read ans

# Set up a return code


RC=0

if [ $ans -eq 1 ]
then
export EVAR="Yes"
else
if [ $ans -eq 2 ]
then
export EVAR="No"
else
# can only reach here with a bad answer
export EVAR="Unknown"
RC=1
fi
fi
echo "The value of EVAR is: $EVAR"
exit $RC
Make it executable and run it:
student:/tmp> chmod +x [Link]
student:/tmp> ./[Link]
Enter 1 or 2, to set the environmental variable EVAR to Yes or No
1
The value of EVAR is: Yes
student:/tmp> ./[Link]
Enter 1 or 2, to set the environmental variable EVAR to Yes or No
2
The value of EVAR is: No
student:/tmp> ./[Link]
Enter 1 or 2, to set the environmental variable EVAR to Yes or No
3
The value of EVAR is: Unknown

Lab Solution: Working with Functions


Create a file named [Link], with the content below.
#!/bin/bash

# Functions (must be defined before use)


func1() {
echo " This message is from function 1"
}
func2() {
echo " This message is from function 2"
}
func3() {
echo " This message is from function 3"
}

# Beginning of the main script

# prompt the user to get their choice


echo "Enter a number from 1 to 3"
read n

# Call the chosen function


func$n
Make it executable and run it:
student:/tmp> chmod +x [Link]
student:/tmp> ./[Link]
Enter a number from 1 to 3
2
This message is from function 2
$ ./[Link]
Enter a number from 1 to 3
7
./[Link]: line 21: func7: command not found

In the following example, an if statement checks to see if a certain file exists, and if the file is found,
it displays a message indicating success or failure:
if [ -f "$1" ] then echo file "$1 exists" else echo file "$1" does not exist fi
We really should also check first that there is an argument passed to the script ($1) and abort if not.
Notice the use of the square brackets ([]) to delineate the test condition. There are many other kinds
of tests you can perform, such as checking whether two numbers are equal to, greater than, or less
than each other and make a decision accordingly; we will discuss these other tests.

You can use the elif statement to perform more complicated tests, and take action appropriate
actions. The basic syntax is:
if [ sometest ] ; then echo Passed test1 elif [ somothertest ] ; then echo Passed test2 fi
In the example shown we use strings tests which we will explain shortly, and show how to pull in an
environment variable with the read statement.

bash provides a set of file conditionals, that can be used with the if statement, including those in the
table.
You can use the if statement to test for file attributes, such as:
• File or directory existence
• Read or write permission
• Executable permission.
For example, in the following example:
if [ -x /etc/passwd ] ; then ACTION fi
the if statement checks if the file /etc/passwd is executable, which it is not. Note the very common
practice of putting:
; then
on the same line as the if statement.
You can view the full list of file conditions typing:
man 1 test.

EDX continue

Condition Meaning
-e file Checks if the file exists.
-d file Checks if the file is a directory.
-f file Checks if the file is a regular file (i.e. not a symbolic link, device node, directory, etc.)
-s file Checks if the file is of non-zero size.
-g file Checks if the file has sgid set.
-u file Checks if the file has suid set.
-r file Checks if the file is readable.
-w file Checks if the file is writable.
-x file Checks if the file is executable.

Boolean expressions evaluate to either TRUE or FALSE, and results are obtained using the various
Boolean operators listed in the table.

Operator Operation Meaning


&& AND The action will be performed only if both the conditions evaluate to true.
|| OR The action will be performed if any one of the conditions evaluate to true.
! NOT The action will be performed only if the condition evaluates to false.

Boolean expressions return either TRUE or FALSE. We can use such expressions when working with
multiple data types, including strings or numbers, as well as with files. For example, to check if a file
exists, use the following conditional test:
[ -e <filename> ]
Similarly, to check if the value of number1 is greater than the value of number2, use the following
conditional test:
[ $number1 -gt $number2 ]
The operator -gt returns TRUE if number1 is greater than number2.

You can use the if statement to compare strings using the operator == (two equal signs). The syntax
is as follows:
if [ string1 == string2 ] ; then ACTION fi
Note that using one = sign will also work, but some consider it deprecated usage. Let’s now consider
an example of testing strings.
In the example illustrated here, the if statement is used to compare the input provided by the user
and accordingly display the result.

You can use specially defined operators with the if statement to compare numbers. The various
operators that are available are listed in the table:

Operator Meaning
-eq Equal to
-ne Not equal to
-gt Greater than
-lt Less than
-ge Greater than or equal to
-le Less than or equal to

The syntax for comparing numbers is as follows: exp1 -op exp2

Lab Solution: Arithmetic and Functions


Create a file named [Link], with the content below.
#!/bin/bash

# Functions. must be before the main part of the script

# in each case method 1 uses $((..))


# method 2 uses let
# method 3 uses expr

add() {
answer1=$(($1 + $2))
let answer2=($1 + $2)
answer3=`expr $1 + $2`
}
sub() {
answer1=$(($1 - $2))
let answer2=($1 - $2)
answer3=`expr $1 - $2`
}
mult() {
answer1=$(($1 * $2))
let answer2=($1 * $2)
answer3=`expr $1 \* $2`
}
div() {
answer1=$(($1 / $2))
let answer2=($1 / $2)
answer3=`expr $1 / $2`
}
# End of functions
#

# Main part of the script

# need 3 arguments, and parse to make sure they are valid types

op=$1 ; arg1=$2 ; arg2=$3

[[ $# -lt 3 ]] && \
echo "Usage: Provide an operation (a,s,m,d) and two numbers" && exit 1

[[ $op != a ]] && [[ $op != s ]] && [[ $op != d ]] && [[ $op != m ]] && \


echo operator must be a, s, m, or d, not $op as supplied

# ok, do the work!

if [[ $op == a ]] ; then add $arg1 $arg2


elif [[ $op == s ]] ; then sub $arg1 $arg2
elif [[ $op == m ]] ; then mult $arg1 $arg2
elif [[ $op == d ]] ; then div $arg1 $arg2
else
echo $op is not a, s, m, or d, aborting ; exit 2
fi

# Show the answers


echo $arg1 $op $arg2 :
echo 'Method 1, $((..)),' Answer is $answer1
echo 'Method 2, let, ' Answer is $answer2
echo 'Method 3, expr, ' Answer is $answer3
Make it executable and run it:
student:/tmp> chmod +x [Link]
student:/tmp> ./[Link]
student:/tmp> for n in a s m d x ; do ./[Link] $n 21 7 ; done
21 a 7 :
Method 1, $((..)), Answer is 28
Method 2, let,
Answer is 28
Method 3, expr,
Answer is 28
21 s 7 :
Method 1, $((..)), Answer is 14
Method 2, let,
Answer is 14
Method 3, expr,
Answer is 14
21 m 7 :
Method 1, $((..)), Answer is 147
Method 2, let,
Answer is 147
Method 3, expr,
Answer is 147
21 d 7 :
Method 1, $((..)), Answer is 3
Method 2, let,
Answer is 3
Method 3, expr,
Answer is 3
operator must be a, s, m, or d, not x as supplied
x is not a, s, m, or d, aborting

A string variable contains a sequence of text characters. It can include letters, numbers, symbols and
punctuation marks. Some examples include: abcde, 123, abcde 123, abcde-123, &acbde=%123.
String operators include those that do comparison, sorting, and finding the length. The following
table demonstrates the use of some basic string operators:

Operator Meaning
[[ string1 > string2 ]] Compares the sorting order of string1 and string2.
[[ string1 == string2 ]] Compares the characters in string1 with the characters in string2.
myLen1=${#string1} Saves the length of string1 in the variable myLen1.
specify: ${string:0:n}. Here, 0 is the offset in the string (i.e. which character to begin from) where the
extraction needs to start and n is the number of characters to be extracted.
To extract all characters in a string after a dot (.), use the following expression: ${string#*.}.

Lab Solution: String Tests and Operations


Create a file named [Link], with the content below.
#!/bin/bash

# check two string arguments were given


[[ $# -lt 2 ]] && \
echo "Usage: Give two strings as arguments" && exit 1

str1=$1
str2=$2

#------------------------------------
## test command

echo "Is string 1 zero length? Value of 1 means FALSE"


[ -z "$str1" ]
echo $?
# note if $str1 is empty, the test [ -z $str1 ] would fail
# but [[ -z $str1 ]] succeeds
# i.e., with [[ ]] it works even without the quotes

echo "Is string 2 nonzero length? Value of 0 means TRUE;"


[ -n $str2 ]
echo $?

## comparing the lengths of two string

len1=${#str1}
len2=${#str2}
echo length of string1 = $len1, length of string2 = $len2

if [ $len1 -gt $len2 ]


then
echo "String 1 is longer than string 2"
else
if [ $len2 -gt $len1 ]
then
echo "String 2 is longer than string 1"
else
echo "String 1 is the same length as string 2"
fi
fi
## compare the two strings to see if they are the same

if [[ $str1 == $str2 ]]
then
echo "String 1 is the same as string 2"
else
if [[ $str1 != $str2 ]]
then
echo "String 1 is not the same as string 2"
fi
fi
student:/tmp> chmod +x [Link]
student:/tmp> ./[Link] str1 str2
Is string 1 zero length? Value of 1 means FALSE
1
Is string 2 nonzero length? Value of 0 means TRUE;
0
length of string1 = 4, length of string2 = 4
String 1 is the same length as string 2
String 1 is not the same as string 2
student:/tmp>./[Link] str1 str2long
Is string 1 zero length? Value of 1 means FALSE
1
Is string 2 nonzero length? Value of 0 means TRUE;
0
length of string1 = 4, length of string2 = 8
String 2 is longer than string 1
String 1 is not the same as string 2

student:/tmp>

The case statement is used in scenarios where the actual value of a variable can lead to different
execution paths. case statements are often used to handle command-line options.
Below are some of the advantages of using the case statement:
* It is easier to read and write.
* It is a good alternative to nested, multi-level if-then-else-fi code blocks.
* It enables you to compare a variable against several values at once.
* It reduces the complexity of a program.

Lab Solution: Using the case Statement


Create a file named [Link], with the content below.
#!/bin/bash
# Accept a number between 1 and 12 as
# an argument to this script, then return the
# the name of the month that corresponds to that number.

# Check to see if the user passed a parameter.


if [ $# -eq 0 ]
then
echo "Error. Give as an argument a number between 1 and 12."
exit 1
fi

# set month equal to argument passed for use in the script


month=$1

################################################
# The example of a case statement:

case $month in

1) echo "January" ;;
2) echo "February" ;;
3) echo "March" ;;
4) echo "April" ;;
5) echo "May" ;;
6) echo "June" ;;
7) echo "July" ;;
8) echo "August" ;;
9) echo "September" ;;
10) echo "October" ;;
11) echo "November" ;;
12) echo "December" ;;
*)
echo "Error. No month matches: $month"
echo "Please pass a number between 1 and 12."
exit 2
;;
esac
exit 0
Make it executable and run it:
student:/tmp> chmod +x [Link]
student:/tmp> ./[Link] 5
May
student:/tmp> ./[Link] 12
December

student:/tmp> ./[Link] 99
Error. No month matches: 99
Please pass a number between 1 and 12
student:/tmp>
By using looping constructs, you can execute one or more lines of code repetitively, usually on a
selection of values of data such as individual files. Usually, you do this until a conditional test returns
either true or false, as is required.

Three types of loops are often used in most programming languages:


* for
* while
* until.
All these loops are easily used for repeating a set of statements until the exit condition is true.

The for loop operates on each element of a list of items. The syntax for the for loop is:
for variable-name in list do execute one iteration for each item in the list until the list is
finished done
In this case, variable-name and list are substituted by you as appropriate (see examples). As with
other looping constructs, the statements that are repeated should be enclosed by do and done.
The screenshot here shows an example of the for loop to print the sum of numbers 1 to 10.

The while loop repeats a set of statements as long as the control command returns true. The syntax
is:
while condition is true do Commands for execution ---- done
The set of commands that need to be repeated should be enclosed between do and done. You can
use any command or operator as the condition. Often, it is enclosed within square brackets ([]).
The screenshot here shows an example of the while loop that calculates the factorial of a number.
Do you know why the computation of 21! gives a bad result?

The until loop repeats a set of statements as long as the control command is false. Thus, it
is essentially the opposite of the while loop. The syntax is:
until condition is false do Commands for execution ---- done
Similar to the while loop, the set of commands that need to be repeated should be enclosed
between do and done. You can use any command or operator as the condition.
The screenshot here shows an example of the until loop that once again computes factorials; it is
only slightly different than the test case for the while loop.

Before fixing an error (or bug), it is vital to know its source.


You can run a bash script in debug mode either by doing bash –x ./script_file, or bracketing parts of
the script with set -x and set +x. The debug mode helps identify the error because:
* It traces and prefixes each command with the + character.
* It displays each command before executing it.
* It can debug only selected parts of a script (if desired) with: set -x # turns on
debugging ... set +x # turns off debugging
The screenshot shown here demonstrates a script which runs in debug mode if run with any
argument on the command line.

In UNIX/Linux, all programs that run are given three open file streams when they are started as listed
in the table:

File stream Description File Descriptor


stdin Standard Input, by default the keyboard/terminal for programs run from the command line
0
stdout Standard output, by default the screen for programs run from the command line 1
stderr Standard error, where output error messages are shown or saved 2

Using redirection, we can save the stdout and stderr output streams to one file or two separate files
for later analysis after a program or command is executed.
The screenshot shows a shell script with a simple bug, which is then run and the error output is
diverted to [Link]. Using cat to display the contents of the error log adds in debugging. Do you see
how to fix the script?

Command Usage
TEMP=$(mktemp /tmp/[Link]) To create a temporary file
TEMPDIR=$(mktemp -d /tmp/[Link]) To create a temporary director

Sloppiness in creation of temporary files can lead to real damage, either by accident or if there is a
malicious actor. For example, if someone were to create a symbolic link from a known temporary file
used by root to the /etc/passwd file, like this:
$ ln -s /etc/passwd /tmp/tempfile There could be a big problem if a script run by root has a line in
like this:
echo $VAR > /tmp/tempfile
The password file will be overwritten by the temporary file contents.
To prevent such a situation, make sure you randomize your temporary file names by replacing the
above line with the following lines:
TEMP=$(mktemp /tmp/[Link]) echo $VAR > $TEMP
Note the screen capture shows similarly named temporary files from different days, but with
randomly generated characters in them.

Certain commands (like find) will produce voluminous amounts of output, which can overwhelm the
console. To avoid this, we can redirect the large output to a special file (a device node)
called /dev/null. This pseudofile is also called the bit bucket or black hole.
All data written to it is discarded and write operations never return a failure condition. Using the
proper redirection operators, it can make the output disappear from commands that would normally
generate output to stdout and/or stderr:
$ ls -lR /tmp > /dev/null
In the above command, the entire standard output stream is ignored, but any errors will still appear
on the console. However, if one does:
$ ls -lR /tmp >& /dev/null
both stdout and stderr will be dumped into /dev/null.

It is often useful to generate random numbers and other random data when performing tasks such
as:
* Performing security-related tasks
* Reinitializing storage devices
* Erasing and/or obscuring existing data
* Generating meaningless data to be used for tests.
Such random numbers can be generated by using the $RANDOM environment variable, which is
derived from the Linux kernel’s built-in random number generator, or by the OpenSSL library
function, which uses the FIPS140 (Federal Information Processing Standard) algorithm to generate
random numbers for encryption
To learn about FIPS140, read Wikipedia's "FIPS 140-2" article.
The example shows you how to easily use the environmental variable method to generate random
numbers.

The Linux kernel offers the /dev/random and /dev/urandom device nodes, which draw on the
entropy pool to provide random numbers which are drawn from the estimated number of bits of
noise in the entropy pool.
/dev/random is used where very high quality randomness is required, such as one-time pad or key
generation, but it is relatively slow to provide values. /dev/urandom is faster and suitable (good
enough) for most cryptographic purposes.
Furthermore, when the entropy pool is empty, /dev/random is blocked and does not generate any
number until additional environmental noise (network traffic, mouse movement, etc.) is gathered,
whereas /dev/urandom reuses the internal pool to produce more pseudo-random bits.

Lab Solution: Using Random Numbers


Create a file named [Link], with the content below.
#!/bin/bash
##
# check to see if the user supplied in the parameter.

[[ $# -eq 0 ]] && echo "Usage: $0 word" && exit 1

echo "$1-$RANDOM"
exit 0
Make it executable and run it:
student:/tmp> chmod +x [Link]
student:/tmp> ./[Link] strA
strA-29294
student:/tmp>./[Link] strB
strB-23911
student:/tmp>./[Link] strC
strC-27782
student:/tmp>

Printing in Linux

The print scheduler reads server settings from several configuration files, the two most important of
which are [Link] and [Link]. These and all other CUPS related configuration files are
stored under the /etc/cups/ directory.
[Link] is where most system-wide settings are located; it does not contain any printer-specific
details. Most of the settings available in this file relate to network security, i.e. which systems can
access CUPS network capabilities, how printers are advertised on the local network, what
management features are offered, and so on.
[Link] is where you will find the printer-specific settings. For every printer connected to the
system, a corresponding section describes the printer’s status and capabilities. This file is generated
or modified only after adding a printer to the system, and should not be modified by hand.
You can view the full list of configuration files by typing ls -lF /etc/cups.

Log files are placed in /var/log/cups and are used by the scheduler to record activities that have
taken place. These files include access, error, and page records.
To view what log files exist, type: $ sudo ls -l /var/log/cups

So, in short, when you execute a print command, the scheduler validates the command and
processes the print job, creating job files according to the settings specified in the configuration files.
Simultaneously, the scheduler records activities in the log files. Job files are processed with the help
of the filter, printer driver, and backend, and then sent to the printer.

Assuming CUPS has been installed you'll need to start and manage the CUPS daemon so that CUPS is
ready for configuring a printer. Managing the CUPS daemon is simple; all management features can
be done with the systemctl utility:
$ systemctl status cups
$ sudo systemctl [enable|disable] cups
$ sudo systemctl [start|stop|restart] cups

A fact that few people know is that CUPS also comes with its own web server, which makes a
configuration interface available via a set of CGI scripts.
This web interface allows you to:
* Add and remove local/remote printers
* Configure printers:
* – Local/remote printers
* – Share a printer as a CUPS server
* Control print jobs:
* – Monitor jobs
* – Show completed or pending jobs
* – Cancel or move jobs.
The CUPS web interface is available on your browser at: [Link]
Some pages require a username and password to perform certain actions, for example to add a
printer. For most Linux distributions, you must use the root password to add, modify, or delete
printers or classes.

CUPS provides two command-line interfaces, descended from the System V and BSD flavors of UNIX.
This means that you can use either lp (System V) or lpr (BSD) to print. You can use these commands
to print text, PostScript, PDF, and image files.
These commands are useful in cases where printing operations must be automated (from shell
scripts, for instance, which contain multiple commands in one file).
lp is just a command line front-end to the lpr utility that passes input to lpr. Thus, we will discuss
only lp in detail. In the example shown here, the task is to print $HOME/.emacs .

Some lp commands and other printing utilities you can use are listed in the table:

Command Usage
lp <filename> To print the file to default printer
lp -d printer <filename> To print to a specific printer (useful if multiple printers are available)
program | lp
echo string | lp To print the output of a program
lp -n number <filename> To print multiple copies
lpoptions -d printer To set the default printer
lpq -a To show the queue status
lpadmin To configure printer queues

lpoptions can be used to set printer options and defaults. Each printer has a set of tags associated
with it, such as the default number of copies and authentication requirements. You can
type lpoptions help to obtain a list of supported options. lpoptions can also be used to set system-
wide values, such as the default printer.

In Linux, command line print job management commands allow you to monitor the job state as well
as managing the listing of all printers and checking their status, and canceling or moving print jobs to
another printer.
Some of these commands are listed in the table.

Command Usage
lpstat -p -d To get a list of available printers, along with their status
lpstat -a To check the status of all connected printers, including job numbers
cancel job-id
OR
lprm job-id To cancel a print job
lpmove job-id newprinter To move a print job to new printer

enscript is a tool that is used to convert a text file to PostScript and other formats. It also supports
Rich Text Format (RTF) and HyperText Markup Language (HTML). For example, you can convert a
text file to two columns (-2) formatted PostScript using the command:
$ enscript -2 -r -p [Link] [Link]
This command will also rotate (-r) the output to print so the width of the paper is greater than the
height (aka landscape mode) thereby reducing the number of pages required for printing.
The commands that can be used with enscript are listed in the table below (for a file called
[Link]).

Command Usage
enscript -p [Link] [Link] Convert a text file to PostScript (saved to [Link])
enscript -n -p [Link] [Link] Convert a text file to n columns where n=1-9 (saved in
[Link])
enscript [Link] Print a text file directly to the default printe

Another possibility is to use the very powerful convert program, which is part of the ImageMagick
package. Some newer distributions have replaced this with Graphics Magick, and the command to
use is gm convert.
Some usage examples:

Command Usage
pdf2ps [Link] Converts [Link] to [Link]
ps2pdf [Link] Converts [Link] to [Link]
pstopdf [Link] [Link] Converts [Link] to [Link]
pdftops [Link] [Link] Converts [Link] to [Link]
convert [Link] [Link] Converts [Link] to [Link]
convert [Link] [Link] Converts [Link] to output.p

The most common Linux PDF readers are:


1. evince is available on virtually all distributions and is the most widely used program.
2. okular is based on the older kpdf and is available on any distribution that provides the KDE
environment.

You can accomplish a wide variety of tasks using qpdf including:

Command Usage
qpdf --empty --pages [Link] [Link] -- [Link] Merge the two documents [Link] and [Link]. The
output will be saved to [Link].
qpdf --empty --pages [Link] 1-2 -- [Link] Write only pages 1 and 2 of [Link]. The output will be
saved to [Link].
qpdf --rotate=+90:1 [Link] [Link]
qpdf --rotate=+90:1-z [Link] [Link] Rotate page 1 of [Link] 90 degrees clockwise and save to
[Link].
Rotate all pages of [Link] 90 degrees clockwise and save to [Link]
qpdf --encrypt mypw mypw 128 -- [Link] [Link] Encrypt with 128 bits [Link] using as the
passwd mypw with output as [Link].

Using qpdf to Crypt/Decrypt Files

pdftk has now been ported to Java! Marc Vinyals has developed and maintained a port to Java for
pdftk which can be found here, together with instructions for installation. Some distributions such as
Ubuntu, may install this version only.
You can accomplish a wide variety of tasks using pdftk including:

Command Usage
pdftk [Link] [Link] cat output [Link] Merge the two documents [Link] and [Link]. The output will
be saved to [Link].
pdftk A=[Link] cat A1-2 output [Link] Write only pages 1 and 2 of [Link]. The output will be saved
to [Link].

If you’re working with PDF files that contain confidential information and you want to ensure that
only certain people can view the PDF file, you can apply a password to it using the user_pw option.
One can do this by issuing a command such as:
$ pdftk [Link] output [Link] user_pw PROMPT
When you run this command, you will receive a prompt to set the required password, which can
have a maximum of 32 characters. A new file, [Link], will be created with the identical content
as [Link], but anyone will need to type the password to be able to view it.

This utility can do most of the operations pdftk can, as well as many others; see man gs for details.
Use is somewhat complicated by the rather long nature of the options. For example:
* Combine three PDF files into one: $ gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -
sOutputFile=[Link] [Link] [Link] [Link]
* Split pages 10 to 20 out of a PDF file: $ gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -
dDOPDFMARKS=false -dFirstPage=10 -dLastPage=20\ -sOutputFile=[Link] [Link]

You can use other tools to work with PDF files, such as:
* pdfinfo It can extract information about PDF files, especially when the files are very large or
when a graphical interface is not available.
* flpsed It can add data to a PostScript document. This tool is specifically useful for filling in
forms or adding short comments into the document.
* pdfmod It is a simple application that provides a graphical interface for modifying PDF
documents. Using this tool, you can reorder, rotate, and remove pages; export images from a
document; edit the title, subject, and author; add keywords; and combine documents using drag-
and-drop action.
For example, to collect the details of a document, you can use the following command: $ pdfinfo
/usr/share/doc/[Link]

Lab Solution: Creating PostScript and PDF from Text Files


1. Try which enscript /usr/bin/enscript If you do not get a positive result, install with
whichever command is appropriate for your Linux distribution: apt-get install enscript
2. yum install enscript
3. zypper install enscript
4. enscript -p /tmp/[Link] /var/log/dmesg
5. evince /tmp/[Link]
6. ps2pdf /tmp/[Link]
7. ls -lh /var/log/dmesg /tmp/[Link] /tmp/[Link] -rw-rw-r-- 1 coop coop 28K Apr 22 13:00
/tmp/[Link]
8. -rw-rw-r-- 1 coop coop 80K Apr 22 12:59 /tmp/[Link]
9. -rw-r--r-- 1 root root 53K Apr 22 11:48 /var/log/dmesg
10. evince /tmp/[Link] /tmp/[Link] Note the difference in sizes. PostScript files tend
to be large, while PDF is a compressed format.
11. You may want to scan the man pages for enscript and ps2pdf to figure out how to use standard
input or standard output instead of files. student:/tmp> enscript -p - /var/log/dmesg | ps2pdf -
dmesg_direct.pdf [ 15 pages * 1 copy ] left in -
12. 85 lines were wrapped student:/tmp> ls -l dmesg*pdf -rw-rw-r-- 1 coop coop 28177 Apr 22
13:20 dmesg_direct.pdf
13. -rw-rw-r-- 1 coop coop 28177 Apr 22 13:00 [Link]
14. student:/tmp> pdfinfo [Link] Title: Enscript Output
15. Author: Theodore Cleaver
16. Creator: GNU Enscript 1.6.6
17. Producer: GPL Ghostscript 9.07
18. CreationDate: Wed Apr 22 13:00:26 2015
19. ModDate: Wed Apr 22 13:00:26 2015
20. Tagged: no
21. Form: none
22. Pages: 15
23. Encrypted: no
24. Page size: 612 x 792 pts (letter)
25. Page rot: 0
26. File size: 28177 bytes

Lab Solution: Combining PDFs


First we can create two PDFs to play with, using enscript and then ps2pdf:
cd /var/log
enscript -p [Link] /var/log/dmesg
enscript -p [Link] /var/log/[Link]
ps2dpf [Link]
ps2dpf [Link]
Of course you may use two pre-existing PDF files and substitute their names below.
* Method 1: Using qpdf: qpdf --empty --pages [Link] [Link] -- [Link]
* Method 2: Using pdftk: pdftk [Link] [Link] cat output [Link]
* Method 3: Using gs: gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=[Link]
[Link] [Link]
Now view them:
ls -l [Link] [Link]
evince [Link] [Link]
How do the files compare?

last command is used to find out which user logged in when

su sudo
When elevating privilege, you need to enter the root password. Giving the root password to a
normal user should never, ever be done. When elevating privilege, you need to enter the
user’s password and not the root password.
Once a user elevates to the root account using su, the user can do anything that the root user can do
for as long as the user wants, without being asked again for a password. Offers more features and is
considered more secure and more configurable. Exactly what the user is allowed to do can be
precisely controlled and limited. By default the user will either always have to keep giving their
password to do further operations with sudo, or can avoid doing so for a configurable time interval.
The command has limited logging features. The command has detailed logging features.

A message such as the following would appear in a system log file (usually /var/log/secure) when
trying to execute sudo for badperson without successfully authenticating the user:
badperson : user NOT in sudoers ; TTY=pts/4 ; PWD=/var/log ; USER=root ; COMMAND=/usr/bin/tail
secure

/etc/sudoers contains a lot of documentation in it about how to customize. Most Linux distributions
now prefer you add a file in the directory /etc/sudoers.d with a name the same as the user. This file
contains the individual user's sudo configuration, and one should leave the master configuration file
untouched except for changes that affect all users.
You should edit any of these configuration files by using visudo, which ensures that only one person
is editing the file at a time, has the proper permissions, and refuses to write out the file and exit if
there are syntax errors in the changes made. The editing can be accomplished by doing a command
such as the following ones:
# visudo /etc/sudoers # visudo -f /etc/sudoers.d/student
The actual specific editor invoked will depend on the setting of your EDITOR environment variable.
By default, sudo commands and any failures are logged in /var/log/[Link] under the
Debian distribution family, and in /var/log/messages and/or /var/log/secure on other systems. This
is an important safeguard to allow for tracking and accountability of sudo use. A typical entry of the
message contains:
* Calling username
* Terminal info
* Working directory
* User account invoked
* Command with arguments.
Running a command such as sudo whoami results in a log file entry such as:
Dec 8 14:20:47 server1 sudo: op : TTY=pts/6 PWD=/var/log USER=root COMMAND=/usr/bin/whoami

Lab Solution: sudo


1. sudo useradd newuser
2. sudo passwd newuser (give the password for this user when prompted)
3.
4. With root privilege, (use sudo visudo) add this line to /etc/sudoers: newuser ALL=(ALL)
ALL Alternatively, create a file named /etc/sudoers.d/newuser with just that one line as content.
5.
6. You can login by doing: sudo su newuser or ssh newuser@localhost which will require giving
newuser's password, and is probably a better solution. Instead of localhost you can give your
hostname, IP address or [Link]. Then as newuser just type: sudo ls /root

On modern systems, passwords are actually stored in an encrypted format in a secondary file named
/etc/shadow. Only those with root access can read or modify this file.

Protecting passwords has become a crucial element of security. Most Linux distributions rely on a
modern password encryption algorithm called SHA-512 (Secure Hashing Algorithm 512 bits),
developed by the U.S. National Security Agency (NSA) to encrypt passwords.
The SHA-512 algorithm is widely used for security applications and protocols. These security
applications and protocols include TLS, SSL, PHP, SSH, S/MIME and IPSec. SHA-512 is one of the most
tested hashing algorithms.
For example, if you wish to experiment with SHA-512 encoding, the word "test" can be encoded
using the program sha512sum to produce the SHA-512 form (see graphic):

IT professionals follow several good practices for securing the data and the password of every user.
* Password aging is a method to ensure that users get prompts that remind them to create a
new password after a specific period. This can ensure that passwords, if cracked, will only be usable
for a limited amount of time. This feature is implemented using chage, which configures the
password expiry information for a user.
* Another method is to force users to set strong passwords using Pluggable Authentication
Modules (PAM). PAM can be configured to automatically verify that a password created or modified
using the passwd utility is sufficiently strong. PAM configuration is implemented using a library
called pam_cracklib.so, which can also be replaced by pam_passwdqc.so to take advantage of more
options.
* One can also install password cracking programs, such as John The Ripper, to secure the
password file and detect weak password entries. It is recommended that written authorization be
obtained before installing such tools on any system that you do not own.

Lab Solution: Password Aging


chage --list newuser
sudo chage -E 2014-31-12 newuser
chage --list newuser
sudo userdel newuser
Note: The example solution works in the US. chage -E uses the default date format for the local
keyboard setting.

For the older GRUB 1 boot method, it was relatively easy to set a password for grub. However, for
the GRUB 2 version, things became more complicated. However, you have more flexibility, and can
take advantage of more advanced features, such as user-specific passwords (which can be their
normal login ones).
Furthermore, you never edit [Link] directly; instead, you can modify the configuration files
in /etc/grub.d and /etc/defaults/grub, and then run update-grub, or grub2-mkconfig and save the
new configuration file.
To learn more, read the following post: "GRUB 2 Password Protection".

You might also like