Linux - Unit-1-Chp-2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Unit-1 Chp-2- Finding Your Way on the

Command Line

Working with the Bash Shell:


To communicate commands to the operating system kernel,
an interface is needed that sits between the kernel and the
end user issuing these commands. This interface is known as
the shell. Several shells are available on RHEL. Bash (short for
the Bourne Again Shell) is the one that is used in most
situations. This is because it is compatible with the Bourne
shell, which is commonly found on UNIX servers. You should,
however, be aware that Bash is not the only shell that can be
used. A partial list of other shells follows:
tcsh - A shell with a scripting language that works like the C
programming language. It is very popular with C
programmers.
zsh - A shell that is compatible with Bash but offers even
more features.
sash - This stands for stand-alone shell. This is a minimal-
feature shell that runs in almost all environments. Therefore,
it is very well suited for system troubleshooting.

Getting the Best of Bash :


Basically, from the Bash environment, an administrator is
working with commands. An example of such a command is
ls, which can be used to display a list of files in a given
directory. To make working with these commands as easy as
possible, Bash has some useful features to offer. Some of the
most used Bash features are automatic completion and the
history mechanism.
Some shells offer the option to complete a command
automatically. Bash also has this feature, but it goes beyond
the option of simply completing commands. Bash can
complete almost everything, not just commands. It can also
complete filenames and shell variables.

Variables:
A shell variable is a common value that is used often by the
shell and commands that work from that shell, and it is
stored with a given name. An example of such a variable is
PATH, which stores a list of directories that should be
searched when a user enters a command. To refer to the
contents of a variable, prepend a $ sign before the name of
the variable. For example, the command echo $PATH would
display the contents of the current search path that Bash is
using.

Useful Bash Key Sequences:


Sometimes, you will enter a command from the Bash
command line and nothing, or something totally unexpected,
will happen. If that occurs, it is good to know that some key
sequences are available to perform basic Bash management
tasks. Here is a short list of the most useful of these key
sequences:
Ctrl+C = Use this key sequence to quit a command that is not
responding (or simply is taking too long to complete). This
key sequence works in most scenarios where the command is
active and producing screen output.
Ctrl+D = This key sequence is used to send the end-of-file
(EOF) signal to a command. Use this when the command is
waiting for more input. It will indicate this by displaying the
secondary prompt >.
Ctrl+R = This is the reverse search feature. When used, it will
open the reverse-i-search prompt. This feature helps you
locate commands you have used previously. The feature is
especially useful when working with longer commands. Type
the first characters of the command, and you will
immediately see the last command you used that started
with the same characters.
Ctrl+Z = Some people use Ctrl+Z to stop a command. In fact, it
does stop your command, but it does not terminate it. A
command that is interrupted with Ctrl+Z is just halted until it
is started again with the fg command as a foreground job or
with the bg command as a background job.
Ctrl+A = The Ctrl+A keystroke brings the cursor to the
beginning of the current command line.
Ctrl+B = The Ctrl+B keystroke moves the cursor to the end of
the current command line.

Working with Bash History:


Another useful aspect of the Bash shell is the history feature.
The history mechanism helps you remember the last
commands you used. By default, the last 1,000 commands of
any user are remembered. History allows you to use the up
and down arrow keys to navigate through the list of
commands that you used previously.
You can see an overview of these remembered commands
when using the history command from the Bash command
line. This command shows a list of all of the recently used
commands. From this list, a command can also be restarted.
For example, if you see command 5 in the list of commands,
you can easily rerun this command by using its number
preceded by an exclamation mark, or !5 in this example.

As an administrator, you sometimes need


to manage the commands that are in the
history list. There are two ways of doing
this.
 First you can manage the file .bash_history (note that
the name of this file starts with a dot), which stores all
of the commands you have used before. Every user has
such a file, which is stored in the home directory of the
user. If, for example, you want to delete this file for the
user joyce, just remove it with the command rm
/home/joyce/. bash_history. Notice that you must be at
the root to do this. Since the name of the file begins
with a dot, it is a hidden file, and normal users cannot
see hidden files.
 A second way of administering history files, which can
be accomplished by regular users, is by using the history
command. The most important option offered by this
Bash internal command is the option -c. This will clear
the history list for the user who uses this command. So,
use history -c to make sure that your history is cleared.
In that case, however, you cannot use the up arrow key
to access commands used previously.

Performing Basic File System Management


Tasks :
Essentially, everything on your RHEL server is stored in a text
or ASCII file. Therefore, working with files is a very important
task when administering Linux
Working with Directories:
Since files are normally organized within directories, it is
important that you know how to handle these directories.
This involves a few commands.
 cd = Use this command to change the current working
