Edx Linux
Edx Linux
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.
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 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.
CLI
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.
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
For example, to find files greater than 10 MB in size and running a command on those files: $ find /
-size +10M -exec command {} ’;’
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
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
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
To terminate a process, you can type kill -SIGKILL <pid> or kill -9 <pid>.
jobs -l provides the same information as jobs, and adds the PID of the background jobs.
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:
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.
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
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
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.
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:
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:
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:
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!
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] ~
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
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
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
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.
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.
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.
• 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 $
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
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 $.
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
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
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
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
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].
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.
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.
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:
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
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.
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.
# Save original directory so we can return to it (could also just use pushd, popd)
ORIG_DIR=$(pwd)
# 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!)
cat file?
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
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
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.
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
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
#
# need 3 arguments, and parse to make sure they are valid types
[[ $# -lt 3 ]] && \
echo "Usage: Provide an operation (a,s,m,d) and two numbers" && exit 1
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#*.}.
str1=$1
str2=$2
#------------------------------------
## test command
len1=${#str1}
len2=${#str2}
echo length of string1 = $len1, length of string2 = $len2
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.
################################################
# 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.
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.
In UNIX/Linux, all programs that run are given three open file streams when they are started as listed
in the table:
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.
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
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].
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]
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
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.
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".