Complit Modul Linux Basic
Complit Modul Linux Basic
1 Introduction
The definition of the word Linux depends on the context in which it is used. Technically speaking, Linux is the
kernel of the system, which is the central controller of everything that happens on the computer. People that say
their computer “runs Linux” are usually referring to the kernel and suite of tools that come with it (called a
distribution). If someone says they have Linux experience, it might refer to configuring systems, running web
servers, or any number of other services and programs that operate on top of Linux. Over time, Linux
administration has evolved to encompass just about every task that a modern business, educational, or government
institution might use in their daily operations.
What about UNIX? UNIX was originally an operating system developed at AT&T Bell Labs in the 1970s. It has
been modified and forked (that is, people modified it, and those modifications served as the basis for other systems)
such that now there are many different variants of UNIX. However, UNIX is now both a trademark and a
specification, owned by an industry consortium called the Open Group. Only software that has been certified by
the Open Group may call itself UNIX. Despite adopting most if not all of the requirements of the UNIX
specification, Linux has not been certified, so Linux really isn’t UNIX! It’s just… UNIX-like.
Note
Much of the early material in this chapter is very similar to what can be found in NDG Linux Essentials. If you
have already taken that course, you can use this as an opportunity to refresh your knowledge or feel free to skip
ahead a few pages.
1.1.1 Role of the Kernel
The three main components of an operating system are the kernel, shell, and filesystem. The kernel of the operating
system is like an air traffic controller at an airport. The kernel dictates which program gets which pieces of
memory, it starts and kills programs, it interprets instructions given to it by the user, and it handles more common
and simple tasks such as displaying text on a monitor. When an application needs to write to disk, it must ask the
kernel to complete the write operation.
The kernel also handles the switching of applications. A computer will have one or more CPUs and a finite amount
of memory. The kernel takes care of unloading tasks and loading new tasks, and can manage multiple tasks across
multiple CPUs. When the current task has run a sufficient amount of time, the CPU pauses the task so that another
may run. This is called preemptive multitasking. Multitasking means that the computer is doing several tasks at
once, and preemptive means that the kernel is deciding when to switch focus between tasks. With the tasks so
rapidly switching, it appears that the computer is doing many things at once.
Each application may think it has a large block of memory on the system, but it is the kernel that maintains this
illusion; remapping smaller blocks of memory, sharing blocks of memory with other applications, or even
swapping out blocks that haven’t been used in a while to the disk.
When the computer starts up, it loads a small piece of code called a bootloader. The bootloader’s job is to give you
a choice (if configured) of options to load one or more versions of Linux, or even other operating systems, and then
to load the kernel of the chosen option and get it started. If you are more familiar with operating systems such as
Microsoft Windows or Apple’s OS X, you probably never see the bootloader, but in the UNIX world, it’s usually
visible so that you can adjust the way your computer boots.
The bootloader loads the Linux kernel and then transfers control. Linux then continues with running the programs
necessary to make the computer useful, such as connecting to the network or starting a web server.
1.1.2 Applications
Like an air traffic controller, the kernel is not useful without something to control. If the kernel is the tower, the
applications are the airplanes. Applications make requests to the kernel and receive resources, such as memory,
CPU, and disk, in return. The kernel also abstracts the complicated details away from the application. The
application doesn’t know if the block of a disk is on a solid-state drive from manufacturer A, a spinning metal hard
drive from manufacturer B, or even a network file share. Applications just follow the kernel’s Application
Programming Interface (API) and in return don’t have to worry about the implementation details
When we, as users, think of applications, we tend to think of word processors, web browsers, and email clients.
The kernel doesn’t care if it is running something that’s user-facing, a network service that talks to a remote
computer, or an internal task. So, from this, we get an abstraction called a process. A process is just one task that is
loaded and tracked by the kernel. An application may even need multiple processes to function, so the kernel takes
care of running the processes, starting and stopping them as requested, and handing out system resources.
Source code is not understood directly by the computer, so it must be compiled into machine instructions by a
compiler. The compiler gathers all of the source files and generates something that can be run on the computer,
such as the Linux kernel.
Historically, most software has been issued under a closed-source license, meaning that you get the right to use the
machine code, but cannot see the source code. Often the license specifically says that you will not attempt to
reverse engineer the machine code back to source code to figure out what it does!
Open source takes a source-centric view of software. The open source philosophy is that you have a right to obtain
the software and to modify it for your own use. Linux adopted this philosophy to great success.
In 1991, Linux started out as a hobby project by Linus Torvalds. He made the source freely available, allowing
others to join in and shape this fledgling operating system. It was not the first system to be developed by a
volunteer group, but since it was built from scratch, early adopters could influence the project’s direction. People
took the source, made changes, and shared them back with the rest of the group, greatly accelerating the pace of
development, and ensuring mistakes from other operating systems were not repeated.
The Linux kernel is licensed under the GNU Public License (GPL) which requires you to make changes available.
This guarantees that those who use the code will also contribute to the greater good by making those changes
available to anyone.
Alongside this, was the GNU project (GNU’s, not UNIX). While GNU (pronounced "guh-noo”) was building their
own operating system, they were far more successful at building the tools that go along with a UNIX operating
system, such as the compilers and user interfaces. The source was all freely available, so Linux was able to target
their tools and provide a complete system. As such, most of the tools that are part of the Linux system come from
these GNU tools.
There are many different variants on open source. However, all agree that you should have access to the source
code, but they differ in how you can, or in some cases, must, redistribute changes.
Like UNIX, there are many different flavors of distributions. These days, there are distributions that focus on
running servers, desktops, or even industry-specific tools like electronics design or statistical computing. The
major players in the market can be traced back to either Red Hat or Debian. The most visible difference is the
software package manager, though you will find other differences on everything from file locations to political
philosophies.
Red Hat started out as a simple distribution that introduced the Red Hat Package Manager (RPM) based on the
.rpm file format. The developer eventually formed a company around it, which tried to commercialize a Linux
desktop for business. Over time, Red Hat started to focus more on the server applications such as web and file
serving and released Red Hat Enterprise Linux, which was a paid service on a long release cycle. The release cycle
dictates how often software is upgraded. A business may value stability and want long release cycles, a hobbyist or
a startup may want the latest software and opt for a shorter release cycle. To satisfy the latter group, Red Hat
sponsors the Fedora Project which makes a personal desktop comprising the latest software but still built on the
same foundations as the enterprise version.
Because everything in Red Hat Enterprise Linux is open source, a project called CentOS came to be, that
recompiled all the RHEL packages and gave them away for free. CentOS and others like it (such as Scientific
Linux) are largely compatible with RHEL and integrate some newer software, but do not offer the paid support
that Red Hat does.
Debian is more of a community effort, and as such, also promotes the use of open source software and adherence
to standards. Debian came up with its own package management system based on the .deb file format. While Red
Hat leaves non-Intel and AMD platform support to derivative projects, Debian supports many of these platforms
directly.
Ubuntu is the most popular Debian derived distribution. It is the creation of Canonical, a company that was made
to further the growth of Ubuntu and makes money by providing support.
1.2 Hardware Platforms
Linux started out as something that would only run on a computer like Linus’: a 386 with a specific hard drive
controller. The range of support grew, as support for other hardware was built. Eventually, Linux started
supporting other chipsets, including hardware that was made to run competitive operating systems!
The types of hardware grew from the humble Intel chip up to supercomputers. Smaller sized Linux-supported chips
were eventually developed to fit in consumer devices (called embedded devices). The support for Linux became
ubiquitous such that it is often easier to build hardware to support Linux and then use Linux as a springboard for
your custom software than it is to build the custom hardware and software from scratch.
Eventually, cellular phones and tablets adopted Linux. A company, later bought by Google, came up with the
Android platform which is a bundle of Linux and the software necessary to run a phone or tablet. This means that
the effort to get a phone to market is significantly less. Instead of long development on a new operating system,
companies can spend their time innovating on the user-facing software. Android is now one of the market leaders
in the phone and tablet space.
Aside from phones and tablets, Linux can be found in many consumer devices. Wireless routers often run Linux
because it has a rich set of network features. The TiVo is a consumer digital video recorder built on Linux. Even
though these devices have Linux at the core, the end users don’t have to know. The custom software interacts with
the user and Linux provides the stable platform
1.3 Shell
An operating system provides at least one shell that allows the user to communicate with the operating system. A
shell is sometimes called an interpreter because it takes the commands that a user issues and interprets them into a
form that the kernel can then execute on the hardware of the computer. The two most common types of shells are
the Graphical User Interface (GUI) and Command Line Interface (CLI).
Microsoft Windows™ typically uses a GUI shell, primarily using the mouse to indicate what you want to
accomplish. While using an operating system in this way might be considered easy, there are many advantages to
using a CLI, including:
Command Repetition: In a GUI shell, there is no easy way to repeat a previous command. In a CLI there
is an easy way to repeat (and also modify) a previous command.
Command Flexibility: The GUI shell provides limited flexibility in the way the command executes. In a
CLI, options are specified with commands to provide a more flexible and powerful interface.
Resources: A GUI shell typically uses a relatively large amount of resources (RAM, CPU, etc.). This is
because a great deal of processing power and memory is needed to display graphics. By contrast, a CLI
uses very little system resources, allowing more of these resources to be available to other programs.
Scripting: In a GUI shell, completing multiple tasks often requires multiple mouse clicks. With a CLI, a
script can be created to execute many complex operations by just typing the name of the script. A script is a
series of commands placed into a single file. When executed, the script runs all of the commands in the file.
Remote Access: While it is possible to execute commands in a GUI shell remotely, this feature isn't
typically set up by default. With a CLI shell, gaining access to a remote machine is easy and typically
available by default.
Development: Normally a GUI-based program takes more time for the developers to create when
compared to CLI-based programs. As a result, there are typically thousands of CLI programs on a typical
Linux OS while only a couple hundred programs in a primarily GUI-based OS like Microsoft Windows.
More programs mean more power and flexibility.
The Microsoft Windows operating system was designed to primarily use the GUI interface because of its
simplicity, although there are several CLI interfaces available, too. For simple commands, there is the Run dialog
box, where you can type or browse to the commands that you want to execute. If you want to type multiple
commands or if you want to see the output of the command, you can use the Command Prompt, also called the
DOS shell. Recently, Microsoft realized how important it is to have a powerful command line environment and
introduced Powershell.
Like Windows, Linux also has both a CLI and GUI. Unlike Windows, Linux lets you easily change the GUI shell
(also called the desktop environment) that you want to use. The two most common desktop environments for Linux
are GNOME and KDE; however, there are many other GUI shells available.
To access the CLI from within the GUI on a Linux operating system, the user can open a software program called a
terminal. Linux can also be configured only to run the CLI without the GUI; this is typically done on servers that
don't require a GUI, primarily to free up system resources.
Users interact with a system by executing commands which are interpreted by the shell and transformed into
actions by the kernel. These actions may or may not return information to the command line depending on the
command issued and its result. For example, when the ls command is typed into the console, it will return the
contents of whichever directory the user is currently in.
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
A command can be followed by options that modify how the command is executed, and arguments, that are
typically the files to be operated on:
Note
Some commands require options and arguments while others, like ls, can be used alone.
Commands entered are considered standard input, (stdin) whether they are typed by an operator, entered by a
script, or as the result of another command. Text returned to the console can be either standard output (stdout), or
standard error (stderr). This deceptively simple method of communicating with the Linux kernel is the basis for
almost every interaction a Linux administrator has with her systems. It can be confusing at first for users who have
only experienced GUI interfaces, but ultimately it gives the experienced operator far more power than any
graphical interface can.
This deceptively simple method of communicating with the Linux kernel is the basis for almost every interaction a
Linux administrator has with their systems. It can be confusing at first for users who have only experienced GUI
interfaces, but ultimately it gives the experienced operator far more power than any graphical interface can.
The Bash shell has numerous built-in commands and features that you will learn including:
Aliases: Give a command a different or shorter name to make working with the shell more efficient.
Re-Executing Commands: To save retyping long command lines.
Wildcard Matching: Uses special characters like ?, *, and [] to select one or more files as a group for
processing.
Input/Output Redirection: Uses special characters for redirecting input, < or <<, and output, >.
Pipes: Used to connect one or more simple commands to perform more complex operations.
Background Processing: Enables programs and commands to run in the background while the user
continues to interact with the shell to complete other tasks.
The shell that your user account uses by default is set at the time your user account was created. By default, many
Linux distributions use Bash for a new user's shell. Typically, a user learns one shell and sticks with that shell;
however, after you have learned the basics of Linux, you may want to explore the features of other shells.
Consider This
An administrator can use the usermod command to specify a different default shell after the account has been
created.
As a user, you can use the chsh command to change your default shell. Most of the time the default shell a system
offers will be adequate for basic tasks. Occasionally, an administrator will want to change the shell to have access
to more advanced features, or simply because they are more familiar with a different shell and the features it offers.
On systems that are near capacity, it may be advisable not to change shells as it could require additional resources
and slow processing for all users.
The location where the system stores the default shell for user accounts is the /etc/passwd file.
GUI-based systems: If the system is configured to present a GUI, then you will need to find a software
application called a Terminal. In the GNOME desktop environment, the Terminal application can be started
by clicking the Applications menu, then the System Tools menu and Terminal icon.
CLI-based systems: Many Linux systems, especially servers, are not configured to provide a GUI by
default, so they present a CLI instead. If the system is configured to present a CLI, then the system runs a
terminal application automatically after you log in.
In the early days of computing, terminal devices were large machines that allowed users to provide input through a
keyboard and displayed output by printing on paper. Over time, terminals evolved and their size shrank down into
something that looked similar to a desktop computer with a video display monitor for output and a keyboard for
input.
Ultimately, with the introduction of personal computers, terminals became software emulators of the actual
hardware. Whatever you type in the terminal is interpreted by your shell and translated into a form that can then be
executed by the kernel of the operating system.
If you are in a remote location, then pseudo-terminal connections can also be made across the network using
several techniques. Insecure connections could be made using protocols such as telnet and programs such as
rlogin, while secure connections can be established using programs like putty and protocols such as ssh.
1.4 Filesystems
In addition to the kernel and the shell, the other major component of any operating system is the filesystem. To the
user, a filesystem is a hierarchy of directories and files with the root / directory at the top of the directory tree. To
the operating system, a filesystem is a structure created on a disk partition consisting of tables defining the
locations of directories and files.
In this course, you will learn about the different Linux filesystems, filesystem benefits and how to create and
manage filesystems using commands.
NDG Introduction to Linux I - Chapter 2: Using the Shell
When a user first encounters the CLI, they can find it challenging because it requires memorizing a dizzying
amount of commands and their options. However, once a user has learned the structure of how commands are used,
where the necessary files and directories are located, and how to navigate the hierarchy of a file system, they can
be immensely productive. This capability provides more precise control, greater speed, and the ability to automate
tasks more easily through scripting.
Furthermore, by learning the CLI, a user can easily be productive almost instantly on ANY flavor or distribution of
Linux, reducing the amount of time needed to familiarize themselves with a system because of variations in a GUI.
2.2 Commands
What is a command? The simplest answer is that a command is a software program that when executed on the
command line, performs an action on the computer.
When you consider a command using this definition, you are really considering what happens when you execute a
command. When you type in a command, a process is run by the operating system that can read input, manipulate
data, and produce output. From this perspective, a command runs a process on the operating system, which then
causes the computer to perform a job.
However, there is another way of looking at what a command is: look at its source. The source is where the
command "comes from" and there are several different sources of commands within the shell of your CLI:
Internal Commands: Also called built-in commands, these commands are built-in to the shell itself. A
good example is the cd (change directory) command as it is part of the Bash shell. When a user types the cd
command, the Bash shell is already executing and knows how to interpret that command, requiring no
additional programs to be started.
External Commands: These commands are stored in files that are searched by the shell. If you type the ls
command, then the shell searches through a predetermined list of directories to try to find a file named ls
that it can execute. These commands can also be executed by typing the complete path to the command.
Aliases: An alias can override a built-in command, function, or a command that is found in a file. Aliases
can be useful for creating new commands built from existing functions and commands.
Functions: Functions can also be built using existing commands to either create new commands, override
commands built-in to the shell or commands stored in files. Aliases and functions are normally loaded from
the initialization files when the shell first starts, discussed later in this section.
2.2.1 External Commands
Commands that are stored in files can be in several forms that you should be aware of. Most commands are written
in the C programming language, which is initially stored in a human-readable text file. These text source files are
then compiled into computer-readable binary files, which are then distributed as the command files.
Users who are interested in seeing the source code of compiled, GPL licensed software can find it through the sites
where it originated. GPL licensed code also compels distributors of the compiled binaries, such as RedHat and
Debian, to make the source code available. Often it is found in the distributors’ repositories.
Note
It is possible to view available software packages, binary programs that can be installed directly at the command
line. Type the following command into the terminal to view the available source packages (source code that can be
modified before it’s compiled into binary programs) for the GNU Compiler Collection:
The apt-cache command allows us to display information from the APT database cache. It is commonly used to
find information about programs you wish to install and the components required to make them work.
Although there are a tremendous number of free and open source programs available, quite often the binary code
you will need as a Linux administrator won’t exist for the particular distribution you are running. Since open
source licensing gives you access to the code for these programs, one of your tasks will be compiling, and
sometimes modifying that code into executable programs that can be installed on the systems you manage. The
Free Software Foundation (FSF) distributes the GNU Compiler Collection (GCC) to make this process easier. The
GCC provides a compiler system (the special programs used to convert source code into usable binary programs)
with front ends for many different programming languages. In fact, the FSF doesn’t limit these tools to just Linux.
There are versions of the GCC that run on Unix, Windows, MacOS, and many other systems including specific
microcontroller environments.
Linux package management will be covered in greater detail later in the course.
Note
Command files can also contain human-readable text in the form of script files. A script file is a collection of
commands that is typically executed at the command line.
The ability to create your own script files is a very powerful feature of the CLI. If you have a series of commands
that you regularly find yourself typing in order to accomplish some task, then you can easily create a Bash shell
script to perform these multiple commands by typing just one command: the name of your script file. You simply
need to place these commands into a file and make the file executable.
2.2.2 Aliases
An alias can be used to map longer commands to shorter key sequences. When the shell sees an alias being
executed, it substitutes the longer sequence before proceeding to interpret commands.
For example, the command ls -l is commonly aliased to l or ll. Because these smaller commands are easier to
type, it becomes faster to run the ls -l command line.
To determine what aliases are set on the current shell use the alias command:
sysadmin@localhost:~$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
The aliases from the previous examples were created by initialization files. These files are designed to make the
process of creating aliases automatic.
New aliases can be created using the following format, where name is the name to be given the alias and command
is the command to be executed when the alias is run.
alias name=command
For example, the cal 2030 command displays the calendar for the year 2030. Suppose you end up running this
command often. Instead of executing the full command each time, you can create an alias called mycal and run the
alias, as demonstrated in the following graphic:
sysadmin@localhost:~$ alias mycal="cal 2019"
sysadmin@localhost:~$ mycal
2019
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
Aliases created this way only persist while the shell is open. Once the shell is closed, the new aliases are lost. Additionally,
each shell has its own aliases, so, aliases created in one shell won’t be available in a new shell that’s opened.
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
Note
By itself, the ls command lists files and directories contained in the current working directory. At this point, you
shouldn't worry too much about the output of the command, but instead, focus on understanding how to format and
execute commands.
Many commands can be used by themselves with no further input. Some commands require additional input to run
correctly. This additional input comes in two forms: options and arguments. Commands typically follow a simple
pattern of syntax:
command [options…] [arguments…]
When typing a command that is to be executed, the first step is to type the name of the command. The name of the
command is often based on what the command does or what the developer who created the command thinks will
best describe the command's function.
For example, the ls command displays a listing of information about files. Associating the name of the command
with something mnemonic for what it does may help you to remember commands more easily.
Keep in mind that every part of the command is normally case-sensitive, so LS is incorrect and will fail, but ls is
correct and will succeed.
An argument can be used to specify something for the command to act upon. Following a command, any desired
arguments are allowed or are required depending on the command. For example, the touch command is used to
create empty files or update the timestamp of existing files. It requires at least one argument to specify the file
name to act upon.
touch FILE...
sysadmin@localhost:~$ touch newfile
The ls command, on the other hand, allows for a path and/or file name to be specified as an argument, but it’s not
required.
ls [FILE]...
An example of a scenario where an argument is allowed but not required is the use of the ls command. If the ls
command is used without an argument, it will list the contents of the current directory:
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
If the ls command is given the name of a directory as an argument, it will list the contents of that directory. In the following
example, the /etc/ppp directory is used as an argument; the resulting output is a list of files contained in the /etc/ppp
directory:
sysadmin@localhost:~$ ls /etc/ppp
ip-down.d ip-up.d
The ls command also accepts multiple arguments. To list the contents of both the /etc/ppp and /etc/ssh directories,
pass them both as arguments:
sysadmin@localhost:~$ ls /etc/ppp /etc/ssh
/etc/ppp:
ip-down.d ip-up.d
/etc/ssh:
moduli ssh_host_ecdsa_key ssh_host_rsa_key
ssh_config ssh_host_ecdsa_key.pub ssh_host_rsa_key.pub
ssh_host_dsa_key ssh_host_ed25519_key ssh_import_id
ssh_host_dsa_key.pub ssh_host_ed25519_key.pub sshd_config
Some commands, like the cp command (copy file) and the mv command (move file), always require at least two
arguments: a source file and a destination file.
cp SOURCE... DESTINATION
In the example below, we will copy the public ssh rsa key called ssh_host_rsa_key.pub which resides in the
/etc/ssh directory, to the /home/sysadmin/Documents directory and verify that it is there:
Consider This
An ssh rsa key is a file that contains an authentication credential (similar to a password) used to verify the identity
of a machine that is being logged into using the ssh command. The ssh command allows users on a system to
connect to another machine across a network, log in, and then perform tasks on the remote machine.
The ssh command is covered in greater detail later in the NDG Introduction to Linux 2.
2.3.1.1 Quoting
Arguments that contain unusual characters like spaces or non-alphanumeric characters will usually need to be
quoted, either by enclosing them within double quotes or single quotes. Double quotes will prevent the shell from
interpreting some of these special characters; single quotes prevent the shell from interpreting any special
characters.
In most cases, single quotes are considered safer and should probably be used whenever you have an argument that
contains characters that aren't alphanumeric.
To understand the importance of quotes, consider the echo command. The echo command displays text to the
terminal and is used extensively in shell scripting.
echo [STRING]...
Consider the following scenario in which you want to list the contents of the current directory using the ls
command and then use the echo command to display the string hello world!! on the screen.
You might first try the echo command without any quotes, unfortunately without success:
sysadmin@localhost:~$ ls
sysadmin@localhost:~$ echo hello world!!
echo hello worldls
hello worldls
Using no quotes failed because the shell interprets the !! characters as special shell characters; in this case, they
mean "replace the !! with the last command that was executed". In this case, the last command was the ls
command, so ls replaced !! and then the echo command displayed hello worldls to the screen.
You may want to try the double quote " characters to see if they will block the interpretation (or expansion) of the
exclamation !! characters. The double quotes block the expansion of some special characters, but not all of them.
Unfortunately, double quotes do not block the expansion of the exclamation !! characters:
sysadmin@localhost:~$ ls
sysadmin@localhost:~$ echo "hello world!!"
echo "hello worldls"
hello worldls
Using double quotes preserves the literal value of all characters that they enclose except metacharacters such as the
$ dollar sign character, the ` backquote character, the \ backslash character and the ! exclamation point character.
These characters, called wild cards, are symbol characters that have special meaning to the shell. Wild card
characters are used for globbing and are interpreted by the shell itself before it attempts to run any command. Glob
characters are useful because they allow you to specify patterns that make it easier to match file names in the
command line. For example, the command ls e?? would list all files in that directory that start with an e and have
any two characters after it. However, because glob characters are interpreted differently by the shell, they need to
be enclosed appropriately to be interpreted literally.
Note
If you enclose text within the ' single quote characters, then all characters have their literal meaning:
sysadmin@localhost:~$ ls
sysadmin@localhost:~$ echo 'hello world!!'
hello world!!
2.3.2 Options
command [options] [arguments]
Options can be used with commands to expand or modify the way a command behaves. If it is necessary to add
options, they can be specified after the command name. Short options are specified with a hyphen - followed by a
single character. Short options are how options were traditionally specified.
In the following example, the -l option is provided to the ls command, which results in a long display output:
sysadmin@localhost:~$ ls -l
total 0
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Desktop
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Documents
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Downloads
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Music
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Pictures
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Public
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Templates
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Videos
Often the character is chosen to be mnemonic for its purpose, like choosing the letter l for long or r for reverse. By
default, the ls command prints the results in alphabetical order, so adding the -r option prints the results in reverse
alphabetical order.
sysadmin@localhost:~$ ls -r
Videos Templates Public Pictures Music Downloads Documents Desktop
In most cases, options can be used in conjunction with other options. They can be given as separate options like -l
-r or combined like -lr. The combination of these two options would result in a long listing output in reverse
alphabetical order:
sysadmin@localhost:~$ ls -l -r
total 0
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Videos
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Templates
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Public
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Pictures
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Music
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Downloads
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Documents
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Desktop
Multiple single options can be either given as separate options like -a -l -r or combined like -alr. The output of
all of these examples would be the same:
ls -l -a -r
ls -rla
ls -a -lr
Generally, short options can be combined with other short options in any order. The exception to this is when an
option requires an argument.
For example, the -w option to the ls command specifies the width of the output desired and therefore requires an
argument. If combined with other options, the -w option can be specified last, followed by its argument and still be
valid, as in ls -rtw 40, which specifies an output width of 40 characters. Otherwise, the -w option cannot be
combined with other options and must be given separately.
sysadmin@localhost:~$ ls -lr -w 40
total 32
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb 22 16:32 Videos
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb 22 16:32 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb 22 16:32 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb 22 16:32 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb 22 16:32 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb 22 16:32 Downloads
drwxr-xr-x 4 sysadmin sysadmin 4096 Feb 22 16:32 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb 22 16:32 Desktop
If you are using multiple options that require arguments, don't combine them. For example, the -T option, which
specifies tab size, also requires an argument. In order to accommodate both arguments, each option is given
separately:
sysadmin@localhost:~$ ls -w 40 -T 12
Desktop Music Templates
Documents Pictures Videos
Downloads Public
Some commands support additional options that are longer than a single character. Long options for commands are
preceded by a double hyphen -- and the meaning of the option is typically the name of the option, like the --all
option, which lists all files, including hidden ones. For example:
sysadmin@localhost:~$ ls --all
. .bashrc .selected_editor Downloads Public
.. .cache Desktop Music Templates
.bash_logout .profile Documents Pictures Videos
For commands that support both long and short options, execute the command using the long and short options
concurrently:
sysadmin@localhost:~$ ls --all --reverse -t
.profile Templates Music Desktop ..
.bash_logout Public Downloads .selected_editor .cache
Videos Pictures Documents .bashrc .
Commands that support long options will often also support arguments that may be specified with or without an
equal symbol (the output of both commands is the same):
ls --sort time
ls --sort=time
sysadmin@localhost:~$ ls --sort=time
Desktop Documents Downloads Music Pictures Public Templates Videos
A special option exists, the lone double hyphen -- option, which can be used to indicate the end of all options for
the command. This can be useful in some circumstances where it is unclear whether some text that follows the
options should be interpreted as an additional option or as an argument to the command.
For example, if the touch command tries to create a file called --badname:
The command tries to interpret --badname as an option instead of an argument. However, if the lone double hyphen --
option is placed before the file name, indicating that there are no more options, then the file name can successfully be
interpreted as an argument:
Consider This
The file name in the previous example is considered to be bad because putting hyphens in the beginning of file
names, while allowed, can cause problems when trying to access the file. For example, using the ls command
without any options to list the path of the --badname file above, will result in an error:
sysadmin@localhost:~$ ls --badname
ls: unrecognized option '--badname'
Try 'ls --help' for more information.
To remove the --badname file from the home directory, use the rm remove command:
sysadmin@localhost:~$ rm -- --badname
The rm command will be covered in greater detail later in the course.
A third type of option exists for a select few commands. While the options used in the AT&T version of UNIX
used a single hyphen and the GNU port of those commands used two hyphens, the Berkeley Software
Distribution (BSD) version of UNIX used options with no hyphen at all.
This no hyphen syntax is fairly rare in most Linux distributions. A couple of notable commands that support the
BSD UNIX style options are the ps and tar commands; both of these commands also support the single and
double hyphen style of options.
In the terminal below, there are two similar commands, the first command is executed with a traditional UNIX
style option (with single hyphens) and the second command is executed with a BSD style option (no hyphens).
sysadmin@localhost:~$ ps -u sysadmin
PID TTY TIME CMD
79 ? 00:00:00 bash
122 ? 00:00:00 ps
sysadmin@localhost:~$ ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
sysadmin 79 0.0 0.0 18176 3428 ? S 20:23 0:00 -bash
sysadmin 120 0.0 0.0 15568 2040 ? R+ 21:26 0:00 ps u
2.4 Scripts
One exception to the basic command syntax used is the exec command, which takes another command to execute
as an argument. What is special about the commands that are executed with exec is that they replace the currently
running shell.
A common use of the exec command is in what is known as wrapper scripts. If the purpose of a script is to simply
configure and launch another program, then it is known as a wrapper script.
A wrapper script often uses the following as the last line of the script to execute another program.
exec program
A script written this way avoids having a shell continue to run while the program that it launched is running, the
result is that this technique saves resources (like RAM).
Although redirection of input and output to a script are discussed in another section, it should also be mentioned
that the exec command can be used to cause redirection for one or more statements in a script.
2.5 Displaying System Information
The uname command displays system information. This command will output Linux by default when it is executed
without any options.
sysadmin@localhost:~$ uname
Linux
The uname command is useful for several reasons, including when you need to determine the name of the computer
as well as the current version of the kernel that is being used.
To display additional information about the system, you can use one of the many options available for the uname
command. For example, to display all the information about the system, use the -a option with the uname
command:
sysadmin@localhost:~$ uname -a
Linux localhost 4.4.0-72-generic #93~14.04.1-Ubuntu SMP Fri Mar 31 15:05:15 UTC
2017 x86_64 x86_64 x86_64 GNU/Linux
To display information about what kernel version the system is running, use the -r option:
sysadmin@localhost:~$ uname -r
4.4.0-72-generic
sysadmin@localhost:~$ pwd
/home/sysadmin
Notice our virtual machines employ a prompt that displays the current working directory, emphasized with the
color blue. In the first prompt above, the blue tilde ~ character is equivalent to /home/sysadmin, representing the
user's home directory:
sysadmin@localhost:~$
After changing directories, the new location can also be confirmed in the new prompt by using the pwd command,
and is again shown in blue:
sysadmin@localhost:~$ cd Documents/
sysadmin@localhost:~/Documents$ pwd
/home/sysadmin/Documents
To get back to the home directory after changing to a new location, use the cd change directory command without
any arguments:
sysadmin@localhost:~/Documents$ cd
sysadmin@localhost:~$
sysadmin@localhost:~$ type ls
ls is aliased to `ls --color=auto'
Using the -a option with the type command will return all locations of the files that contain a command; also called an
executable file:
sysadmin@localhost:~$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls
In the output above, the /bin/ls file path is the file location of the ls command.
This command is helpful for getting information about commands and where they reside on the system. For
internal commands, like the pwd command, the type command will identify them as shell builtins
For external commands like the ip command, the type command will return the location of the command, in this case, the
/sbin directory:
sysadmin@localhost:~$ type ip
ip is /sbin/ip
Consider This
The /bin directory contains executable programs needed for booting a system, commonly used commands, and
other programs needed for basic system functionality.
The /sbin directory also contains executable programs; mainly commands and tools designed for system
administration.
If a command does not behave as expected or if a command is not accessible, that should be, it can be beneficial to
know where the shell is finding the command.
The which command searches for the location of a command in the system by searching the PATH variable.
sysadmin@localhost:~$ which ls
/bin/ls
Note
The PATH variable contains a list of directories that are used to search for commands entered by the user.
The PATH variable will be covered in greater detail later in the course.
There will be times when you type a character or two and press the Tab key, only to discover that Bash does not
automatically complete the command. This will happen when you haven't typed enough characters to match only
one command. However, pressing the Tab key a second time in this situation will display the possible completions
(possible commands) available.
A good example of this would be if you typed ca and pressed Tab; then nothing would be displayed. If you
pressed Tab a second time, then the possible ways to complete a command starting with ca would be shown
sysadmin@localhost:~$ ca
cal capsh cat cautious-launcher
calendar captoinfo catchsegv
caller case catman
sysadmin@localhost:~$ ca
Another possibility may occur when you have typed too little to match a single command name uniquely. If there
are more possible matches to what you've typed than can easily be displayed, then the system will use a pager to
display the output, which will allow you to scroll through the possible matches.
For example, if you just type c and press the Tab key twice, the system will provide you with many matches that
you can scroll through:
c_rehash cksum
cal clear
calendar clear_console
caller cmp
capsh codepage
captoinfo col
case colcrt
cat colrm
catchsegv column
catman comm
cautious-launcher command
cd compgen
cfdisk complete
chage compopt
chardet3 compose
chardetect3 continue
chattr coproc
chcon corelist
chcpu cp
chfn cpan
chgpasswd cpan5.26-x86_64-linux-gnu
chgrp cpgr
chmem cpio
--More--
You may not wish to scroll through all these options; in that case, press Q to exit the pager. In a situation like this,
you should probably continue to type more characters to achieve a more refined match.
A common mistake when typing commands is to misspell the command name. Not only will you type commands
faster, but you will type more accurately if you use command completion. Using the Tab key to complete the
command automatically helps to ensure that the command is typed correctly.
Note that completion also works for arguments to commands when the arguments are file or directory names
2.9 Getting Help
As previously mentioned, UNIX was the operating system from which the foundation of Linux was built. The
developers of UNIX created help documents called man pages (short for manual page).
Referring to the man page for a command will provide you with the basic idea behind the purpose of the command,
as well as details regarding the options of the command and a description of its features.
man command
For example, the following graphic shows the partial man page for the cal command:
NAME
cal, ncal -- displays a calendar and the date of Easter
SYNOPSIS
cal [-3hjy] [-A number] [-B number] [[month] year]
cal [-3hj] [-A number] [-B number] -m month [year]
ncal [-3bhjJpwySM] [-A number] [-B number] [-s country_code] [[month]
year]
ncal [-3bhJeoSM] [-A number] [-B number] [year]
ncal [-CN] [-H yyyy-mm-dd] [-d yyyy-mm]
DESCRIPTION
The cal utility displays a simple calendar in traditional format and ncal
offers an alternative layout, more options and the date of Easter. The
new format is a little cramped but it makes a year fit on a 25x80 termi-
nal. If arguments are not specified, the current month is displayed.
Consider This
It is possible to print man pages from the command line. If you wanted to send a man page to a default printer from
a local machine, you would execute the following command:
man -t command | lp
To view the various movement commands that are available, use the H key or Shift+H while viewing a man page.
This will display a help page.
Note
If you are working on a Linux distribution that uses the more command as a pager, your output will be different
from the partial example shown here.
MOVING
Command Function
Return (or Enter) Go down one line
Space Go down one page
/term Search for term
n Find next search item
1G Go to the beginning of the page
G Go to the end of the page
h Display help
q Quit man page
The following table describes some of the more common sections that you will find in man pages:
NAME
NAME
ls - list directory contents
SYNOPSIS
A brief summary of the command or function's interface. A summary of how the command line syntax of the
program looks.
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci-
fied.
OPTIONS
Lists the options for the command as well as a description of how they are used. Often this information is found in
the DESCRIPTION section and not in a separate OPTIONS section.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
--author
with -l, print the author of each file
-b, --escape
print C-style escapes for nongraphic characters
--block-size=SIZE
scale sizes by SIZE before printing them; e.g., '--block-size=M'
prints sizes in units of 1,048,576 bytes; see SIZE format below
FILES
Lists the files that are associated with the command as well as a description of how they are used. These files may
be used to configure the command's more advanced features. Often this information is found in the DESCRIPTION
section and not in a separate FILES section.
AUTHOR
Provides the name of the person who created the man page and (sometimes) how to contact the person.
AUTHOR
Written by Richard M. Stallman and David MacKenzie.
REPORTING BUGS
REPORTING BUGS
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report ls translation bugs to <http://translationproject.org/team/>
COPYRIGHT
COPYRIGHT
Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
Provides you with an idea of where you can find additional information. This often includes other commands that
are related to this command
SEE ALSO
Full documentation at: <http://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'
SYNOPSIS
cal [-3hjy] [-A number] [-B number] [[month] year]
The square brackets [ ] are used to indicate that this feature is not required to run the command. For example, [-
3hjy] means you can use the options -3, -h, -j, -y, but none of these options are required for the cal command to
function properly.
The second set of square brackets [-A number] allows you to specify the number of months to be added to the end
of the display.
The third set of square brackets in the [-B number] allows you to specify the number of months to be added to the
beginning of the display.
The fourth set of square brackets in the [[month] year]demonstrates another feature; it means that you can specify a
year by itself, but if you specify a month you must also specify a year.
Another component of the SYNOPSIS that might cause some confusion can be seen in the SYNOPSIS of the date
command:
SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
In this SYNOPSIS there are two syntaxes for the date command. The first one is used to display the date on the
system while the second is used to set the date.
The ellipses ... following [OPTION], indicate that [OPTION] may be repeated.
Additionally, the [-u|--utc|--universal] notation means that you can either use the -u option or the --utc
option, or the --universal option. Typically, this means that all three options really do the same thing, but
sometimes this format (use of the | character) is used to indicate that the options can't be used in combination, like
a logical or.
If the term is not found, or you have reached the end of the matches, then the program will report Pattern not found
(press Return). If a match is found and you want to move to the next match of the term, press N. To return to a
previous match of the term, press Shift+N.
Note
If you haven't already, notice here that pager commands are viewed on the final line of the screen.
Additionally, there are several different types of commands (user commands, system commands, and
administration commands) as well as other features that require documentation, such as libraries and kernel
components.
As a result, there are thousands of man pages on a typical Linux distribution. To organize all of these man pages,
the pages are categorized by sections, much like each individual man page is broken into sections.
By default, there are nine sections of man pages:
When you use the man command, it searches each of these sections in order until it finds the first match. For
example, if you execute the command man cal, the first section (Executable programs or shell commands) is
searched for a man page called cal. If not found, then the second section is searched. If no man page is found after
searching all sections, you will receive an error message:
Consider This
Typically, there are many more sections than the standard nine. Custom software designers make man pages, using
non-standard section names, for example "3p".
For example, there is a command called passwd that allows you to change your password. There is also a file
called passwd that stores account information. Both the command and the file have a man page.
The passwd command is a user command, so the following command will display the man page for the passwd
command, located in the first section, by default:
NAME
passwd - change user password
To specify a different section, provide the number of the section as the first argument of the man command. For example,
the command man 5 passwd will look for the passwd man page in section 5 only:
NAME
passwd - the password file
The -f option to the man command will display man pages that match, or partially match, a specific name and
provide a brief description of each man page:
Note that on most Linux distributions, the whatis command does the same thing as man -f. On those distributions,
both will produce the same output.
For example, what if you knew you wanted a man page that displays how to change your password, but you didn't
remember the exact name? You could run the command man -k password
Warning
When you use this option, you may end up with a large amount of output. The preceding command, for example,
provided over 60 results.
Recall that there are thousands of man pages, so when you search for a keyword, be as specific as possible. Using a
generic word, such as "the" could result in hundreds or even thousands of results.
Note that on most Linux distributions, the apropos command does the same thing as man -k. On those
distributions, both will produce the same output.
Consider This
Want to learn more about man pages? Execute the following command:
Sumber : https://content.netdevgroup.com/contents/lpic1-s1/294KjJnWHt/
Lab 2: Using the Shell
2.1 Introduction
In this lab, you will interact with the shell and gain an understanding of how basic commands are executed. You
will also learn how to seek help on various commands and topics.
We will begin with simple commands and then go on to execute commands with options and arguments.
2.1.1 Step 1
You will begin by running three commands: pwd, ls, and uname. These commands, in their simplest forms, are
executed without any options or arguments.
At the shell prompt, type the pwd command and press the Enter key. The pwd command displays the directory
where the user is currently located in the file system:
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$
The working directory is the directory that your terminal window or shell is currently "in". This is also called the
current directory. This is an important consideration when you are running commands, since they may behave
differently based on the directory you are currently in.
The output of the pwd command (/home/sysadmin in this example) is called a path. The first slash represents the
root directory, the top level of the directory structure; home is a directory under the root directory, and sysadmin is
a directory under the home directory.
2.1.2 Step 2
Execute the ls command at the prompt and press Enter; this command will list the files and directories that are in
the current directory:
ls
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates Documents Music Public Videos
sysadmin@localhost:~$
Warning: Linux commands are case sensitive. Running the ls command is not the same as LS or Ls.
Try executing LS at the prompt and notice that it will report an error:
LS
sysadmin@localhost:~$ LS
Command 'LS' not found, but can be installed with:
sudo apt install sl
sysadmin@localhost:~$
2.1.3 Step 3
The uname command displays information about the current system. Execute the following uname command and
review the output:
uname
sysadmin@localhost:~$ uname
Linux
sysadmin@localhost:~$
2.1.4 Step 4
There are two types of options for Linux commands:
The ls command can be executed using various options. Execute the following ls command with the –a option.
You will notice that with the –a option specified, the result now includes hidden files that begin with a dot .
character:
ls -a
sysadmin@localhost:~$ ls -a
. .bashrc .selected_editor Downloads Public
.. .cache Desktop Music Templates
.bash_logout .profile Documents Pictures Videos
sysadmin@localhost:~$
File names displayed in different colors denote different file types. For example, blue denotes directories and white denotes
plain files
sysadmin@localhost:~$ ls --all
. .bashrc .selected_editor Downloads Public
.. .cache Desktop Music Templates
.bash_logout .profile Documents Pictures Videos
sysadmin@localhost:~$
Warning: If you don't include two hyphens and instead use the option -all, your output will be different because you will
really be executing the ls -a -l command.
2.1.6 Step 6
Execute the ls command with the –l option; this will show the listing in long format:
ls -l
sysadmin@localhost:~$ ls -l
total 32
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Desktop
drwxr-xr-x 4 sysadmin sysadmin 4096 Mar 29 17:36 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Downloads
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Videos
sysadmin@localhost:~$
2.1.7 Step 7
The –r option for the ls command produces the listing in reverse sorting order. Compare it with the output from
the ls command executed earlier.
ls -r
sysadmin@localhost:~$ ls -r
Videos Templates Public Pictures Music Downloads Documents Desktop
sysadmin@localhost:~$
2.1.8 Step 8
It is typically possible to combine more than one option in any order. Execute the following ls command with all
three options you have previously used:
ls -alr
sysadmin@localhost:~$ ls -alr
total 60
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Videos
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Downloads
drwxr-xr-x 4 sysadmin sysadmin 4096 Mar 29 17:36 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Desktop
-rw-r--r-- 1 sysadmin sysadmin 74 Mar 29 17:36 .selected_editor
-rw-r--r-- 1 sysadmin sysadmin 807 Apr 4 2018 .profile
drwx------ 2 sysadmin sysadmin 4096 Apr 2 15:27 .cache
-rw-r--r-- 1 sysadmin sysadmin 3768 Mar 29 17:36 .bashrc
-rw-r--r-- 1 sysadmin sysadmin 220 Apr 4 2018 .bash_logout
drwxr-xr-x 1 root root 4096 Mar 29 17:36 ..
drwxr-xr-x 1 sysadmin sysadmin 4096 Apr 2 15:27 .
sysadmin@localhost:~$
This ls command output has the combined effect of all the options specified:
Warning: Not all options can be combined. Some options are conflicting; for example, an option to the ls
command that sorts files by name would conflict with an option that sorts files by size. Later in this lab, you will
see how to consult documentation to determine which options will conflict.
2.1.9 Step 9
Just to confirm that it has the same combined effect, specify all of the options separately. You will note that it
produces the same result:
ls -a -l -r
sysadmin@localhost:~$ ls -a -l -r
total 60
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Videos
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Downloads
drwxr-xr-x 4 sysadmin sysadmin 4096 Mar 29 17:36 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Desktop
-rw-r--r-- 1 sysadmin sysadmin 74 Mar 29 17:36 .selected_editor
-rw-r--r-- 1 sysadmin sysadmin 807 Apr 4 2018 .profile
drwx------ 2 sysadmin sysadmin 4096 Apr 2 15:27 .cache
-rw-r--r-- 1 sysadmin sysadmin 3768 Mar 29 17:36 .bashrc
-rw-r--r-- 1 sysadmin sysadmin 220 Apr 4 2018 .bash_logout
drwxr-xr-x 1 root root 4096 Mar 29 17:36 ..
drwxr-xr-x 1 sysadmin sysadmin 4096 Apr 2 15:27 .
sysadmin@localhost:~$
2.1.10 Step 10
Earlier, without any options specified, the uname command had produced the output Linux. Now, execute the
uname command with various options and notice the additional information it can display:
uname –a
uname –s
uname –n
uname –m
uname –p
uname –i
uname -o
sysadmin@localhost:~$ uname -a
Linux localhost 4.4.0-141-generic #167~14.04.1-Ubuntu SMP Mon Dec 10 13:20:24 UTC 2018
x86_64 x86_64 x86_64 GNU/Linux
sysadmin@localhost:~$ uname -s
Linux
sysadmin@localhost:~$ uname -n
localhost
sysadmin@localhost:~$ uname -m
x86_64
sysadmin@localhost:~$ uname -p
x86_64
sysadmin@localhost:~$ uname -i
x86_64
sysadmin@localhost:~$ uname -o
GNU/Linux
sysadmin@localhost:~$
Note that the uname command provides various system information, including system name (-n) and processor type (-p).
2.1.11 Step 11
After the command and its options, many commands will accept one or more arguments. Commands that use
arguments may require multiple values. The -w option, when specified with the ls command, can be used to
specify the width of the screen (normally this is determined by the actual terminal window size):
ls –rw 40
sysadmin@localhost:~$ ls -rw 40
Videos Picture Documents
Templates Music Desktop
Public Downloads
sysadmin@localhost:~$
2.1.12 Step 12
Since the -w option requires an argument, it must be specified at the end of the options. Combining options where
the -w option is not at the end will not work. Execute the following command and notice the error that is reported:
ls –wr 40
sysadmin@localhost:~$ ls -wr 40
ls: invalid line width: 'r'
sysadmin@localhost:~$
2.1.13 Step 13
It is not possible to combine multiple options that require arguments. In such a case, they have to appear separately.
Execute the following to see a demonstration:
ls –w 40 –I "T*"
sysadmin@localhost:~$ ls -w 40 -I "T*"
Desktop Downloads Pictures Videos
Documents Music Public
sysadmin@localhost:~$
The ignore -I option will cause the ls command to not display specific files that match a pattern. The pattern "T*" means
any file or directory that begins with a capital T. In this case, the Templates directory was not displayed.
2.1.14 Step 14
It is possible to use both short and long options in the same command line. Execute the ls command using both
long and short options as shown below:
ls --all –rl
sysadmin@localhost:~$ ls --all -rl
total 60
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Videos
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Downloads
drwxr-xr-x 4 sysadmin sysadmin 4096 Mar 29 17:36 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 29 17:36 Desktop
-rw-r--r-- 1 sysadmin sysadmin 74 Mar 29 17:36 .selected_editor
-rw-r--r-- 1 sysadmin sysadmin 807 Apr 4 2018 .profile
drwx------ 2 sysadmin sysadmin 4096 Apr 2 15:27 .cache
-rw-r--r-- 1 sysadmin sysadmin 3768 Mar 29 17:36 .bashrc
-rw-r--r-- 1 sysadmin sysadmin 220 Apr 4 2018 .bash_logout
drwxr-xr-x 1 root root 4096 Mar 29 17:36 ..
drwxr-xr-x 1 sysadmin sysadmin 4096 Apr 2 15:27 .
sysadmin@localhost:~$
2.1.15 Step 15
Similarly, it is possible to run commands with long options and with arguments. For example, execute the
following to display files with the ls command without having the file names color-coded by file type:
ls --color=never
sysadmin@localhost:~$ ls --color=never
Desktop Documents Downloads Music Pictures Public Templates Videos
sysadmin@localhost:~$
2.1.16 Step 16
A lone double hyphen indicates the end of all the options for the command. Without this double hyphen, the
following sequence of commands would not work since --badname would be considered an option to these
commands rather than a file name:
The touch command creates a file with a specific timestamp. The ls command displays this file with its timestamp. The rm
command deletes the file. Without the -- characters before the --badname file name, these commands would consider --
badname to be an option to the commands.
2.1.17 Step 17
Some commands make use of arguments that don't require any options. For example, execute the following touch
command with a file name. This will create a file. Then execute the ls command immediately after to confirm that
it has created the file:
touch lpicfile.txt
ls
rm lpicfile.txt
ls
sysadmin@localhost:~$ rm lpicfile.txt
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
sysadmin@localhost:~$$
2.1.19 Step 19
Some commands, like the cp command, require at least two arguments. The cp command requires the source file to
copy and the destination file (where to copy the file to). For example, execute the following to copy the hosts file
from the /etc directory into the current directory with a new name of myhosts:
cp /etc/hosts myhosts
ls
2.1.20 Step 20
Arguments that contain spaces or non-alphanumeric characters need to be enclosed within single quotes ' or
double quotes ". The use of single quotes is safer because it prevents the shell from interpreting any special
characters. For example, without quotes, the shell will interpret a double exclamation point !! as repeat the last
command executed.
Note: Special characters such as the exclamation point ! character, the question mark ? character, the asterisk *
character, and more will be covered in greater detail later in the course.
Use the echo command to illustrate the effects of different quotes on special characters:
ls
echo something new!!
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates myhosts
Documents Music Public Videos
sysadmin@localhost:~$ echo something new!!
echo something newls
something newls
The shell interprets the double exclamation point !! as the previously executed ls command and then the echo
command returns the string something newls. This time, try executing the echo command with double quotes " :
ls
echo "something new!!"
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates myhosts
Documents Music Public Videos
sysadmin@localhost:~$ echo "something new!!"
something newls
Slightly better, but the ls command is still appended to the end of the string something new. Now use single quotes
' to let the shell know that everything inside should be interpreted literally:
ls
echo 'something new!!'
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates myhosts
Documents Music Public Videos
sysadmin@localhost:~$ echo 'something new!!'
something new!!
This time the echo command worked properly, displaying only the data contained within single quotes
2.1.21 Step 21
Aliases serve as a nickname for another command or series of commands. As you can see by the commands that
you have executed so far, commands in the Linux command shell require a fair amount of memorizing and typing.
Aliases can help save such repetitive effort. New aliases can be created using the following format, where name is
the name to be given the alias and command is the command to be executed when the alias is run.
alias name=command
Instead of typing the command ls –alr each time, you can create an alias called l as shown below. Single quotes
' are used to ensure that all the characters retain their literal meaning:
Consider This
Later, you can remove entries from your list of aliases by using the unalias command:
unalias l
l
sysadmin@localhost:~$ unalias l
sysadmin@localhost:~$ l
l: command not found
sysadmin@localhost:~$
2.1.22 Step 22
The Bash shell provides a feature designed to avoid typing mistakes and save typing effort; this feature is called
command completion. Command completion allows you to type a few characters of a command (or its file name
argument) and then press the Tab key. The Bash shell will complete the command (or its file name argument)
automatically for you.
ec
sysadmin@localhost:~$ ec
Next, press the Tab key, and the shell will automatically complete the command echo for you (it even places a
space after the command so you can immediately start typing the option or argument):
sysadmin@localhost:~$ echo_
2.1.23 Step 23
When the characters that are typed are insufficient to match a single command, all the possible choices are
displayed if you press the Tab key twice:
ca
sysadmin@localhost:~$ ca
cal capsh cat cautious-launcher
calendar captoinfo catchsegv
caller case catman
sysadmin@localhost:~$ ca
Finish this command by pressing the letter l, followed by the Enter key:
l
sysadmin@localhost:~$ cal
April 2020
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
sysadmin@localhost:~$
2.1.24 Step 24
The type command is used to determine what a file is and/or where it is located. Use the type command to get
information about the pwd command.
type pwd
The pwd command is identified as a shell builtin; a command that is part of the shell rather than a separate program.
Let's find out more about the man command that’s used for viewing manual pages.
type man
2.2.1 Step 1
To access man pages of the ls command, execute the following command:
man ls
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci
-fied.
Mandatory arguments to long options are mandatory for short
options too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
--author
Manual page ls(1) line 1 (press h for help or q to quit)
2.2.2 Step 2
To view the various movement commands that are available, type the H key or Shift+h while viewing a man page
2.2.3 Step 3
Press the Spacebar to read through the help section. When you are ready to go back to the man page for the ls
command, press the Q key:
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci
-fied.
Mandatory arguments to long options are mandatory for short
options too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
--author
Manual page ls(1) line 1 (press h for help or q to quit)
2.2.4 Step 4
Take a few moments to explore a few of the movement commands that are available when displaying a man page:
Command Function
Return (or Enter) Go down one line
Space Go down one page
/term Search for term
n Find next search item
1G Go to the beginning
G Go to the end
h Display help
q Quit man page
2.2.5 Step 5
When you are finished exploring the man page, exit the ls man page by typing the Q key:
q
sysadmin@localhost:~$ man ls
sysadmin@localhost:~$
2.2.6 Step 6
A command may be included in more than one section of man pages. To display a specific section for the passwd
command, execute the following:
man 5 passwd
NAME
passwd - the password file
DESCRIPTION
/etc/passwd contains one line for each user account, with seven fields
delimited by colons (":"). These fields are:
o login name
o numerical user ID
o numerical group ID
Consider This
2.2.7 Step 7
When you are finished exploring the man page, exit the man page by typing the Q key:
2.2.8 Step 8
If you do not know the exact man page you are looking for, you can search for man pages that match a keyword by
using the -k option to the man command. For example, execute the following:
man –k passwd
Note: When you use the -k option, both the man page names and descriptions are searched.
2.2.9 Step 9
To search only for the man page name, use the -f option:
man –f passwd
2.2.10 Step 10
Alternatively, you can try the whatis command, which produces the same result as the man -f command:
whatis passwd
Consider This
Want to learn more about man pages? Execute the command man man:
Sumber : https://content.netdevgroup.com/labs/lpic1-s1/EfrmyJmPoR/
NDG Introduction to Linux I - Chapter 3: Configuring the
Shell
3.1 Introduction
One key component of the Bash shell is shell variables. These variables are critical because they store vital system
information and modify the behavior of the Bash shell, as well as many commands. This chapter will discuss in
detail what shell variables are and how they can be used to configure the shell.
You will learn how the PATH variable affects how commands are executed and how other variables affect your
ability to use the history of your commands. You will also see how to use initialization files to make shell variables
persistent, so they will be created each time you log into the system.
Additionally, this chapter will cover how to use the command history and the files that are used to store and
configure the command history.
Variables typically hold a single value, like 0 or bob. Some may contain multiple values separated by spaces like
Joe Brown or by other characters, such as colons; /usr/bin:/usr/sbin:/bin:/usr/bin:/home/joe/bin.
You assign a value to a variable by typing the name of the variable immediately followed by the equal sign =
character and then the value. For example:
name="Bob Smith"
Variable names should start with a letter (alpha character) or underscore and contain only letters, numbers and the
underscore character. It is important to remember that variable names are case sensitive; a and A are different
variables. Just as with arguments to commands, single or double quotes should be used when special characters are
included in the value assigned to the variable to prevent shell expansion.
The Bash shell and many commands make extensive use of variables. One of the main uses of variables is to
provide a means to configure various preferences for each user. The Bash shell and commands can behave in
different ways, based on the value of variables. For the shell, variables can affect what the prompt displays, the
directories the shell will search for commands to execute and much more.
3.2.1 Local and Environment Variables
A local variable is only available to the shell in which it was created. An environment variable is available to the
shell in which it was created, and it is passed into all other commands/programs started by the shell.
To set the value of a variable, use the following assignment expression. If the variable already exists, the value of
the variable is modified. If the variable name does not already exist, the shell creates a new local variable and sets
the value:
variable=value
In the example below, a local variable is created, and the echo command is used to display its value:
sysadmin@localhost:~$ name="judy"
sysadmin@localhost:~$ echo $name
judy
By convention, lowercase characters are used to create local variable names, and uppercase characters are used
when naming an environment variable. For example, a local variable might be called test while an environment
variable might be called TEST. While this is a convention that most people follow, it is not a rule. An environment
variable can be created directly by using the export command.
export variable=value
In the example below, an environment variable is created with the export command:
Recall that the echo command is used to display output in the terminal. To display the value of the variable, use a
dollar sign $ character followed by the variable name as an argument to the echo command:
We can see the difference between local and environment variables by opening a new shell with the bash
command
sysadmin@localhost:~$ bash
To run a command as administrator (user "root"), use "sudo" command.
See "man sudo_root" for details.
sysadmin@localhost:~$ echo $name
sysadmin@localhost:~$ echo $JOB
engineer
Notice that the local variable name is empty, while the environment variable JOB returns the value engineer. Also notice
that if you return to the first shell by using the exit command, both variables are still available in the original shell:
sysadmin@localhost:~$ exit
exit
sysadmin@localhost:~$ echo $name
judy
sysadmin@localhost:~$ echo $JOB
engineer
The local variable name is not available in the new shell because by default, when a variable is assigned in the
Bash shell, it is initially set as a local variable. When you exit the original shell, only the environment variables
will be available. There are several ways that a local variable can be made into an environment variable.
First, an existing local variable can be exported with the export command.
export variable
In the example below, the local variable name is exported to environment (with the standard convention of all
caps):
sysadmin@localhost:~$ NAME=judy
sysadmin@localhost:~$ export NAME
sysadmin@localhost:~$ echo $NAME
judy
Second, a new variable can be exported and assigned a value with a single command as demonstrated below with
the variable DEPARTMENT:
Third, the declare or typeset command can be used with the export -x option to declare a variable to be an
environment variable. These commands are synonymous and work the same way:
The env command is used to run commands in a modified environment. It can also be used to temporarily create or
change environment variables that are only passed to a single command execution by using the following syntax:
For example, servers are often set to Coordinated Universal Time (UTC), which is good for maintaining consistent
time on servers across the planet, but can be frustrating for practical use to simply tell the time:
sysadmin@localhost:~$ date
Sun Mar 10 22:47:44 UTC 2020
To temporarily set the time zone variable, use the env command. The following will run the date command with
the temporary variable assignment:
The TZ variable is set only in the environment of the current shell, and only for the duration of the command. The
rest of the system will not be affected by this variable. In fact, running the date command again will verify that the
TZ variable has reverted to UTC.
sysadmin@localhost:~$ date
Sun Mar 10 22:49:46 UTC 2020
Note
The example above utilizes a command line pipe, represented by the | character. The pipe character can be used to
send the output of one command to another.
The command line pipes will be covered in greater detail later in the course.
Note
The tail command is used to display only the last few lines of a file, (or, when used with a pipe, the output of a
previous command). By default, the tail command displays ten lines of a file provided as an argument.
The tail command will be covered in greater detail later in the course.
To display only environment variables, you can use several commands that provide nearly the same output:
env
declare -x
typeset -x
export -p
To display the value of a specific variable, use the echo command with the name of the variable prefixed by the $ (dollar
sign). For example, to display the value of the PATH variable, you would execute echo $PATH:
Consider This
Variables can also be enclosed in the curly brace {} characters in order to delimit them from surrounding text.
While the echo ${PATH} command would produce the same result as the echo $PATH command, the curly braces
set the variable apart visually, making it easier to see in scripts in some contexts.
unset VARIABLE
sysadmin@localhost:~$ example=12
sysadmin@localhost:~$ echo $example
12
sysadmin@localhost:~$ unset example
sysadmin@localhost:~$ echo $example
Warning
Do not unset critical system variables like the PATH variable, as this may lead to a malfunctioning environment.
3.2.4 PATH Variable
The PATH variable is one of the most critical environment variables for the shell, so it is important to understand the
effect it has on how commands will be executed.
The PATH variable contains a list of directories that are used to search for commands entered by the user. When the
user types a command and then presses the Enter key, the PATH directories are searched for an executable file that
matches the command name. Processing works through the list of directories from left to right; the first executable
file that matches what is typed is the command the shell will try to execute.
Note
Before searching the PATH variable for the command, the shell will first determine if the command is an alias or
function, which may result in the PATH variable not being utilized when that specific command is executed.
Additionally, if the command happens to be built-in to the shell, the PATH variable will not be utilized.
Using the echo command to display the current $PATH will return all the directories that files can be executed from.
The following example displays a typical PATH variable, with directory names separated from each other by a colon
: character:
The following table illustrates the purpose of some of the directories displayed in the output of the previous
command:
Directory Contents
/home/sysadmin/bin
A directory for the current user sysadmin to place programs. Typically used by users who
create their own scripts.
/usr/local/sbin
Normally empty, but may have administrative commands that have been compiled from
local sources.
/usr/local/bin Normally empty, but may have commands that have been compiled from local sources.
/usr/sbin Contains the majority of the administrative command files.
/usr/bin Contains the majority of the commands that are available for regular users to execute.
/sbin Contains the essential administrative commands.
/bin
Contains the most fundamental commands that are essential for the operating system to
function.
To execute commands that are not contained in the directories that are listed in the PATH variable, several options
exist:
The command may be executed by typing the absolute path to the command.
The command may be executed with a relative path to the command.
The PATH variable can be set to include the directory where the command is located.
The command can be copied to a directory that is listed in the PATH variable.
To demonstrate absolute paths and relative paths an executable script named my.sh is created in the home
directory. Next, that script file is given “execute permissions” (permissions are covered in a later chapter):
An absolute path specifies the location of a file or directory from the top-level directory through all of the
subdirectories to the file or directory. Absolute paths always start with the / character representing the root
directory. For example, /usr/bin/ls is an absolute path. It means the ls file, which is in the bin directory, which
is in the usr directory, which is in the / (root) directory. A file can be executed using an absolute path like so:
sysadmin@localhost:~$ /home/sysadmin/my.sh
Hello World!
A relative path specifies the location of a file or directory relative to the current directory. For example, in the
/home/sysadmin directory, a relative path of test/newfile would actually refer to the
/home/sysadmin/test/newfile file. Relative paths never start with the / character.
Note
Visualizing the directory structure using paths can be confusing at first, but it is a necessary skill for effectively
navigating the filesystem.
Absolute paths and relative paths are covered in greater detail in the NDG Linux Essentials course.
Using a relative path to execute a file in the current directory requires the use of the . character, which symbolizes
the current directory:
sysadmin@localhost:~$ ./my.sh
Hello World!
Sometimes a user wants their home directory added to the PATH variable in order to run scripts and programs
without using ./ in front of the file name. They might be tempted to modify the PATH variable like so:
Unfortunately, modifying a variable this way overwrites the contents. Therefore, everything that was previously
contained in the PATH variable will be lost.
sysadmin@localhost:~$ uname -a
Linux localhost 3.15.6+ #2 SMP Wed Jul 23 01:26:02 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
After assigning the home directory to the PATH variable, the uname -a command will need its full path name to execute
successfully:
sysadmin@localhost:~$ PATH=/home/sysadmin
sysadmin@localhost:~$ uname -a
-bash: uname: command not found
sysadmin@localhost:~$ /bin/uname -a
Linux localhost 3.15.6+ #2 SMP Wed Jul 23 01:26:02 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Important
If you changed the PATH variable like we did in the previous example and want to reset it, simply use the exit
command to logout:
sysadmin@localhost:~$ exit
Once logged back in, the PATH variable will be reset to its original value.
It is possible to add a new directory to the PATH variable without overwriting its previous contents. Import the
current value of the $PATH variable into the newly defined PATH variable by using it on both sides of the assignment
statement:
sysadmin@localhost:~$ PATH=$PATH
sysadmin@localhost:~$ PATH=$PATH:/home/sysadmin
sysadmin@localhost:~$ echo $PATH
/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/
usr/local/games:/home/sysadmin
Now, scripts located in the /home/sysadmin directory can execute without using a path:
sysadmin@localhost:~$ my.sh
Hello World!
Warning
In general, it is a bad idea to modify the $PATH variable. If it were to change, administrators would view it as
suspicious activity. Malicious forces want to gain elevated privileges and access to sensitive information residing
on Linux servers. One way to do this is to write a script which shares the name of a system command, then change
the PATH variable to include the administrator’s home directory. When the administrator types in the command, it
actually runs the malicious script!
There are two types of initialization files: global initialization files that affect all users on the system and local
initialization files that are specific to an individual user.
The global configuration files are located in the /etc directory. Local configuration files are stored in the user's
home directory.
Each shell uses different initialization files. Additionally, most shells execute different initialization files when the
shell is started via the login process (called a login shell) versus when a shell is started by a terminal (called a non-
login shell or an interactive shell).
The following diagram illustrates the different files that are started with a typical login shell versus an interactive
shell:
Note
Note
File names preceded by a period . character indicates hidden files. You can view these files in your home directory
by using the ls command with the all -a option.
sysadmin@localhost:~$ ls -a
. .bashrc .selected_editor Downloads Public
.. .cache Desktop Music Templates
.bash_logout .profile Documents Pictures Videos
When Bash is started as a login shell, the /etc/profile file is executed first. This file typically executes all files
ending in .sh that are found in the /etc/profile.d directory.
The next file that is executed is usually ~/.bash_profile (but some users may use ~/.bash_login or
~/.profile file). The ~/.bash_profile file typically also executes the ~/.bashrc file which in turn executes the
/etc/bashrc file.
Consider This
Since the ~/.bash_profile file is under the control of the user, the execution of the ~/.bashrc and /etc/bashrc
files are also controllable by the user.
When Bash is started as an interactive shell, it executes the ~/.bashrc file, which may also execute the
/etc/bashrc file, if it exists. Again, since the ~/.bashrc file is owned by the user who is logging in, the user can
prevent execution of the /etc/bashrc file.
With so many initialization files, a common question at this point is "what file am I supposed to use?" The
following chart illustrates the purpose of each of these files, providing examples of what commands you might
place in each file:
File Purpose
This file can only be modified by the administrator and will be executed by every user
/etc/profile who logs in. Administrators use this file to create key environment variables, display
messages to users as they log in, and set key system values.
Each user has their own .bash_profile file in their home directory. The purpose of
~/.bash_profile
~/.bash_login
this file is the same as the /etc/profile file, but having this file allows a user to
~/.profile customize the shell to their own tastes. This file is typically used to create customized
environment variables.
Each user has their own .bashrc file in their home directory. The purpose of this file is
~/.bashrc to generate items that need to be created for each shell, such as local variables and
aliases.
This file may affect every user on the system. Only the administrator can modify this
/etc/bashrc file. Like the .bashrc file, the purpose of this file is to generate items that need to be
created for each shell, such as local variables and aliases.
The best practice is to create a backup before modifying configuration files as insurance against errors; a backup
can always be restored if something should go wrong. The following example references CentOS, a community-
supported Linux distribution based on source code from Red Hat Enterprise Linux.
In some distributions, the default ~/.bash_profile file contains the following two lines which customize the
PATH environment variable:
PATH=$PATH:$HOME/bin
export PATH
The first line sets the PATH variable to the existing value of the PATH variable with the addition of the bin
subdirectory of the user's home directory. The $HOME variable refers to the user's home directory. For example, if
the user logging in is joe, then $HOME/bin is /home/joe/bin.
The second line converts the local PATH variable into an environment variable.
./etc/bashrc
The period . character used to source a file in order to execute it. The period . character also does have a synonym
command, the source command, but the use of the period . character is more common than using the source
command.
Sourcing can be an effective way to test changes made to initialization files, enabling you to correct any errors
without having to log out and log in again. If you update the ~/.bash_profile to alter the PATH variable, you
could verify that your changes are correct by executing the following:
.~/.bash_profile
echo $PATH
Warning
Our virtual environment is not persistent, therefore, any changes made to the configuration files will not be saved if
the VM is reset.
Consider This
When a new user is created, the files from the /etc/skel directory are automatically copied into the new user's
home directory. As an administrator, you can modify the files in the/etc/skel directory to provide custom
features to new users.
3.4 Command History
In a sense, the ~/.bash_history file could also be considered to be an initialization file, since Bash also reads this
file as it starts up. By default, this file contains a history of the commands that a user has executed within the Bash
shell. When a user exits the Bash shell, it writes out the recent history to this file.
There are several ways that this command history is advantageous to the user:
You can use the Up ↑ and Down ↓ Arrow Keys to review your history and select a previous command to
execute again.
You can select a previous command and modify it before executing it.
You can do a reverse search through history to find a previous command to select, edit, and execute it. To
start the search, press Ctrl+R and then begin typing a portion of a previous command.
You execute a command again, based upon a number that is associated with the command.
If the user goes back too far, the Down ↓ Arrow Key can be used to return to a more recent command. Once the
correct command is displayed, the Enter key can be pressed to execute it.
The Left ← and Right → Arrow Keys can also be used to position the cursor within the command. The
Backspace and Delete keys are used to remove text, and additional characters can be typed into the command line
to modify it before pressing the Enter key to execute it.
There are more keys that can be used for editing a command on the command line. The following table summarizes
some helpful editing keys:
To bind the keys to match the key assignments found with another popular text editor, the vi editor. The shell can
be configured with the set -o vi command. To set the key bindings back to the emacs text editor, use the set -o
emacs command.
Note
If you don't know how to use the vi editor yet, you may want to stick with the emacs keys as the vi editor has a
bigger learning curve.
To automatically configure the edit history options at login, edit the ~/.inputrc file. If this file doesn't exist, the
/etc/inputrc file is used instead. Key bindings are set differently in configuration files than on the command
line; for example, to enable the vi key binding mode for an individual, add the following lines to the ~/.inputrc
file:
set editing-mode vi
set keymap vi
If the situation is reversed, perhaps due to the above two lines being added to the /etc/inputrc file, a user can
enable the emacs mode by adding the following lines to the ~/.inputrc file:
sysadmin@localhost:~$ history
1 ls
2 cd test
3 cat alpha.txt
4 ls -l
5 cd ..
6 ls
7 history
Note that each command is assigned a number that a user can use to re-execute the command.
The history command has numerous options; the most common of these options are listed below:
Option Purpose
Option Purpose
-c Clear the list
-r Read the history file and replace the current history
-w Write the current history list to the history file
As the history list commonly contains five hundred or more commands, it is often helpful to filter the list. The
history command accepts a number as an argument to indicate how many commands to list. For example,
executing the following command will only show the last three commands from your history.
sysadmin@localhost:~$ history 3
5 cd ..
6 ls
7 history
Note
The grep command is very useful for filtering the output of commands that produce copious output. For example,
to view all of the commands in your history that contain the ls command, search for the ls pattern using the
following command:
The grep command will be covered in greater detail later in the course.
If a user wants to store the history commands in a file that is different from ~/.bash_history, then the user can
specify an absolute path to the different file as the value for the HISTFILE local variable:
HISTFILE=/path/to/file
The HISTSIZE variable will determine how many commands to keep in memory for each Bash shell. If the size of
HISTSIZE is greater than the size of HISTFILESIZE, then only the most recent number of commands specified by
HISTFILESIZE will be written to the history file when the Bash shell exits.
Although it is not normally set to anything by default, you may want to take advantage of setting a value for the
HISTCONTROL variable in an initialization file like the ~/.bash_profile file. The HISTCONTROL variable could be
set to any of the following features:
HISTCONTROL=ignoredups
Any command that begins with a space will not be stored. This provides the user with an easy way to execute a
command that won't go into the history list.
HISTCONTROL=ignoreboth
Consecutive duplicates and any commands that begin with a space will not be stored.
HISTCONTROL=erasedups
Commands that are identical to another command in your history will not be stored. (The previous entry of the
command will be deleted from the history list.)
HISTCONTROL=ignorespace:erasedups
Another variable that will affect what gets stored in the history of commands is the HISTIGNORE variable. Most
users do not want the history list cluttered with basic commands like the ls, cd, exit, and history commands.
The HISTIGNORE variable can be used to tell Bash not to store certain commands in the history list.
To have commands not included in the history list, include an assignment like the following in the
~/.bash_profile file:
HISTIGNORE='ls*:cd*:history*:exit'
Note
The * character is used to indicate anything else after the command. So, ls* would match any ls command, such
as ls -l or ls /etc.
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
sysadmin@localhost:~$ !!
ls
Desktop Documents Downloads Music Pictures Public Templates Videos
The following table provides some examples of using the exclamation ! character:
History Command Meaning
!! Repeat the last command
!-4 Execute the command that was run four commands ago
!555 Execute command number 555
!ec Execute the last command that started with ec
!?joe Execute the last command that contained joe
Another way to use the history list is to take advantage of the fact that the last argument of every command is
stored. Often a user will type a command to refer to a file, perhaps to display some information about that file.
Subsequently, the user may want to type another command to do something else with the same file. Instead of
having to type the path again, a user can use a couple of keyboard shortcuts to recall that file path from the
previous command line.
By pressing either Esc+. (Escape+Period) or Alt+. (Alt+Period), the shell will bring back the last argument of the
previous command. Repeatedly pressing one of these key combinations will recall, in reverse order, the last
argument of each previous command.
NDG Introduction to Linux I - Lab 3: Configuring the
Shell
One key component of the Bash shell is shell variables. These variables are critical because they store vital system
information and modify the behavior of the Bash shell, as well as many commands. In this lab, you will learn how to
configure shell features such as variables and command history. Understanding these shell features will make it much easier
for you to work in the command line environment.
3.1 Step 1
A local variable is only available to the shell in which it was created. To set the value of a variable, use the
following assignment expression:
variable=value
name="bob"
echo $name
sysadmin@localhost:~$ name="bob"
sysadmin@localhost:~$ echo $name
bob
3.2 Step 2
An environment variable is available to the shell in which it was created, and it is passed into all other
commands/programs started by the shell. An environment variable can be created directly by using the export
command:
export variable=value
export AGE=25
echo $AGE
Open another shell and view the contents of the two variables that were created:
bash
echo $name
echo $AGE
sysadmin@localhost:~$ bash
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
sysadmin@localhost:~$ echo $name
sysadmin@localhost:~$ echo $AGE
25
Notice in the output of the commands above that the $name variable didn't exist in the new shell because local variables
are not passed into new processes. However, the $AGE variable did exist in the new shell, since environment variables are
passed into new processes.
3.4 Step 4
The set command by itself will display all variables (local and environment). To display variables in the original
shell, exit the current shell and use the set command with a pager, along with the less command, in a command
pipeline by using the pipe | character:
exit
set | less
sysadmin@localhost:~$ exit
sysadmin@localhost:~$ set | less
AGE=25
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote
:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:s
ourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_VERSINFO=([0]="2" [1]="8")
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="4" [2]="19" [3]="1" [4]="release" [5]="x86_64-pc-lin
ux-gnu")
BASH_VERSION='4.4.19(1)-release'
COLUMNS=80
DIRSTACK=()
EDITOR=vi
EUID=1001
GROUPS=()
HISTCONTROL=ignoreboth
HISTFILE=/home/sysadmin/.bash_history
HISTFILESIZE=2000
:
3.5 Step 5
The set command used in the previous step displays all variables, both local and environment. To only display
environment variables, execute the env command:
env
sysadmin@localhost:~$ env
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or
=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*
.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01
;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;
31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=0
1;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.wa
r=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31
:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;
35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*
.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01
;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.
mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;
35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli
=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.e
mf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36
:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=0
0;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
LESSCLOSE=/usr/bin/lesspipe %s %s
AGE=25
HUSHLOGIN=FALSE
USER=sysadmin
PWD=/home/sysadmin
HOME=/home/sysadmin
MAIL=/var/mail/sysadmin
SHELL=/bin/bash
TERM=xterm
SHLVL=1
LOGNAME=sysadmin
PATH=/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/ga
mes:/usr/local/games
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/env
sysadmin@localhost:~$
Note
The following commands will also display only environment variables and will provide similar output to the env
command:
declare -x
typeset -x
export -p
3.6 Step 6
If it is no longer necessary for a variable to be defined, the unset command can be used to delete it:
unset VARIABLE
Execute the following commands to unset these variables and then verify that they are no longer set:
unset name
unset AGE
echo $name
echo $AGE
Important
Do not unset critical system variables like the PATH variable, as this may lead to a malfunctioning environment.
3.7 Step 7
The PATH variable contains a list of directories that are used to search for commands entered by the user. When the
user types a command and then presses the Enter key, the PATH directories are searched for an executable file that
matches the command name.
Display the value of the PATH variable by executing the following command:
echo $PATH
sysadmin@localhost:~$ echo $PATH
/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/
usr/local/games
The following table illustrates the purpose of some of the directories displayed in the output of the previous
command:
Directory Contents
/home/sysadmin/bin
A directory for the current sysadmin user to place programs. Typically used by users who
create their own scripts.
/usr/local/sbin
Normally empty, but may have administrative commands that have been compiled from
local sources.
/usr/local/bin Normally empty, but may have commands that have been compiled from local sources.
/usr/sbin Contains the majority of the administrative command files.
/usr/bin Contains the majority of the commands that are available for regular users to execute.
/sbin Contains the essential administrative commands.
/bin
Contains the most fundamental commands that are essential for the operating system to
function.
Note
Recall that when you execute a command without specifying a complete path name, the shell uses the value of the
PATH variable to determine where the command is stored.
3.8 Step 8
Using the which command, you can determine where a command resides in the PATH. Execute the following to
determine where the ls command resides:
which ls
sysadmin@localhost:~$ which ls
/bin/ls
3.9 Step 9
If the command doesn't reside in a directory that is specified in the PATH variable, no output is displayed:
which zzz
Consider This
You can also use the type command, which more clearly indicates that the command is not located in the PATH:
type zzz
sysadmin@localhost:~$ type zzz
-bash: type: zzz: not found
3.10 Step 10
Recall that when the user types a command and then presses the Enter key, the PATH directories are searched for
an executable file that matches the command name. However, it is possible to execute commands that are not
contained in the directories that are listed in the PATH variable using the following options:
The command may be executed by typing the absolute path to the command.
The command may be executed with a relative path to the command.
The PATH variable can be set to include the directory where the command is located.
The command can be copied to a directory that is listed in the PATH variable.
There are two types of paths commonly used when invoking a command or locating a file in the Linux filesystem:
absolute paths and relative paths.
An absolute path specifies the location of a file or directory from the top-level directory through all of the
subdirectories to the file or directory. Absolute paths always start with the / character representing the root
directory. A relative path specifies the location of a file or directory relative to the current directory.
Note
If a command or file seems to be missing, often the reason is that it is not located within directories in the PATH
variable, and the user is trying to access it with a relative path. While specifying an absolute path should remedy
the problem, it can be cumbersome, especially when a file is located several layers deep into a directory structure.
A relative path is used for files that are within the directory structure you are currently located in. For example,
users normally log in to their home directories, so changing to the Documents directory (located under the user’s
home directory) using the relative path is as simple as typing the following command:
cd Documents
sysadmin@localhost:~$ cd Documents
sysadmin@localhost:~/Documents$
If, however, you were in the /etc directory and you want to change to the Documents directory, just typing the
command below from the /etc directory will return an error, because the Documents directory is not located
within the /etc directory:
cd /etc
cd Documents
sysadmin@localhost:~/Documents$ cd /etc
sysadmin@localhost:/etc$ cd Documents
-bash: cd: Documents: No such file or directory
Instead, you would need to use the absolute path by typing the following command:
cd /home/sysadmin/Documents
sysadmin@localhost:/etc$ cd /home/sysadmin/Documents
sysadmin@localhost:~/Documents$
3.11 Step 11
In this step, you will set the PATH variable to include a directory where a command would be located. Make a
directory and then add it to the PATH variable by executing the following commands:
mkdir mybin
PATH=$PATH:/home/sysadmin/mybin
sysadmin@localhost:~/Documents$ cd
sysadmin@localhost:~$ mkdir mybin
sysadmin@localhost:~$ PATH=$PATH:/home/sysadmin/mybin
Verify that the PATH now includes the /home/sysadmin/mybin directory using the echo command:
echo $PATH
Notice in the output above that the /home/sysadmin/mybin directory is appended to the PATH variable:
/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/
usr/local/games:/home/sysadmin/mybin
3.12 Step 12
In the following steps, you will copy a command (executable program) to a directory that is listed in the PATH
variable. Create a simple Bash shell program in your current directory by executing the following commands:
Note
The second command in the example above gives the hello.sh script file execute permissions, which allows for a
file to be run as a process.
/home/sysadmin/hello.sh
sysadmin@localhost:~$ /home/sysadmin/hello.sh
hello
3.14 Step 14
Note that you can't just execute hello.sh, you must include the path to the command. Execute the following to
verify:
hello.sh
sysadmin@localhost:~$ hello.sh
hello.sh: command not found
3.15 Step 15
However, if you place this script in the /home/sysadmin/mybin directory, which is included in the PATH variable,
you can execute the command without specifying the full path. Use the mv command to move the hello.sh file to
the /home/sysadmin/mybin directory, which is located in the PATH and then execute the hello.sh file alone to
verify:
mv hello.sh /home/sysadmin/mybin
hello.sh
Note
which hello.sh
Each shell uses different initialization files. Additionally, most shells execute different initialization files when the
shell is started via the login process (called a login shell) versus when a shell is started by a terminal (called a non-
login shell or an interactive shell).
Use the less pager command to view your account's .bashrc file to see what customizations are set by this file:
less .bashrc
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
3.17 Step 17
Adding custom paths to the PATH environment variable provides users with a means to customize their shell. For
example, if you wanted to create a custom shell script that could run from a directory other than where it was
located, you could add the path to it by modifying the PATH environment variable. The grep command can be used
to specifically see any changes made to the PATH variable in the .bashrc file:
The grep command provides the ability to filter text and will only display lines that contain the argument passed to
the command, in this case, PATH.
The grep command will be covered in greater detail later in the course.
3.18 Step 18
If you wanted to add more to the PATH variable, you could either edit the .bashrc file directly with a text editor
(such as vi, nano, or others covered in a later chapter) or execute the following commands:
n the example above, the >> characters are used to redirect the output of the echo command and append it to the
bottom of the .bashrc file. The tail command displays the last ten lines of the .bashrc file so you can verify the
file was successfully edited.
Note
Redirection and the tail command will be covered in greater detail later in the course.
3.19 Step 19
The ~/.bash_history file could also be considered to be an initialization file since Bash also reads this file as it
starts up. By default, this file contains a history of the commands that a user has executed within the Bash shell.
The command history will allow a user to use the Up ↑ Arrow Key to view previous commands. With each press
of the up arrow, the shell displays one more command back in the history list.
If the user goes back too far, the Down ↓ Arrow Key can be used to return to a more recent command. Once the
correct command is displayed, the Enter key can be pressed to execute it.
The Left ← and Right → Arrow Keys can also be used to position the cursor within the command. The
Backspace and Delete keys are used to remove text, and additional characters can be typed into the command line
to modify it before pressing the Enter key to execute it. The following steps will demonstrate how to use the
command history.
cp /etc/host .
sysadmin@localhost:~$ cp /etc/host .
cp: cannot stat '/etc/host': No such file or directory
3.20 Step 20
The file name should be hosts, not host. To fix this, press the Up Arrow ↑ Key one time, use your Left Arrow
← Keys to move over to the space after host, type an s, and then press the Enter key:
Up Arrow Key
Left Arrow Key
Left Arrow Key
s
Enter Key
sysadmin@localhost:~$ cp /etc/hosts .
3.21 Step 21
Execute the history command to view previously executed commands:
history
sysadmin@localhost:~$ history
1 bash
2 name="bob"
3 echo $name
4 export AGE=25
5 echo $AGE
6 bash
7 set
8 bash
9 env
10 unset name
11 unset AGE
12 echo $name
13 echo $AGE
14 echo $PATH
15 which ls
16 which zzz
17 type zzz
18 mkdir mybin
19 PATH=$PATH:/home/sysadmin/mybin
20 echo "echo hello" > hello.sh
21 chmod a+x hello.sh
22 /home/sysadmin/hello.sh
23 hello.sh
24 mv hello.sh /home/sysadmin/mybin
25 hello.sh
26 which hello.sh
27 more .bashrc
28 grep PATH .bashrc
29 echo 'PATH=$PATH:/home/sysadmin/mybin' >> .bashrc
30 tail .bashrc
31 cp /etc/host .
32 cp /etc/hosts .
33 history
3.22 Step 22
The ! exclamation mark is a special character to the Bash shell to indicate the execution of a command within the
history list. There are many ways to use the ! exclamation character to re-execute commands; for example,
executing two exclamation characters will repeat the previous command.
Execute the history command again by executing the command history shortcut !!:
!!
sysadmin@localhost:~$ !!
1 bash
2 name="bob"
3 echo $name
4 export AGE=25
5 echo $AGE
6 bash
7 set
8 bash
9 env
10 unset name
11 unset AGE
12 echo $name
13 echo $AGE
14 echo $PATH
15 which ls
16 which zzz
17 type zzz
18 mkdir mybin
19 PATH=$PATH:/home/sysadmin/mybin
20 echo "echo hello" > hello.sh
21 chmod a+x hello.sh
22 /home/sysadmin/hello.sh
23 hello.sh
24 mv hello.sh /home/sysadmin/mybin
25 hello.sh
26 which hello.sh
27 more .bashrc
28 grep PATH .bashrc
29 echo 'PATH=$PATH:/home/sysadmin/mybin' >> .bashrc
30 tail .bashrc
31 cp /etc/host .
32 cp /etc/hosts .
33 history
3.23 Step 23
Execute the previous tail command by typing an exclamation ! character followed by the number before the tail
command in the history list. For example, given the previous output, you would execute the following:
The number in the command history in our virtual environment may be different from the example.
!15
sysadmin@localhost:~$ !15
which ls
/bin/ls
3.24 Step 24
Execute the following commands to have the history feature ignore duplicate commands:
HISTCONTROL=ignoredups
ls
ls
history | tail
sysadmin@localhost:~$ HISTCONTROL=ignoredups
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates hosts
Documents Music Public Videos mybin
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates hosts
Documents Music Public Videos mybin
sysadmin@localhost:~$ history | tail
37 history
38 bash
39 env
40 history
41 echo $AGE
42 history
43 which ls
44 HISTCONTROL=ignoredups
45 ls
46 history | tail
Notice that only one of the ls commands was recorded. The history | tail command line takes the output of the
history command and only displays the last ten lines of that output.
3.25 Step 25
To prevent the history feature from recording commands that begin with a space character, execute the following
commands. (Note that the cal command used in the example below has a space in front of it.):
HISTCONTROL=ignorespace
date
cal
history | tail
sysadmin@localhost:~$ HISTCONTROL=ignorespace
sysadmin@localhost:~$ date
Tue Apr 9 15:26:35 UTC 2019
sysadmin@localhost:~$ cal
April 2019
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
3.26 Step 26
To clear the history list, use the -c option with the history command:
history -c
sysadmin@localhost:~$ history -c
sysadmin@localhost:~$ history
1 history
Sumber : https://content.netdevgroup.com/labs/lpic1-s1/g75k2uFDxF/
NDG Introduction to Linux I - Chapter 4: File Globbing
File globbing might sound like an unusual term, but the concept of globbing is probably a familiar one. Like a
joker in a deck of cards, which you might be able to use as any card (a wildcard), glob characters are special
characters that can be used to match file or path names.
For the details of how globbing works, you can view the man page on glob in section 7 of the manual pages by
executing:
A string is a wildcard pattern if it contains one of the characters '?', '*', or '['. Globbing is the operation
that expands a wildcard pattern into the list of path names matching the pattern.
The shell is what performs the expansion of the wildcards; therefore, the use of globbing is not limited to a
particular command. Globbing is especially useful for file management since it can be used to match the path
names to files that you want to manage.
sysadmin@localhost:~$ echo *
Desktop Documents Downloads Music Pictures Public Templates Videos
In the screen capture above, the echo command is used to see which files a glob pattern will match. In this case, it
displays the name of every file in the user's current directory.
Consider This
An empty string is a string that has nothing in it. It is commonly referred to as "".
In order to limit the * wildcard, so that it matches fewer file names, it can be combined with other characters. For
example, the D* pattern will only match file names that start with a D character:
sysadmin@localhost:~$ echo D*
Desktop Documents Downloads
The * character can appear anywhere in the pattern; it can even appear multiple times. It is possible to find a file
that not only starts with a D, but also contains the letter n, by using the D*n* pattern:
sysadmin@localhost:~$ cd /usr/bin
sysadmin@localhost:/usr/bin$ echo /usr/bin/?
/usr/bin/[ /usr/bin/w
By adding more question mark ? characters, it is possible to match additional characters. In order to see which files
had two or three characters as their file names, execute the following two echo commands:
sysadmin@localhost:/usr/bin$ echo ??
du ex hd id nl od pg pr sg tr ul vi wc xz
sysadmin@localhost:/usr/bin$ echo ???
a2p apt awk cal cmp col cut dig env eqn fmt gpg lcf ldd man pdb pic ptx rcp rev
rsh s2p scp see seq ssh sum tac tbl tee tic toe top tty ucf who xxd yes zip
The incorporation of other characters to patterns using the question mark ? character, including other wildcard
characters, allow for more specific searches. For example, the w?? pattern will match files that started with the
letter w, and are exactly three characters long:
While the w??* pattern will match files that started with the letter w, but are at least three characters long:
For example, using a pattern like [abcd]?? will match file names that start with an a, b, c, or d character and
that are three characters long. Alternatively, letters that are consecutive can also be expressed as a range like the
[a-d]?? pattern:
Consider This
When the hyphen - character is used with square brackets, it is referred to as a range. For example: the [a-z]
pattern would match any single character that is within the range of characters from a to z.
This range is defined by the ASCII text table. Simply use a lower value character from the ASCII table as the first
character and a higher value character as the second.
To see the numeric value assigned to each character in the ASCII text table, either do a simple search for "ASCII
text table" on the internet or view the table with the ascii command:
sysadmin@localhost:~$ ascii
The touch command in the following example is used to create the files that will be matched. Single quotes were
used when specifying the file name to create with the touch command because within single quotes, globbing
characters lose their special meaning to the shell.
sysadmin@localhost:/usr/bin$ cd ~/Documents
sysadmin@localhost:~/Documents$ touch '*' '**' '***' '?' '??' '???' '[][][]'
sysadmin@localhost:~/Documents$ ls
'*' School alpha-third.txt letters.txt people.csv
'**' Work alpha.txt linux.txt profile.txt
'***' '[][][]' animals.txt longfile.txt red.txt
'?' adjectives.txt food.txt newhome.txt spelling.txt
'??' alpha-first.txt hello.sh numbers.txt words
'???' alpha-second.txt hidden.txt os.csv
When placed inside the square brackets, the wildcard characters lose their wildcard meaning and only match
themselves. So, a pattern like [*]* would match a file name that begins with an * character. A pattern of [?]*
would match a file name that begins with a ? character.
Even using square brackets inside of the square brackets will match a square bracket, so [[]]* would match a file
name that starts with the square brackets:
Warning
Normally, you want to avoid using special characters like *, ?, [ and ] within file names because these
characters, when used in a file name on the command line, can end up matching other file names unintentionally.
Just as with the other wildcard characters, the square brackets can appear multiple times in a pattern. For example,
to match a file in the /usr/bin directory that contains at least two digits in the file name, use the /etc/*[0-9][0-
9]* pattern:
sysadmin@localhost:~/Documents$ cd /usr/bin
sysadmin@localhost:/usr/bin$ echo *[0-9][0-9]*
base64 i386 linux32 linux64 perl5.18.2 pstree.x11 sha224sum sha256sum sha384sum sha512sum
x86_64
The square brackets also support negating the set of characters (also called complementation). If the first character
inside of the square brackets is either an exclamation ! character or a caret ^ character, then that first character has
a special meaning of not the following characters, meaning match a single character that is not within the set of
characters that follows it.
In other words, the [!a-v]* pattern would match a file name that does not begin with a letter a through v:
Sumber : https://1579225036.netacad.com/courses/1009461/modules/items/66957930
NDG Introduction to Linux I - Lab 4: File Globbing
4.0 Introduction
In this task, you will explore the usage of glob characters, which are wildcards used to match files or path names.
Globbing expands the given wildcard pattern into a list of path names matching the pattern. This function is
performed by the shell and is extremely useful for file operations. It can be used with the grep and echo commands
as well as various file commands such as ls, cp, mv, and rm.
File globbing allows you to expand or restrict your search by modifying the wildcard pattern.
4.1 Step 1
To display the names of all files in your current working directory, execute the echo command with the asterisk *
character as the argument:
echo *
sysadmin@localhost:~$ echo *
Desktop Documents Downloads Music Pictures Public Templates Videos
The echo command can be used to see which files a glob pattern will match. The asterisk * character is a wildcard
pattern that is used for matching any string, including an empty string.
In the output above, all the files in your current working directory are displayed.
4.2 Step 2
To view all files and directories with names starting with the letter D, execute the following command:
echo D*
sysadmin@localhost:~$ echo D*
Desktop Documents Downloads
4.3 Step 3
The asterisk * character can be used at any position within the pattern. To list all files and directories with names
starting with the letter D and containing the letter n, execute the following command:
echo D*n*
Important
In Linux, file names are case sensitive. So, the output of D*n* will be different from that of D*N*. Also, the shell
will not match files starting with a period . character when it matches an asterisk * character, as these are special
files called hidden files.
4.4 Step 4
To list all the files in the /usr/bin directory with names beginning with pyd, execute the following command:
echo /usr/bin/pyd*
4.5 Step 5
The question mark ? character can be used for matching exactly one character. To view all files with names
consisting of exactly two characters in the /usr/bin directory, execute the following command:
echo /usr/bin/??
echo D?*
4.7 Step 7
To search for a specific set of characters, use the square brackets [] notation. For example, to view all the files and
directories with names starting with the letters P, M, or T, followed by zero or more characters, execute the
following command:
echo [PMT]*
4.8 Step 8
The square brackets [] are used to specify supported ASCII characters. To specify a range of possible characters
from 3 to 6, execute the following command:
echo /etc/rc[3-6]*
4.9 Step 9
To view all file names with the initial character in the range from A to D and containing at least three letters total,
execute the following command:
echo [A-D]??*
echo M[p-u]???
4.11 Step 11
The wildcard characters can have multiple occurrences within a pattern. To list all file names starting with the
letter p, containing the letter t and ending with the numbers in the range [2-6], execute the following command:
echo /usr/bin/p*t*[2-6]
Note
The square brackets notation [] also supports the negation operator. The characters ! or ^ are used for negation; if
either of these characters is placed at the first position within the square bracket, then the pattern is evaluated as to
match any character except the listed characters.
4.12 Step 12
If the first character inside of the square brackets is either an exclamation ! character or a caret ^ character, then
that first character has a special meaning of not the following characters.
To view the file names that begin with rc in the /etc directory, excluding those containing the numbers 3-6,
execute the following command:
echo /etc/rc[^3-6]*
Alternatively, you can use the exclamation point ! character to match the pattern from the previous step:
echo /etc/rc[!3-6]*
sysadmin@localhost:~$ echo /etc/rc[!3-6]*
/etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rcS.d
4.13 Step 13
To view all files with names beginning with rc in the /etc directory except for those that include the number 1,
execute the following command:
echo /etc/rc[!1]*
Note
The wildcard characters lose their special meaning inside the [] notation and are treated as regular characters. For
example, if you specify [*]*, it will try to match with a file name starting with the asterisk * character. Typically
file names don't include wildcard characters, so this feature is rarely needed.
https://content.netdevgroup.com/labs/lpic1-s1/SzrYdqSxui/
Chapter 5: File Manipulation
Everything is considered a file in Linux. Therefore, file management is an important topic. Not only are your
documents considered files, but hardware devices such as hard drives and memory are considered files as well.
Even directories are considered files since they are special files that are used to contain other files.
The essential commands needed for file management are often named by short abbreviations of their functions.
You can use the ls command to list files, the cp command to copy files, the mv command to move or rename
files, and the rm command to remove files.
For working with directories, use the mkdir command to make directories, the rmdir command to remove
directories (if they are empty), and the rm command to recursively delete directories containing files.
Note
While permissions will be covered in greater detail later in the course, they can have an impact on file management
commands.
Permissions determine the level of access that a user has on a file. When a user runs a program and the program
accesses a file, then the permissions are checked to determine if the user has the correct access rights to the file.
There are three types of permissions: read, write, and execute. Each has a different meaning depending on whether
they are applied to a file or a directory.
Read:
On a file, this allows processes to read the contents of the file, meaning the contents can be viewed and
copied.
On a directory, file names in the directory can be listed, but other details are not available.
Write:
A file can be written to by the process, so changes to a file can be saved. Note that the write permission
usually requires the read permission on the file to work correctly with most applications.
On a directory, files can be added to or removed from the directory. Note that the write permission requires
the execute permission on the directory to work correctly.
Execute:
Typically, there are two places where you should always have the write and execute permissions on the directory:
your home directory (for permanent files) and the /tmp directory (for temporary files).
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
For arguments, the ls command will accept an arbitrary number of different path names to attempt to list:
ls [OPTION]... [FILE]...
Note the use of the period . character in the following example. When used where a directory is expected, it
represents the current directory.
/usr/local:
bin etc games include lib man sbin share src
Documents:
School alpha-third.txt hidden.txt numbers.txt spelling.tx
Work alpha.txt letters.txt os.csv words
adjectives.txt animals.txt linux.txt people.csv
alpha-first.txt food.txt longfile.txt profile.txt
alpha-second.txt hello.sh newhome.txt red.txt
However, when the period . character is used at the beginning of a file or directory name, it makes the file hidden
by default. These hidden files are not normally displayed when using the ls command, though many can be
commonly found in user home directories. Hidden files and directories are typically used for individual specific
data or settings. Using the -a option will display all files, including hidden files:
sysadmin@localhost:~$ ls -a
. .bashrc .selected_editor Downloads Public
.. .cache Desktop Music Templates
.bash_logout .profile Documents Pictures Videos
The -A option to the ls command will display almost all files of a directory. The current directory file . and the
parent directory file .. are omitted.
sysadmin@localhost:~$ ls -A
.bash_logout .profile Documents Pictures Videos
.bashrc .selected_editor Downloads Public
.cache Desktop Music Templates
The -d option to the ls command is also important. When listing a directory, the ls command normally will show
the contents of that directory, but when the -d option is added, then it will display the directory itself:
Listing the contents of a directory using the ls command only requires the read permission on the directory.
Command File Type Permission
ls Directory Read
Long Listing
To learn the details about a file, such as the type of file, permissions, ownership, or the timestamp, perform a long
listing, using the -l option to the ls command. Because it provides a variety of output, the /var/log directory is
listed as an example below:
sysadmin@localhost:~$ ls -l /var/log/
total 900
-rw-r--r-- 1 root root 15322 Feb 22 16:32 alternatives.log
drwxr-xr-x 1 root root 4096 Jul 19 2018 apt
-rw-r----- 1 syslog adm 560 Mar 8 02:17 auth.log
-rw-r--r-- 1 root root 35330 May 26 2018 bootstrap.log
-rw-rw---- 1 root utmp 0 May 26 2018 btmp
-rw-r----- 1 syslog adm 293 Mar 8 02:17 cron.log
-rw-r--r-- 1 root adm 85083 Feb 22 16:32 dmesg
-rw-r--r-- 1 root root 351960 Jul 19 2018 dpkg.log
-rw-r--r-- 1 root root 32064 Feb 22 16:32 faillog
drwxr-xr-x 2 root root 4096 Jul 19 2018 journal
-rw-rw-r-- 1 root utmp 292584 Mar 8 02:11 lastlog
-rw-r----- 1 syslog adm 14289 Mar 8 02:17 syslog
-rw------- 1 root root 64128 Feb 22 16:32 tallylog
-rw-rw-r-- 1 root utmp. 384 Mar 8 02:11 wtmp
Viewing the above output as fields that are separated by spaces, they indicate:
File Type
The first character of each line indicates the type of file. The file types are:
- regular file Includes readable files, images files, binary files, and compressed files.
Permissions
The next nine characters demonstrate the permissions of the file. Permissions indicate how certain users can access
a file.
The permissions are read r, write w, and execute x. Breaking this down a bit:
r w - r - - r - -
Permissions will be covered in greater detail later in the course.
User Owner
The syslog user owns this file. Every time a file is created, the ownership is automatically assigned to the user
who created it. This is important because the owner has the rights to set permissions on a file.
Group Owner
This indicates which group owns this file. This is important because any member of this group has a set of
permissions on the file. Although root created this file, any member of the utmp group has read and write access to
it (as indicated by the group permissions).
File Size
Timestamp
-rw-rw---- 1 root utmp 0 May 26 2018 btmp
-rw-r--r-- 1 root adm 85083 Feb 22 16:32 dmesg
This indicates the time that the file's contents were last modified. This time would be listed as just the date and year
if the file was last modified more than six months from the current date. Otherwise, the month, day, and time is
displayed. Using the ls command with the --full-time option will display timestamps in full detail.
File Name
The final field contains the name of the file or directory. In the case of symbolic links, the link name will be shown
along with an arrow and the path name of the file that is linked is shown.
While listing the contents of a directory using the ls command only requires the read permission on the directory,
viewing file details with the -l option also requires the execute permission on the directory.
Sorting
The output of the ls command is sorted alphabetically by file name. It can sort by other methods as well:
Option Function
-t Sorts by timestamp
With both time and size sorts, add the -l option or the --full-time option, to be able to view those details:
sysadmin@localhost:~$ ls -t --full-time
total 32
drwxr-xr-x 2 sysadmin sysadmin 4096 2019-02-22 16:32:34.000000000 +0000 Desktop
drwxr-xr-x 4 sysadmin sysadmin 4096 2019-02-22 16:32:34.000000000 +0000 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 2019-02-22 16:32:34.000000000 +0000 Downloads
drwxr-xr-x 2 sysadmin sysadmin 4096 2019-02-22 16:32:34.000000000 +0000 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 2019-02-22 16:32:34.000000000 +0000 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 2019-02-22 16:32:34.000000000 +0000 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 2019-02-22 16:32:34.000000000 +0000 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 2019-02-22 16:32:34.000000000 +0000 Videos
Recursion
When managing files, it is important to understand what the term recursive option means. When the recursive
option is used with file management commands, it means to apply that command to not only the specified
directory, but also to all subdirectories and all of the files within all subdirectories. To perform a recursive listing
with the ls command, add the -R option.
/var/log/apt:
total 144
-rw-r--r-- 1 root root 13232 Jul 19 2018 eipp.log.xz
-rw-r--r-- 1 root root 18509 Jul 19 2018 history.log
-rw-r----- 1 root adm 108711 Jul 19 2018 term.log
/var/log/journal:
total 0
Warning
Use the recursive option carefully because its impact can be huge! With the ls command, using the -R option can
result in a large amount of output in your terminal. For the other file management commands, the recursive option
can impact a large number of files and a large amount of disk space.
Consider This
Some commands use the -r option for recursion while others use the -R option. The -R option is used for recursive
with the ls command because the -r option is used for reversing how the files are sorted.
Many commands that you will use in Linux expect that the file name provided as an argument is a text file (rather
than some sort of binary file). As a result, it is a good idea to use the file command before attempting to access a
file to make sure that it does contain text. For example, the Documents/newhome.txt file is in ASCII English text
format and can be viewed without any problem:
In the next example, the file /bin/ls is an Executable Link Format (ELF); this file shouldn't be displayed with the
cat command. While the cat command is able to output the contents of binary files, the terminal application may
not handle displaying the content of binary files correctly.
Note
Important
If you use a command that is expecting a text file argument, you may get strange results if you provide a non-text
file. Your terminal may become disrupted by attempting to output binary content; as a result, the terminal may not
be able to function properly anymore.
The result will be strange characters being displayed in the terminal. If this occurs, use the exit or logout
commands to leave the terminal you were using and then reconnect or log in again. It may also be possible to
execute the reset command. If this does not solve the problem, closing the terminal and opening a new one will
solve the problem.
If the argument provided is an existing file, then the touch command will update the file's timestamp:
sysadmin@localhost:~$ ls -l Documents/red.txt
-rw-r--r-- 1 sysadmin sysadmin 51 Feb 22 16:32 Documents/red.txt
sysadmin@localhost:~$ touch Documents/red.txt
sysadmin@localhost:~$ ls -l Documents/red.txt
-rw-r--r-- 1 sysadmin sysadmin 51 Mar 8 02:59 Documents/red.txt
If the argument is a file that does not exist, a new file is created with the current time for the timestamp. The
contents of this new file will be empty:
The last time the file's contents were modified. This is the timestamp provided by the ls -l command by
default. The touch command modified this timestamp by default.
The last time the file was accessed. To modify this timestamp, use the -a option with the touch command.
The last time the file attributes were modified. These file attributes, which include permissions and file
ownership, are also called the file's metadata. To modify this timestamp, use the -c option with the touch
command.
The touch command will normally update the specified time to the current time, but the -t option with a
timestamp value can be used instead.
In the above command, the date/time syntax for the touch command is CCYYMMDDHHMM (optionally add SS for the
seconds). The table below breaks down the date/time structure used in the example above in greater detail:
Because the example above uses the precise time of 12:00 AM, we don’t need to put 00 in the seconds (SS) field.
In order to view all three timestamps that are kept for a file, use the stat command with the path to the file as an
argument:
To create a file with the touch command, you must have the write and execute permission on the parent directory.
If the file already exists, modifying it with the touch command only requires the write permission on the file itself.
If a copy of a file is created before changes are made, then it is possible to revert back to the original.
It can be used to transfer a file to removable media devices.
A copy of an existing document can be made; in this way, it is possible to take advantage of the existing
layout and content to get started more quickly than from scratch.
The cp command is used to copy files. It takes at least two arguments: the first argument is the path to the file to be
copied and the second argument is the path to where the copy will be placed. The files to be copied are sometimes
referred to as the source, and the location where the copies are placed is called the destination.
Recall that the tilde ~ character represents your home directory; it is commonly used as a source or destination. To
view the current contents of the home directory, use the ls command:
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
To demonstrate how the cp command works, the /etc/services file can be copied to the home directory using
the following command:
sysadmin@localhost:~$ cp /etc/services ~
The result of executing the previous command would create a copy of the contents of the /etc/services file in
your home directory. The new file in your home directory would also be named services. The success of the cp
command can be verified using the ls command:
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates newfile
Documents Music Public Videos services
To copy a file and rename the file in one step you can execute a command like:
The previous command would copy the contents of the /etc/services file into a new file named ports in your
home directory:
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates newfile services
Documents Music Public Videos ports
To copy multiple files into a directory, additional file names to copy can be included as long as the last argument is
a destination directory:
Although they may only represent one argument, a wildcard pattern can be expanded to match multiple path
names. The following command copies all files in the Documents directory that start with an n to the home
directory; as a result, newhome.txt and numbers.txt are copied.
sysadmin@localhost:~$ cp Documents/n* ~
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates newfile numbers.txt services
Documents Music Public Videos newhome.txt ports
An issue that frequently comes up when using wildcard patterns with the cp command is that sometimes they
match the names of directories as well as regular files. If an attempt is made to copy a directory, the cp command
will print an error message.
Using the -v option makes the cp command print verbose output, so you can always tell what copies succeed as
well as those that fail. In the following wildcard example, we've marked in red the lines which indicate that the
directories were matched, but not copied.
sysadmin@localhost:~$ cp -v /etc/b* ~
'/etc/bash.bashrc' -> '/home/sysadmin/bash.bashrc'
cp: -r not specified; omitting directory '/etc/bind'
'/etc/bindresvport.blacklist' -> '/home/sysadmin/bindresvport.blacklist'
cp: -r not specified; omitting directory '/etc/binfmt.d'
In order to copy a directory, its contents must be copied as well, including all files within the directories and all of
its subdirectories. This can be done by using the -r or -R recursive options.
sysadmin@localhost:~$ cp -R -v /etc/perl ~
'/etc/perl' -> '/home/sysadmin/perl'
'/etc/perl/CPAN' -> '/home/sysadmin/perl/CPAN'
'/etc/perl/Net' -> '/home/sysadmin/perl/Net'
'/etc/perl/Net/libnet.cfg' -> '/home/sysadmin/perl/Net/libnet.cfg'
Copying a file using the cp command requires the execute permission to access the directory where the file is
located and the read permission for the file you are trying to copy.
You will also need to have the write and execute permissions on the directory where you want to put the copied
file.
Consider This
The archive -a option of the cp command copies the contents of the file and also attempts to maintain the original
timestamps and file ownership. For regular users, only original timestamps can be maintained, since regular users
can't create files owned by other users. If the root user uses the -a option, then the cp command will create a new
file owned by the original file owner and also use the original file's timestamps.
The -a option also implies that recursion will be done. Therefore, it can also be used to copy a directory.
If you move a file from one directory to another, and you don't specify a new name for the file, then it will retain
its original name. For example, the following will move the ~/Documents/red.txt file into the home directory
and the resulting file name will be red.txt:
sysadmin@localhost:~$ cd Documents
sysadmin@localhost:~/Documents$ mv red.txt ~
sysadmin@localhost:~/Documents$ ls ~
Desktop Downloads Pictures Templates red.txt
Documents Music Public Videos
Like the cp command, the mv command will allow you to specify multiple files to move, as long as the final
argument provided to the command is a directory.
For example:
There is no need for a recursive option with the mv command. When a directory is moved, everything it contains is
automatically moved as well.
Moving a file within the same directory is an effective way to rename it. For example, in the following command,
the people.csv file is moved from the current directory to the current directory and given a new name of
founders.csv. In other words, people.csv is renamed founders.csv:
Use the mv command to move a directory from one location to another. Just like moving files, the original directory
will be deleted from its previous location. In the following example, the mv command is used to move the
Engineering directory from the School directory to the Work directory:
sysadmin@localhost:~/Documents$ cd
sysadmin@localhost:~$ ls Documents/School
Art Engineering Math
sysadmin@localhost:~$ mv Documents/School/Engineering Documents/Work
sysadmin@localhost:~$ ls Documents/Work
Engineering
Moving a file using the mv command requires the write and execute permissions on both the origin and destination
directories.
rm [OPTION]... [FILE]...
Warning
It is important to keep in mind that deleted files and directories do not go into a "trash can" as with desktop-
oriented operating systems. When a file is deleted with the rm command, it is permanently gone.
Without any options, the rm command is typically used to remove regular files:
sysadmin@localhost:~$ cd Documents
sysadmin@localhost:~/Documents$ rm alpha.txt
sysadmin@localhost:~/Documents$ ls alpha.txt
ls: cannot access alpha.txt: No such file or directory
Extra care should be applied when using wildcards to specify which files to remove, as the extent to which the
pattern might match files may be beyond what was anticipated. To avoid accidentally deleting files when using
globbing characters, use the -i option. This option makes the rm command confirm interactively every file that you
delete:
sysadmin@localhost:~/Documents$ rm -i a*
rm: remove regular file 'adjectives.txt'? y
rm: remove regular file 'alpha-first.txt'? y
rm: remove regular file 'alpha-first.txt.original'? y
rm: remove regular file 'alpha-second.txt'? y
rm: remove regular file 'alpha-third.txt'? y
rm: remove regular file 'animals.txt'? Y
sysadmin@localhost:~/Documents$ cd
sysadmin@localhost:~$
Some distributions make the -i option a default option by making an alias for the rm command.
Deleting a file with the rm command requires the write and execute permissions on its parent directory. Regular
users typically only have this type of permission in their home directory and its subdirectories.
Consider This
The /tmp and /var/tmp directories do have the special permission called sticky bit set on them so that files in these
directories can only be deleted by the user that owns them (with the exception of the root user who can delete any
file in any directory). So, this means if you copy a file to the /tmp directory, then other users of the system will not
be able to delete your file.
Typically, you have a handful of directories in your home directory by default. Exactly what directories you have
will vary due to the distribution of Linux, what software has been installed on your system, and actions that may
have been taken by the administrator.
For example, upon successful graphical login on a default installation of Ubuntu, the following directories have
already been created automatically in the user's home directory:
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
The bin directory is a common directory for users to create in their home directory. It is a useful directory to place
scripts that the user has created. In the following example, the user creates a bin directory within their home
directory:
The mkdir command can accept a list of space-separated path names for new directories to create:
Directories are often described in terms of their relationship to each other. If one directory contains another
directory, then it is referred to as the parent directory. The subdirectory is referred to as a child directory. In the
following example, /home is the parent directory and /sysadmin is the child directory:
sysadmin@localhost:~$ pwd
/home/sysadmin
When creating a child directory, its parent directory must first exist. If an administrator attempted to create a
directory named blue inside of the non-existent /home/sysadmin/red directory, there would be an error:
By adding the -p option, the mkdir command automatically creates the parent directories for any child directories
about to be created. This is especially useful for making deep path names:
red/blue:
yellow
red/blue/yellow:
green
red/blue/yellow/green:
To create a directory with the mkdir command, you must have the write and execute permission on the parent of
the proposed directory. For example, to create the /etc/test directory requires the write and execute permissions
in the /etc directory.
Using the -p option with the rmdir command will remove directory paths, but only if all of the directories contain
other empty directories.
Otherwise, if a directory contains anything except other directories, you'll need to use the rm command with a
recursive option. The rm command alone will ignore directories that it's asked to remove; to delete a directory, use
either the -r or -R recursive options. Just be careful as this will delete all files and all subdirectories:
To delete a directory with the rmdir command, you must have the write and execute permission on the parent
directory. For example, to remove the /etc/test directory requires the write and execute permissions in the /etc
directory.
cp
Command used to copy files and directories. cp will copy a SOURCE to a DEST, or multiple SOURCES to
a DIRECTORY.
Section 5.5
file
Command used to determine the type of file. file tests each argument in an attempt to classify it. There are
three sets of tests, preformed in this order: filesystem test, magic tests, and language tests.
Section 5.3
ls
Command that will list information about files. The current directory is listed by default.
| Section 5.2
mkdir
Command used to create directories, if they do not already exist.
Section 5.8
rm
Command used to remove files or directories. By default the rm command will not remove directories.
Section 5.7
rmdir
Command that is used to remove empty directories in the filesystem.
Section 5.9
touch
Command used to change the file timestamps. touch will allow a user to update the access and modification
times of each FILE to the current time.
Section 5.4
Lab 5: File Manipulation
5.0 Introduction
In this task, you will learn the commands required for managing files and directories. For Linux, everything is
considered a file, including regular files, directories, hardware devices, and sockets. The file management
commands such as ls, cp, mv, and rm are used very frequently. Similarly, to work with directories, the mkdir and
rmdir commands are used.
5.1 Step 1
The ls command is used to list files. The ls command will accept an arbitrary number of different path names to
attempt to list as arguments:
ls [OPTION]... [FILE]...
Used alone without any options, the ls command will list the files in the current directory. To list all the files in
your current working directory, execute the following command:
ls
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
5.2 Step 2
To view all the files in the current working directory, including hidden files, execute the following command:
ls -a
sysadmin@localhost:~$ ls -a
. .bashrc .selected_editor Documents Pictures Videos
.. .cache .sudo_as_admin_successful Downloads Public
.bash_logout .profile Desktop Music Templates
In the output above, the files with a period . character at the beginning, such as .bashrc and .bash_logout, are
hidden files.
5.3 Step 3
To learn the details about a file, such as the type of file, permissions, ownership, or the timestamp, perform a long
listing, using the -l option to the ls command. To display a detailed listing of the files in the /usr/lib/openssh
directory, execute the following command:
ls –l /usr/lib/openssh
sysadmin@localhost:~$ ls -l /usr/lib/openssh
total 888
-rwxr-xr-x 1 root root 700 Jan 25 2018 agent-launch
-rwxr-xr-x 1 root root 105608 Mar 4 12:17 sftp-server
-rwsr-xr-x 1 root root 436552 Mar 4 12:17 ssh-keysign
-rwxr-xr-x 1 root root 354400 Mar 4 12:17 ssh-pkcs11-helper
-rwxr-xr-x 1 root root 239 Jan 16 2018 ssh-session-cleanup
The first field is used to indicate the file type along with its permissions.
The second field contains the number of files that are hard linked.
The third and fourth fields indicate the user owner and group owner of the file.
The sixth field indicates the timestamp when the file was last modified.
The output of the ls –l command is sorted alphabetically by the file name. There are other options for sorting
such as -S to sort files by size and -t to sort by modification timestamp
5.4 Step 4
To display a listing of all files in the /etc/ssh directory, sorted by file size, execute the ls command with the -l
and -S options:
ls –lS /etc/ssh
sysadmin@localhost:~$ ls -lS /etc/ssh
total 580
-rw-r--r-- 1 root root 553122 Mar 4 12:17 moduli
-rw-r--r-- 1 root root 3264 Mar 4 12:17 sshd_config
-rw------- 1 root root 1675 Mar 29 17:36 ssh_host_rsa_key
-rw-r--r-- 1 root root 1580 Mar 4 12:17 ssh_config
-rw------- 1 root root 411 Mar 29 17:36 ssh_host_ed25519_key
-rw-r--r-- 1 root root 399 Mar 29 17:36 ssh_host_rsa_key.pub
-rw-r--r-- 1 root root 338 Mar 29 17:36 ssh_import_id
-rw------- 1 root root 227 Mar 29 17:36 ssh_host_ecdsa_key
-rw-r--r-- 1 root root 179 Mar 29 17:36 ssh_host_ecdsa_key.pub
-rw-r--r-- 1 root root 99 Mar 29 17:36 ssh_host_ed25519_k
5.5 Step 5
To reverse the results of any listing option, use the -r option to the ls command. To reverse the sorting based on
file size for the /etc/ssh directory, execute the following command:
ls –lSr /etc/ssh
sysadmin@localhost:~$ ls -lSr /etc/ssh
total 580
-rw-r--r-- 1 root root 99 Mar 29 17:36 ssh_host_ed25519_key.pub
-rw-r--r-- 1 root root 179 Mar 29 17:36 ssh_host_ecdsa_key.pub
-rw------- 1 root root 227 Mar 29 17:36 ssh_host_ecdsa_key
-rw-r--r-- 1 root root 338 Mar 29 17:36 ssh_import_id
-rw-r--r-- 1 root root 399 Mar 29 17:36 ssh_host_rsa_key.pub
-rw------- 1 root root 411 Mar 29 17:36 ssh_host_ed25519_key
-rw-r--r-- 1 root root 1580 Mar 4 12:17 ssh_config
-rw------- 1 root root 1675 Mar 29 17:36 ssh_host_rsa_key
-rw-r--r-- 1 root root 3264 Mar 4 12:17 sshd_config
-rw-r--r-- 1 root root 553122 Mar 4 12:17 moduli
5.6 Step 6
The -R option is used with the ls command for a recursive listing of directories. It will display the listing of the
specified directories, as well as all other subdirectories within that directory. To display the recursive listing of all
files and directories in the /usr/lib/systemd directory, execute the following command:
ls –R /usr/lib/systemd
sysadmin@localhost:~$ ls -R /usr/lib/systemd
/usr/lib/systemd:
boot tests user-environment-generators user-preset
catalog user user-generators
/usr/lib/systemd/boot:
efi
/usr/lib/systemd/boot/efi:
linuxx64.efi.stub systemd-bootx64.efi
/usr/lib/systemd/catalog:
systemd.be.catalog systemd.de.catalog systemd.pt_BR.catalog
[email protected] systemd.fr.catalog systemd.ru.catalog
systemd.bg.catalog systemd.it.catalog systemd.zh_CN.catalog
systemd.catalog systemd.pl.catalog systemd.zh_TW.catalog
/usr/lib/systemd/tests:
testdata
...
Some output has been omitted in the example above.
5.7 Step 7
To display only the directory, not the contents of the directory, use the ls command with the -d option. To list only
the /usr/lib directory, execute the following command:
ls –d /usr/lib
sysadmin@localhost:~$ ls -d /usr/lib
/usr/lib
5.8 Step 8
The wildcard characters, such as the ones used for file globbing, can be used with the ls command. For example,
execute the following command to list all the files and directories in the /etc directory starting with the letter l:
ls /etc/l*
sysadmin@localhost:~$ ls /etc/l*
/etc/ld.so.cache /etc/libaudit.conf /etc/localtime /etc/lsb-release
/etc/ld.so.conf /etc/locale.alias /etc/login.defs /etc/ltrace.conf
/etc/legal /etc/locale.gen /etc/logrotate.conf
/etc/ld.so.conf.d:
libc.conf x86_64-linux-gnu.conf
/etc/logcheck:
ignore.d.server
/etc/logrotate.d:
alternatives apt dpkg rsyslog ufw
In the output of the command above, the l* pattern matched all files that start with the letter l. Notice the
highlighted output above shows that not only was the ld.so.conf.d directory listed, but so were the contents of
this directory.
5.9 Step 9
To avoid listing the contents of directories that match (as shown in the previous step), execute the ls command
with the -d option:
ls -d /etc/l*
sysadmin@localhost:~$ ls -d /etc/l*
/etc/ld.so.cache /etc/libaudit.conf /etc/logcheck /etc/lsb-release
/etc/ld.so.conf /etc/locale.alias /etc/login.defs /etc/ltrace.conf
/etc/ld.so.conf.d /etc/locale.gen /etc/logrotate.conf
/etc/legal /etc/localtime /etc/logrotate.d
5.10 Step 10
The file command can be used to check the contents of a file to report what kind of file it is. To verify the file
type of the /etc/hosts file, execute the following command:
file /etc/hosts
sysadmin@localhost:~$ file /etc/hosts
/etc/hosts: ASCII text
Consider This
Many commands expect input files to be of a specific format, such as text. The file command is used to verify the
file type prior to running these other commands.
5.11 Step 11
To verify the file type of the /usr/bin/sudo file, execute the following command:
file /usr/bin/sudo
sysadmin@localhost:~$ file /usr/bin/sudo
/usr/bin/sudo: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically
linked, interpreter /lib64/l, for GNU/Linux 3.2.0,
BuildID[sha1]=103d78e3fba072f46aa467b5cf28b0649c08c9d3, stripped
The output of the previous command indicates that /usr/bin/sudo is an Executable Link Format (ELF), not a text
file.
5.12 Step 12
The touch command is used to create an empty file as well as update the timestamp of an existing file.
If the file specified as a parameter does not exist, then the touch command creates a new file. To create a new file
called testfile and verify its timestamp, execute the following commands:
touch testfile
ls –l testfile
sysadmin@localhost:~$ touch testfile
sysadmin@localhost:~$ ls -l testfile
-rw-rw-r-- 1 sysadmin sysadmin 0 May 17 14:43 testfile
5.13 Step 13
If an existing file is specified, then the touch command will update the file's timestamp with the current
timestamp. Execute the touch command and then the ls command on the newly created testfile file to update
and verify the timestamp:
touch testfile
ls –l testfile
sysadmin@localhost:~$ touch testfile
sysadmin@localhost:~$ ls -l testfile
-rw-rw-r-- 1 sysadmin sysadmin 0 May 17 14:45 testfile
5.14 Step 14
To set the timestamp to a value other than the current time, use the -t option to the touch command. For example,
execute the following commands:
5.15 Step 15
The touch command modifies the modification timestamp by default. There is also an access timestamp and a
change timestamp.
The stat command provides detailed information about the different timestamps associated with a file, such as the
last time the file's contents were modified, the last time the file was accessed and the last time the file attributes
were modified.
To view the different timestamps associated with the testfile file, execute the following command:
stat testfile
sysadmin@localhost:~$ stat testfile
File: testfile
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 158336372 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/sysadmin) Gid: ( 1001/sysadmin
Access: 2019-02-15 14:35:00.000000000 +0000
Modify: 2019-02-15 14:35:00.000000000 +0000
Change: 2019-05-17 14:47:37.666082226 +0000
Birth: -
5.16 Step 16
The cp command is used to copy files. It takes at least two arguments: the first argument is the path to the file to be
copied and the second argument is the path to where the copy will be placed:
To copy the /etc/hosts file to your home directory, execute the following command:
cp /etc/hosts ~
ls –l hosts
sysadmin@localhost:~$ cp /etc/hosts ~
sysadmin@localhost:~$ ls -l hosts
-rw-r--r-- 1 sysadmin sysadmin 172 May 17 14:49 hosts
5.17 Step 17
To copy the /etc/hosts file to your home directory and rename it as myhosts, execute the following command:
cp /etc/hosts ~/myhosts
ls –l *hosts
sysadmin@localhost:~$ cp /etc/hosts ~/myhosts
sysadmin@localhost:~$ ls -l *hosts
-rw-r--r-- 1 sysadmin sysadmin 172 May 17 14:49 hosts
-rw-r--r-- 1 sysadmin sysadmin 172 May 17 14:50 myhosts
5.18 Step 18
To view and copy the contents of the /usr/share/misc directory to the current user's home directory, execute the
following commands:
ls -ld /usr/share/misc/* ~
cp /usr/share/misc/* ~
sysadmin@localhost:~$ ls -ld /usr/share/misc/* ~
drwxr-xr-x 1 sysadmin sysadmin 4096 May 17 14:50 /home/sysadmin
lrwxrwxrwx 1 root root 13 Mar 13 16:43 /usr/share/misc/magic -> ../
file/magic
lrwxrwxrwx 1 root root 24 Mar 13 16:43 /usr/share/misc/magic.mgc ->
../../lib/file/magic.mgc
-rw-r--r-- 1 root root 1126913 Feb 10 18:25 /usr/share/misc/pci.ids
lrwxrwxrwx 1 root root 25 Apr 21 2017 /usr/share/misc/usb.ids -> /
var/lib/usbutils/usb.ids
sysadmin@localhost:~$ cp /usr/share/misc/* ~
cp: -r not specified; omitting directory '/usr/share/misc/magic'
5.19 Step 19
The cp command skips subdirectories within the source directory and displays the error omitting directory, as
shown in the output of the previous command. To include the contents of subdirectories within the source
directory, the recursive -r option must be specified. To copy the contents of the /usr/share/misc directory,
including subdirectories, to your home directory, execute the following commands:
ls
cp –r /usr/share/misc/* ~
ls
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates hosts myhosts testfile
Documents Music Public Videos magic.mgc pci.ids usb.ids
sysadmin@localhost:~$ cp -r /usr/share/misc/* ~
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates hosts magic.mgc pci.ids usb.ids
Documents Music Public Videos magic myhosts
5.20 Step 20
To copy the contents of a directory to your home directory while maintaining the original timestamps, the -a
option must be specified with the cp command. To view the original timestamp of the magic file contained in the
subdirectory /usr/share/misc, execute the following command:
ls -l /usr/share/misc/magic
sysadmin@localhost:~$ ls -l /usr/share/misc/magic
lrwxrwxrwx 1 root root 13 Mar 13 16:43 /usr/share/misc/magic -> ../file/magic
To copy the /usr/share/misc directory maintaining the original timestamps, execute the following command:
cp –a /usr/share/misc/* ~
sysadmin@localhost:~$ cp -a /usr/share/misc/* ~
To verify that the timestamp of the magic file copied into your home directory matches the original, execute the
following command:
ls -l magic
sysadmin@localhost:~$ ls -l magic
lrwxrwxrwx 1 sysadmin sysadmin 13 Mar 13 16:43 magic -> ../file/magic
Note
The -a option is especially effective when used by the root user because it maintains the original owner of the file
in the copied version. When a regular user uses the -a option, then the timestamp is preserved, but the new file is
owned by the user who copied the file.
5.21 Step 21
The mv command is used to move a file from one path name in the filesystem to another.
To move the testfile file from the current directory to the ~/Public directory, execute the following command:
mv testfile ~/Public
ls -ltr ~/Public
sysadmin@localhost:~$ mv testfile ~/Public
sysadmin@localhost:~$ ls -ltr ~/Public
total 0
-rw-rw-r-- 1 sysadmin sysadmin 0 Feb 15 14:35 testfile
5.22 Step 22
To move the ~/Public/testfile file to the current directory and rename it as mytestfile, execute the following
command:
mv ~/Public/testfile ~/mytestfile
ls -lt
sysadmin@localhost:~$ mv ~/Public/testfile ~/mytestfile
sysadmin@localhost:~$ ls -ltr
total 1144
lrwxrwxrwx 1 sysadmin sysadmin 25 Apr 21 2017 usb.ids -> /var/lib/usbutils
/usb.ids
-rw-r--r-- 1 sysadmin sysadmin 1126913 Feb 10 18:25 pci.ids
-rw-rw-r-- 1 sysadmin sysadmin 0 Feb 15 14:35 mytestfile
lrwxrwxrwx 1 sysadmin sysadmin 24 Mar 13 16:43 magic.mgc -> ../../lib/file/
magic.mgc
lrwxrwxrwx 1 sysadmin sysadmin 13 Mar 13 16:43 magic -> ../file/magic
drwxr-xr-x 2 sysadmin sysadmin 4096 Apr 24 16:24 Videos
drwxr-xr-x 2 sysadmin sysadmin 4096 Apr 24 16:24 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Apr 24 16:24 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Apr 24 16:24 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Apr 24 16:24 Downloads
drwxr-xr-x 4 sysadmin sysadmin 4096 Apr 24 16:24 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Apr 24 16:24 Desktop
-rw-r--r-- 1 sysadmin sysadmin 172 May 17 14:49 hosts
-rw-r--r-- 1 sysadmin sysadmin 172 May 17 14:50 myhosts
drwxr-xr-x 1 sysadmin sysadmin 4096 May 17 15:13 Public
5.23 Step 23
The rm command is used to remove files and directories.
rm [OPTION]... [FILE]...
To delete the file mytestfile from the current directory and verify the deletion, execute the following commands:
rm mytestfile
ls –l mytestfile
sysadmin@localhost:~$ rm mytestfile
sysadmin@localhost:~$ ls -l mytestfile
ls: cannot access 'mytestfile': No such file or directory
Important
The rm command is used to remove files and directories permanently. Linux does not have the concept of a "soft
delete" where you can retrieve the contents again from a "trash can", so you should be careful when deleting files.
By default, rm will only delete files; use the -r or -R options in order to delete directories also.
5.24 Step 24
The mkdir command allows you to create (make) a directory.
To create a directory named testdir in the current directory and verify the new directory exists, execute the
following commands:
mkdir testdir
ls –d testdir/
sysadmin@localhost:~$ mkdir testdir
sysadmin@localhost:~$ ls -d testdir/
testdir/
5.25 Step 25
To create multiple directories in the current directory and verify they are created, execute the following commands:
Note
Recall that the question mark ? character in a string will match exactly one character.
5.26 Step 26
To create a nested structure of directories, the -p option is used with the mkdir command. To create the inner
directory which is inside the outer directory, execute the following commands:
mkdir –p outer/inner
ls –R outer
sysadmin@localhost:~$ mkdir -p outer/inner
sysadmin@localhost:~$ ls -R outer
outer:
inner
outer/inner:
5.27 Step 27
The rmdir command is used to remove empty directories:
rmdir testdir
ls –d testdir
sysadmin@localhost:~$ rmdir testdir
sysadmin@localhost:~$ ls -d testdir
ls: cannot access testdir: No such file or directory
5.28 Step 28
To remove the outer directory along with the inner subdirectory, execute the following command:
rmdir –p outer/inner/
ls –R outer/
sysadmin@localhost:~$ rmdir -p outer/inner/
To verify the outer directory and the inner subdirectory no longer exist, use the following command:
ls –R outer/
sysadmin@localhost:~$ ls -R outer/
ls: cannot access outer/: No such file or directory
5.29 Step 29
The rmdir command is used to remove empty directories. If a directory contains any files, it can be deleted using
the rm –r command.
Create a directory called newtestdir and a file within the directory called newtestfile using the following
commands:
mkdir newtestdir
touch newtestdir/newtestfile
sysadmin@localhost:~$ mkdir newtestdir
sysadmin@localhost:~$ touch newtestdir/newtestfile
Attempt to remove the newtestdir directory by executing the rmdir and rm commands without options:
rmdir newtestdir
rm newtestdir
sysadmin@localhost:~$ rmdir newtestdir
rmdir: failed to remove 'newtestdir': Directory not empty
sysadmin@localhost:~$ rm newtestdir
rm: cannot remove 'newtestdir': Is a directory
rm -r newtestdir
sysadmin@localhost:~$ rm -r newtestdir
sysadmin@localhost:~$ ls -ld newtestdir
ls: cannot access 'newtestdir': No such file or directory
Chapter 6: Finding Files
6.1 Introduction
In Linux, everything is considered a file. Files are used to store data such as text, graphics, and programs.
Directories are a type of file used to store other files. This chapter will cover how to find files, which requires
knowledge about the Linux directory structure, typically called a filesystem and the filesystem standards supported
by the Linux Foundation; the Filesystem Hierarchy Standard (FHS). You will also learn about how to find files
and commands within the Linux filesystem from the command line.
Consider This
An illustration containing many more of the distributions and their origins can be found at
http://futurist.se/gldt/wp-content/uploads/12.02/gldt1202.svg.
Since there are so many Linux distributions, it would be expected that numerous people would change the names
of the files and folders, eventually making the distributions incompatible. This makes a basic agreement necessary
concerning the naming and location of important system files and directories.
The Filesystem Hierarchy Standard (FHS) is an agreement to standardize the names and locations of directories
and their content for use within most Linux filesystems. It helps to know what directories to expect to find, and
what files one should expect to find in them. More importantly, it allows programmers to write programs that will
be able to work across a wide variety of systems that conform to this standard.
During the development of the first series of this standard from 1994 to 1995, it was known as the Filesystem
Standard (FSSTND). When the second series was started in 1997, it was renamed to the Filesystem Hierarchy
Standard (FHS). The final 2.3 version of this second series of this FHS standard was published in 2004 at
http://refspecs.linuxfoundation.org/fhs.shtml. In 2011, a draft version of the third series of this standard was
published at http://www.linuxbase.org/betaspecs/fhs/fhs.html.
The Linux file structure is best visualized as an upside-down tree, with directories and files branching out from the
top-level root / directory. While the actual standard details many more directories than listed below, the image and
table highlight some of the most important ones to know.
Directory Purpose
/ The root of the primary filesystem hierarchy.
/bin Contains essential user binary executables.
/boot Contains the kernel and bootloader files.
/dev Populated with files representing attached devices.
/etc Configuration files specific to the host.
/home Common location for user home directories.
/lib Essential libraries to support /bin and /sbin executables.
/mnt Essential libraries to support /bin and /sbin executables.
/opt Optional third-party add-on software.
Directory Purpose
/root Home directory for the root user.
/sbin Contains system or administrative binary executables.
srv May contain data provided by system services.
/tmp Location for creating temporary files.
/usr The root of the secondary filesystem hierarchy.
/usr/bin Non-vital system or administrative executables.
/usr/include Header files for compiling C-based software.
/usr/lib Shared libraries to support /usr/bin and /usr/sbin.
/usr/local The root of the third filesystem hierarchy for local software.
/usr/sbin Non-vital system or administrative executables.
/usr/share Location for architecturally-independent data files.
/usr/share/dict Word lists.
/usr/share/doc Documentation for software packages.
/usr/share/info Information pages for software packages.
/usr/share/locale Locale information.
/usr/share/man Location for man pages.
/usr/share/nls Native language support files.
Vendors of Linux distributions have continued to make some changes, even though a new version of the standard
has not been published in over ten years. Two notable new additions include the /run directory and the /sys
directory. The /run directory is being considered for use in the forthcoming FHS versions to contain volatile data
that changes at runtime. Previously, this data was supposed to be stored under the /var/run directory, but due to
the unavailability of this directory at boot time, this data can become scattered in other places, such as hidden files
in the /dev directory.
The /sys directory in some traditional UNIX systems was used to hold files related to the kernel. In modern Linux
systems, the /sys directory is used to mount the sysfs pseudo-filesystem. This filesystem is used to export
information about kernel objects and their relationships to each other. The kernel objects are represented by
directories, and the files that they contain are named for the attributes of those objects. The contents of the files
represent the value for that attribute. Symbolic links are used to represent relationships between objects.
Another notable change that some Linux distributions are making is the conversion of the /bin, /sbin and /lib
directories into symbolic links which point to /usr/bin, /usr/sbin and /usr/lib, respectively. All user
executables are now in the /usr/bin directory, administrator executables are now in the /usr/sbin directory, and
the libraries to support all these executables are now in the /usr/lib directory.
Note
A symbolic link, also called a soft link, is simply a file that points to another file.
The merger of the /bin, /sbin and /lib directories into the /usr/bin, /usr/sbin and /usr/lib directories has
been somewhat controversial. Many administrators are comfortable with the long-standing subdivisions of these
files into different directories.
Because the way that UNIX booted, the /bin, /sbin and /lib directories had to be part of the root filesystem as
they contain critical boot executables. Some developers now argue that the reason for having them split is no
longer valid. In early versions of UNIX, the developers had two filesystems of about 1.5 MiB each on two separate
disks for the root filesystem and the /usr filesystem. As the root filesystem started to become full, the developers
decided to move some of the executable files that were in the /bin and /sbin directories that were not essential to
booting the system into the corresponding directories /usr/bin and /usr/sbin (in the separate /usr filesystem).
The FHS standard categorizes each system directory in a couple of ways for security purposes:
Shareable / Unshareable
Shareable files can be stored on one host and used on others. For instance, /var/www is often used as the
root directory of a web server, which shares files with other hosts. Another example is the user home
directories.
Unshareable files should not be shared between hosts. These include process states in the /var/run
directory and the /boot directory.
Variable / Static
Static files generally do not change, including library files and documentation pages. An example is the info
pages located at /usr/share/info.
Variable files normally change during the execution of programs. The /var/run directory contains files
that are both variable and unshareable.
The table below summarizes the main distinctions between file types:
Shareable Unshareable
/usr /etc
Static
/opt /boot
/var/mail /var/run
Variable
/var/spool/news /var/lock
In its simplest form, the locate command accepts a search string as an argument. For example, to find a file
named passwd, use the following command:
The locate command will only return results of files that the current user would normally have access to.
The locate command will display all files that have the search term anywhere in the file name. For
example, the search term passwd would match both /etc/opasswd and /etc/thishaspasswdinit.
Like most things in Linux, the locate command is case sensitive. For example, the search term passwd
would not match a file named /etc/PASSWD. To have the locate command not be case sensitive, use the -
i option.
Note
The locate command accepts the -r option to use regular expressions in the search pattern which provides a more
powerful way to search for files.
The locate command is dependent on a database. This database can be updated manually by an administrator
using the updatedb command, though typically this command is run automatically every day through cron, a
system scheduling service that runs commands on a particular recurring schedule.
When executed, the updatedb command creates a database of all files that it finds on the computer for quick
searching. This command can only be executed by a user with administrative access, which can be achieved using
the sudo command:
Note
The sudo command allows users to execute commands using the privileges of another user. The sudo command is
the preferred way to run commands with escalated privileges, and the root user is assumed by default.
The sudo command circumvents the need to login as root; this is bad practice because all commands are executed
with root privileges. This is seldom necessary and increases the risk that potentially dangerous commands may be
executed as root, even though root privileges were not required or desired.
Accordingly, some Linux distributions now prevent root logins entirely and require sudo to escalate privileges.
The system administrator must specifically authorize each user that can use the sudo command and must specify
which user(s) they can impersonate. For Linux system administrators, sudo plays a vital role is among the most
frequently used commands.
The updatedb command can be told not to search a particular name, path, or filesystem by changing the
corresponding line in its configuration file, /etc/updatedb.conf. Below is an example of the default file in its
entirety:
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs
coda devpts ftpfs devfs devtmpfs fuse.mfs shfs sysfs cifs lustre tmpfs usbfs ud
f fuse.glusterfs fuse.sshfs curlftpfs ceph fuse.ceph fuse.rozofs ecryptfs fusesm
b"
The /etc/updatedb.conf file can be edited as the root user. Any name, path, or filesystem that is listed in the file
on the appropriate line will not be added to the database. Any line starting with the # symbol will be ignored since
it is commented out.
Since the locate command works with a database, it is able to work very quickly even on a system with hundreds
of thousands of files. However, if you want to use the locate command to search for a file that was created very
recently, it will fail to find the file if the database hasn't been updated since the file creation.
Likewise, the database may contain outdated information about files that might have existed in the very recent past,
so the command will report them incorrectly as still existing.
The following example demonstrates the consequences that arise when the database is not updated in real time:
A new file called lostfile isn't initially found by the locate command.
sysadmin@localhost:~$ touch lostfile
sysadmin@localhost:~$ locate lostfile
After the database is updated by the updatedb command, the locate command can find the lostfile file.
sysadmin@localhost:~$ sudo updatedb
[sudo] password for sysadmin:
sysadmin@localhost:~$ locate lostfile
/home/sysadmin/lostfile
After the lostfile file has been deleted, the locate command will still report that the file exists.
sysadmin@localhost:~$ rm lostfile
sysadmin@localhost:~$ locate lostfile
/home/sysadmin/lostfile
6.3.2 find Command
If you want to search for files that are currently in the filesystem, then you should use the find command. The
find command is slower than the locate command because it searches directories in real time; however, it doesn't
suffer from problems associated with an outdated database.
The find command expects a directory as the first argument. This will be the starting point of the search. The find
command will search this directory and all of its subdirectories. If no directory is specified, then the find
command will start the search at the current directory.
Note that the period . character refers to the current directory in the following example, but the current directory
could also be referred to using the ./ notation. The find command uses the -name option to search for files by
name, in this case, the Downloads directory.
The first search yielded no results because the string must match the exact name of the file, not just part of the
name. The third command demonstrates that globbing can be used, and the fourth command demonstrates multiple
matches (notice that single quotes were added so that the find command will interpret the glob, rather than the
shell).
If the search for the Downloads directory was conducted from the root / directory, many errors would arise:
These errors can be ignored for now, but just be aware that errors like these are typical when either a regular user is
attempting to search root-only areas, or when the root user is attempting to search areas that are dedicated to the
system’s processes.
The find command also offers many options for searching files, unlike the locate command which searches only
for files based on file name. The following table illustrates some of the more commonly used criteria:
Example Meaning
-iname LOSTFILE Case insensitive search by name.
-mtime -3 Files modified less than three days ago.
-mmin -10 Files modified less than ten minutes ago.
Example Meaning
-size +1M Files larger than one megabyte.
-user joe Files owned by the user joe.
-nouser Files not owned by any user.
-empty Files that are empty.
-type d Files that are directory files.
-maxdepth 1 Do not use recursion to enter subdirectories; only search the primary directory.
If multiple criteria are specified, then all criteria must match as the find command automatically assumes a logical
AND condition between criteria, meaning all of the conditions must be met. This could be explicitly stated by using
the -a option between criteria. For example, the Downloads directory must also be owned by the sysadmin user in
order for the find command to produce a result in the following example:
Logical OR conditions can be specified between criteria with the -o option, meaning at least one of the conditions
must be true. The following output lists files that are either named Downloads or owned by the sysadmin user.
Logical groupings of criteria can also be specified using the parentheses. Be careful to precede the parentheses with
a backslash or to use single quotes around them so that the shell doesn't attempt to interpret them as special
characters. Also, as mentioned previously, use quotes around any globs that the find command should interpret
instead of the shell, as shown in the following example:
In plain text, the find command in the preceding example will return files in the current directory, with the non-
case sensitive name starting with desk OR files with the exact name Downloads owned by the sysadmin user.
By default, the find command simply prints the names of the files that it finds. To generate output showing
additional file details, similar the ls -l command, you can specify the -ls option:
To make the output exactly like the output of the ls -l command, use the -exec option to execute ls -l on each
file found. For example:
sysadmin@localhost:~$ find -name 'Documents' -exec ls -l {} \;
total 1100
drwxr-xr-x 5 sysadmin sysadmin 4096 Mar 8 19:10 School
drwxr-xr-x 2 sysadmin sysadmin 4096 Mar 8 19:10 Work
-rw-r--r-- 1 sysadmin sysadmin 39 Mar 8 19:10 adjectives.txt
-rw-r--r-- 1 sysadmin sysadmin 90 Mar 8 19:10 alpha-first.txt
-rw-r--r-- 1 sysadmin sysadmin 106 Mar 8 19:10 alpha-second.txt
-rw-r--r-- 1 sysadmin sysadmin 195 Mar 8 19:10 alpha-third.txt
...
The previous example tells the find command to execute the ls -l command for each file found. The pair of curly
braces {} is a placeholder for the name of each file found; note that there is no space between them. The \; is an
escaped semicolon that is added between each command so that multiple commands may be executed in series.
In order to make the find command confirm the execution of the command for each file found, use the action
option -ok instead of the -exec option:
The command above tells the find command to execute the rm command for files that were modified less than two
minutes ago. Did you notice how the first file that the rm command tried to remove was the current directory
(represented by the period. character)? That is enough reason not to use the -exec option because the rm command
would have tried to remove the current directory.
There will be situations where it’s useful to find a file or set of files depending on their age, or when they were last
modified, such as all files that have been changed since a given time (such as the last backup). The modification
time -mtime option to the find command provides the ability to give time as criteria for a search.
First, a word about how the find command uses times is relevant here. When looking for files modified within the
period of a day, such as 3 days ago, you would use -mtime 3, but if you are looking for files that have changed
anytime between 3 days ago and now, you would use -mtime -3. If you are looking to find files older than 3 days,
you would use -mtime +3.
Option Function
-mtime N Files modified N*24 hours ago.
-mtime -N Files modified less than N*24 hours ago.
-mtime +N Files modified more than N*24 hours ago.
To summarize, a number without a + or - means exactly N days ago, a number with a - means anytime between N
days ago and NOW, and a number with a + means N days ago or more.
For example, if you backup every seven days, use the following command to backup all files that are 3 days or less
old:
The above command starts in the /tmp directory and finds all files that have been modified in the last three 24-hour
periods, or 72 hours.
The find command is less exact by default than you might want it to be. When using numbers of days with the -
atime, -ctime or -mtime operators, the days refer to 24-hour blocks. Therefore, you may think that you are
searching to find all files that occurred between now and your last backup by referring to that day, but depending
on the time of day, you may not have found all of the files.
Since the find command’s time operators are effectively tied to 24-hour blocks or days, you can use a reference
file to make certain you have found all files that occurred precisely since your last backup.
To find all files that are newer than your last backup, find out the date and time of that backup, and create a
reference file with that date/time so you can use it with the find -newer command. For example, if the backup
was done at precisely 2300 (11:00 PM) on the 1st of March, 2020, you can create a reference file with that exact
date and time with the following command:
Now that we have a file that mirrors precisely the date and time of our last backup, we can search for all files in the
/home directory that might have been created or modified since the last backup date with the following command:
Note
The output will vary, depending on what your system includes for users and what they have been doing, but
typically at least the current user’s .bash_history file will appear as having been changed.
The find command also allows the use of friendly notation to find files that are larger or smaller than a particular
size. Trying to find files that are above 1 Kilobyte on a busy system is like searching for the word “the” on Google;
so much output is returned it’s essentially useless. When searching for files of a given size, or those that are smaller
or larger than a given size, try to restrict the search to the smallest valid set of directories and files possible.
For example, to find all files that are smaller than 1 Kilobyte in the /etc directory, you would use:
Consider This
The -size option rounds up, to ensure that the find command will display the results you want.
For example, if you have three files, one less than 512 Kilobytes in size, one above 512 Kilobytes but less than
1024 Kilobytes in size, and one that is above 1024 Kilobytes in size, the -size option will not show anything for
the output of -size -1M, since it rounds down to 0.
The output of -size -512K would show only the file that is 512K or less, and if you run the -size +512K
command, the output would show both the file that is 512K or more and the file that is 1024K or more. You may
wish to experiment with this option so you know what to expect.
The find command can also help you find files by specifying a file type, such as regular file, directory, and more,
using the -type option. The table below shows all the different types of files you can find using the -type option:
To demonstrate, in order to search only for directories under the /home directory, you could use the following
command:
The command above will search the /home/sysadmin directory tree structure and filter out all results except for
files that are the specified directory type.
Consider This
To limit the search to just a single level below the /home directory, -maxdepth option to the find command can be
used. The -maxdepth option can be set to almost any numeric value. For example, a depth of 1 will show the top-
level directories in the /home/sysadmin directory:
The echo command can be used to determine which directories are in the PATH:
To find out where the grep command is located, use whereis without any options:
The output of the whereis command returns three directories. The first is where the grep command is located, in
the /bin/grep directory. There is also a path given for the man page of the grep command at
/usr/share/man/man1/grep.1.gz and another path for the info page at /usr/share/info/grep.info.gz.
Without an option, the whereis command provides all three pieces of information.
To view the location of the binary separate from the man page and info page, use the -b and -m options
respectively:
The -s option can be used to find source code that has been installed for a given command. In the case of grep,
only the binary is currently installed:
Notice that no results are returned because the source code for the grep command is not installed.
The -u option can be used to identify commands that do not have an entry for a requested attribute. To find out
which commands in the /bin directory do not have man pages, use the following command:
sysadmin@localhost:~$ cd /bin
sysadmin@localhost:/bin$ whereis -m -u *
dir: /usr/share/man/man1/dir.1.gz /usr/share/info/dir /usr/share/info/dir.old
grep: /usr/share/man/man1/grep.1.gz /usr/share/info/grep.info.gz
gzip: /usr/share/man/man1/gzip.1.gz /usr/share/info/gzip.info.gz
hostname: /usr/share/man/man7/hostname.7.gz /usr/share/man/man5/hostname.5.gz /u
sr/share/man/man1/hostname.1.gz
ip: /usr/share/man/man7/ip.7.gz /usr/share/man/man8/ip.8.gz
nano: /usr/share/man/man1/nano.1.gz /usr/share/info/nano.info.gz
sed: /usr/share/man/man1/sed.1.gz /usr/share/info/sed.info.gz
sysadmin@localhost:/bin$ cd
sysadmin@localhost:~$
In the previous example, the asterisk * character is used to consider every file in the current directory.
The example below identifies a specific command that does not have a man page:
This means that there is no man page for the type command:
If the same example is repeated for the ls command, there will be no output because ls has a man page.
sysadmin@localhost:~$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
To limit the search to a specific path or paths, capitalized options can be used. The -B option searches for binaries,
the -M option for manuals and documentation, and the -S option for sources. The -f option must be used to
indicate the end of the path list and the beginning of the search term.
This type of searching, however, may be best left to the find command, as it allows for more specific criteria to be
used in a search.
The -a option can be used with the which command to locate multiple executable files. This would be useful to
know if an executable script was inserted maliciously to override an existing command.
By using the which command, an administrator can be fairly certain that the only executable running by the name
of the ping command is located in the /bin directory.
The type command can also identify commands that are built into the Bash (or other) shell:
This output is significantly different than the output of the which command:
Using the -a option, the type command can also reveal the path of another command:
sysadmin@localhost:~$ type ll
ll is aliased to `ls -alF'
sysadmin@localhost:~$ type ls
ls is aliased to `ls --color=auto'
The output of these commands indicates that ll is an alias for ls -alF, and even ls is an alias for ls --
color=auto. Again, the output is significantly different than the which command:
sysadmin@localhost:~$ which ll
sysadmin@localhost:~$ which ls
/bin/ls
The type command supports other options, and can lookup multiple commands simultaneously. To display only a
single word describing the echo, ll, and which commands, use the -t option:
Sumber : https://content.netdevgroup.com/contents/lpic1-s1/1YpkgvdWxD/
Key Term
/etc/updatedb.conf
A configuration file for the updatedb utility. In some implementations, updatedb.conf is a shell script that
allows more flexibility in defining variables
Section 6.3.1
find
Command used to search for files in a directory hierarchy. find searches the directory tree rooted at each
given file name by evaluating the given the expression from left to right.
Section 6.3.2
locate
Command used to search for files by name. locate reads one or more databases prepared by the updatedb
utility and writes file names matching at least one of the PATTERNs to standard output.
Section 6.3.1
type
Command that indicates how a name would be interpreted if used as a command. When using the type
utility, the path to the command will be displayed.
Section 6.3.5
updatedb
The updatedb utility creates or updates a database to be used by the locate utility.
Section 6.3.1
whereis
Command that is used to locate source/binary and manuals sections for specified rules. This will locate
binary, source, and manual pages for a command.
Section 6.3.3
which
Command that returns the pathnames of the files, or links, which would be executed in the current
environment. It does this by searching the PATH for executables matching the names of the arguments.
Section 6.3.4
LAB 06
6.0 Introduction
In Linux, files are used to store data such as text, graphics, and programs; while directories are a type of file used
to store other files. In this lab, we will cover how to find files in the Linux directory structure, called a filesystem.
The locate and find commands are used to search for a file within a file system. While both commands perform
similar tasks, each does so by using a different technique, with its own distinctive advantages and disadvantages.
The locate command uses a database that is updated once a day using a cron job, while the find command
searches the live filesystem.
It is also useful to know where commands are located in the filesystem. The whereis command can be used to
search the PATH, the man page and source files for any given command. The which command can be used to find
out which executable file is used when executing a command. In addition, the type command can be used to
determine information about various commands.
6.1 Step 1
The locate command searches a database that contains the location of the files on the filesystem. The locate
command accepts a search string as an argument.
To locate all files which have the word hostname in the file name, execute the following command:
locate hostname
sysadmin@localhost:~$ locate hostname
/bin/hostname
/etc/hostname
/lib/systemd/systemd-hostnamed
/lib/systemd/system/dbus-org.freedesktop.hostname1.service
/lib/systemd/system/hostname.service
/lib/systemd/system/systemd-hostnamed.service
/usr/bin/hostnamectl
/usr/lib/gettext/hostname
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname/__init__.py
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname/__pycache__
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname/_implementati
on.py
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname/__pycache__/_
_init__.cpython-36.pyc
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname/__pycache__/_
implementation.cpython-36.pyc
/usr/share/bash-completion/completions/hostname
/usr/share/bash-completion/completions/hostnamectl
/usr/share/dbus-1/system-services/org.freedesktop.hostname1.service
/usr/share/dbus-1/system.d/org.freedesktop.hostname1.conf
...
Some output has been omitted in the example above.
6.2 Step 2
To locate all files which have the word hostname in the file name and make the search case insensitive, execute the
following command:
locate –i hostname
sysadmin@localhost:~$ locate -i hostname
/bin/hostname
/etc/hostname
/lib/systemd/systemd-hostnamed
/lib/systemd/system/dbus-org.freedesktop.hostname1.service
/lib/systemd/system/hostname.service
/lib/systemd/system/systemd-hostnamed.service
/usr/bin/hostnamectl
/usr/lib/gettext/hostname
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname/__init__.py
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname/__pycache__
/usr/lib/python3/dist-packages/urllib3/packages/ssl_match_hostname/_implementation.py
/usr/lib/python3/dist-
packages/urllib3/packages/ssl_match_hostname/__pycache__/__init__.cpython-36.pyc
/usr/lib/python3/dist-
packages/urllib3/packages/ssl_match_hostname/__pycache__/_implementation.cpython-36.pyc
/usr/lib/x86_64-linux-gnu/perl/5.26.1/Sys/Hostname.pm
/usr/lib/x86_64-linux-gnu/perl/5.26.1/auto/Sys/Hostname
/usr/lib/x86_64-linux-gnu/perl/5.26.1/auto/Sys/Hostname/Hostname.so
...
Some output has been omitted in the example above for brevity.
6.3 Step 3
The locate command will only return results of files that the current user would normally have access to. The
find command will search a directory and all of its subdirectories (including files that the current user may not
have access to).
The find command searches for files by name when the -name option is used. To find the hostname file in the
/etc directory, execute the following command:
The find command generates a lot of output related to files that it cannot access due to inadequate permissions. To
suppress this type of output, redirect it to /dev/null, as shown in the example below. To find the hostname file in
the /etc directory and also redirect the error stream, execute the following command:
6.5 Step 5
To find all files which were modified less than 30 minutes ago in the /etc directory, execute the following
command:
6.6 Step 6
To find files which are larger than 8MB in size, execute the following command:
touch sample
find ~/ -empty 2>/dev/null
sysadmin@localhost:~$ touch sample
sysadmin@localhost:~$ find ~/ -empty 2>/dev/null
/home/sysadmin/Videos
/home/sysadmin/Documents/Work/.emptydir
/home/sysadmin/Templates
/home/sysadmin/Music
/home/sysadmin/Pictures
/home/sysadmin/Public
/home/sysadmin/Downloads
/home/sysadmin/Desktop
/home/sysadmin/sample
/home/sysadmin/.cache/motd.legal-displayed
6.8 Step 8
To find all files that are directory files in your home directory, execute the following commands:
6.9 Step 9
To display a count of all of the files in the /etc directory structure that end in .conf, execute the following
command:
6.11 Step 11
To find all files starting with hosts in the /etc directory structure and display detailed information about the files,
use the following command:
6.12 Step 12
The -exec option is used to execute a specific command using the results of find as the input. To find all files in
your home directory that end in .conf and delete them with the rm command, execute the following commands:
cp /etc/*.conf .
ls
find ~ -name "*.conf" –exec rm {} \;
ls
sysadmin@localhost:~$ cp /etc/*.conf .
sysadmin@localhost:~$ ls
Desktop ca-certificates.conf host.conf resolv.conf
Documents debconf.conf ld.so.conf rsyslog.conf
Downloads deluser.conf libaudit.conf sample
Music dir1 logrotate.conf sysctl.conf
Pictures dir2 ltrace.conf ucf.conf
Public dir3 mke2fs.conf updatedb.conf
Templates fuse.conf nsswitch.conf
Videos gai.conf pam.conf
adduser.conf hdparm.conf popularity-contest.conf
sysadmin@localhost:~$ find ~ -name "*.conf" -exec rm {} \;
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates dir1 dir3
Documents Music Public Videos dir2 sample
6.13 Step 13
The -ok option is used to execute a specific command using the results of the find command as the input after
confirming with the user. To find all files that end with the word .conf and then delete them interactively, execute
the following commands:
cp /etc/*.conf .
ls
find ~ -name "*.conf" –ok rm {} \;
6.14 Step 14
The whereis command can be used to search the source files for any given command.
To find out where the find command is located, use the whereis command without any options:
whereis find
sysadmin@localhost:~$ whereis find
find: /usr/bin/find /usr/share/man/man1/find.1.gz /usr/share/info/find.info.gz
The output of the whereis command returns three directories. The first is where the find command is located, in
the usr/bin/find directory. There is also a path given for the man page of the find command at
/usr/share/man/man1/find.1.gz and another path for the info page at /usr/share/info/find.info.gz.
6.15 Step 15
To view the location of the binary for the find command separate from the man page and info page, use the -b and
-m options respectively:
whereis -b find
whereis -m find
sysadmin@localhost:~$ whereis -b find
find: usr/bin/find
sysadmin@localhost:~$ whereis -m find
find: /usr/share/man/man1/find.1.gz /usr/share/info/find.info.g
6.16 Step 16
When the whereis command returns more than one result for a command, it may be useful to know which
command is being used. The which command can be used to find out which result is used when executing the
command.
To find all the binaries for the touch command, execute the following command:
whereis -b touch
sysadmin@localhost:~$ whereis -b touch
touch: /usr/bin/touch /bin/touch
To find out which of the touch command results is the real command, use the which command:
which touch
sysadmin@localhost:~$ which touch
/usr/bin/touch
6.17 Step 17
The -a option can be used with the which command to locate multiple executable files. To locate all the executable
files for the less command, execute the following command:
which -a less
sysadmin@localhost:~$ which -a less
/usr/bin/less
/bin/less
6.18 Step 18
The type command can be used to determine information about various commands.
To find out which specific file the less command originates from, execute the following command:
type less
sysadmin@localhost:~$ type less
less is /usr/bin/less
The output of the command above is similar to the output of the which command:
The output of the command above is similar to the output of the which command:
To reveal all the paths of the less command, execute the type command with the -a option:
type -a less
sysadmin@localhost:~$ type -a less
less is /usr/bin/less
less is /bin/less
Sumber : https://content.netdevgroup.com/labs/lpic1-s1/PrQVzWcZ2f/
NDG Introduction to Linux I - Chapter 7: Text Utilities
A core philosophy that started with UNIX, and lives on in Linux, is that commands should read text as input and write text as
output. To remain true to this philosophy, a large number of commands have been designed with the primary purpose of
processing text files. As you will see, many of these commands act as filters, as they somehow alter the text that they read
before they produce their output.
However, the cat command gets its name from the word concatenate, meaning to merge or append. This command
has the ability to accept two or more files as input and then output the concatenation (merging) of those files:
While the cat command is often used to quickly view the contents of small files, using it to display the contents of
larger files will result in frustration since the data will be displayed much too quickly and will scroll off the screen.
To display the file one page at a time, use a pager, either the more or the less command.
To view a file with the less command, pass the file name as an argument:
less FILE
sysadmin@localhost:~/Documents$ less words
The more and less commands allow users to navigate the document using keystroke commands. For example,
when using less as a pager, to advance forward a page, press the Spacebar:
The less command might look familiar since it is the pager generally used to view man pages. As you may recall,
the less command requires the use of movement commands to navigate a file. When viewing a file with the less
command, use the H key or Shift + H to display a help screen with a summary of the less commands:
This can also be a useful way to send larger files across a slow network that has connection issues. Breaking files
into smaller pieces would make it easier to transfer the data across a poor network connection. Once the pieces are
copied to the other system, the cat command can be used to reassemble the pieces into a single file once again.
The INPUT argument is typically a file name, but the split command is able to read from standard input as well.
Note
Standard input and output streams will be covered in greater detail later in the course.
By default, the new files will be named with a prefix of x and an alphabetical suffix of aa, ab, etc.:
As indicated by the command syntax, it is possible to specify a prefix other than x. For example, the following
example denotes file. as the prefix:
The -d option will allow for the split files to have a numeric suffix instead of a default alphabetic suffix. If -d is
added to the previous command, the resulting file names would be:
By default, the split command will break apart a file into 1,000 line chunks. The first 1,000 lines of the original
file will go into the first file, the second 1,000 lines will go into the second file, etc. The above file longfile.txt
splits into 11 files, meaning it is approximately 11,000 lines long.
The -l option can be used to specify the number of lines to split upon. Alternatively, the -b option may be used to
specify the maximum number of bytes to use per file.
nl [OPTION]... [FILE]...
sysadmin@localhost:~/Documents$ nl newhome.txt
1 Thanks for purchasing your new home!!
7 Good luck!!!
The nl command offers many formatting options for line numbering, including separate styles for headers, footers,
sections, and the body of a file. For example, to have the nl command number every line in the newhome.txt file,
execute the following command:
sysadmin@localhost:~/Documents$ nl -b a newhome.txt
This is one of the reasons some Linux users prefer the long argument technique. Using a long argument, the
command could have been written:
Consider This
It is also possible to number lines using the cat -n command with the -n option, which numbers every line by
default.
By default, the head command will display the first ten lines of a file's contents. For example, the following
command displays the first ten lines of the alpha.txt file:
There are several options for the head command that are useful. For instance, there is the ability to use a number as
an option to indicate how many lines of output to display. For example, to display the first three lines of the
alpha.txt file, execute:
There is also the -n option which takes an argument for the number of lines to display. So, the following would
also display the first three lines of the alpha.txt file:
The alpha.txt file is 26 lines long. The following command omits the last 24 lines, resulting in only the first two
lines of the file being displayed:
The tail command also displays ten lines by default with no options given:
sysadmin@localhost:~/Documents$ tail alpha.txt
Q is for Quark
R is for Rat
S is for Sloth
T is for Turnip
U is for Up
V is for Velvet
W is for Walrus
X is for Xenon
Y is for Yellow
Z is for Zebra
You may use either a number or the -n option as an argument to the tail command to specify how many lines you
want to output from the end of the file. Therefore, the following two commands are equivalent:
With the tail command, the -n option has an interesting twist to it, as well. When using the -n option with a
number prefixed by a plus sign + character, the number is interpreted as the line number in the file to start
displaying content from; it will display from that line to the end of the file. In other words, the -n option used with
the +20 argument will display the contents of the file starting at line twenty and continuing until the end of the file.
Consider This
Another unusual, but useful feature of the tail command is its ability to follow a file. When the tail command is
executed with the -f option and a file name argument, then it will initially output the number of lines specified.
Instead of exiting after displaying the lines, the tail command continues to run, following any changes in the file
and displaying new content as it is added to the end of the file.
One of the main applications for this follow feature is for administrators to watch log files change while they are
troubleshooting problems.
Additionally, the -F option is used when following a log file that may have been rotated or archived, and a new
empty file of the same name replaces it. The -F option allows tail to notice that the underlying inode number that
matches the log file name has changed, and it will continue to watch the new log file.
To demonstrate the paste command’s functionality, the numbers.txt and letters.txt will be used:
To paste these files together with a tab between the elements, use the paste command:
It is also possible to use other characters as delimiters. For instance, if the intent is to create a file that a spreadsheet
could easily open, a comma would be chosen as the delimiter. The -d option is used to specify the delimiter.
Similar to the paste command, the join command is able to combine two files. Instead of simply going line by
line through the files, the join command matches the values of fields to determine which lines to combine. In
other words, it will join files based on a common field between the files.
Consider This
For those familiar with Structured Query Language (SQL), the join command is similar to the SQL join
statement, which combines records from two tables of related tables by matching values in joined fields.
In order to break a line into fields, the join command uses either tabs or spaces between the contents of the files
and breaks each line into separate fields.
If the data that is used to join the files is the first field, then there is no need to specify which join fields to use.
However, the field that is used in the joining process must be sorted in both files first, or else errors may occur.
Also, missing elements in the key of either file will not appear in the results.
In the following example, the first field is used to merge the two files together. When the files are joined, the
number field is used as a key to determine which words to join:
The first field in each file containing numbers is only shown once in the output, followed by a space and the second
field from the first file, finally by another space and the second field from the second file. For illustration purposes,
the same two files are shown being combined with the paste and cat commands:
Use the -t option to specify an alternate delimiter. For example, if two spreadsheets were exported into comma
separated value .csv files, join could use those commas to distinguish one field from another.
The next example illustrates a more advanced use of the join command. Two files, people.csv and os.csv., will
be combined by the join command using data that isn't in the first field.
The following join command will join the files according to the field containing last names:
To specify that the join field for the first file is the second field, -1 2 is used. The -1 option means "field of the
first file" and the 2 argument means "the second field".
To specify that the join field for the second file is the third field, -2 3 is used. The -2 option means "field of the
second file" and the 3 argument means "the third field".
These files also use a delimiter that is neither a tab nor a space, so the delimiter is specified as a comma , character
with the -t',' option:
The head and tail commands are often used in command pipelines to limit the number of lines when dealing with
large files. For example, to number only the last three lines of the alpha.txt file, pipe the output of the tail
command to the nl command:
Command pipelines can also be used to filter command output. For example, to list just the first five files in the
current directory, pipe the output of the ls command to the head command:
sysadmin@localhost:~/Documents$ ls | head -5
School
Work
adjectives.txt
alpha-first.txt
alpha-second.txt
Note
Note the output of the following head command for the first line of the /etc/passwd command. There are 7 fields,
each separated by the colon : character as a delimiter:
To extract certain fields from the /etc/passwd file, specify the delimiter using the colon character as an argument
to the -d option. If only fields 1,5,6, and 7 were important, they could be extracted using the following command:
The same result could be achieved by combining the fields 5,6,7 into the range 5-7:
Given a file that has fields at fixed character positions, the cut command can be used to extract the characters one
at a time by using the character -c option followed by a range.
For example, in the /var/syslog file; the first fifteen characters specify a timestamp. To extract only those
characters from the text, specify 1 through 15:
This command is able to sort using any field, the same way that a spreadsheet is able to sort by any column. By
default, sort breaks up each line of a file into fields using whitespace (tabs or spaces) as delimiters. To specify an
alternate delimiter, use the -t option. To specify the fields to sort from first to last, use one or more -k options. In
the following example, the os.csv file is sorted based on the second field of data, using the comma , character as
a delimiter:
While it is possible to sort the first field of os.csv using the ASCII ordering, a numerical sort makes more sense.
In order to have the sort command treat a field numerically, add an n as an argument to the -k option for that key
field specification. Be aware in the following example the -k option has two arguments, the 1 indicates the first
field, and the n specifies sorting that field numerically:
To reverse the sort direction from ascending to descending, add an r argument to the key field specification:
The sort command can be combined with other commands to accomplish more sophisticated tasks. For example,
the output of the cut -f7 -d: /etc/passwd command contains many duplicate values:
By piping this output to the sort command and using the unique -u option, duplicate lines will be removed from
the output:
There are a couple of reasons to use the uniq command instead of the sort -u command. First, there may be times
when the lines should not be sorted first. The uniq command can simply remove lines that are currently
consecutive.
Second, the count -c option to the uniq command outputs the number of duplicates that were counted (a feature
that the sort command lacks):
sysadmin@localhost:~/Documents$ cut -f7 -d: /etc/passwd | sort | uniq -c
3 /bin/bash
1 /bin/sync
23 /usr/sbin/nologin
od [OPTION]... [FILE]...
The following table summarizes the formats that the od command can output:
sysadmin@localhost:~/Documents$ od -c hidden.txt
0000000 T h i s \r i s \r a \r h i d
0000020 d e n \r m e s s a g e \r N G
0000040 D L P I C - 1 C u r r i c u
0000060 l u m \n I s T h e B e s t
0000100 . \n \n
0000103
It appears that there was a hidden message in that file that the cat command missed. The point, however, is not to
use od to send secret messages. The od command can be used for file recovery, or to open a file that crashes other
programs.
As the name implies, od can display raw file content in octal form. It also supports hexadecimal and decimal
output. This can be very useful when analyzing a precompiled binary, such as a virus:
sysadmin@localhost:~/Documents$ od -x hidden.txt
0000000 6854 7369 0d20 7369 0d20 2061 680d 6469
0000020 6564 206e 6d0d 7365 6173 6567 0d20 474e
0000040 2044 504c 4349 312d 4320 7275 6972 7563
0000060 756c 206d 490a 2073 6854 2065 6542 7473
0000100 0a2e 000a
0000103
Two other options control where the od command will read its data from the file:
Option Function
-j Specify how many bytes to skip from the input.
-N Specify the total number of bytes the od command will read from the file.
See how these options work with the same hidden.txt file from the previous example, skipping thirteen bytes and
displaying only fifteen bytes total:
sysadmin@localhost:~/Documents$ od -j 13 -N 15 -c hidden.txt
0000015 h i d d e n \r m e s s a g e
0000034
Additional options that are used to affect the output of the od command:
Option Function
-w Specify the width in bytes to output next to the offset address. By default, the width will be sixteen bytes.
The -A option has several ways to specify the offset address to the left of the data as the formatting options covered
earlier:
Option Function
-An Prevents the od command from outputting the offset address to the left of the data.
-Ad Outputs file offsets using decimal addresses.
-Ax Outputs file offsets using hexadecimal addresses.
File offsets are non-negative integers that specify a point within a file that is a specific number of bytes from a
known position. For example, an offset of 64 from the beginning of a file would the denote 65th bit in the file.
Offsets are a way to specify a particular piece of information located within a data file. They can be useful for
dealing with files that are open or have been damaged, or in computer forensics for recovering deleted information.
The following example, the od command displays the file with a width of eight bytes of data, the offset address
suppressed, and the bytes of data being displayed as ASCII characters:
The following example will translate all of the lowercase letters in alpha-first.txt to uppercase letters:
The characters that the tr command is translating do not have to be in a range; they can simply be listed for each
set of characters. Be careful to balance the sets to make sure that they both have the same number of characters, or
else strange results may occur. The following example has the tr command replace the vowel characters with
symbols:
Aside from performing a translate function, the tr command can also "squeeze" repeated characters and delete
characters. In performing these functions, only one set of characters needs to be specified. For example, to
eliminate duplicates, use the -s option to squeeze repeats of characters from the first set:
Using the -d option will delete the characters in the first set:
To do a simple search and replace operation, use the following script, or expression. This will search for the pattern
between the first two slashes, and if it finds that text, then it replaces it with what is specified between the last two
slashes:
s/PATTERN/REPLACEMENT/
In the alpha-first.txt file, to replace the word Animal with the word Apple, execute the following command:
Unlike most filter commands, the sed command can modify the original file by using the -i option.The -i option
allows for an optional argument, which will be an extension added to the original file. The result is a new file
which is a copy of the original file. For example, if you wanted to backup the original file as alpha-
first.txt.original file, execute the following:
The original file was modified as a result of the -i option. For the rest of the examples, the -i option will not be
used. The output will be sent to the terminal instead, and the original file will not be changed.
When performing a search and replace operation, the sed command will only replace the first occurrence of the
search pattern by default. To replace all occurrences of the pattern, add the global modifier g after the final slash.
s/PATTERN/REPLACEMENT/g
The following example will only replace the first occurrence of the oo pattern with the 00 pattern:
If you want to replace every occurrence of oo with 00, add the g modifier:
The sed command is also able to either insert text before a pattern or after a pattern. For this type of expression, do
not use the s character before the first slash; use an insert change with the i\ expression or an append change with
the a\ expression.
/PATTERN/i\TEXT\
/PATTERN/a\TEXT\
Inserting text with the i\ expression affects the line before the line containing the search term:
Appending text with the a\ expression affects the line after the line containing the search term:
The sed command can also be used to search for a line of text containing a pattern and then delete the lines that
match. Place the pattern to search for between two slashes followed by a d to carry out this operation:
/PATTERN/d
Notice the animals.txt file has five lines, but the following example command removes the lines that contain an
a:
To change an entire line that matches a pattern to something else, use a slash, the pattern to match, another slash,
c\, and then the new text.
/PATTERN/c\REPLACEMENT
For example, to change a line containing 3 to three, use the following command:
Keep in mind the entire line will be replaced, not just the matching pattern:
The sed command normally parses only one expression to filter text. To use multiple expressions, use a -e option
with each expression to modify the file.
Notice that the order in which those expressions are placed makes a difference. In the first attempt, both
expressions are effective, but in the second attempt, only the first is effective. After the first expression deletes all
lines containing an a, the second expression doesn't have anything to do because the line that contained 3 was
removed:
Note
The use of the term pattern in this section refers to one of the most powerful aspects of the sed command, its
compatibility with regular expressions. In each example so far, the sed command has been using literal
expressions involving alphanumeric characters like t or o or strings like three. Regular expressions can be used to
create patterns for use with the sed command and many other, far more powerful commands.
sysadmin@localhost:~/Documents$ wc alpha-first.txt
6 24 90 alpha-first.txt
The wc command reports that there are 6 lines, containing 24 words, totaling 90 bytes in length. The total number
of bytes includes spaces, punctuation, and carriage returns.
sysadmin@localhost:~/Documents$ wc alpha-first.txt -l
6 alpha-first.txt
sysadmin@localhost:~/Documents$ wc alpha-first.txt -w
24 alpha-first.txt
sysadmin@localhost:~/Documents$ wc alpha-first.txt -m
90 alpha-first.txt
In the example, the -l option was used to count the number of lines. In the second example, the -w option was used
to count the number of words. Finally, the -m option indicates the number of characters.
One might imagine that the -c option would count the number of characters, but -c is used to count the number of
bytes. The number of bytes can vary if there are non-printable characters in the file. In this case, they are the same:
sysadmin@localhost:~/Documents$ wc alpha-first.txt -c
90 alpha-first.txt
One last option, -L returns the maximum line length in the file:
sysadmin@localhost:~/Documents$ wc alpha-first.txt -L
17 alpha-first.txt
If the word count of all the files in a particular folder is desired, the asterisk wildcard * can be used:
sysadmin@localhost:~/Documents$ wc *
wc: School: Is a directory
0 0 0 School
wc: Work: Is a directory
0 0 0 Work
5 10 39 adjectives.txt
6 24 90 alpha-first.txt
6 24 90 alpha-first.txt.original
7 28 106 alpha-second.txt
13 52 195 alpha-third.txt
26 104 390 alpha.txt
5 10 42 animals.txt
1 3 14 food.txt
7 14 647 hello.sh
3 11 67 hidden.txt
5 5 10 letters.txt
8 32 187 linux.txt
10240 10236 66540 longfile.txt
13 36 236 newhome.txt
5 5 10 numbers.txt
4 4 77 os.csv
4 4 59 people.csv
6 21 110 profile.txt
11 11 51 red.txt
2 26 130 spelling.txt
102305 102305 971578 words
112682 112965 1040667 total
The output above reveals that longfile.txt really is the longest file in the folder.
Key Terms
split
Command used to take one file and break it into multiple parts. By default it breaks the file up into 1000
line sections.
Section 7.3
tail
Prints the last ten lines of a file by default. The number of lines can be set to any desired value.
Section 7.6
tr
Command used to translate one set of characters to another.
Section 7.13
uniq
Command to remove duplicate lines that are currently consecutive.
Section 7.11
wc
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.
Section 7.15
LAB 7
7.0 Introduction
Text is the primary form of input and output for Linux commands and therefore there are many commands whose
primary purpose is processing text files.
7.1 Step 1
The cat command (derived from the word concatenate) accepts multiple files as input and outputs a merged file.
To view the contents of the /etc/hosts and /etc/hostname files, execute the following commands:
cat /etc/hosts
cat /etc/hostname
sysadmin@localhost:~$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.2 localhost
sysadmin@localhost:~$ cat /etc/hostname
localhost
To concatenate the contents of files /etc/hosts and /etc/hostname, execute the following command:
7.2 Step 2
The standard output of the cat command can be sent to another file by using redirection. Standard output
redirection is achieved by following a command with the greater-than > character and a destination file.
To view the result file created above, execute the following command:
cat result
sysadmin@localhost:~$ cat result
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.2 localhost
localhost
7.3 Step 3
To view the result file along with line numbers prefixed to each line, execute the following command:
cat –n result
sysadmin@localhost:~$ cat -n result
1 127.0.0.1 localhost
2 ::1 localhost ip6-localhost ip6-loopback
3 fe00::0 ip6-localnet
4 ff00::0 ip6-mcastprefix
5 ff02::1 ip6-allnodes
6 ff02::2 ip6-allrouters
7 192.168.1.2 localhost
8 localhost
The cat command can be used for viewing text files only.
7.4 Step 4
While viewing small files with the cat command poses no problems, it is not an ideal choice for large files. For
larger files, a pager command can be used to view the contents. Pager commands display one page of data at a
time, allowing you to move forward and backward in the file by using movement keys.
The less command provides a very advanced paging capability. It is usually the default pager used by commands
like the man command.
To view a file with the less command, pass the file name as an argument:
less /etc/passwd
sysadmin@localhost:~$ less /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
systemd-network:x:101:102:systemd Network Management,,,:/run/systemd/netif:/usr/sbin/nologin
systemd-resolve:x:102:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nol
/etc/passwd
7.5 Step 5
While you are in the output of the less command, you can view the help screen by pressing the h key:
MOVING
.6 Step 6
While you are in the help screen, you can return to the file by pressing the q key.
q
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
systemd-network:x:101:102:systemd Network Management,,,:/run/systemd/netif:/usr/
sbin/nologin
systemd-resolve:x:102:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nol
/etc/passwd
Then, to exit the less command entirely, press the q key again:
q
sysadmin@localhost:~$
7.7 Step 7
There are two ways to search the output of the less command: searching forward or searching backward from
your current position.
To start a search to look forward from your current position, use the slash / key. Then, type the text or pattern to
match and press the Enter key.
To search for the word sbin in the /etc/passwd file, use the slash / key:
less /etc/passwd
/sbin
Enter
sysadmin@localhost:~$ less /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
systemd-network:x:101:102:systemd Network Management,,,:/run/systemd/netif:/usr/
sbin/nologin
systemd-resolve:x:102:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nol
/sbin_
q
sysadmin@localhost:~$
7.8 Step 8
The split command is used to split the input file into two or more files. The syntax for the split command is:
To split a big file, such as the words file into smaller pieces, first copy the /usr/share/dict/words file to the
current directory:
cp /usr/share/dict/words .
sysadmin@localhost:~$ cp /usr/share/dict/words .
To split the words file into smaller pieces with the prefix output and then verify the result of the split command,
execute the following commands:
By default, the new files created with the split command will be named with a prefix of x and an alphabetical
suffix of aa, ab, etc.:
PREFIX
xaa xab xac xad xae xaf xag xah xai xaj xak
SUFFIX
xaa xab xac xad xae xaf xag xah xai xaj xak
In the output of the command above, the words file is split into smaller files which are labeled with the specified
prefix output and the default alphabetical suffix:
PREFIX
SUFFIX
7.9 Step 9
The split command allows files to have a numeric suffix instead of a default alphabetic suffix. To split the words
file into files prefixed with the name result and suffixed by numbers, execute the following commands:
7.11 Step 11
The nl command will number the lines of its output. The syntax for the nl command is:
nl [OPTION]... [FILE]...
To display the newhome.txt file in the Documents directory with line numbers, execute the following commands:
cd Documents/
nl newhome.txt
sysadmin@localhost:~$ cd Documents/
sysadmin@localhost:~/Documents$ nl newhome.txt
1 Thanks for purchasing your new home!!
7 Good luck!!!
7.12 Step 12
By default, the nl command is used to number non-blank lines in the output. To count the number of lines in the
newhome.txt file, including blank lines, execute the following command:
nl –ba newhome.txt
cd
sysadmin@localhost:~/Documents$ nl -ba newhome.txt
1 Thanks for purchasing your new home!!
2
3 **Warning** it may be haunted.
4
5 There are three bathrooms.
6
7 **Beware** of the ghost in the bedroom.
8
9 The kitchen is open for entertaining.
10
11 **Caution** the spirits don't like guests.
12
13 Good luck!!!
sysadmin@localhost:~/Documents$ cd
sysadmin@localhost:~$
7.13 Step 13
The purpose of the head command is to view the beginning of a file or output.
To view the beginning of the /usr/share/dict/words file, execute the following command:
head /usr/share/dict/words
sysadmin@localhost:~$ head /usr/share/dict/words
A
A's
AMD
AMD's
AOL
AOL's
Aachen
Aachen's
Aaliyah
Aaliyah's
7.14 Step 14
If the number of lines is not specified, the head command will display the first 10 lines. To view the first 15 lines
of the output of the /usr/share/dict/words file, execute the following command:
7.15 Step 15
To view the first 5 lines of the /usr/share/dict/words file, execute the following command:
head –n 5 /usr/share/dict/words
sysadmin@localhost:~$ head -n 5 /usr/share/dict/words
A
A's
AMD
AMD's
AOL
7.16 Step 16
To view the first 20 lines of the output of the man ls command, execute the following command:
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci-
fied.
-a, --all
do not ignore entries starting with .
-A, --almost-all
7.17 Step 17
The tail command displays contents from the end of the file and not the beginning.
To view the last 10 lines of the /usr/share/dict/words file, execute the following command:
tail /usr/share/dict/words
sysadmin@localhost:~$ tail /usr/share/dict/words
élan's
émigré
émigré's
émigrés
épée
épée's
épées
étude
étude's
études
7.18 Step 18
To view the last 5 lines of the /usr/share/dict/words file, execute the following command:
tail -5 /usr/share/dict/words
sysadmin@localhost:~$ tail -5 /usr/share/dict/words
épée's
épées
étude
étude's
études
7.19 Step 19
To view the last 5 lines of the output of the ls –ltr command, execute the following command:
7.20 Step 20
To display the last 4 lines of the /etc/hosts file (the total number of lines in this file is 7, only the last 4 lines will
be displayed), execute the following command:
tail -n -4 /etc/hosts
sysadmin@localhost:~$ tail -n -4 /etc/hosts
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.2 localhost
7.21 Step 21
To view the contents of the /etc/hosts file starting from the 3rd line to the end of the file, execute the following
command:
tail –n +3 /etc/hosts
sysadmin@localhost:~$ tail -n +3 /etc/hosts
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.2 localhost
7.22 Step 22
The paste command will merge the lines of one or more files, line by line, separating them with a tab as a
delimiter (separator) by default. The paste command is especially useful for files that contain data in column
format. The syntax for the paste command is:
Execute the following commands to create two files named head and tail and then merge them together into a file
named total:
7.23 Step 23
To merge the two files again using the colon : character as the delimiter instead of the default tab delimiter,
execute the following command:
7.24 Step 24
The cut command is used to extract fields from a text file. The space and tab are the default delimiters and can be
changed using the -d option.
To extract the 1st, 3rd and 4th fields from the /etc/passwd file, execute the following command:
7.25 Step 25
A range of fields can also be provided to the cut command. To extract the fields 1 – 4 from the /etc/passwd file,
execute the following command:
7.26 Step 26
Using the cut command, a specific field or set of fields can also be extracted from data containing fixed width
columns. To extract the contents from column 31 to 44 from the output of the ls -l command, execute the
following command:
Jul 1 21:35
Mar 7 21:00
Mar 29 17:36
Mar 22 17:41
Mar 22 17:41
Mar 29 17:36
Mar 22 17:40
Apr 4 2018
Apr 2 2018
7.27 Step 27
The sort command is useful for working with data organized in columns. It is used to display a file sorted on a
specific field of data.
To specify the fields to sort from first to last, use the sort command with one or more -k options.
First, view the file adjectives.txt in the Documents directory using the cat command:
cd Documents/
cat adjectives.txt
sysadmin@localhost:~$ cd Documents/
sysadmin@localhost:~/Documents$ cat adjectives.txt
1 golden
2 honey
3 fruit
4 grey
5 bald
To sort the adjectives.txt file using the second field (i.e., the adjective: 1 golden) as a key, execute the
following command:
In the command above, the -k option has one argument: the 2, which indicates the second field to sort.
7.28 Step 28
By default, sort breaks up each line of a file into fields using whitespace (tabs or spaces) as delimiters. To specify
an alternate delimiter, use the -t option.
To sort the /etc/passwd file using the first field (i.e., the user name: bin:x:2:2:bin:/bin:/usr/sbin/nologin)
as a key and using the colon : character as the delimiter, execute the following command:
cd
sort –t: -k1 /etc/passwd | head –n 4
sysadmin@localhost:~/Documents$ cd
sysadmin@localhost:~$ sort -t: -k1 /etc/passwd | head -n 4
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
bind:x:106:111::/var/cache/bind:/usr/sbin/nologin
The head command is used to limit the output to four lines.
7.29 Step 29
To reverse the sorting order in the example from the previous step from ascending to descending, execute the
following command:
In the command above, the -k option has two arguments: the 1 indicates the first field to sort and the r argument to
reverse the sort.
7.30 Step 30
For certain fields, the sorting order required may be numerical instead of alphabetical. In order to have the sort
command treat a field numerically, add an n as an argument to the -k option for that key field specification. To
treat the sorting field numerically for the third field of the /etc/passwd file, execute the following command:
A closer look at the output above demonstrates that the third field of the /etc/passwd file is now sorted
numerically:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
7.31 Step 31
Use the -u option to the sort command to remove duplicate entries, as demonstrated below:
In the command above, the -u option to the sort command rearranges the rows in the file and then removes
duplicates. The uniq command also provides duplicate removal functionality in a slightly different way. It removes
duplicates which are already on consecutive lines i.e. it does not sort data.
The uniq command provides another additional option not supported by the sort command; it can display the
number of duplicates that were counted.
7.33 Step 33
The tr command can be used to translate from one set of characters to another.
To capitalize the /etc/hosts file with the tr command, execute the following command:
7.34 Step 34
To replace the five letters (h, i, p, o, and n) with the five characters (!, @, #, $, and %) in the /etc/hosts file,
execute the following command:
When performing a search and replace operation, the sed command will only replace the first occurrence of the
search pattern by default. To replace all occurrences of the pattern, add the global modifier g after the final slash.
s/PATTERN/REPLACEMENT/g
To replace all occurrences of the word host with the word NAME in the /etc/hosts file globally, execute the
following command:
cat /etc/hosts
sed 's/host/NAME/g' /etc/hosts
sysadmin@localhost:~$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.2 localhost
sysadmin@localhost:~$ sed 's/host/NAME/g' /etc/hosts
127.0.0.1 localNAME
::1 localNAME ip6-localNAME ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.2 localNAME
7.36 Step 36
The sed command is also able to insert text before a pattern. For this type of expression, do not use the s character
before the first slash; use an insert change with the i\ expression.
/PATTERN/i\TEXT\
To insert the word Report in the /etc/hosts file preceding the line containing 127.0.0.1, execute the following
command:
/PATTERN/a\TEXT\
To append the string End of report in the /etc/hosts file following the line containing allrouters, execute
the following command:
7.38 Step 38
The sed command can also be used to search for a line of text containing a pattern and then delete the lines that
match. Place the pattern to search for between two slashes followed by a d to carry out this operation:
/PATTERN/d
To delete lines that contain "localhost" in the /etc/hosts file, execute the following command:
8.1 Introduction
Regular expressions (or regex) define a search pattern. They are similar to globbing in that they can be expanded
to match certain sequences of characters in text, but have more variety and power.
A literal character in a pattern represents the character itself. It has no special meaning, and no substitution is
performed. Literal characters include letters, numbers, some punctuation characters, and basically any other
character that is not an operator.
An operator is a special character or character sequence that has special meaning. Operators give regular
expressions their superpowers! Operators are sometimes referred to as metacharacters. The word “operator” more
readily conveys transformative action, so we will use that term in this chapter.
A regular expression has two common forms: basic and extended. Extended regular expressions support more
operators than their basic counterparts. Most commands that can use regular expressions can interpret basic regular
expressions. Extended regular expressions are not available for all commands and may require an additional option
for them to work correctly.
The man page for regex is a good source of information to learn about basic vs. extended regex, the syntax for
regex, and regex compatibility:
NAME
regex - POSIX.2 regular expressions
DESCRIPTION
Regular expressions ("RE"s), as defined in POSIX.2, come in two forms:
modern REs (roughly those of egrep; POSIX.2 calls these "extended" REs)
and obsolete REs (roughly those of ed(1); POSIX.2 "basic" REs). Obso-
lete REs mostly exist for backward compatibility in some old programs;
they will be discussed at the end. POSIX.2 leaves some aspects of RE
syntax and semantics open; "(!)" marks decisions on these aspects that
may not be fully portable to other POSIX.2 implementations.
The examples displayed in this chapter will make use of the grep command to demonstrate regular expressions.
The grep command is able to filter text from data, providing a very visual demonstration of how regular
expressions work. The grep command that will be used is actually an alias that runs grep --color so that the text
that matches will be displayed in a red color.
Note
Unlike globs, regular expressions are only supported by a small set of commands. This is because glob expansion
is usually provided by the shell; the command (in most cases) does not need to worry about the implementation of
glob expansion.
On the other hand, commands that support regular expressions must implement regular expression logic, and all the
complexity and extra code that comes with them.
Important
The pattern argument of a command should be protected by strong quotes to prevent the shell from misinterpreting
them as special shell characters. This means that you should place single quotes ' around a regular expression. The
following example demonstrates how not placing single quotes around special characters will result in unexpected
output, which is resolved by adding single quotes:
Although very useful for the grep command, literal characters alone provide no wildcarding or special matching
capabilities. For that, let‟s introduce the operators that are available in basic regular expressions.
The front anchor ^ operator can be used to ensure that a pattern appears at the beginning of the line. For example,
to find all lines in /etc/passwd that start with root, use the ^root pattern. Note that the ^ operator must be the
first character in the pattern to be effective.
The back anchor $ operator can be used to ensure a pattern appears at the end of the line, thereby effectively
reducing the search results. To find the lines that end with an r in the alpha-first.txt file, use the r$ pattern:
sysadmin@localhost:~$ cd Documents
sysadmin@localhost:~/Documents$ grep 'r$' alpha-first.txt
B is for Bear
F is for Flower
Again, the position of this operator is important. The $ operator must be the last character in the pattern in order to
be effective as an anchor.
If the ^ and $ operators are used at the beginning and end of the same pattern, the entire line must match the
pattern. Meaning that the only way a line would be returned is if the entire line matched exactly, with no other
characters on the line.
Pattern Meaning
^Hello Matches any line that begins with the Hello string
World$ Matches any line that ends with the World string
^Hello World$ Matches any line which matches the Hello World string exactly
The r..t pattern will find any line that contained the letter r followed by exactly two characters (which can be any
character except a newline) and then the letter t:
Remember, the line does not have to be an exact match, it simply must contain the pattern, as seen here when the
r..t pattern is searched for in the /etc/passwd file:
To find all the lines in the profile.txt file that have a number in them, use either the [0123456789] or [0-9]
pattern:
Note
In the last example, the pipe | character is used to send the output of one command to another. Some of the
following examples use the cat command to send the contents of a file to the grep command.
As explained later in this chapter, the pipe | character can also be used as an alternation operator within an
extended regular expression. This should not be confused with the use of pipes in output chains, such as the cat |
grep examples.
To find all the lines which contain any non-numeric characters, insert the negation ^ operator as the first character
inside the brackets. This negates the characters listed:
Note
Do not mistake [^0-9] to match lines which do not contain numbers. It actually matches lines which contain non-
number characters. Look at the original file to see the difference. The third and sixth lines only contain numbers,
they do not contain non-numbers, so those lines do not match.
When other regular expression operators are placed inside of the list [ ] operators, they are treated as literal
characters. For example, the . character normally matches any one character, but when placed inside the square
brackets, then it will just match the . character itself. In the example, only lines which contain the literal .
character are matched.
When used with only one other character, the asterisk * operator isn't very helpful. Any of the following patterns
would match every string or line in the file: '.*' 'e*' 'b*' 'z*' This is because the asterisk * operator can
match zero occurrences of a pattern.
In order to make the asterisk * operator useful, it is necessary to create a pattern which includes more than just the
one character preceding the asterisk * operator. For example, the results above can be refined by adding another e
to make the pattern ee* effectively matching every line which contains at least one e.
Extended regular expression patterns support the basic regex operators PLUS the following additional operators:
The grep command understands both basic and extended regular expressions. By default, the grep command
assumes only basic regular expressions, meaning the +, ?, {, |, (, and ) operators are interpreted as literal
characters.
Historically, there is a command called egrep, which is similar to the grep command, but can understand extended
regular expressions. Now, the egrep command is deprecated in favor of using grep with the -E option.
Note
The term subpattern introduced above will be covered in the next section.
This grouping is considered to be a subpattern of the pattern. A subpattern is a smaller pattern within a pattern. The
matching operators *, ?, +, and { } that match single characters, can also be applied to subpatterns.
In the example below, parentheses are used to match the M character, followed by the iss subpattern repeated zero
or more times:
Note
Don't forget to use grep -E in order to get the command to recognize the extended regular expression character.
8.3.2 Match a Repeated Character With +
Another solution to the problems of using the asterisk * operator is to use the extended regex plus + operator,
instead. This matches at least one of the previous characters, instead of zero, so it is much more selective. For
example, the e+ pattern would only match if the text contained one or more e characters, yielding the same results
as the previously used ee* pattern:
In the example below, grouping is used in conjunction with the + operator to match the recurring iss subpattern:
Pattern Meaning
a{0,} Zero or more a characters
Pattern Meaning
a{1,} One or more a characters
a{0,1} Zero or one a characters
a{5} Five a characters
a{,5} Five or fewer a characters
a{3,5} From three to five a characters
To demonstrate, the { } operator in action, the example below matches one or more instances of the letter r in the
animals.txt file:
Pattern Meaning
xyz*
Matches the xy string followed by zero or more of the z character
xyz{0,}
x(yz)*
Matches the x character followed by zero or more copies of the yz string
x(yz){0,}
xyz+
Matches the xy string followed by one or more of the z character
xyz{1,}
(xyz)+
Matches one or more copies of the xyz string
(xyz){1,}
xyz?
Matches the xy string followed by zero or one of the z character
xyz{0,1}
xyz{,5} Matches the xy string followed by five or fewer of the z character
xyz{5,} Matches the xy string followed by five or more of the z character
xyz{3,5} Matches the xy string followed by three to five of the z character
[0-9]{1,3}% Matches one to three numeric characters, followed by the % character
8.3.5 Match Subpatterns With |
When used in extended regular expressions, the alternation | operator separates alternative expressions that can
match. It acts similarly to a Boolean OR.
For example, to match both the word gray or its alternative spelling grey, use the expression gray|grey:
Parentheses can also be used with alternation. The previous example of gray|grey could be rewritten as
gr(a|e)y. When parentheses are used in regular expressions from the command line, remember to enclose them in
the single quotes to prevent the shell from interpreting them:
However, the backslash \ character can also be used for designated backslash character combinations, called
backslash sequences. Backslash sequences can represent special operators or character classes, as illustrated in the
following chart.
A character class represents a range of characters. The digit character class (abbreviated \d) matches any number
[0-9]. The word character class (abbreviated \w) matches any upper and lowercase ASCII character, and numeric
digits [A-Za-z0-9]. More character classes will be introduced in the extended regular expressions section.
Note
Recall that to do a simple search and replace operation with the sed command, use the following script, or
expression:
s/PATTERN/REPLACEMENT/
In the syntax above, the s character signifies substitution and the forward slash / character is used as a delimiter.
Notice that the sed command searched its input for is and replaced it with was. It didn't limit its replacement to the
is that was a separate word, it also affected part of the word This replacing it with Thwas.
It would be better to use the \bis\b search criteria (the is pattern with word boundaries around it) to match the
word is:
To delete certain text, the sed command can be used to remove characters defined within square brackets.
For example, if you wanted to delete the leading numbers in the animals.txt file and create a new file without the
numbers, you could use the sed command and define the range 0-9 to be removed:
Consider This
In some commands, parentheses can be used for referring back to what was matched. What was matched by the
first set of parentheses can be referred to as \1, the second as \2, and so on. For example, if you had a file that
contained a list of people with their first name and then their last name, you could reverse the names with a comma
between them with the following sed command:
The -r option to the sed command indicates the use of extended regular expressions.
Good luck!!!
The asterisk * character functions an operator, so searching for the re* pattern matches every line which contained
an r character, followed by zero or more of the e character (which means it matches any line containing an r
character).
But what if you want to search for a literal asterisk * character? In basic regular expressions, the backslash \
character is as an escape character, meaning the character immediately following it will be interpreted as a literal
character, instead of an operator.
To look for a literal asterisk * character, escape it by placing a backslash \ character before the * character:
The backslash \ character behaves slightly differently depending on whether or not you are in the realm of basic or
extended regular expressions, and whether or not you are using basic or extended operators.
The Realm of Basic Regular Expressions
When you use the grep command without the -E option, you are in the realm of basic regular expressions. In this
realm, basic operators are interpreted as basic operators. And, as seen in the preceding examples, escaping a basic
operator (placing a backslash \ character in front of it) forces it to be interpreted as a literal character.
In the realm of basic regular expressions, the extended regular expression operators do not have special meaning;
they are interpreted as literal characters. As a result, the backslash \ character has the opposite effect on the
extended operators as it does on basic operators. If an extended operator is escaped in basic regular expressions, it
acts like an extended operator, instead of a literal character.
Basic Operators
Operators Literal
. [ ] ^ * ^ $
Extended Operators
Literal Operators
? + { } | ( )
When the grep -E or egrep commands are used, you are in the realm of extended regular expressions. This means
all regular expression operators (both basic and extended) are interpreted as operators.
Similarly, in extended regular expressions, the backslash \ character treats all operators the same. When an
operator is escaped in the realm of extended regular expressions, it is interpreted as a literal character, regardless of
whether it is a basic or extended operator.
Basic Operators
Operators Literal
. [ ] ^ * ^ $
Extended Operators
? + { } | ( )
Option Meaning
-i Case insensitive
-v Invert search results (logically negates criteria) - returns all lines that don't contain the specified pattern
-l List the file name of content in file matches
-r Perform a recursive search including subdirectories
-w Match whole word only
-q Quietly operate without producing output
The -i option makes the grep command case insensitive, ignoring capitalization in the expression:
The -v option inverts the search, displaying the lines that don't match the expression. The following search pattern
matches all lines which do not contain numeric characters:
Normally, the grep command will be able to match the expression, even if it is part of another word. When using
the -w option, the expression must match a complete word. This is similar to the word boundary regular expression
that was previously mentioned, however, keep in mind that \b works for many commands that support extended
regular expressions, while the -w option is specific to the grep command:
Rather than displaying every line in every file that matches the pattern, it may be preferable to see a list of the file
names that have at least one line that matches the pattern. The -l option will list file names that contain matches,
rather than the matches themselves:
Using the -r option will allow for directory searches to be recursive, rather than just the files in the current
directory. Often, this option is used in conjunction with the -l option:
Note
The errors above are generated by directories the user does not have permission to view. It is possible to eliminate
this error output from the terminal by redirecting the standard error.
8.0 Introduction
Regular expressions are patterns interpreted by certain commands. The patterns are enclosed in single quotes to
prevent the shell from misinterpreting them. Regular expressions differ from globbing, which is a function
performed by the shell and therefore not associated directly with any command.
There are two types of regular expressions – basic and extended. While the basic expressions are interpreted by
most commands, the extended expressions can be used along with an option in commands that support their
interpretation.
8.1 Step 1
The simplest of all regular expressions just use literal characters, such as alphanumeric characters. Although very
useful for the grep command, literal characters alone provide no wildcarding or special matching capabilities. For
that, you will need to use the operators that are available in basic regular expressions.
To find the occurrences of the pattern root in the /etc/passwd file, execute the following command:
8.3 Step 3
The back anchor $ operator can be used to ensure a pattern appears at the end of the line. To find the occurrences
of the pattern bash at the end of the line in the /etc/passwd file, execute the following command:
8.4 Step 4
The period operator . is used to match any character except the newline character. To match the pattern r..t (i.e.
any pattern containing r, followed by exactly two characters, and then followed by t) in the /etc/passwd file,
execute the following command:
8.5 Step 5
The list operator [] (bracket notation) that is used in file globbing has a similar implementation in regular
expressions, which is to define a list or range of literal characters that can match one character. For example, to
match the single characters 0, 1, 2, 3, and 4 between colon : characters in the /etc/passwd file, execute the
following command:
8.7 Step 7
The asterisk operator * is used to match zero or more instances of the previous value. Execute the following
command to match a colon : character, followed by zero or more numbers, followed by a colon:
The output in the example above shows how, sometimes, using the asterisk * operator results in bad matches. For
example, in the last six lines of the output, there are five lines that match two colon :: characters only. In most
cases, it may be more useful to match one or more characters rather than zero or more. However, this will require
the use of an extended regular expression (the plus + operator) which will be demonstrated later in the lab.
In this example, the pipe | character is used to send the output of one command to another. Some of the following
examples use the cat command to send the contents of a file to the grep command. Pipes will be covered in
greater detail later in the course.
The pipe | character can also be used as an alternation operator within an extended regular expression. This should
not be confused with the output chaining use of pipe in the cat | grep examples
8.8 Step 8
In basic regular expressions, putting a backslash \ character in front of another character means to match that
character literally. For example, using the \. pattern is an appropriate way to match the . character.
To display the file names of the files in the /etc directory that start with the letters rc, followed by a digit in the
range 3-6, and ending in the file extension .d, execute the following command with the regular expression rc[3-
6]*\.d:
In the command above, the asterisk * operator is used to find zero or more instances of digit characters in the range
3-6.
8.9 Step 9
The use of extended regular expressions often requires a special option to be provided to the command to recognize
them. The grep -E command is used to recognize the extended regular expression character. Extended regular
expression patterns support the basic regex operators PLUS the following additional operators:
As indicated in the table above, the plus + operator is used to match one or more instances of the previous value.
Execute the following command to match a colon : character, followed by one or more numbers, followed by
another colon : character:
tail /etc/passwd | grep -E ':[0-9]+:'
sysadmin@localhost:~$ tail /etc/passwd | grep -E ':[0-9]+:'
uuidd:x:103:105::/run/uuidd:/usr/sbin/nologin
syslog:x:104:108::/home/syslog:/usr/sbin/nologin
messagebus:x:105:109::/nonexistent:/usr/sbin/nologin
bind:x:106:111::/var/cache/bind:/usr/sbin/nologin
sshd:x:107:65534::/run/sshd:/usr/sbin/nologin
operator:x:1000:37::/root:/bin/bash
sysadmin:x:1001:1001:System Administrator,,,,:/home/sysadmin:/bin/bash
alice:x:1002:1005:Alice White:/home/alice:/bin/bash
maya:x:1003:1006:Maya Brown:/home/maya:/bin/bash
joe:x:1004:1007:Joe Green:/home/joe:/bin/bash
8.10 Step 10
The question mark ? operator is a part of extended regular expression characters and is used for making the
previous character optional in the pattern. For example, to match the two different ways to spell aging (aging or
ageing) in the dictionary file, execute the following command:
8.11 Step 11
The extended regex plus + operator matches one or more of the previous characters, instead of zero or more like
the basic regex asterisk * operator. To display the file names of the files in the /etc directory that have the literal
characters rc, followed by one or more digit characters in the range 3-6, a literal period . character, and the letter
d, use the extended regular expression rc[3-6]+\.d:
8.12 Step 12
The egrep command is an alternative to the grep –E command. To match the same extended regular expression
used in the previous example, execute the following command:
8.14 Step 14
The extended regex curly brace {} operator is used to specify the number of occurrences of the preceding character
or subpattern.
Pattern Meaning
a{0,} Zero or more a characters
a{1,} One or more a characters
a{0,1} Zero or one a characters
a{5} Five a characters
a{,5} Five or fewer a characters
a{3,5} From three to five a characters
To find all the occurrences of the letter o exactly twice in the /etc/passwd file, execute the following command:
8.15 Step 15
The grouping () operator is used to group together characters that can be targeted by matching operators like *, +,
?, or the curly braces {}. This grouping is considered to be a subpattern of the pattern.
In the example below, parentheses are used to match the gr string followed by the ey or ay subpattern repeated
zero or more times:
8.16 Step 16
The grouping () operator can also be used to refer back to what was previously matched by referring to the match
corresponding to the first set of parentheses as \1 and the second as \2. For example, to reverse the order of the last
two fields of the /etc/passwd, execute the following command:
head /etc/passwd
head /etc/passwd | sed –r 's/([a-z/]+):([a-z/]+)$/\2:\1/'
sysadmin@localhost:~$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
sysadmin@localhost:~$ head /etc/passwd | sed -r 's/([a-z/]+):([a-z/]+)$/\2:\1/'
root:x:0:0:root:/bin/bash:/root
daemon:x:1:1:daemon:/usr/sbin/nologin:/usr/sbin
bin:x:2:2:bin:/usr/sbin/nologin:/bin
sys:x:3:3:sys:/usr/sbin/nologin:/dev
sync:x:4:65534:sync:/bin/sync:/bin
games:x:5:60:games:/usr/sbin/nologin:/usr/games
man:x:6:12:man:/usr/sbin/nologin:/var/cache/man
lp:x:7:7:lp:/usr/sbin/nologin:/var/spool/lpd
mail:x:8:8:mail:/usr/sbin/nologin:/var/mail
news:x:9:9:news:/usr/sbin/nologin:/var/spool/news
In the second command above, the entire expression matches the last two fields in the /etc/passwd file. For
example, consider the first line in the /etc/passwd file.
's/([a-z/]+):([a-z/]+)$/\2:\1/'
root:x:0:0:root:/bin/bash:/root
's/([a-z/]+):([a-z/]+)$/\2:\1/'
root:x:0:0:root:/bin/bash:/root
The dollar sign $ operator, following the second subpattern is used to match the fields at the end of the line:
's/([a-z/]+):([a-z/]+)$/\2:\1/'
root:x:0:0:root:/bin/bash:/root
The list [ ] operators treat other regular expression operators as literal characters. Therefore, to ensure that the
forward slash / character in the file name is interpreted literally, it is placed inside the brackets:
's/([a-z/]+):([a-z/]+)$/\2:\1/'
root:x:0:0:root:/bin/bash:/root
Lastly, the grouping () operator is used to refer to the first set of parentheses as \1 and the second as \2, which the
sed command will use to reverse the order of the last two fields:
's/([a-z/]+):([a-z/]+)$/\2:\1/'
8.17 Step 17
Special characters, such as the asterisk * operator and the plus + operator, need to be escaped using the backslash \
character to avoid their interpretation as a special character during expression evaluation. To find all occurrences of
the pattern * in the /etc/rsyslog.conf file, execute the following command:
8.18 Step 18
An alternative to using the backslash \ character to escape every special character is to use the fgrep command,
which always treats its pattern as literal characters.
To match the * pattern in the /etc/rsyslog.conf file, execute the following command:
8.19 Step 19
Regular expressions also use the backslash \ character for designated backslash character combinations, called
backslash sequences. Backslash sequences can represent special operators or character classes. For example, \b
indicates the word boundary operator, \s indicates the whitespace character, and \w indicates a word character.
Word boundary patterns can be specified when searching for a pattern by using the backslash sequence: \b. To
replace the word is with was in the /etc/wgetrc file, execute the following command:
# You can set the default proxies for Wget to use for http, https, and ftp.
# They will override the value in the environment.
#https_proxy = http://proxy.yoyodyne.com:18023/
#http_proxy = http://proxy.yoyodyne.com:18023/
#ftp_proxy = http://proxy.yoyodyne.com:18023/
# You can customize the retrieval outlook. Valid options are default,
# binary, mega and micro.
8.20 Step 20
The grep command provides the -i option for making the pattern matching case insensitive. To match both upper
and lower case of the abid pattern in the /usr/share/dict/words file, execute the following command:
8.21 Step 21
The -v option for the grep command will cause all lines that don't match the pattern to be displayed. To display all
lines that do not contain the local pattern in the /etc/hosts file, execute the following commands:
more /etc/hosts
grep –v "local" /etc/hosts
sysadmin@localhost:~$ more /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.2 localhost
sysadmin@localhost:~$ grep -v "local" /etc/hosts
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
8.22 Step 22
In the examples seen so far, the grep command has been used for searching patterns within a single file. The grep
command can also be used for searching in multiple files. To match the test pattern in the files that match m* in
the /etc directory, execute the following command:
8.23 Step 23
To search multiple files but view only the file names instead of every matching line, execute the following
command:
8.24 Step 24
Using the -r option to the grep command will allow for directory searches to be recursive. To match the find
pattern by recursively searching all of the files in the /etc directory, and then view the matching file names,
execute the following command:
9.1 Introduction
The premier text editor for Linux and UNIX is the vi program. While there are numerous editors available for
Linux that range from the tiny editor nano to the massive emacs editor, there are several advantages to the vi
editor:
The vi editor is available on every Linux distribution in the world. This is not true of any other editor.
The vi editor can be executed both in a CLI and a GUI. While graphical editors, like gedit from the
Gnome desktop environment or kedit from K desktop environment, are easier to use, they require a GUI,
which servers won't always have running.
While new features have been added to the vi editor, the core functions have been around for decades. This
means if someone learned the vi editor in the 1970s, they could use a modern version without any problem.
While that may seem trivial today, it may not seem so trivial twenty years from now.
Consider This
The correct way to pronounce "the vi editor" is "the vee-eye editor." The letters vi are short for "visual", but it was
never pronounced "vi" by the developers, but rather the letter "v" followed by the letter "i".
The original vi editor was written by Bill Joy, the co-founder of Sun Microsystems. Since vi is part of the Single
UNIX Specification (SUS), it is required that conforming UNIX-based systems have it. Since the Linux Standards
Base (LSB) mirrors the requirements of SUS, Linux systems that conform to LSB must also include the vi editor.
In reality, most Linux systems don't include the original vi, but instead include an improved version of it, known
as vim, for vi improved. This fact may be hidden by most Linux distributions. On some distributions, the vi file
will link to vim:
sysadmin@localhost:~$ which vi
/usr/bin/vi
sysadmin@localhost:/etc/alternatives$ ls -l vi
lrwxrwxrwx 1 root root 17 Mar 2 2016 vi -> /usr/bin/vim.tiny
While on other distributions, an alias exists that will execute vim when the vi command is run:
For the most part, vim works just like vi but has additional features, including the ability to use the arrow ←↓↑→
keys for navigation.
For the topics that will be covered in this course, either vi or vim will work. For simple editing tasks, nano is
available on many systems. Users who don’t have all the keyboard commands memorized that are needed for vi
will appreciate its menu at the bottom of the editing screen and straightforward command structure.
On the other end of the spectrum is the emacs editor, an “extensible, customizable, self-documenting real-time
display editor.” For programmers and others that need sophisticated editing tools in a command line environment,
this piece of GNU software provides maximum capability and flexibility. Emacs goes far beyond just text editing
with a built-in Lisp programming interpreter and content-aware editing modes that allow syntax coloring for
different file types. It includes a packaging system for downloading and installing custom user extensions to
expand its capabilities, which include project planning, mail and news readers, debuggers, calendars, and many
other productivity tools. Emacs also has a built-in documentation system with tutorials, and full Unicode support
for almost every character set and script imaginable, so practically anyone can use it in their native language.
Essentially, it’s a power tool for power users that can perform as an infinitely customizable user interface for those
that wish to control every aspect of their systems.
This chapter will mainly focus on how to use vi and how to configure the default editor in the shell. To get started
using the vi editor, simply type the command, followed by the path name of the file to edit or create:
sysadmin@localhost:~$ vi newfile
Initially, the program starts in command mode. Command mode is used to type commands, such as those used to
move around a document, manipulate text, and access the other two modes. To return to command mode at any
time, press the Esc key.
Once some text has been added into a document, to perform actions like moving the cursor, the Esc key needs to
be pressed first to return to command mode. This seems like a lot of work, but remember that vi works in a
terminal environment where a mouse is useless.
Movement commands in vi have two aspects: a motion and an optional number prefix, which indicates how many
times to repeat that motion. The general format is as follows:
[count] motion
Motion Result
h Left one character
j Down one line
k Up one line
l Right one character
w One word forward
b One word back
^ Beginning of the line
$ End of the line
Note
Since the upgrade to vim, it is also possible to use the arrow ←↓↑→ keys instead of hjkl respectively.
These motions can be prefixed with a number to indicate how many times to perform the movement. For example,
the 5h command would move the cursor five characters to the left and 3w would move the cursor three words to the
right.
To move the cursor to a specific line number, type that line number followed by the G character. For example, to
get to the fifth line of the file, type the 5G command. The 1G or gg commands can be used to go to the first line of
the file, while a single G will take you to the last line. To find out which line the cursor is currently on, use Ctrl+G.
Standard Vi Meaning
cut d delete
copy y yank
paste P | p put
The motions learned from the previous page are used to specify where the action is to take place, always beginning
with the present cursor location. Either of the following general formats for action commands is acceptable:
Delete
Delete removes the indicated text from the page and saves it into the buffer, the buffer being the equivalent of the
"clipboard" used in Windows or Mac OSX. The following table provides some common usage examples:
Action Result
Change is very similar to delete, where the text is removed and saved into the buffer. However, when using
change, the program is switched to insert mode to allow immediate changes to the text. The following table
provides some common usage examples:
Action Result
Yank
Yank places content into the buffer without deleting it. The following table provides some common usage
examples:
Action Result
Put
Put places the text saved in the buffer either before or after the cursor position. Notice that these are the only two
options as put does not use the motions like the previous action commands.
Action Result
Searching in vi
Another standard function that word processors offer is the find function. Often, people use Ctrl+F or look under
the edit menu. The vi program uses search. Search is more powerful than find because it supports both literal text
patterns and regular expressions.
To search forward from the current position of the cursor, use the slash / character to start the search, type a search
term, and then press the Enter key to begin the search. The cursor will move to the first match that is found.
To proceed to the next match using the same pattern, press the N key. To go back to a previous match, press the
Shift+N key combination. If the end or the beginning of the document is reached, it will automatically wrap around
to the other side of the document.
To start searching backward from the cursor position, start by typing ?, then type the pattern to search for matches
and press the Enter key.
Action Result
Input Purpose
a Enter insert mode right after the cursor
A Enter insert mode at the end of the line
i Enter insert mode right before the cursor
I Enter insert mode at the beginning of the line
o Enter insert mode on a blank line after the cursor
O Enter insert mode on a blank line before the cursor
9.5 Ex Mode
Originally, the vi editor was called the ex editor. The name vi was the abbreviation of the visual command inside
the ex editor that switched the editor to "visual" mode.
In the original normal mode, the ex editor only allowed users to see and modify one line at a time. In the visual
mode, users could see as much of the document that will fit on the screen. Since most users preferred the visual
mode to the line editing mode, the ex program file was linked to a vi file, so that users could start ex directly in
visual mode when they ran the vi link.
Eventually, the actual program file was renamed vi, and the ex editor became a link that pointed to the vi editor.
When the ex mode of the vi editor is being used, it is possible to view or change settings, as well as carry out file-
related commands like opening, saving, or aborting changes to a file. In order to get to the ex mode, type a colon :
character in command mode. The following table lists some common actions performed in ex mode:
Input Purpose
:w Write the current file to the filesystem
:w filename Save a copy of the current file as filename
:w! Force writing to the current file
:1 Go to line number 1 or whatever number is given
:e filename Open filename
:q Quit if no changes made to file
:q! Quit without saving changes to file
A quick analysis of the table above reveals if an exclamation mark ! character is added to a command, then the
command attempts to force the operation. For example, imagine you make changes to a file in the vi editor and
then try to quit with the :q command, only to discover that the "quit" command fails. The vi editor doesn't want to
quit without saving the changes you made to a file, but you can force it to quit with the :q! command.
Note
While it may seem impossible, the vi editor can save changes to a read-only file. The command :w! will try to
write to a file, even if it is read-only, by attempting to change the permissions on the file, perform the write to the
file, and then change the permissions back to what they were originally.
This means that the root user can make changes to almost any file in the vi editor, regardless of the permissions on
the file. However, ordinary users will only be able to force writing to the files that they own. Using vi doesn't
change the fact that regular users can't modify the permissions on files that they do not own.
Although the ex mode offers several ways to save and quit, there's also the ZZ command that is available in
command mode; this is the equivalent of :wq. There are many more overlapping functions between ex mode and
command mode. For example, ex mode can be used to navigate to any line in the document by typing the colon :
character followed by the line number, while the G command can be used in command mode as previously
demonstrated.
Note
We encourage you to use the terminal as often as possible while reading the chapter content. However, there are
places where the output will differ, or you will not be able to reproduce the examples altogether. The examples in
this section were designed to introduce a concept, whereas the labs that follow the chapters were designed to take
you through the material step-by-step, working with the CLI to give you practical experience.
Typing the /usr/bin/editor command will invoke the standard editor, which in the case of our Ubuntu virtual
machine, is VIM.
sysadmin@localhost:~$ /usr/bin/editor
~
~
~ VIM - Vi IMproved
~
~ version 8.0.1453
~ by Bram Moolenaar et al.
~ Modified by [email protected]
~ Vim is open source and freely distributable
~
~ Help poor children in Uganda!
~ type :help iccf<Enter> for information
~
~ type :q<Enter> to exit
~ type :help<Enter> or <F1> for on-line help
~ type :help version8<Enter> for version info
~
There are a number of ways to change the standard editor, but the easiest one on Ubuntu 18.04 is to use the
update-alternatives command with the --config editor option. Note that this command requires
administrative access. After executing the command, type the selection number of the editor:
Note
The sudo command allows a user to execute a command as another user without creating a new shell. To execute a
command with administrative privileges, use it as an argument to the sudo command. The sudo command assumes
by default the root user account should be used to execute commands.
Using sudo in our virtual machine environment requires the root password netlab123. As a security measure, the
password will not be visible as it is typed.
Now the /usr/bin/editor command will invoke the nano editor as the standard editor instead of the vi editor:
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
^X Exit ^R Read File ^\ Replace ^U Uncut Text^T To Spell ^_ Go To Line
Another way of changing the standard editor is by editing the .bashrc file. Since the .bashrc file only loads when
a new shell is created, it still shows vi as the standard editor; we can confirm this by viewing the $EDITOR variable:
To edit the .bashrc file using nano , you would type the following command:
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
^X Exit ^R Read File ^\ Replace ^U Uncut Text^T To Spell ^_ Go To Line
In order to see these changes, a new shell will need to be opened using the bash command in order to reload the
.bashrc file. In the new shell, displaying the $EDITOR variable can be used to confirm the changes:
Of course, the vi editor will still be available at the command line. To continue to using vi after changing the
standard editor, simply type the vi command followed by the path and name of the file to edit or the file you wish
to create:
sysadmin@localhost:~$ vi newfile
Key Terms
/, ?
This is used to search for text while in command mode. the / is used to start searching. Enter a key term and
press enter to begin searching the file for the text entered. If the user would like to search backwards in the
document, a ? can be used instead of the /.
Section 9.3
EDITOR
Section 9.6
ZZ, :w!, :q!
These keys are used to exit the vi editor from command mode. ZZ is used to save and quit the file. It must
be done for each file. :w! will force the writing of the current file. :q! will exit the editor without saving
changes to the current file.
Section 9.5
d, p, y, dd, yy
These are used to copy, replace and paste text when in command mode. d is used to cut one alphabetic
word, where as dd is used to cut an entire line of text. y is used to copy one one alphabetic word, where as
yy is used to copy and entire line at a time. If a number precedes either dd or yy, this will copy that number
of lines. For example if 3dd is typed this will cut 3 lines at a time.
Section 9.3
h,j,k,l
These keys are used for basic cursor movement in vi when in command mode. h moves left one character, j
moves down one line, k moves up one line, and l moves right one character.
Section 9.2
i, o, a
i, o, and a are used to enter insert mode from command mode. i will allow a user to start inserting text at the
current location of the cursor. o will allow a user to start inserting text a line below the current location of
the cursor, and a will allow a user to insert text one postion after the current location of the cursor.
Section 9.4
vi
A screen-oriented text editor originally created for Unix operating systems. vi is also known as a modal
editor in which the user must switch modes to create, edit, and search text in a file.
Section 9.1 | Section 9.2 | Section 9.3 | Section 9.4 | Section 9.5
LAB 09
9.0 Introduction
The vi editor is available on every Linux distribution in the world and can execute both in a CLI as well as a GUI
interface. Most Linux systems have an alias for vi that executes the vim command, an improved version of the
original vi editor. The commands provided in this lab work both in vi and vim.
The vi editor works in three modes: command mode, insert mode, and ex mode. The command mode is used to
type commands to navigate a document, the insert mode is used to enter new text into a document, and the ex mode
is used for file operations such as saving and quitting.
While example terminal boxes are useful for most of the labs in this course, because of the nature of vi (most
commands when typed are not visible on the screen), example terminal boxes will not be useful in all steps of this
lab. As a result, some of the steps in this lab do not include example terminal boxes.
9.1 Step 1
To create a new file called edit.txt using vi, execute the following command:
vi edit.txt
sysadmin@localhost:~$ vi edit.txt
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"edit.txt" [New File]
9.2 Step 2
To exit the vi editor, type in the following:
:q!
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:q!
9.3 Step 3
To make this lab more effective, you will create a file with the output of the ls command. This provides you with a
file that already contains data (rather than having you create a file from scratch). Execute the following command
to generate a file that you can work with:
9.4 Step 4
To open the file data.txt for editing, execute the following command:
vi data.txt
The vi editor will open the file, data.txt, for editing as shown below:
/usr:
total 32
drwxr-xr-x 1 root root 4096 Apr 3 22:24 bin
drwxr-xr-x 2 root root 4096 Apr 24 2018 games
drwxr-xr-x 1 root root 4096 Mar 22 17:41 include
drwxr-xr-x 1 root root 4096 Mar 29 17:36 lib
drwxr-xr-x 1 root root 4096 Mar 7 21:00 local
drwxr-xr-x 1 root root 4096 Mar 29 17:36 sbin
drwxr-xr-x 1 root root 4096 Mar 29 17:36 share
drwxr-xr-x 2 root root 4096 Apr 24 2018 src
/usr/bin:
total 42800
-rwxr-xr-x 1 root root 51384 Jan 18 2018 [
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-enabled
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-exec
-rwxr-xr-x 1 root root 22608 Oct 15 20:29 addpart
lrwxrwxrwx 1 root root 6 Aug 4 2018 apropos -> whatis
-rwxr-xr-x 1 root root 14424 Mar 11 09:34 apt
-rwxr-xr-x 1 root root 80032 Mar 11 09:34 apt-cache
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-cdrom
-rwxr-xr-x 1 root root 22616 Mar 11 09:34 apt-config
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-extracttemplates
"data.txt" 27916 lines, 1324425 characters
9.5 Step 5
To switch to the insert mode and enter data on the first line where the cursor is positioned, type i. Then, type
Start inserting now:
i
Start inserting now
Start inserting now/usr:
total 32
drwxr-xr-x 1 root root 4096 Apr 3 22:24 bin
drwxr-xr-x 2 root root 4096 Apr 24 2018 games
drwxr-xr-x 1 root root 4096 Mar 22 17:41 include
drwxr-xr-x 1 root root 4096 Mar 29 17:36 lib
drwxr-xr-x 1 root root 4096 Mar 7 21:00 local
drwxr-xr-x 1 root root 4096 Mar 29 17:36 sbin
drwxr-xr-x 1 root root 4096 Mar 29 17:36 share
drwxr-xr-x 2 root root 4096 Apr 24 2018 src
/usr/bin:
total 42800
-rwxr-xr-x 1 root root 51384 Jan 18 2018 [
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-enabled
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-exec
-rwxr-xr-x 1 root root 22608 Oct 15 20:29 addpart
lrwxrwxrwx 1 root root 6 Aug 4 2018 apropos -> whatis
-rwxr-xr-x 1 root root 14424 Mar 11 09:34 apt
-rwxr-xr-x 1 root root 80032 Mar 11 09:34 apt-cache
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-cdrom
-rwxr-xr-x 1 root root 22616 Mar 11 09:34 apt-config
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-extracttemplates
"data.txt" 27916 lines, 1324425 characters
9.6 Step 6
Press the Escape key to return to the command mode, then press the Down Arrow ↓ key six times to move down
six lines. Press i to insert text at the beginning of the line and type Inserting on line 7:
ESC
Down Arrow 6x
I
Inserting on line 7
Start inserting now/usr:
total 32
drwxr-xr-x 1 root root 4096 Apr 3 22:24 bin
drwxr-xr-x 2 root root 4096 Apr 24 2018 games
drwxr-xr-x 1 root root 4096 Mar 22 17:41 include
drwxr-xr-x 1 root root 4096 Mar 29 17:36 lib
Inserting on line 7drwxr-xr-x 1 root root 4096 Mar 7 21:00 local
drwxr-xr-x 1 root root 4096 Mar 29 17:36 sbin
drwxr-xr-x 1 root root 4096 Mar 29 17:36 share
drwxr-xr-x 2 root root 4096 Apr 24 2018 src
/usr/bin:
total 42800
-rwxr-xr-x 1 root root 51384 Jan 18 2018 [
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-enabled
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-exec
-rwxr-xr-x 1 root root 22608 Oct 15 20:29 addpart
lrwxrwxrwx 1 root root 6 Aug 4 2018 apropos -> whatis
-rwxr-xr-x 1 root root 14424 Mar 11 09:34 apt
-rwxr-xr-x 1 root root 80032 Mar 11 09:34 apt-cache
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-cdrom
-rwxr-xr-x 1 root root 22616 Mar 11 09:34 apt-config
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-extracttemplates
"data.txt" 27916 lines, 1324425 characters
9.7 Step 7
Press the Escape key to return to the command mode. Type dd to delete the current line:
ESC
dd
9.8 Step 8
Move to the end of the document by typing a G character:
G
-rw-r--r-- 1 root root 3105 Jan 28 2018 _busctl
-rw-r--r-- 1 root root 2270 Jan 28 2018 _hostnamectl
-rw-r--r-- 1 root root 5975 Jan 28 2018 _journalctl
-rw-r--r-- 1 root root 651 Jan 28 2018 _kernel-install
-rw-r--r-- 1 root root 3585 Jan 28 2018 _localectl
-rw-r--r-- 1 root root 5804 Jan 28 2018 _loginctl
-rw-r--r-- 1 root root 1243 Jan 28 2018 _networkctl
-rw-r--r-- 1 root root 116 Jan 28 2018 _sd_hosts_or_user_at_host
-rw-r--r-- 1 root root 279 Jan 28 2018 _sd_outputmodes
-rw-r--r-- 1 root root 255 Jan 28 2018 _sd_unit_files
-rw-r--r-- 1 root root 15069 Feb 28 21:03 _systemctl
-rw-r--r-- 1 root root 3839 Jan 28 2018 _systemd
-rw-r--r-- 1 root root 2912 Jan 28 2018 _systemd-analyze
-rw-r--r-- 1 root root 564 Jan 28 2018 _systemd-delta
-rw-r--r-- 1 root root 1061 Jan 28 2018 _systemd-inhibit
-rw-r--r-- 1 root root 2378 Jan 28 2018 _systemd-resolve
-rw-r--r-- 1 root root 3136 Jan 28 2018 _systemd-run
-rw-r--r-- 1 root root 736 Jan 28 2018 _systemd-tmpfiles
-rw-r--r-- 1 root root 2042 Jan 28 2018 _timedatectl
-rw-r--r-- 1 root root 5997 Jan 28 2018 _udevadm
/usr/src:
total 0
9.9 Step 9
To add a new line at the end of the document, type an o character and then type the end. Press the Escape key to
return to the command mode:
o
the end
ESC
-rw-r--r-- 1 root root 2270 Jan 28 2018 _hostnamectl
-rw-r--r-- 1 root root 5975 Jan 28 2018 _journalctl
-rw-r--r-- 1 root root 651 Jan 28 2018 _kernel-install
-rw-r--r-- 1 root root 3585 Jan 28 2018 _localectl
-rw-r--r-- 1 root root 5804 Jan 28 2018 _loginctl
-rw-r--r-- 1 root root 1243 Jan 28 2018 _networkctl
-rw-r--r-- 1 root root 116 Jan 28 2018 _sd_hosts_or_user_at_host
-rw-r--r-- 1 root root 279 Jan 28 2018 _sd_outputmodes
-rw-r--r-- 1 root root 255 Jan 28 2018 _sd_unit_files
-rw-r--r-- 1 root root 15069 Feb 28 21:03 _systemctl
-rw-r--r-- 1 root root 3839 Jan 28 2018 _systemd
-rw-r--r-- 1 root root 2912 Jan 28 2018 _systemd-analyze
-rw-r--r-- 1 root root 564 Jan 28 2018 _systemd-delta
-rw-r--r-- 1 root root 1061 Jan 28 2018 _systemd-inhibit
-rw-r--r-- 1 root root 2378 Jan 28 2018 _systemd-resolve
-rw-r--r-- 1 root root 3136 Jan 28 2018 _systemd-run
-rw-r--r-- 1 root root 736 Jan 28 2018 _systemd-tmpfiles
-rw-r--r-- 1 root root 2042 Jan 28 2018 _timedatectl
-rw-r--r-- 1 root root 5997 Jan 28 2018 _udevadm
/usr/src:
total 0
the end
9.10 Step 10
Go to line #1 by typing 1G. Search for local by typing /local and press Enter. Type the letter n to move to the
second occurrence of local:
1G
/local
Enter
n
-rwxr-xr-x 1 root root 92744 Aug 4 2018 lexgrog
-rwxr-xr-x 1 root root 15775 Nov 19 15:54 libnetcfg
-rwxr-xr-x 1 root root 30904 Jan 18 2018 link
-rwxr-xr-x 1 root root 4090 Jun 21 2016 linux-check-removal
-rwxr-xr-x 1 root root 6320 Jun 5 2016 linux-update-symlinks
-rwxr-xr-x 1 root root 2696 Sep 17 2016 linux-version
lrwxrwxrwx 1 root root 7 Oct 15 2018 linux32 -> setarch
lrwxrwxrwx 1 root root 7 Oct 15 2018 linux64 -> setarch
-rwxr-xr-x 1 root root 22888 Feb 26 2018 lnstat
lrwxrwxrwx 1 root root 13 Mar 22 17:41 loadkeys -> /bin/loadkeys
-rwxr-xr-x 1 root root 26760 Jan 22 2018 loadunimap
-rwxr-xr-x 1 root root 50592 Apr 16 2018 locale
-rwxr-xr-x 1 root root 10240 Feb 5 07:32 locale-check
-rwxr-xr-x 1 root root 22600 Feb 28 21:03 localectl
-rwxr-xr-x 1 root root 338744 Apr 16 2018 localedef
lrwxrwxrwx 1 root root 24 Mar 22 17:41 locate -> /etc/alternatives/locate
-rwxr-xr-x 1 root root 47792 Oct 15 2018 logger
-rwxr-xr-x 1 root root 30904 Jan 18 2018 logname
-rwxr-xr-x 1 root root 10472 Jan 17 2018 look
-rwxr-xr-x 1 root root 2885 Jan 17 2018 lorder
-rwxr-xr-x 1 root root 10240 Jan 24 23:11 lsattr
-rwxr-xr-x 1 root root 3638 Aug 7 2017 lsb_release
/local
9.11 Step 11
Delete the current word by typing dw:
dw
-rwxr-xr-x 1 root root 92744 Aug 4 2018 lexgrog
-rwxr-xr-x 1 root root 15775 Nov 19 15:54 libnetcfg
-rwxr-xr-x 1 root root 30904 Jan 18 2018 link
-rwxr-xr-x 1 root root 4090 Jun 21 2016 linux-check-removal
-rwxr-xr-x 1 root root 6320 Jun 5 2016 linux-update-symlinks
-rwxr-xr-x 1 root root 2696 Sep 17 2016 linux-version
lrwxrwxrwx 1 root root 7 Oct 15 2018 linux32 -> setarch
lrwxrwxrwx 1 root root 7 Oct 15 2018 linux64 -> setarch
-rwxr-xr-x 1 root root 22888 Feb 26 2018 lnstat
lrwxrwxrwx 1 root root 13 Mar 22 17:41 loadkeys -> /bin/loadkeys
-rwxr-xr-x 1 root root 26760 Jan 22 2018 loadunimap
-rwxr-xr-x 1 root root 50592 Apr 16 2018 locale
-rwxr-xr-x 1 root root 10240 Feb 5 07:32 -check
-rwxr-xr-x 1 root root 22600 Feb 28 21:03 localectl
-rwxr-xr-x 1 root root 338744 Apr 16 2018 localedef
lrwxrwxrwx 1 root root 24 Mar 22 17:41 locate -> /etc/alternatives/locate
-rwxr-xr-x 1 root root 47792 Oct 15 2018 logger
-rwxr-xr-x 1 root root 30904 Jan 18 2018 logname
-rwxr-xr-x 1 root root 10472 Jan 17 2018 look
-rwxr-xr-x 1 root root 2885 Jan 17 2018 lorder
-rwxr-xr-x 1 root root 10240 Jan 24 23:11 lsattr
-rwxr-xr-x 1 root root 3638 Aug 7 2017 lsb_release
/local
9.12 Step 12
Type 1G to return to the first line and then 5dd to delete five lines:
1G
5dd
drwxr-xr-x 1 root root 4096 Apr 17 20:09 lib
drwxr-xr-x 1 root root 4096 Apr 17 20:09 sbin
drwxr-xr-x 1 root root 4096 Apr 17 20:09 share
drwxr-xr-x 2 root root 4096 Apr 24 2018 src
/usr/bin:
total 57052
-rwxr-xr-x 1 root root 51384 Jan 18 2018 [
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-enabled
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-exec
-rwxr-xr-x 1 root root 22608 Oct 15 2018 addpart
lrwxrwxrwx 1 root root 26 Sep 12 2018 addr2line -> x86_64-linux-gnu-add
r2line
lrwxrwxrwx 1 root root 6 Aug 4 2018 apropos -> whatis
-rwxr-xr-x 1 root root 14424 Mar 11 09:34 apt
-rwxr-xr-x 1 root root 80032 Mar 11 09:34 apt-cache
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-cdrom
-rwxr-xr-x 1 root root 22616 Mar 11 09:34 apt-config
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-extracttemplates
-rwxr-xr-x 1 root root 247968 Mar 11 09:34 apt-ftparchive
-rwxr-xr-x 1 root root 43168 Mar 11 09:34 apt-get
-rwxr-xr-x 1 root root 27391 Mar 11 09:34 apt-key
-rwxr-xr-x 1 root root 43168 Mar 11 09:34 apt-mark
5 fewer lines
9.13 Step 13
Copy three lines by typing 3yy:
3yy
drwxr-xr-x 1 root root 4096 Apr 17 20:09 lib
drwxr-xr-x 1 root root 4096 Apr 17 20:09 sbin
drwxr-xr-x 1 root root 4096 Apr 17 20:09 share
drwxr-xr-x 2 root root 4096 Apr 24 2018 src
/usr/bin:
total 57052
-rwxr-xr-x 1 root root 51384 Jan 18 2018 [
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-enabled
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-exec
-rwxr-xr-x 1 root root 22608 Oct 15 2018 addpart
lrwxrwxrwx 1 root root 26 Sep 12 2018 addr2line -> x86_64-linux-gnu-add
r2line
lrwxrwxrwx 1 root root 6 Aug 4 2018 apropos -> whatis
-rwxr-xr-x 1 root root 14424 Mar 11 09:34 apt
-rwxr-xr-x 1 root root 80032 Mar 11 09:34 apt-cache
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-cdrom
-rwxr-xr-x 1 root root 22616 Mar 11 09:34 apt-config
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-extracttemplates
-rwxr-xr-x 1 root root 247968 Mar 11 09:34 apt-ftparchive
-rwxr-xr-x 1 root root 43168 Mar 11 09:34 apt-get
-rwxr-xr-x 1 root root 27391 Mar 11 09:34 apt-key
-rwxr-xr-x 1 root root 43168 Mar 11 09:34 apt-mark
3 lines yanked
Then, move to the end of the document by typing G. Paste the three lines by typing p:
G
p
-rw-r--r-- 1 root root 3585 Jan 28 2018 _localectl
-rw-r--r-- 1 root root 5804 Jan 28 2018 _loginctl
-rw-r--r-- 1 root root 1243 Jan 28 2018 _networkctl
-rw-r--r-- 1 root root 116 Jan 28 2018 _sd_hosts_or_user_at_host
-rw-r--r-- 1 root root 279 Jan 28 2018 _sd_outputmodes
-rw-r--r-- 1 root root 255 Jan 28 2018 _sd_unit_files
-rw-r--r-- 1 root root 15069 Feb 28 21:03 _systemctl
-rw-r--r-- 1 root root 3839 Jan 28 2018 _systemd
-rw-r--r-- 1 root root 2912 Jan 28 2018 _systemd-analyze
-rw-r--r-- 1 root root 564 Jan 28 2018 _systemd-delta
-rw-r--r-- 1 root root 1061 Jan 28 2018 _systemd-inhibit
-rw-r--r-- 1 root root 2378 Jan 28 2018 _systemd-resolve
-rw-r--r-- 1 root root 3136 Jan 28 2018 _systemd-run
-rw-r--r-- 1 root root 736 Jan 28 2018 _systemd-tmpfiles
-rw-r--r-- 1 root root 2042 Jan 28 2018 _timedatectl
-rw-r--r-- 1 root root 5997 Jan 28 2018 _udevadm
/usr/src:
total 0
the end
drwxr-xr-x 1 root root 4096 Apr 17 20:09 lib
3 more lines
9.14 Step 14
Undo the paste command by typing the letter u:
u
-rw-r--r-- 1 root root 3585 Jan 28 2018 _localectl
-rw-r--r-- 1 root root 5804 Jan 28 2018 _loginctl
-rw-r--r-- 1 root root 1243 Jan 28 2018 _networkctl
-rw-r--r-- 1 root root 116 Jan 28 2018 _sd_hosts_or_user_at_host
-rw-r--r-- 1 root root 279 Jan 28 2018 _sd_outputmodes
-rw-r--r-- 1 root root 255 Jan 28 2018 _sd_unit_files
-rw-r--r-- 1 root root 15069 Feb 28 21:03 _systemctl
-rw-r--r-- 1 root root 3839 Jan 28 2018 _systemd
-rw-r--r-- 1 root root 2912 Jan 28 2018 _systemd-analyze
-rw-r--r-- 1 root root 564 Jan 28 2018 _systemd-delta
-rw-r--r-- 1 root root 1061 Jan 28 2018 _systemd-inhibit
-rw-r--r-- 1 root root 2378 Jan 28 2018 _systemd-resolve
-rw-r--r-- 1 root root 3136 Jan 28 2018 _systemd-run
-rw-r--r-- 1 root root 736 Jan 28 2018 _systemd-tmpfiles
-rw-r--r-- 1 root root 2042 Jan 28 2018 _timedatectl
-rw-r--r-- 1 root root 5997 Jan 28 2018 _udevadm
/usr/src:
total 0
the end
~
3 fewer lines; before #7 26 seconds ago
9.15 Step 15
Type :q and then press the Enter key to attempt to quit the document:
:q
Enter
-rw-r--r-- 1 root root 5975 Jan 28 2018 _journalctl
-rw-r--r-- 1 root root 651 Jan 28 2018 _kernel-install
-rw-r--r-- 1 root root 3585 Jan 28 2018 _localectl
-rw-r--r-- 1 root root 5804 Jan 28 2018 _loginctl
-rw-r--r-- 1 root root 1243 Jan 28 2018 _networkctl
-rw-r--r-- 1 root root 116 Jan 28 2018 _sd_hosts_or_user_at_host
-rw-r--r-- 1 root root 279 Jan 28 2018 _sd_outputmodes
-rw-r--r-- 1 root root 255 Jan 28 2018 _sd_unit_files
-rw-r--r-- 1 root root 15069 Feb 28 21:03 _systemctl
-rw-r--r-- 1 root root 3839 Jan 28 2018 _systemd
-rw-r--r-- 1 root root 2912 Jan 28 2018 _systemd-analyze
-rw-r--r-- 1 root root 564 Jan 28 2018 _systemd-delta
-rw-r--r-- 1 root root 1061 Jan 28 2018 _systemd-inhibit
-rw-r--r-- 1 root root 2378 Jan 28 2018 _systemd-resolve
-rw-r--r-- 1 root root 3136 Jan 28 2018 _systemd-run
-rw-r--r-- 1 root root 736 Jan 28 2018 _systemd-tmpfiles
-rw-r--r-- 1 root root 2042 Jan 28 2018 _timedatectl
-rw-r--r-- 1 root root 5997 Jan 28 2018 _udevadm
/usr/src:
total 0
the end
~
E37: No write since last change (add ! to override)
9.16 Step 16
The previous command :q failed because you have not saved the changes that you made in the file. To quit without
saving, type :q! and press the Enter key to quit the document and not save any changes:
:q!
Enter
:q!
sysadmin@localhost:~$
9.17 Step 17
Open the data.txt file by executing the following command:
vi data.txt
sysadmin@localhost:~$ vi data.txt
9.18 Step 18
Type 1G to return to the first line of the file (if needed). Change (replace) the first line of the document by typing
cc and then Output:.
1G
cc
Output:
Output:
total 32
drwxr-xr-x 1 root root 4096 Apr 24 16:25 bin
drwxr-xr-x 2 root root 4096 Apr 24 2018 games
drwxr-xr-x 1 root root 4096 Apr 17 20:09 include
drwxr-xr-x 1 root root 4096 Apr 17 20:09 lib
drwxr-xr-x 1 root root 4096 Mar 7 21:00 local
drwxr-xr-x 1 root root 4096 Apr 17 20:09 sbin
drwxr-xr-x 1 root root 4096 Apr 17 20:09 share
drwxr-xr-x 2 root root 4096 Apr 24 2018 src
/usr/bin:
total 57052
-rwxr-xr-x 1 root root 51384 Jan 18 2018 [
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-enabled
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-exec
-rwxr-xr-x 1 root root 22608 Oct 15 2018 addpart
lrwxrwxrwx 1 root root 26 Sep 12 2018 addr2line -> x86_64-linux-gnu-add
r2line
lrwxrwxrwx 1 root root 6 Aug 4 2018 apropos -> whatis
-rwxr-xr-x 1 root root 14424 Mar 11 09:34 apt
-rwxr-xr-x 1 root root 80032 Mar 11 09:34 apt-cache
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-cdrom
"data.txt" 32447 lines, 1589342 characters
9.19 Step 19
Press the Escape key to return to the command mode. Then, type :w to save the changes:
ESC
:w
:w
Output:
total 32
drwxr-xr-x 1 root root 4096 Apr 24 16:25 bin
drwxr-xr-x 2 root root 4096 Apr 24 2018 games
drwxr-xr-x 1 root root 4096 Apr 17 20:09 include
drwxr-xr-x 1 root root 4096 Apr 17 20:09 lib
drwxr-xr-x 1 root root 4096 Mar 7 21:00 local
drwxr-xr-x 1 root root 4096 Apr 17 20:09 sbin
drwxr-xr-x 1 root root 4096 Apr 17 20:09 share
drwxr-xr-x 2 root root 4096 Apr 24 2018 src
/usr/bin:
total 57052
-rwxr-xr-x 1 root root 51384 Jan 18 2018 [
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-enabled
-rwxr-xr-x 1 root root 22696 Sep 27 2018 aa-exec
-rwxr-xr-x 1 root root 22608 Oct 15 2018 addpart
lrwxrwxrwx 1 root root 26 Sep 12 2018 addr2line -> x86_64-linux-gnu-add
r2line
lrwxrwxrwx 1 root root 6 Aug 4 2018 apropos -> whatis
-rwxr-xr-x 1 root root 14424 Mar 11 09:34 apt
-rwxr-xr-x 1 root root 80032 Mar 11 09:34 apt-cache
-rwxr-xr-x 1 root root 22688 Mar 11 09:34 apt-cdrom
"data.txt" 32447 lines, 1589344 characters written
9.20 Step 20
Type :q to quit the vi editor:
:q
Output Omitted...
:q
You can verify that your updates were written to the file by entering the following command, which will display
the first 10 lines of the file.
head data.txt
sysadmin@localhost:~$ head data.txt
Output:
total 32
drwxr-xr-x 1 root root 4096 Apr 3 22:24 bin
drwxr-xr-x 2 root root 4096 Apr 24 2018 games
drwxr-xr-x 1 root root 4096 Mar 22 17:41 include
drwxr-xr-x 1 root root 4096 Mar 29 17:36 lib
drwxr-xr-x 1 root root 4096 Mar 7 21:00 local
drwxr-xr-x 1 root root 4096 Mar 29 17:36 sbin
drwxr-xr-x 1 root root 4096 Mar 29 17:36 share
drwxr-xr-x 2 root root 4096 Apr 24 2018 src
9.21 Step 21
The standard editor is the program that the system calls by default for editing text files. It’s possible to change the
standard editor by customizing the system you are logged into. Regular users can change this default for their own
login but changing it for all users requires root privileges. First, let's determine what the standard editor is on our
system by typing the following command:
echo $EDITOR
sysadmin@localhost:~$ echo $EDITOR
vi
9.22 Step 22
To use nano to view and edit the .bashrc file, use the following command:
nano .bashrc
sysadmin@localhost:~$ nano .bashrc
The .bashrc file contains the environment variables for the standard editor at the bottom of the file. Unlike vi ,
nano allows you to simply use the Arrow Keys ←↑→↓ to navigate to any place in the document. Use the Down
Arrow ↓ key to scroll to the bottom of the file:
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
^X Exit ^R Read File ^\ Replace ^U Uncut Text^T To Spell ^_ Go To Line
9.23 Step 23
Press the Down Arrow ↓ key (or Up Arrow ↑ key if you scrolled too far down) until you get to # set the
editor and visual envs to vim, then use the Arrow Keys ←↑→↓ to replace vi with nano:
nano .bashrc
GNU nano 2.9.3 .bashrc Modified
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
^X Exit ^R Read File ^\ Replace ^U Uncut Text^T To Spell ^_ Go To Line
9.24 Step 24
Press Ctrl+X to exit and type y to save:
Ctrl+x
y
GNU nano 2.9.3 .bashrc Modified
Press Enter:
Enter
GNU nano 2.9.3 .bashrc Modified
bash
sysadmin@localhost:~$ bash
sysadmin@localhost:~$
echo $EDITOR
sysadmin@localhost:~$ echo $EDITOR
nano
9.26 Step 26
Of course, vi is still available at the command line. To get started using vi, simply type the command followed by
the path and name of the file to edit or the file you wish to create:
vi newfile
sysadmin@localhost:~$ vi newfile
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"newfile" [New File]
NDG Introduction to Linux I - Chapter 10: Standard Text
Streams and Redirection
10.1 Introduction
One of the key points in the UNIX philosophy was that all CLI commands should accept text as input and produce
text as output. As this concept was applied to the development of UNIX (and later Linux), commands were
developed to accept text as input, perform some kind of operation on the text and then produce text as output.
Commands that read in text as input, alter that text in some way, and then produce text as output are sometimes
known as filters.
In order to be able to apply filter commands and work with text streams, it is helpful to understand a few forms of
redirection that can be used with most commands: pipelines, standard output redirection, error output redirection,
and input redirection.
It is possible to redirect standard out from a command so it will go to a file instead of the terminal. Standard output
redirection is achieved by following a command with the greater-than > character and a destination file. For
example, the ls ~ command will list the files in the home directory. To save a list of the files in the home
directory, you must direct the output into a text file. To create the /tmp/home.txt file:
Redirecting output using a single greater-than > character will create a new file, or overwrite the contents of an
existing file with the same name. Redirecting standard output with two greater-than >> characters will also create a
new file if it does not exist. The difference is that when using the >> characters, the output of the command will be
appended to the end of a file if it does already exist. For example, to append to the file that was created by the
previous command, execute the following:
sysadmin@localhost:~$ date >> /tmp/home.txt
There is a number associated with the standard output file descriptor (the > character): the number 1 (one).
However, since standard output is the most commonly redirected command output, the number can be omitted.
Technically, the commands should be executed as shown below:
If you tried to execute the ls /junk command, then the command would produce standard error messages because
the /junk directory does not exist.
sysadmin@localhost:~$ ls /junk
ls: cannot access /junk: No such file or directory
Because this output goes to standard error, the greater-than > character alone will not successfully redirect it, and
the output of the command will still be sent to the terminal:
To redirect these error messages, you must use the correct file descriptor, which for standard error is the number 2.
Execute the following, and the error will be redirected into the /tmp/ls.err file:
Just like standard output, the use of a single > character for redirection will either create the file if it doesn’t exist or
clobber (overwrite) an existing file's contents. To prevent clobbering an existing file when redirecting standard
error, use the double >> characters after the number 2 to append instead:
These two different outputs can be redirected into two separate files by using the following syntax:
The cat command can be used to verify the success of the redirection above:
Sometimes it isn't useful to have the error messages displayed in the terminal or stored in a file. To discard these
error messages, use the /dev/null file.
The /dev/null file is like a trash can, where anything sent to it disappears from the system; it's sometimes called
the bit bucket or black hole. Any type of output can be redirected to the /dev/null file; most commonly users will
redirect standard error to this file, rather than standard output.
The syntax to use the /dev/null file is the same as it is for redirecting to a regular file:
sysadmin@localhost:~$ find /etc -name passw 2> /dev/null
/etc/pam.d/passwd
/etc/passwd
What if you wanted all output (standard error and standard out) sent to one file? There are two techniques to
redirect both standard error and standard out:
Both of the preceding command lines will create a file called /tmp/ls.all that contains all standard out and
standard error. The first command redirects stdout to /tmp/ls.all and the 2>&1 expression means "send stderr
wherever stdout is going". In the second example, the &> expression means "redirect all output".
Note
One common way that text files are used as standard input for commands is by creating script files. Scripts are
plain text files which are interpreted by the shell when given the proper permissions and prefaced with #!/bin/sh
on the first line, which tells the shell to interpret the script as standard input:
#!/bin/sh
echo HelloWorld
When the script file is invoked at the prompt using the ./ syntax, the shell will run all commands in the script file
and return the result to the terminal window, or wherever the output is specified to be sent to:
sysadmin@localhost:~$ ./examplescriptfile.sh
HelloWorld
One example of a command, that isn’t normally executed properly from a text file, is the cd command. When the
following script is run in the terminal, it is run as a child process, however, in order to successfully change to
another directory, the cd command must be run as a parent process. Consider the example testcd.sh script file
below, which specifies to use the cd command to change to the School directory then use the echo command to
print the string HelloWorld:
#!/bin/sh
cd /home/sysadmin/Documents/School
echo HelloWorld
[ Read 3 lines ]
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
^X Exit ^R Read File ^\ Replace ^U Uncut Text^T To Linter ^_ Go To Line
sysadmin@localhost:~$ ./testcd.sh
HelloWorld
The script executes the echo command and prints HelloWorld, but the cd command cannot run as a parent
process, so we are still in the directory where we started.
Therefore, even though the standard input doesn’t return an error, it won’t create the desired outcome of changing
the directory to /home/sysadmin/Documents/School. However, when the standard input is typed by a user, and
the desired directory is provided as an argument to the cd command at the command line, it functions normally:
sysadmin@localhost:~$ cd /home/sysadmin/Documents/School
sysadmin@localhost:~/Documents/School$
In some cases, it is useful to redirect standard input, so it comes from a file instead of the keyboard. A good
example of when input redirection is desirable involves the tr command. The tr command translates characters
by reading data from standard input; translating one set of characters to another set of characters and then writing
the changed text to standard output.
For example, the following tr command would take input from a user (via the keyboard) to perform a translation
of all lowercase characters to uppercase characters. Execute the following command, type some text, and press
Enter to see the translation:
The tr command doesn't stop reading from standard input unless it's terminated or receives an "End of
Transmission" character. This can be accomplished by typing Ctrl+D.
The tr command won't accept a file name as an argument on the command line. To perform a translation using a
file as input, utilize input redirection. To use input redirection, type the command with its options and arguments
followed by the less-than < character and a path to a file to use for input. For example:
The output shows the animals.txt file "translated" into all uppercase characters.
Important
Do not attempt to use the same file for input and output redirection, as the results are probably not desirable (you
end up losing all data). Instead, capture the output and place it into another file; use a different file name as shown
below:
Note
Even better, take the output of the history command and filter the output by using the grep command. In the
following example, the text output by the history command is redirected to the grep command as input. The grep
command matches the ls strings and sends its output to standard out:
Command pipelines become really powerful when three or more commands are combined. For example, view the
contents of the os.csv file in the Documents directory:
The following command line will extract some fields from the os.csv file with the cut command, then sort these
lines with the sort command, and finally eliminate duplicate lines with the uniq command:
The tee command can be very useful to create a log of a command or a script. For instance, to record the runtime
of a process, start with the date command and make a copy of the output into the timer.txt file:
sysadmin@localhost:~$ date | tee timer.txt
Fri Nov 7 02:21:24 UTC 2020
The timer.txt file now contains a copy of the date, the same output displayed in the preceding example:
Run the process that needs to be timed. The sleep command is being substituted for a timed process; it pauses for
a given number of seconds:
sysadmin@localhost:~$ sleep 15
Then run the date command again. This time, append the time to the end of the timer.txt file by using the -a
option:
To run all of the above commands as a single command, use a semicolon ; character as a separator:
The command above will display and record the first output of the date command, pause for 15 seconds, then
display and record the output of the second date command. The timer.txt file now contains a permanent log of
the runtime.
sysadmin@localhost:~$ cat timer.txt
Fri Nov 7 02:35:47 UTC 2020
Fri Nov 7 02:36:02 UTC 2020
sysadmin@localhost:~$ xargs
Hello
There
Hello There
sysadmin@localhost:~$
Important
Pressing Ctrl+D after exiting the xargs command by using Ctrl+C will log you out of the current shell. To send
the input of the xargs command to the echo command without logging out of the shell, press Ctrl+D while you
are still running the xargs command.
The xargs command is most useful when it is called in a pipe. In the next example, four files will be created using
the touch command. The files will be named 1a, 1b, 1c, and 1d based on the output of the echo command.
These four files can be removed just as easily by changing the touch command to the rm command:
A delimiter can be set using the -d option with the xargs command. To view the contents of the ~/Documents
directory containing the word alpha with all instances of the dash - character replaced with a space, type the
following:
sysadmin@localhost:~$ cd Documents/
sysadmin@localhost:~/Documents$ ls | grep alpha | xargs -d '-'
alpha first.txt
alpha second.txt
alpha third.txt
alpha.txt
The double pipe as the output of the ls command became the input of the grep command, and finally the xargs
command.
In the following example, the red.txt file will be used as the input of the xargs command:
Before running the xargs command, the contents of the ~/Documents directory are:
sysadmin@localhost:~/Documents$ ls
School alpha-third.txt hidden.txt numbers.txt spelling.txt
Work alpha.txt letters.txt os.csv words
adjectives.txt animals.txt linux.txt people.csv
alpha-first.txt food.txt longfile.txt profile.txt
alpha-second.txt hello.sh newhome.txt red.txt
After running the xargs command with the touch command, eleven new files appear:
In the previous example, the output of the cat command was used as input to the touch command using xargs and
a command-line pipe. The xargs command can also be used with pipes to send the output of a command as an
argument to another command:
In the example above, the find command output is used as an argument to the du disk usage command using the
xargs command. The first command preceding the pipe finds all file names that match the D* pattern. The output
of the find command is then passed to the du command as an argument to display the disk usage of the files.
Consider This
The xargs command has many, many more options that can be studied using the corresponding man page.
Key Terms
tee
Command used to read from standard input and write to standard output and files.
Section 10.6
xargs
Command used to build and execute command lines from standard input.
Section 10.7
LAB 10
10.1 Introduction
Filters are commands that read in text as input, alter it and then produce text as output. Standard input, output, error
redirection, and pipes are used to apply filter commands and to work with text streams.
10.2 Step 1
Standard output is redirected using the ">" symbol followed by the name of the file to which it will be written. To
redirect the output of the ls command to the ls_output.txt file, execute the following command:
ls > ls_output.txt
10.3 Step 2
To append the output of the date command to the file created in the previous step, execute the following
command:
The number 1 is associated with the standard output file descriptor when used with the ">" and ">>" operators. The
number is implied in the examples shown above; the example can also be written as date 1 >> ls_output.txt.
10.4 Step 3
Similar to standard output, standard error can also be redirected to files. The number 2 is associated with the
standard error file descriptor. To redirect the standard error while executing the find command, execute the
following commands:
10.5 Step 4
To redirect both standard output and standard error of the find command, execute the following command:
Note: To discard the output of any stream instead of redirecting it to any file, redirect it to the /dev/null file.
10.6 Step 5
To redirect both standard output and standard error of the find command to the same file, execute the following
command:
Note: The notation "2>&1" means send stderr to where stdout is going and the notation "&>" means redirect all
output.
10.8 Step 7
The "<" symbol is used for input redirection and used to pass the contents of a file as input to a command. To
translate all the lower-case characters to upper-case in the /etc/hosts file, execute the following command:
10.9 Step 8
Both input as well as output redirection can be used together in the same command. To translate all the lower-case
characters to upper-case in the /etc/hosts file and save the output in the ~/myhosts file, execute the following
command:
ls -l /etc | more
10.11 Step 10
The "|" symbol can be used multiple times within a command. To extract some fields from the /etc/passwd file,
sort them and finally eliminate duplicate lines, execute the following command:
10.12 Step 11
The "|" symbol can also be combined with redirection. To count the number of occurrences of "man" in the
command history and redirect the result to the count.txt file, execute the following command:
11.1 Introduction
In this chapter, you will learn how to run processes in the background or foreground, as well as how to make a
process switch between the background and foreground. You will also be taught how to control processes by
sending them signals using the kill command. In addition, different techniques for monitoring the resources that a
process is using will be presented. You will see how to control the priority of processes to affect how much
computing resources the processes will use. Finally, you will become familiar with utilities that allow multiple
processes inside a single shell.
Although there are exceptions, generally the operating system will differentiate users based upon whether they are
the administrator (also called the root user) or not. Non-root users are referred to as regular users. Users who have
logged into the root account can control any user processes, including stopping any user process.
ps [OPTION]...
Traditional UNIX style short options that use a single hyphen in front of a character
GNU style long options that use two hyphens in front of a word
BSD style options that use no hyphens and single character options
The ps command will display the processes that are running in the current terminal by default:
sysadmin@localhost:~$ ps
PID TTY TIME CMD
80 ? 00:00:00 bash
94 ? 00:00:00 ps
When the ps command is run with a BSD style option, the CMD column is replaced with the COMMAND column,
which shows not just the command, but also its options and arguments. An additional column called STAT is also
displayed, which conveys the state of the processes.
For example, to see all of the current user's processes, use the x BSD option:
sysadmin@localhost:~$ ps x
PID TTY STAT TIME COMMAND
80 ? S 0:00 -bash
95 ? R+ 0:00 ps x
State Description
D Uninterruptible Sleep
R Running
S Interruptible Sleep
T Stopped
Z Zombie
Note
The Process ID in our virtual machine environment will likely be different than the one in the example.
To see all processes (processes belonging to all users and not limited to the current shell) as well as display the user
owners of the processes, use the aux BSD option:
sysadmin@localhost:~$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18376 2968 ? Ss 16:41 0:00 /bin/bash /init
syslog 9 0.0 0.0 191328 3808 ? Ssl 16:41 0:00 /usr/sbin/rsysl
root 13 0.0 0.0 28356 2656 ? Ss 16:41 0:00 /usr/sbin/cron
root 15 0.0 0.0 72296 3272 ? Ss 16:41 0:00 /usr/sbin/sshd
bind 24 0.0 0.0 1141532 39364 ? Ssl 16:41 0:00 /usr/sbin/named
root 43 0.0 0.0 78636 3624 ? S 16:41 0:00 /bin/login -f
sysadmin 56 0.0 0.0 18508 3436 ? S 16:41 0:00 -bash
sysadmin 99 0.0 0.0 34400 2848 ? R+ 16:53 0:00 ps aux
In the example above, the aux option is actually a combination of three options:
Option Meaning
a Allows the ps command to show all processes.
u Show processes by all users and ignore restrictions to only list the current user’s processes.
x
List all processes and remove the restriction to only display the processes that are running in the current
terminal.
Instead of viewing just the processes running in the current terminal, users may want to view every process running
on the system. With traditional (non-BSD) options, the -e option will display every process. Typically, the -f
option is also used as it provides full details of the command, including options and arguments:
sysadmin@localhost:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 17:16 ? 00:00:00 /sbin??? /init
syslog 33 1 0 17:16 ? 00:00:00 /usr/sbin/rsyslogd
root 38 1 0 17:16 ? 00:00:00 /usr/sbin/cron
root 40 1 0 17:16 ? 00:00:00 /usr/sbin/sshd
bind 57 1 0 17:16 ? 00:00:00 /usr/sbin/named -u bind
root 70 1 0 17:16 ? 00:00:00 /bin/login -f
sysadmin 80 70 0 17:16 ? 00:00:00 -bash
sysadmin 96 80 0 17:26 ? 00:00:00 ps -ef
The ps command can be used along with the grep command to search for a specific process by name. To find all
processes that match the sshd string, you would type:
The command above uses the pipe | character to send the ps aux command to the grep command and the -i
option to perform a case insensitive search for the sshd process
The pgrep command parses through the running processes and looks for the specified names or other criteria of
processes, rather than having to run the ps command, pipe its output to the grep command and then look at the
results. Users can simply use the pgrep command and various options to return what they need to find. For
example, recall that the -i option makes the grep command case insensitive. This option can also be used with the
pgrep command.
If you wanted to find all instances of the sshd command running on the system, you could use the following
command:
The output shows the process IDs (PID) of the processes running that match the sshd string. Additionally, the -l
option can be used with the pgrep command to list the process name along with the PID:
sysadmin@localhost:~$ pgrep -li sshd
15 sshd
It is also possible to search for processes owned by a specific user by using the -u option:
In the example above, the -u and -l options displayed the bash process which has the PID 56 and is owned by the
sysadmin user.
A command that would normally execute and exit could be used with the watch command to execute repeatedly. A
good visual example would be to run the watch command with the date command to view the date command
running repeatedly:
In the example above, the watch command will run the date command repeatedly and display the updated output.
By default, the watch command runs the given command every two seconds so the output of the date command
will change to show a two-second difference.
Important
The watch command can also be used with the ps command to monitor running processes in the shell:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18376 3048 ? Ss 17:44 0:00 /bin/bash /init
syslog 9 0.0 0.0 191328 3760 ? Ssl 17:44 0:00 /usr/sbin/rsysl
ogd
root 13 0.0 0.0 28356 2512 ? Ss 17:44 0:00 /usr/sbin/cron
root 15 0.0 0.0 72296 3216 ? Ss 17:44 0:00 /usr/sbin/sshd
bind 24 0.3 0.0 813852 39648 ? Ssl 17:44 0:00 /usr/sbin/named
-u bind
root 43 0.0 0.0 78636 3704 ? S 17:44 0:00 /bin/login -f
sysadmin 60 0.0 0.0 18508 3444 ? S 17:44 0:00 -bash
sysadmin 75 0.0 0.0 11180 2328 ? S+ 17:47 0:00 watch ps aux
sysadmin 136 0.0 0.0 11180 344 ? S+ 17:47 0:00 watch ps aux
sysadmin 137 0.0 0.0 4628 920 ? S+ 17:47 0:00 sh -c ps aux
sysadmin 138 0.0 0.0 34400 2952 ? R+ 17:47 0:00 ps aux
In the example above, the watch command executes the ps aux command, which displays all running processes in
the shell, repeatedly. If a new program was executed in the shell, the output of the ps aux command would update
to show the new process.
Recall that the watch command’s default interval is two seconds. Therefore, to change the interval, use the -n
option followed by the specific interval desired (in seconds) after, and then specify the command to monitor.
watch -n # [COMMAND...]
The watch command will re-execute that command on a consistent basis at the specified interval until you quit that
command or Ctrl+C to stop the watch command itself.
The watch command can also be used to keep track of changes in a configuration file, log files or anything that
changes over time. For example, the watch command can be used with the tail command to monitor the log file
/var/log/ndg/web.log.
Note
To execute the next example, first use the command below to generate new log entries in the
/var/log/ndg/web.log file:
sysadmin@localhost:~$ start_log
Starting log...
To monitor changes to the last twenty lines of the /var/log/ndg/web.log log file and execute the tail command
every fifteen seconds, use the following command:
You can also highlight any differences between each successive execution of the program that is being watched by
specifying the -d option. In the example below, the watch command is used with the -d option to run the free
command, used to display memory on the system, repeatedly:
The output above will show the amount of used, free, and available memory in the Mem: row updating every two
seconds, and the differences in the memory will be highlighted in each new refresh of the screen.
11.2.4 Foreground Processes
So far, the commands that have been presented in this course have been executing in what is known as the
foreground. For simple commands that run quickly and then exit, using foreground execution is appropriate. A
foreground process is one that prevents the user from using the shell until the process is complete.
When one process starts another, the first process is referred to as the parent process, and the new process is called
a child process. So, another way of thinking of a foreground process is that when running in the foreground, a child
process doesn't allow any further commands to be executed in the parent process until the child process ends.
You do not need to add anything to a command in order to make that command execute in the foreground, as that
is the default behavior.
Normally, users only type one command per command line, but by using the semicolon ; character as a delimiter
between commands, a user can type multiple commands on one command line. Rarely is it really necessary to run
two or more commands from one command line, but sometimes it can be useful.
When commands are separated by the semicolon, the command to the left of the semicolon executes; when it
finishes, the command to the right of the semicolon executes. Another way of describing this is to say that the
commands execute in sequence.
COMMAND;COMMAND[;COMMAND]...
COMMAND; COMMAND[; COMMAND]...
The sleep command will pause for a specified number of seconds before continuing to the next command. The
following example will print Hello, pause for 5 seconds, then print World:
Recall that an alias is a feature that allows a user to create nicknames for commands or command lines. An alias
that runs multiple commands can be very useful; to accomplish this, the user would use the ; character between
each of the commands when creating the alias. For example, to create an alias called welcome which outputs the
current user, the date, and the current directory listing, execute the following command:
Now the alias welcome will execute the whoami, date, and ls commands in series like so:
sysadmin@localhost:~$ welcome
sysadmin
Tue Mar 26 21:07:10 UTC 2019
Desktop Documents Downloads Music Pictures Public Templates Videos
A real-life scenario involving multiple commands occurs when an administrator needs to take down and restart the
network connection remotely. If the user typed the one command to bring down the network and then executed it,
then the network connection would be terminated, and the user wouldn't be able to bring the network back up
again. Instead, the user could type the command to take down the network, followed by a semicolon, then the
command to bring up the network. After pressing the Enter key, both commands would execute, one right after the
other.
COMMAND &
For example, the following executes the sleep command for three seconds in the background. Pressing Enter after
the sleep command runs will display information about the background process:
As shown in the output, when executing commands in the background; after each command starts executing, it
outputs a job number followed by a space and then the process identification number (PID). These numbers are
used for controlling the process. The sleep command in the preceding example has a job number of 1 and a PID of
87.
While there are still background processes being run in the terminal, they can be displayed by using the jobs
command. It is important to point out that the jobs command will only show background processes in the current
terminal. If background processes are running in another terminal, they will not be shown by running the jobs
command in the current terminal. The following is an example of using the jobs command:
In the example above, the jobs command shows the two sleep processes running in the background. The job
number of a process is sometimes followed by a minus - or a plus + character. The plus + character denotes the last
process that was started, while the minus - character denotes a process started previous to the latest one. Once a
third job is created, only the most recent job will have the + next to it and the next-most recent, or previous, job
will have the - next to it. All other jobs will have a blank in that space. This allows you to see what the most recent
and next-most recent jobs are very quickly.
To terminate the processes, send them to the foreground by using the fg command with the job number of the
process to terminate, and while the process is running in the foreground, use Ctrl+C, a signal which stops the
process:
sysadmin@localhost:~$ fg 1
sleep 1000
^C
sysadmin@localhost:~$ fg 2
sleep 2000
^C
sysadmin@localhost:~$ jobs
sysadmin@localhost:~$
Note
The fg command, as well as a more efficient method for terminating processes, will be covered in greater detail
later in this chapter.
To have multiple commands run in the background on one command line, place an ampersand & character after
each command on the command line. In the next example, all three commands execute at the same time and release
control back to the shell, so a user does not have to wait for any of the commands to finish before executing
another command (although the user may need to press Enter again to get a prompt):
sysadmin@localhost:~$ echo Hello & echo World & echo '!' &
[1] 94
[2] 95
Hello
[3] 96
sysadmin@localhost:~$ World
!
To make the terminal available again, the user would have to use Ctrl+Z, a signal which pauses the process:
^Z
[1]+ Stopped sleep 1000
Now the terminal is back, but the sleep command has been paused. To put the paused command in the
background, execute the bg command.
bg [JOB_ID]...
The bg command resumes jobs without bringing them to the foreground. If no argument is provided, it will use the
most recently suspended job.
sysadmin@localhost:~$ bg
[1]+ sleep 1000 &
A command that has been paused or sent to the background can then be returned to the foreground using the fg
command.
fg [JOB_ID]...
Similar to the bg command, if no argument is provided, the job that was most recently suspended or placed in the
background will be used. To bring the sleep command back to the foreground, locking up the terminal again, use
the fg command:
sysadmin@localhost:~$ fg
sleep 1000
_
^C
sysadmin@localhost:~$
Both bg and fg can take the job number as an argument to specify which process should be resumed. The
following commands will resume sleep 1000 in the background and resume sleep 2000 in the foreground
respectively:
sysadmin@localhost:~$ bg 1
[1]- sleep 1000 &
sysadmin@localhost:~$ fg 2
sleep 2000
^Z
[2]+ Stopped sleep 2000
It is also possible to use the name of the command as an argument to the bg and fg commands:
Given multiple tasks and only one terminal to use with them, the fg and bg commands provide an administrator
with the ability to manually multi-task.
There are many different signals; each of them has a symbolic name and a numeric identifier. For example, Ctrl+C
is assigned the symbolic name SIGINT and the numeric identifier of 2.
To see a list of all of the signals available for your system, execute the kill -l command:
sysadmin@localhost:~$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
These signals can have unique meanings that are specific to a certain command (as the programmer who created
the command can adjust the behavior of the program), but generally they allow for processes to be stopped and
resumed, for processes to reconfigure themselves, or to end a process. All the signals with a number greater than 31
are for controlling real-time processes, a topic which is beyond the scope of this course. Some of the more
common signals are summarized in the following table:
There are several commands that will allow you to specify a signal to send to a process; the kill command is the
most commonly used. The syntax for the kill command looks like the following:
The kill command specifies which signal to be sent using options. It accepts three different ways to specify the
signal:
-<signal>
-s <signal>
--signal <signal>
The signal can be specified by using the number, the short name, or the full name. For example, to specify the
Interrupt signal, any of the following options could be used:
-2
-INT
-SIGINT
If the user doesn't specify a signal with an option, then the kill command defaults to sending the Terminate
SIGTERM signal.
When sending a signal, specify one or more processes to send the signal to. There are numerous techniques to
specify the process or processes. The more common techniques include:
For example, imagine a scenario where a user is running some process in the background and that user wants to
send a signal to the process. For this demonstration, the sleep command is run in the background:
A couple of items are noteworthy from the output of starting this process in the background. First, notice the job
number in square brackets [1]. Second, notice the process identifier (PID) which is 2901. To send the Terminate
signal, to this process, you can use either of the following commands:
As indicated earlier, the Terminate signal normally will end a process. Sometimes a process will trap the
Terminate signal so it may not end that process. Trapping occurs when a process behaves differently from the norm
when it receives a signal; this can be the result of how the programmer created the code for the command.
A user could try to use other signals, like SIGQUIT or SIGINT, to try to end a process, but these signals can also be
trapped. The only signal that will end a process and can't be trapped is a SIGKILL signal. So, if other signals have
failed to end a process, use the SIGKILL signal to force the process to end.
Users should not normally use the SIGKILL signal as the initial attempt to try to end a process because this forces
the process to end immediately and will not allow the process the opportunity to "clean up" after itself. Processes
often perform critical operations, such as deleting temporary files, when they exit naturally.
The following examples show two ways to send the Kill signal to a process:
There are other commands that send processes signals, such as the killall and pkill commands, which are
useful to stop many processes at once; whereas the kill command is a good choice for sending signals to a single
process. The pkill command can be used to terminate one or more processes by name and other criteria such as
the terminal and UID. Similar to how the pgrep command searches for a process by matching its name or pattern,
the pkill command allows users to specify a pattern that matches a process name and then sends signals to that
process. The following is a commonly used syntax for the pkill command:
Like the kill command, signals can be specified using either the number or signal name for both the killall and
pkill commands. Unlike the kill command, these other commands can be used to terminate all processes of a
particular user with the -u option. For example, killall -u bob would stop all of the process owned by the bob
user.
The killall and pkill commands also accept the name of the process instead of a process or job number. Just be
careful as this may end up stopping more processes than you had expected. An example of stopping a process using
the process name:
Consider This
You could very easily feed the output of the pgrep command to the kill command to terminate a process. For
example, by using the xargs command to help feed the lines of the output to the kill command one at a time, so it
works properly:
This will result in the kill -9 command being run on each of the PIDs that pgrep brought back for the nano
query.
In some cases, a user may want to execute a command that won't automatically exit when it is sent a HUP signal. To
have a process ignore a Hang Up signal, start the process with the nohup command.
For example, consider a scenario where a user has a script named myjob.sh that needs continue to run all night
long. The user should start that script in the background of the terminal by executing:
After executing the script, the user could proceed to log out. The next time the user logs in, the output of the script,
if any, would be contained in the nohup.out file in that user's home directory.
1.2.10 Process Priority
When a process runs, it needs to have access to the CPU to perform actions. Not all processes have the same access
to the CPU. For example, system processes typically have a higher priority when accessing the CPU.
The Linux kernel dynamically adjusts the priority of processes to try to make the operating system seem responsive
to the user and efficient at performing tasks. A user can influence the priority that will be assigned to a process by
setting a value of something called niceness.
The higher you set the niceness value, the lower the priority that will be assigned to a process. The default value of
niceness for processes is zero; most user processes run at this nice value. Only a user with administrative (root)
access can set negative niceness values or alter the niceness of an existing process to be a lower niceness value.
To set the initial niceness of a command, use the nice command as a prefix to the command to execute.
The -n option indicates the desired niceness value. For example, to execute a command at the lowest priority
possible, execute the following command:
If a user logs in as the root user, they could also execute a command with the highest priority possible by executing
the following command:
Note
The su command used below allows a regular user to become the root user temporarily. The user is required to
enter the root account password, when prompted. To use the su command in our virtual environment, enter the
root password netlab123.
sysadmin@localhost:~$ su -
Password:
root@localhost:~# nice -n -20 cat /dev/zero > /dev/null
^Z
[1]+ Stopped nice -n -20 cat /dev/zero > /dev/null
root@localhost:~# nice -n 19 cat /dev/zero > /dev/null
^Z
[2]+ Stopped nice -n 19 cat /dev/zero > /dev/null
To adjust the niceness of an existing process, use the renice command. This can be useful when the system
becomes less responsive after running a CPU intensive command. A user could make the system more responsive
again by making that process run "nicer".
To accomplish this, the user would need to discover the process identifier for the process by using the ps
command. For example:
Note
The Process ID in our virtual machine environment will likely be different than the one in the example.
Next, use the renice command to adjust the priority back to normal. Like the nice command, the -n option
indicates the niceness value. The -p option indicates the process ID to operate on.
sysadmin@localhost:~$ top
top - 16:47:34 up 51 days, 2:12, 1 user, load average: 1.37, 1.56, 1.49
Tasks: 13 total, 4 running, 9 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 37.2 sy, 0.2 ni, 62.1 id, 0.0 wa, 0.1 hi, 0.0 si, 0.0 st
KiB Mem: 16438128 total, 13108516 used, 3329612 free, 4276 buffers
KiB Swap: 0 total, 0 used, 0 free. 9808716 cached Mem
Upon closer investigation of the niceness column NI and the CPU usage percentage column %CPU in the output
above, we can see that the cat command with the lowest niceness (0) is using the highest percentage CPU (87.4),
while the cat command with the highest niceness (19) is using the lowest percentage CPU (1.7).
The top command has numerous features; for example, it can be manipulated in an interactive manner. Pressing
the H key while the top command is running will result in it displaying a help screen:
Z,B,E,e Global: 'Z' colors; 'B' bold; 'E'/'e' summary/task memory scale
l,t,m Toggle Summary: 'l' load avg; 't' task/cpu stats; 'm' memory info
0,1,2,3,I Toggle: '0' zeros; '1/2/3' cpus or numa node views; 'I' Irix mode
f,F,X Fields: 'f'/'F' add/remove/order/sort; 'X' increase fixed-width
The K and R keys are used to manage tasks or processes within the top program.
Pressing the K key will allow a user to kill or send a signal to a process. After pressing the K key, the top
command will prompt for a PID and then for a signal to send to that process.
Pressing the R key will allow a user to renice a process by prompting for the PID and then the new niceness value.
The uptime command shows the current time and the amount of time the system has been running, followed by the
number of users who are currently logged in and the load averages during the past one, five, and fifteen-minute
intervals.
To get a good idea of how busy a system is, use the uptime command:
sysadmin@localhost:~$ uptime
18:24:58 up 5 days, 10:43, 1 user, load average: 0.08, 0.03, 0.05
The numbers reported for the load averages are based upon how many CPU cores are available. Think of each
processor as having 100% resources (CPU time) available. One processor = 100%, four processors = 400%. The
uptime command is reporting the amount of resources used, but it is dividing by 100. So, 1.00 is actually 100% of
the CPU time being used, 2.00 is 200% and so on.
If a system has only one CPU core, then a value of 1.00 indicates that the system was fully loaded with tasks. If the
system has two CPU cores, then a value of 1.00 would indicate a 50% load (1.00/2.00). An uptime reading of 1.00
(or 100%) usage on a 4 core CPU would imply that 1.00/4.00 (1/4 or 25%) of the CPU's total computational
resources are being used.
Consider This
If you don’t know how many CPU cores are available on a Linux system, there is a way to obtain this information
on the command line. The lscpu command can be used to see how many CPU cores are available on the system:
sysadmin@localhost:~$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 16
On-line CPU(s) list: 0-15
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 2
Output Omitted...
In the example above, the output indicates that there are eight total CPU cores. Two sockets and four cores per
socket totals eight CPU cores.
Although the lscpu command is beyond the scope of this course, it is covered in further detail in NDG Linux
Essentials, where you can learn about understanding system hardware.
To get an idea of how your system is using memory, the free command is helpful. This command displays not
only the values for the random-access memory (RAM) but also for swap, which is space on the hard disk that is
used as memory for idle processes when the random-access memory starts to become scarce.
The free command reports the total amount of memory, as well as how much is currently being used and how
much is free (available) for use. The output also breaks down the use of memory for buffers and caches:
sysadmin@localhost:~$ free
total used free shared buffers cached
Mem: 16438128 13106024 3332104 3200 4276 9808896
If logged into the Gnome desktop environment, the user can use the gnome-system-monitor program. This tool
(pictured below) is analogous to the Task Manager in Microsoft Windows. It provides four tabs to view
information about the System, Processes, Resources, and File Systems.
This graphical tool allows users to select a process and send a signal to it to terminate it, as well as view all current
processes updated in real-time.
Consider This
The K desktop environment (KDE) has a similar program, the ksysguard command. The Gnome desktop
environment is not available in the virtual environment for this chapter.
11.4 Multi-Session Command Line Utilities
Now that we have discussed process creation and management, we can talk about a related topic, which is how you
can manage multiple processes inside a single Bash shell environment.
The need for multiple processes inside a single shell is not very important to a console user, who can simply switch
to another TTY (terminal session accessible via hardware means) and run another shell, or open another instance of
the graphical terminal app on a desktop session. However, imagine the user is connected via the ssh command to
another system that does not have those multiple terminal capabilities but needs to accomplish several tasks inside
their one connected shell session on that remote system.
Such a user might need to run a command to monitor CPU usage, edit a configuration file, and possibly execute a
command to initiate a system update. However, without special program assistance, they would only be able to do
one at a time, or would have to use the jobs command method of putting each command into the background and
having little more than the ability to switch between the jobs.
There are a couple of options to accomplish true multiple terminal session productivity when limited to a single
Bash shell connection on a system. We will focus on the screen and tmux commands, both of which allow the user
to start processes in a session that can be detached while still running, then re-attached and managed by various
methods.
Normally, when using a terminal, running a process will cause the terminal to be occupied until that process
finishes executing, is stopped or terminated, or is placed in the background. However, the screen command allows
for multiple processes to run within separate sessions under a single terminal. This is possible because, by default,
the screen command provides a new session with a shell in it where a command can be executed with the ability
to detach from that session and return the shell to start new sessions.
To start the screen command by itself, type the command at the prompt:
sysadmin@localhost:~$ screen
GNU Screen version 4.06.02 (GNU) 23-Oct-17
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program (see the file COPYING); if not, see http://www.gnu.org/licenses/,
or contact Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,