directory. When using cd, make sure to use proper
syntax. First, names of commands and directories are
case-sensitive; therefore, /bin is not the same as /BIN.
Next, you should be aware that Linux uses a forward
slash instead of a backslash. So, use cd /bin and not cd \
bin to change the current directory to /bin.
 pwd = The pwd command stands for Print Working
Directory. You can often see your current directory from
the command line, but not always. If the latter is the
case, pwd offers help.
 mkdir = If you need to create a new directory, use mkdir.
With Linux mkdir, it is possible to create a complete
directory structure in one command using the -p option,
something that you cannot do on other operating
systems. For example, the command mkdir/some
/directory will fail if /some does not exist beforehand. In
that case, you can force mkdir to create /some as well if
it doesn’t already exist. Do this by using the mkdir -p
/some /directory command.
 rmdir = The rmdir command is used to remove
directories. Be aware, however, that it is not the most
useful command available, because it will work only on
directories that are already empty. If the directory still
has fi les and/or subdirectories in it, use rm -r instead,
as explained below.

Using ls to List Files:


To manage files on your server, you must first know what fi
les are available. For this purpose, the ls command is used. If
you just use ls to show the contents of a given directory, it
will display a list of files. These files, however, also have
properties. For example, every fi le has a user who is the
owner of the fi le, some permissions, a size that is stored in
the fi le system, and more. To see this information, use ls -l.
Wildcards can be used when working with the ls command.
For example, ls * will show a list of all fi les in the current
directory, ls / etc/*a.* will show a list of all fi les in the
directory /etc that have an a followed by a . (dot) somewhere
in the filename, and ls [abc]* will show a list of all fi les where
the name starts with either a, b, or c in the current directory.
Now without the option –d, something strange will happen. If
a directory matches the wildcard pattern, the entire contents
of that directory are displayed as well. This isn’t very useful,
and for that reason, the -d option should always be used with
the ls command when using wildcards.
When displaying files using ls, note that some files are
created as hidden fi les. These are files where the name starts
with a dot. By default, hidden files are not shown. To display
hidden files, use the ls -a command.
A hidden file is one where the name starts with a dot. Most
configuration files that are stored in user home directories
are created as hidden files. This prevents the user from
deleting the file by accident.

Removing Files with rm :


Cleaning up the file system is a task that also needs to be
performed on a regular basis. The rm command is used for
this purpose. For example, use rm /tmp/somefile to remove
somefile from the /tmp directory. If you are at the root and
have all the proper permissions Removing Files with rm
Cleaning up the fi le system is a task that also needs to be
performed on a regular basis. The rm command is used for
this purpose. For example, use rm /tmp/somefile to remove
somefile from the /tmp directory. If you are at the root and
have all the proper permissions for this fi le (or if you are the
root), you will succeed without any problem. Since removing
fi les can be delicate (imagine removing the wrong fi les), the
shell will ask your permission by default (see Figure 2.1).
Therefore, it may be necessary to push the rm command a
little. You can do this by using the -f (force) switch. For
example, use rm -f somefile if the command states that some
fi le cannot be removed for some reason.
The rm command can also be used to wipe entire directory
structures. In this case, the -r option has to be used. When
this option is combined with the -f option, the command
becomes very powerful. For example, use rm -rf /somedir/*
to clear out the entire contents of /somedir. This command
doesn’t remove the directory itself, however. If you want to
remove the directory in addition to the contents of the
directory, use rm -rf /somedir. You should be very careful
when using rm this way, especially since a small typing
mistake can result in very serious consequences. Imagine, for
example, that you type rm -rf / somedir (with a space
between / and somedir) instead of rm -rf /somedir. As a
result, the rm command will first remove everything in /, and
when it is fi nished with that, it will remove somedir as well.
Note that the second part of the command is actually no
longer required once the first part of the command has

completed.

Copying Files with cp:


If you need to copy fi les from one location on the fi le
system to another location, use the cp command. This
straightforward command is easy to use. For example, use cp
~/* / tmp to copy all fi les from your home directory (which is
referred to with the ~ sign) to the directory /tmp. If
subdirectories and their contents need to be included in the
copy command, use the option -r. You should, however, be
aware that cp normally does not copy hidden fi les where the
name starts with a dot. If you need to copy hidden fi les as
well, make sure to use a pattern that starts with a .(dot). For
example, use cp ~/.* /tmp to copy all fi les where the name
starts with a dot from your home directory to the directory
/tmp.

Moving Files with mv


An alternative method for copying files is to move them. In
this case, the file is removed from its source location and
placed in the target location. For example, use mv ~/somefile
/tmp/otherfile to move the filename somefile to /tmp. If a
subdirectory with the name otherfile exists in /tmp, somefile
will be created in this subdirectory. If, however, no directory
with this name exists in /tmp, the command will save the
contents of the original file somefile under its new name,
otherfile, in the directory /tmp. The mv command is not just
used to move fi les. You can also use it to rename directories
or files, regardless of whether there are any fi les in those
directories. For example, if you need to rename the
directory /somedir to /somethingelse, use mv /somedir
/somethingelse.

Viewing the Contents of Text Files


When administering your RHEL server, you will very often find
that you are modifying configuration fi les, which are all ASCII
text files. Therefore, the ability to browse the content of
these files is very important. Different methods exist to
perform this task. cat - This command displays the contents
of a file by dumping it to the screen. This can be useful if the
contents of the file do not fit on the screen. You will see some
text scrolling by, and as the final result, you will see only the
last lines of the file being displayed on the screen.
tac - This command does the same thing as cat but inverts
the result; that is, not only is the name of tac the opposite of
cat, but the result is the opposite as well. This command will
dump the contents of a file to the screen, but with the last
line first and the first line last.
tail - This command shows only the last lines of a text file. If
no options are used, this command will show the last 10 lines
of a text file. The command can also be modified to show any
number of lines on the bottom of a file. For example, tail -n
2 /etc/passwd will show you the last two lines of the
configuration file where usernames are stored.
head This command is the opposite of tail. It displays the first
lines of a text file.

Creating Empty Files


It is often useful to create fi les on a fi le system. This is a
useful test to check to see whether a file system is writable.
The touch command helps you do this. For example, use
touch somefile to create a zero-byte file with the name
somefile in the current directory. It was never the purpose of
touch to create empty files. The main purpose of the touch
command is to open a file so that the last access date and time
of the file displayed by ls is modified to the current date and
time. For example, touch * will set the time stamp to the
present time on all files in the current directory. If touch is
used with the name of a file that doesn’t exist as its argument,
it will create this file as an empty file.
Piping and Redirection
The piping and redirection options are among the most powerful features of the Linux command
line. Piping is used to send the result of a command to another command, and redirection sends the
output of a command to a fi le. This file doesn’t necessarily need to be a regular file, but it can also
be a device fi le, as you will see in the following examples. Piping The goal of piping is to execute a
command and send the output of that command to the next command so that it can do something
with it. See the example described in Exercise 2.1.
EXERCISE 2.1
Discovering the Use of Pipes In this exercise, you’ll see how a pipe is used to add functionality to a
command. First you’ll execute a command where the output doesn’t fit on the screen. Next, by
piping this output through less, you can see the output screen by screen.
1. Open a shell, and use su - to become the root. Enter the root password when prompted.
2. Type the command ps aux. This command provides a list of all the processes that are currently
running on your computer. You’ll notice that the list doesn’t fit on the screen.
3. To make sure you can see the complete result page by page, use ps aux | less. The output of ps is
now sent to less, which outputs it so that you can browse it page by page.
Another very useful command that is often used in a pipe construction is grep. This command is
used as a fi lter to show just the information that you want to see and nothing else. Imagine, for
example, that you want to check whether a user with the name linda exists in the user database
/etc/passwd. One solution is to open the fi le with a viewer like cat or less and then browse the
contents of the fi le to check whether the string you are seeking is present in the fi le. However,
that’s a lot of work. A much easier solution is to pipe the contents of the fi le to the filter grep, which
would select all of the lines that contain the string mentioned as an argument of grep. This command
would read cat /etc/passwd | grep linda.

Redirection
Whereas piping is used to send the result of a command to another command, redirection sends the
result of a command to a file. While this file can be a text file, it can also be a special file, such as a
device file.
In Exercise 2.3, first you’ll use the ps aux command without redirection. The results of the command
will be written to the terminal window in which you are working. In the next step, you’ll redirect the
output of the command to a fi le. In the fi nal step, you’ll display the contents of the file using the
less utility.
EXERCISE 2.3
Redirecting Output to a File
1. From a console window, use the command ps aux. You’ll see the output of the command on the
current console.
2. Now use ps aux > ~/psoutput.txt. You don’t see the actual output of the command, because it is
written to a fi le that is created in your home directory, which is designated by the ~ sign.
3. To show the contents of the file, use the command less ~/psoutput.txt.
Do not use the single redirector sign (>) if you don’t want to overwrite the content of existing files.
Instead, use a double redirector sign (>>). For example, who > myfile will put the result of the who
command (which displays a list of users currently logged in) in a file called myfile. If then you want to
append the result of another command, for example the free command (which shows information
about memory usage on your system), to the same fi le myfile, then use free >> myfile.

You might also like