Unit I Chapter 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

Unit-I

Chapter-2

Command Line
Working with the Bash Shell
Working with the Bash Shell

Bash: stands for Bourne Again Shell and it is the default shell
on many Linux distributions.
The Bash shell is a combination of features from the Bourne
Shell and the C Shell.it is the interface that enables the user to
issue command to the kernel and thus act as a mediator between
the kernel and user.
A partial list of other shells follows:
ator
 Tcsh(Tenex C shell): 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(stand Alone Shell) : This stands for stand-alone shell.


This is a minimal-feature shell that runs in almost all
Useful Bash Key Sequences

The most useful of these key sequences:

 Ctrl+C: Use this key sequence to quit a command that is not


responding.

 Ctrl+D: This key sequence is used to send the end-of-file (EOF) signal
to a command.

 Ctrl+R : This is the reverse search feature.

 Ctrl+Z : Some people use Ctrl+Z to stop a command.

 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
• The Bash shell is the history feature. The history mechanism
helps you remember the last commands you used.

• History allows you to use the up and down arrow keys to


navigate through the list of commands that you used previously .
Performing Basic File System Management Tasks

Everything on your RHEL server is stored in a text or ASCII file.

1. Working with Directories


The most common commands to work with directories : pwd, cd, mkdir,
rmdir.
 pwd - Displays the full path-name for the current directory.
Open a command line interface and type pwd.

root@hnl etc] # pwd


/home/wccbm

 cd- You can change your current directory (working directory) with
the cd command.
root@hnl etc] # cd /etc
root@hnl etc] # pwd
/etc
root@hnl etc] # cd /bin
root@hnl etc /bin] # pwd
/bin

 mkdir - mkdir command is used to create a new dir. You have to give at
least one parameter to mkdir, the name of the new directory to be created.

rmdir -This command will remove/delete an existing directory, provided it


is empty. In the following example will are removing/deleting an existing
directory example.

# rmdir example
Working with the Red Hat Linux File
System
The Red Hat Linux file system is the structure in which all the information
on your computer is stored. Files are organized within a hierarchy of
directories. Each directory can contain files, as well as other directories.

At the top is the root directory, which is represented by a single slash (/).

Below that is a set of common directories in the Linux system, such


as /bin, /dev, /home, /lib, and /tmp, to name a few.

Each of those directories, as well as directories added to the root, can


contain subdirectories.

Figure 1: The Red


Hat Linux file
system is organized
as a hierarchy of
directories
Some of the Red Hat Linux directories that may interest you include the
following:

/bin — Contains common Linux user commands, such as ls, sort, date,
and chmod.

/boot — Has the bootable Linux kernel and boot loader configuration files
(GRUB).

/dev — Contains files representing access points to devices on your systems.


These include terminal devices (tty*), floppy disks (fd*), hard disks (hd*), RAM
(ram*), and CD-ROM (cd*). (Users normally access these devices directly
through the device files.)

/etc — Contains administrative configuration files.

/home — Contains directories assigned to each user with a login account.


/mnt — Provides a location for mounting devices, such as remote file systems
and removable media (with directory names of cdrom, floppy, and so on).

/root — Represents the root user's home directory.

/sbin — Contains administrative commands and daemon processes.

/tmp — Contains temporary files used by applications.

/usr — Contains user documentation, games, graphical files (X11), libraries


(lib), and a variety of other user and administrative commands and files.

/var — Contains directories of data used by various applications. In particular,


this is where you would place files that you share as an FTP server (/var/ftp) or
a Web server (/var/www). It also contains all system log files (/var/log).
Working with Files
To manage fi les on your server, you must fi rst know what fi les are
available. For this purpose, the ls command is used.

Using ls to List Files


 The ls command is a basic, useful.
 The ls command allows us to list files in a directory.

ls with no options
ls -l

 The -l option signifies the long list format. This shows a lot more
information presented to the user than the standard command.
 You will see the file permissions, the number of links, owner name,
owner group, file size, time of last modification, and the file or
directory name. This option is used in conjunction with many other
options on a regular basis.
Wildcards can be used when working with the ls
command.

For example, ls * will show a list of all files in the


current directory, ls / etc/*a.

* will show a list of all files in the directory /etc that


have an a followed by a . (dot)somewhere in the
filename
Removing Files with rm
The rm command removes (deletes) files and directories.

For example, let's remove the today.txt file :

$ rm today.txt

Try the following example:


$ touch newfile.txt
$ rm -i newfile.txt

rm: remove regular empty file 'newfile.txt'?

The -i makes rm interactive. Answer with a y and the file goes away.
Answer with an n and you keep the file. To be safe, you can always use
the -i switch with rm. There's no harm or shame in it and you'll be glad
you did at some point in the future.
COPYING FILES WITH CP
 Copying files and directories is a very common task for Linux system
administrators.
 The copy (cp) command, the file or directory source, and the
destination. To copy a file, file.txt, to the /opt/files directory, use:

$ cp file.txt /opt/files

 To copy an entire directory and all its contents, including


subdirectories, use the -R (Recurse) option. Copy the data directory
in your home directory to /opt/files. You can use either the -r or -
R to recurse copy files:

$ cp -R data /opt/files

 The cp command is rare in that both the upper- and the lowercase
options for an action are the same. Of course, you can use wildcards
when copying files to filter them with patterns:

$ cp *.txt /opt/files
MOVING FILES WITH MV

 To change the location of a file, use the mv command.


 mv the file is physically moved from one place to another, instead

of being duplicated, as with cp.


$ mv abc def
$ mv abc ~
Of the two move (mv) commands, the first moves the file abc to the
file def in the same directory (essentially renaming it), whereas
the second moves the file abc to your home directory (~).

https://www.youtube.com/watch?v=Mz6pJ7TnFUc
https://www.youtube.com/watch?v=KoyAZonYSEI

learn cat command in linux – YouTube learn cat command in linux


- YouTube
VIEWING THE CONTENTS OF TEXT FILES
THERE ARE SEVERAL COMMANDS AND PROGRAMS PROVIDED BY LINUX FOR
VIEWING THE CONTENTS OF FILE.

1. head Command
The head command reads the first ten lines of a any given
file name. The basic syntax of head command is:
head [options] [file(s)]
For example, the following command will display the first ten
lines of the file named ‘/etc/passwd‘.
If more than one file is given, head will show the first ten lines
of each file separately. For example, the following command
will show ten lines of each file.

# head /etc/passwd /etc/shadow

==> /etc/passwd <== root:x:0:0:root:/root:/bin/bash


bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
==> /etc/shadow <== root:$6$85e1:15740:0:99999:7:::
bin:*:15513:0:99999:7::: daemon:*:15513:0:99999:7:::
adm:*:15513:0:99999:7::: lp:*:15513:0:99999:7:::
sync:*:15513:0:99999:7::: shutdown:*:15513:0:99999:7:::
halt:*:15513:0:99999:7::: mail:*:15513:0:99999:7:::
If it is desired to retrieve more number of lines than the
default ten, then ‘-n‘ option is used along with an integer
telling the number of lines to be retrieved.

For example, the following command will display first 5 lines


from the file ‘/var/log/yum.log‘ file.
1. tail Command
The tail command allows you to display last ten lines of any
text file. Similar to the head command above, tail command
also support options ‘n‘ number of lines and ‘n‘ number of
characters.
The basic syntax of tail command is:

For example, the following command will print the last ten lines
of a file called ‘access.log‘.
 cat Command
The ‘cat‘ command is most widely used, universal tool. It
copies standard input to standard output. The command
supports scrolling, if text file doesn’t fit the current screen.
The basic syntax of cat command is:

The most frequent use of cat is to read the contents of files. All
that is required to open a file for reading is to type cat followed by
a space and the file name.
 The cat command also used to concatenate number of files
together.
 It can be also used to create files as well. It is achieved by
executing cat followed by the output redirection operator
and the file name to be created.

We can have custom end maker for ‘cat’ command. Here it is


implemented.
 tac Command
‘tac‘ is a command under Linux. It is better to show an
example of ‘tac’ than to talk anything about it.
Create a text file with the names of all the month, such that one
word appears on a line
1. more command
more command is used to view the text files in the command
prompt, displaying one screen at a time in case the file is large.
The more command also allows the user do scroll up and down
through the page. The syntax along with options and command
is as follows.
When the output is large, we can use more command to see
output one by one.
While viewing the text file use these controls:
LESS COMMAND
less is a command that displays file contents or command output
one page at a time in your terminal. less is most useful for viewing the
content of large files or the results of commands that produce many
lines of output. The content displayed by less can be navigated by
entering keyboard shortcuts.
The general syntax for the less program is as follows:

less [OPTIONS] filename


Piping and Redirection!
Every program we run on the command line automatically has
three data streams connected to it.
 STDIN (0) - Standard input (data fed into the program)

 STDOUT (1) - Standard output (data printed by the program,

defaults to the terminal)


 STDERR (2) - Standard error (for error messages, also

defaults to the terminal)

Piping is used to send the result of a command to another


command.
redirection sends the output of a command to a file. This file
doesn’t necessarily need to be a regular file, but it can also be a
Pipe is used to combine two or more commands, and in this, the
output of one command acts as input to another command, and
this command’s output may act as input to the next command
and so on.
Pipes are unidirectional i.e data flows from left to right through
the pipeline.

In Piping, the output from a command is taken to be the input to


another command.
The standard output and standard error redirection means writing
the output, error messages, or both to a file instead of the default
destination (terminal screen).
Example :
1. Listing all files and directories and give it as input
to more command.
Redirection of Channels

Redirection of standard output is generally achieved by the use of the


">". operator. Lets take a look at a simple example:
Finding Files
 Working with an Editor
• Saving and Quitting
After activating command mode, you use the appropriate
command to save your work. The most common command is
:wq
Copy, Cut and Paste in Visual Mode

1. Vim’s visual mode allows you to select and manipulate text.

2. Place the cursor to the line you want to begin copping or cutting.

3. The visual mode has three subtypes.


• Press v to enter the visual mode.
• Press V to enter visual line mode, where the text is selected by line.
• Press Ctrl+v to enter visual block mode. In this mode, the text is selected by rectangle
blocks.
Entering the visual mode also marks a starting selection point.

4. Move the cursor to the end of the text you want to copy or cut. You can use a movement
command or up, down, right, and left arrow keys.
6. Press y to copy, or d to cut the selection.
7. Move the cursor to the location where you want to paste the contents.
8. Press P to paste the contents before the cursor, or p to paste it after the cursor.

Deleting Text
Another action you will often do when working with vi is deleting text.
There are many methods that can be used to delete text with vi. The
easiest is from insert mode: just use the Delete and Backspace keys
to get rid of any text you like. This works just like a word processor.

Some options are available from vi command mode as well.

1. Use x to delete a single character. This has the same effect as


using the Delete key while in insert mode.
2. Use dw to delete the rest of the word. That is, dw will delete
anything from the current position of the cursor to the end of the
word.
3. Use D to delete from the current cursor position up to the end of
the line.
4. Use dd to delete a complete line.
 #cat abc

You might also like