0% found this document useful (0 votes)
4 views49 pages

Basic Linux Commands

The document provides an overview of the Linux shell, its features, and basic commands. It explains the role of the shell as an interpreter between the user and the operating system, detailing functionalities such as shell scripts, input/output redirection, and piping mechanisms. Additionally, it covers how to change the running shell, create user accounts, and utilize command syntax effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views49 pages

Basic Linux Commands

The document provides an overview of the Linux shell, its features, and basic commands. It explains the role of the shell as an interpreter between the user and the operating system, detailing functionalities such as shell scripts, input/output redirection, and piping mechanisms. Additionally, it covers how to change the running shell, create user accounts, and utilize command syntax effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Operating System Concepts and Basic Linux Commands

Shell and Basic Linux


Commands

129
Shital Vivek Ghate

5.1 Shell:
The shell program is the interpreter through which we
communicate with the operating system. As we type in a command
at the shell prompt, it is interpreted and passed to the Linux kernel.
The kernel tells the computer what to do. Shell is the command
interpreter. It is the most widely used utility program in Linux.
Shell offers the following features to the users.

i. Interactive Environment: The shell allows the user to create a


dialogue between the user and the host system. This dialogue lasts
until the user ends the session.

ii. Shell scripts: Shell script is the user interface to the computer
system for the Linux operating system. The shell contains internal
commands which can be utilized by the user. Shell scripts are
groups of Linux commands strung together and executed as
individual files

iii. Input/output redirections: Linux commands can be instructed


to take their instructions from files, and not from the keyboard.
The shell also allows the user to place the output of commands into
a file and not on screen of the terminal. Output can also be
redirected to other devices, such as to a printer or another terminal
on the network.

iv. Piping Mechanism: Linux supplies ‗pipe‘ facility that allows


the output of one command to be used as input in another Linux
command. The Linux file system contains many utilities that are
useful for such purpose.

v. Metacharacter facility: Apart from the normal characters


found on a keyboard, (letters and digits) the shell provides the use
of ‗metacharacters‘. these allows the user to apply selection criteria
when accessing files, for example the user could access all files
that begin with the letter ‗m‘.

130
Operating System Concepts and Basic Linux Commands

vi. Background processing: The true multi-tasking facilities allow


the user to run commands in the background. This allows the
command to be processed in background while the user can
proceed with other task in foreground. When background task is
completed, the user is notified.
There are many number of shells available in Linux, Bourne
Shell is the oldest UNIX shell. The default shell on Linux
distribution is the GNU Bourne again shell (bash). Shells available
include the source version of the C Shell (tch), k shell (pdksh).

a. Bourne again Shell (bash): Bourne shell is the oldest UNIX


shell. It is the most widely distributed UNIX shell. It is probably
present on every existing UNIX system. The original UNIX shell
was written in the mid-1970s by Stephen R. Bourne while he was
at AT&T Bell Labs in New Jersey. The Bourne shell was the first
shell to appear on UNIX systems, thus it is referred to as "the
shell." The GNU/ Linux version of the Bourne (sh) shell is GNU
Bourne again shell (bash). It is written by Brian Fox and Ramey. It
is a feature enriched version of many of the feature found in the c
shell (sh) and korn shell (ksh). The Bourne Again shell, bash, was
developed as part of the GNU project and has replaced the Bourne
shell, sh, for GNU-based systems like Linux. All major Linux
distributions, including Red Hat, Slack ware, and Caldera, ship
with bash as their sh replacement.

b. Korn Shell (ksh): Korn shell (ksh) is unix shell originally


created by Dave Korn, is an also available in an open source form.
The public domain free version of the shell created by Eric Gisin is
called the public domain korn shell (pdksh). In march 2000, AT &
T released a freely downloadable version of the original korn shell
updated by Dave Korn in 1993, called ‗ksh93‘.

c. c shell: Bill Joy developed the C shell while he was at the


University of California at Berkeley in the early 1980s. It was
designed to make interactive use of the shell easier for users.
Another design goal was to change the syntax of the shell from the

131
Shital Vivek Ghate

Bourne shell's older ALGOL style to the newer C style. The open
source version of the c shell is tcsh.

5.2 Changing the running shell:


To check which shell is running , display the shell you are using.
This information is found in SHELL environment variable. Type
‗echo $SHELL‘ at the shell prompt and press Enter. If you are
using bash shell system will display the following massage:

[aditi @localhost /aditi]$ echo $SHELL


/bin/bash
[aditi @localhost /aditi]$

If you are not using bash shell and want to change to bash
shell, then type ‗bash‘ at the shell prompt and press Enter. The
system will display the following:

[aditi @localhost /aditi]$

Check again the shell you are using, type ‗echo $SHELL‘ and
press Enter.
To display the bash help function, type help and press Enter.
The help command lists all the commands that are built into the
shell.

5.3 Shell Prompt:


When you log in to your Linux account, you will see the shell
prompt. It will look as follows:

[aditi@localhost/aditi]$

The first aditi is the username and it is followed by the


hostname of the computer and the final aditi is the directory name
that user aditi is in presently. You are automatically placed in your
own user account when you first log in to the system.

132
Operating System Concepts and Basic Linux Commands

When you are logged in as root, the shell prompt displayed is


changes to use a pound sign (#) instead of the dollar sign ($).
[root@localhost/root]#

5.3.1 Changing the shell prompt:


The shell prompt can be changed to display information such as
the username for a user, the directory in which the user is working,
the current date and time or some special message. To change the
shell prompt for your current login session, follow the steps given
below:

1. Display the shell variables. At the shell prompt, type ‗printenv‘


and press Enter. If you cannot read the entire screen, send the
printenv command to the less pager.
(Type printenv |less)

2. Look for the PS1 variable. It may look as follows:


PS1=[\u@\r\w]\$
This variable tells the system to display the username for
whoever is using the host computer and that user‘s current working
directory inside brackets followed by the $ prompt ( or # prompt
for the root account).

3. Write down the information from the PS1 variable.

4. Decide how you want the prompt to look. Following table shows
the characters to customize the shell prompt.

133
Shital Vivek Ghate

Example: If you want to change the prompt so that it will


display the time, the date, the current working directory and the $
prompt, then type PS1= ‗[\t \d \w]\$‘ and press Enter. The shell
prompt will change to display the following: [12:18:40 Sat Jan 21
aditi]$
Once you log out of your account, the shell prompt will return
to the default shell prompt.

5.4 Creating user account:


When you installed Linux, you created a superuser (root user)
account. The shell loads automatically when you log in to your
account. Superuser account should be used only for system
administration and configuration job. To executing commands and
applications and exploring Linux operating system you need to
create a login for a user account. To create a login for new user
‗adduser‘ and ‗passwd‘ commands are used. Following are some
steps to create a new user account.

134
Operating System Concepts and Basic Linux Commands

1. Select a username. Each user on the system needs a unique login


name to access the system and his user account. Usernames should
be no more than eight characters. For the new user account the user
name we have selected is ‗aditi‘.

2. Choose a password. A good password should be a combination


of uppercase letters, lowercase letters and numbers. A password
should be at least eight characters.

3. Login as superuser. At the login prompt, type ‗root‘ and press


Enter. You are then prompted for password. Type the password
and press Enter. The shell prompt for superuser account look like
follows:

[root@localhost/root]#

4. Create the user account. At the shell prompt type ‗adduser aditi‘
and press Enter. Your display should look like as follows:

[root@localhost /root]# adduser aditi


[root@localhost /root]#

5. Assign the password to the account. Type ‗passwd aditi‘ and


press Enter. You will see the following:

[root@localhost /root]# passwd aditi


Changing password for user aditi
New password:

Type the password and press Enter. If the system uses shadow
passwords, the password does not appear on the screen. Verify the
password by entering it second time and press Enter.

Retype new password:


Passwd: all authentication tokens updated successfully.
[root@localhost /root]#

135
Shital Vivek Ghate

6. Press Alt+F2 to display a new terminal window and login


prompt is displayed as follows:

Linux Mandrake release 7.1b (hydrogen)


Kernel [Link].25mdk on an 1586 / tty2
Localhost login:

Here tty2 is the virtual terminal that you are currently using. If you
need to return to the superuser account, press Alt+F1.

7. Log in to your user account. At the login prompt, type the


username and press Enter. Your shell prompt will be displayed as
follows:

[aditi@localhost /aditi]$

5.5 Basic syntax for command:


Linux commands can be executed by typing its name on command
line, followed by option list , and then parameters. A typical
command line is as follows:

$ commandname –option [parameter]

The single line command is referred to as command line and it


is typed after the shell prompt. The command name always appears
first, it tells the shell WHAT to do. To execute the command
press Enter. The minimum usage for most commands is the
command and one or more command options.
Command options are typed after the command and are used
to modify the result of the command. There must be a space
between the command and the command option. Then, the dash (-)
character must precede the command option. The dash tells Linux
to treat each letter that follows the dash as a command option.
There is no space between dash and the command option. There
can be more than one command option, but do not put any space
between the dash and the option.

136
Operating System Concepts and Basic Linux Commands

For example:
$ls –l /usr/doc

The ls command displays a file list for the contents of the current
working directory. The command option ls –l, which modifies the
information displayed about each file in the listing. The parameter
tells the command to list the contents of the /usr/doc directory
instead of current working directory.
A command parameter can be a file, a directory, or a period of
time. Parameters are not preceded by dash. Parameter specify,
which file or directory a command is to be acted upon, or they can
set a time limit in which the command is to be executed.

5.6 Creating alias for long command:


A command alias is a unique short name that is assigned to a
command. It is possible assign a short name for a long command
using alias. To create an alias for the following command

cd /user/doc/file1

Suppose ‗move‘ is a sample alias name, to create this alias, type at


the shell prompt:

$alias move= ―cd/user/doc/file1‖

The alias command is followed by the alias name. the equal


sign (=)precedes the command that will be executed when the alias
name is typed at the shell prompt. Do not give the space to the
either side of equal sign. Now to change to ‗file1‘ directory just
type ‗move‘ and press Enter.

5.7 Input/output Redirection:


When you execute a command, the command is read from the
keyboard (the standard Input) and the result of the
command(output) appears on the screen (the standard output). If
you want the command output to appear in a different place, or if
you want to send the output to another command for processing ,
137
Shital Vivek Ghate

then we need to change the output with process called Input/output


redirection.

5.7.1 Redirecting Standard Output:


To redirect standard output, use ‗>‘ symbol. Placing ‗>‘ after a
command (e.g. cat command) will direct its output to the filename
following the symbol.
Using cat by itself simply outputs whatever you input to
screen, as if it were repeating line you just typed.

Example:

To redirect cat output to a file, type the following at shell prompt.

You can then use cat to read the file. At the shell prompt, type:

138
Operating System Concepts and Basic Linux Commands

When you redirect the output to a file, you can overwrite an


existing file, so that the name of the file you are creating does not
match name of pre-existing file, unless you want to replace it.

Use output redirection again for another file and name it [Link]
For example:

Now, use cat to join [Link] with [Link] and redirect output of
both files to a new
file [Link]

$cat [Link] [Link]>[Link]

After execution of the above command, execute the following


command

$cat [Link]
You will get the following output

139
Shital Vivek Ghate

In this example, cat has added [Link] and [Link] and the output
is stored to a new file [Link].

5.7.2 Appending standard output:


you can use output redirection to add new information to the end of
existing file. Use ‗>>‘ to append data to an existing file.
When you use ‗>>‘ symbol, you are adding information to a file,
rather than replacing the contents of a file entirely.
For example:
Take two already created files and join them by using the append
symbol.

$cat [Link]>>[Link]

Here the contents of [Link] are appended at the bottom of


[Link].

5.7.3 Redirecting Standard Input:


It is possible to perform the same type of redirection with standard
input. The standard input redirection symbol is ‗<‘, when you use
this symbol, you are telling the shell that you want the input to be
read from a file.

For example:
$cat<[Link]

Here the output of [Link] was read by cat command and it


displays contents of the file [Link].
140
Operating System Concepts and Basic Linux Commands

5.7.4 Pipe lines:


Pipes (| character) is used to redirect output from one command to
become the input to another command. This reduces the necessity
of writing new programs for complex operation. A pipe line is a
mechanism, which accepts the output of a command as its input for
the next command. Using the pipe operator ―|‖ (vertical bar), the
standard output of one command can be piped into the standard
input of another:
command1| command2

for example:

The ‗who‘ command produces a list of users, one user per line.
Then wc command will count the number of lines and the output
will appear on the screen.
Any command that writes to standard output can be used on
the left hand side of pipe. Any command that reads from standard
input can be used on the right hand side of pipe. Filters are
commands that accepts data from standard input and write data to
standard output, can be used anywhere in the pipeline. Multiple
commands can be chained together with pipes.

5.7.5 Filters
Pipelines are often used to perform complex operations on data. It
is possible to put several commands together into a pipeline.
Frequently, the commands used this way are referred to as filters.
Filters take input, change it somehow and then output it. The first
one we will try is sort. Imagine we wanted to make a combined list
of all of the executable programs in /bin and /usr/bin, put them in
sorted order and view it:

[aditi@localhost ~]$ ls /bin /usr/bin | sort | less

141
Shital Vivek Ghate

Since we specified two directories (/bin and /usr/bin), the


output of ls would have consisted of two sorted lists, one for each
directory. By including sort in our pipeline, we changed the data to
produce a single, sorted list.

uniq - Report Or Omit Repeated Lines


The uniq command is often used in conjunction with sort. uniq
accepts a sorted list of data from either standard input or a single
filename argument (see the uniqman page for details) and, by
default, removes any duplicates from the list. So, to make sure our
list has no duplicates (that is, any programs of the same name that
appear in both the /bin and /usr/bin directories) we will add uniq to
our pipeline:

[aditi@localhost ~]$ ls /bin /usr/bin | sort | uniq | less

In this example, we use uniq to remove any duplicates from the


output of the sort command. If we want to see the list of duplicates
instead, we add the ―-d‖ option to
uniq like so:

[aditi@localhost ~]$ ls /bin /usr/bin | sort | uniq -d | less

wc– Print Line, Word, And Byte Counts


The wc(word count) command is used to display the number of
lines, words, and bytes contained in files. For example:

[aditi@localhost ~]$ wc [Link]


7902 64566 503634 [Link]

In this case it prints out three numbers: lines, words, and bytes
contained in [Link]. Like our previous commands, if
executed without command line arguments, wc accepts standard
input. The ―-l‖ option limits its output to only report lines. Adding
it to a pipeline is a handy way to count things. To see the number
of programs we have in our sorted list, we can do this:

142
Operating System Concepts and Basic Linux Commands

[aditi@localhost ~]$ls /bin /usr/bin | sort | uniq | wc -l


2728

grep– Print Lines Matching A Pattern


grep is a powerful program used to find text patterns within files.
It's used like this:
grep pattern[file...]

When grep encounters a ―pattern‖ in the file, it prints out the


lines containing it. The patterns that grep can match can be very
complex, but for now we will concentrate on simple text matches.
Let's say we want to find all the files in our list of programs
that had the word ―zip‖ embedded in the name. Such a search
might give us an idea of some of the programs on our system that
had something to do with file compression. We would do this:

There are a couple of handy options for grep: ―-i‖ which


causes grep to ignore case when performing the search (normally
searches are case sensitive) and ―-v‖ which tells grep to only print
lines that do not match the pattern.

head/ tail– Print First / Last Part Of Files


Sometimes you don't want all of the output from a command. You
may only want the first few lines or the last few lines. The head
143
Shital Vivek Ghate

command prints the first ten lines of a file and the tail command
prints the last ten lines. By default, both commands print ten lines
of text, but this can be adjusted with the ―-n‖ option:

[aditi@localhost ~]$head -n 5 [Link]

Above command prints the first 5 lines of file [Link].

[aditi@localhost ~]$tail -n 5 [Link]

The above command prints last 5 lines of file [Link]

tee– Read From Stdinput And Output To Stdoutput And Files


The tee creates a ―tee‖ fitting on our pipe. The tee program reads
standard input and copies it to both standard output and to one or
more files. This is useful for capturing a pipeline's contents at an
intermediate stage of processing. Here we repeat one of our earlier
examples, this time including tee to capture the entire directory
listing to the file [Link] before grep filters the pipeline's contents:

here the output of command ‗ls/usr/bin‘ is stored in file ‗[Link]‘


after that grep searches the pattern ‗zip‘.

144
Operating System Concepts and Basic Linux Commands

5.8 Listing files and directories: (ls command)


In UNIX there are three basic types of files:
 Ordinary Files
 Directories
 Special Files
An ordinary file is a file on the system that contains data, text,
or program instructions. Directories store both special and ordinary
files. Special files provide access to hardware such as hard drives,
CD-ROM drives, modems, and Ethernet adapters. Other special
files are similar to aliases or shortcuts and enable you to access a
single file using different names.

ls command:
First, list the files and directories stored in the current directory.
Use the following command:
$ ls
Here's a sample directory listing:

This output indicates that several items are in the current directory,
but this output does not tell us whether these items are files or
directories. To find out which of the items are files and which are
directories, specify the –F option to ls:

$ ls –F

Now the output for the directory is slightly different:

145
Shital Vivek Ghate

As you can see, some of the items now have a /at the end: each of
these items is a directory. The other items, such as hw1, have no
character appended to them. This indicates that they are ordinary
files. When the –F option is specified to ls, it appends a character
indicating the file type of each of the items it lists.

Following table shows all the options for ls command:

To list invisible files, specify the –a option to ls:

$ ls –a

The directory listing now looks like this:

As you can see, this directory contains many invisible files. Notice
that in this output, the file type information is missing. To get the
file type information, specify the –F and the –a options as follows:

$ ls -a –F
146
Operating System Concepts and Basic Linux Commands

The output changes to the following:

With the file type information you see that there are two hidden
directories (.and ..). These two directories are special entries
that are present in all directories. The first one, ., represents the
current directory. The second one, .., represents the parent
directory.

Option Grouping :
In the previous example, the command that you used specified the
options to ls separately. These options can also be grouped
together. For example, the commands

$ ls -aF
$ ls -Fa
are the same as the command
$ ls -a –F

5.9 cat command:


To view the content of a file, use the cat (short for concatenate)
command. Its syntax is as follows:

$cat ‗files‘

Here ‗files‘ are the names of the files that you want to view. For
example,

147
Shital Vivek Ghate

options:
-n : used to display numbering to the output lines. The numbered
output shows us that the last line in this file is blank.

-b: skip numbering blank lines using the –b option:


In this case the output looks like the following:

The blank line is still there, it is no longer numbered.

5.10 wc command:
You can use the wc command to get a count of the total number of
lines, words, and characters contained in a file. The basic syntax of
this command is
$wc [options] files

148
Operating System Concepts and Basic Linux Commands

Files are the files you want examined.

5.11 Manipulating files and directories:

5.11.1 Copying Files (cp)


To make a copy of a file use the cp command. The basic syntax of
the command is

$cp source destination

Here source is the name of the file that is copied and destination is
the name of the copy. For example, the following command makes
a copy of the file test_results and places the copy in a file named
test_results.orig:

$ cp test_results test_results.orig

Copying Files to a Different Directory


If the destination is a directory, the copy has the same name as the
source but is located in the destination directory. For example, the
command

$ cp test_results work/

places a copy of the file test_results in the directory work.

5.11.2 Renaming Files & moving files and directories( mv):


To change the name of a file use the mv command. Its basic syntax
is

$mv source destination

Here source is the original name of the file and destination is the
new name of the file. As an example,

149
Shital Vivek Ghate

$ mv test_result test_result.orig

changes the name of the file test_result to test_result.orig. it is also


used to move files and directories between different locations in
the directory tree. The basic syntax is this:

$mv source destination


Here source is the name of the file or directory you want to move,
and destination is the directory where you want the file or directory
to end up. For example

$ mv /home/aditi/names /tmp

moves the file names located in the directory /home/aditi to the


directory /tmp.
Moving a directory is exactly the same:

$ mv docs/ work/
moves the directory docs into the directory work.

5.11.3 Removing Files ( rm):


To remove files use the rm command. The syntax is

$rm files

Here files is a list of one or more files to remove. For example, the
command

$ rm res.01 res.02

removes the files res.01and res.02.

5.11.4 pwd command:


It displays the present working directory of user.
$ pwd
/home/aditi
150
Operating System Concepts and Basic Linux Commands

This indicates that I am in my home directory. Your home


directory is the initial directory where you start when you log in to
a UNIX machine.

5.11.5 Changing Directories (cd ):


You can use the cd to change to any directory by specifying a
valid path. The syntax is as follows:
$cd directory
Here, directory is the name of the directory that you want to
change to. For example, the command

$ cd /usr/local/bin

changes to the directory /usr/local/bin.

5.11.6 Creating Directories(mkdir):


You can create directories with the mkdir command. Its syntax is

$mkdir directory

Here, directory is the absolute or relative pathname of the directory


you want to create. For example, the command

$ mkdir new
creates the directory ‗new‘ in the current directory. Here is another
example:

$ mkdir /tmp/test-dir
This command creates the directory test-dir in the /tmp directory.
The mkdir command produces no output if it successfully creates
the requested directory.

If you give more than one directory names on the command line,
mkdir creates each of the directories. For example

$ mkdir docs pub

151
Shital Vivek Ghate

creates the directories docs and pub under the current directory.

5.11.7 Removing directories(rmdir):


This command is used to remove directories. Its syntax is :

$rmdir directories

Here, directories includes the names of the directories you want


removed. For example, the command

$ rmdir ch01 ch02 ch03

removes the directories ch01, ch02, and ch03 if they are empty.
The rmdir command produces no output if it is successful. A
directory can be deleted only if it is empty( does not contain files
or subdirectory). Current directory can not be deleted. Complete
path names can also be specified with rmdir as shown in mkdir
command.

5.12 vi Editor:
The first version of vi was written in 1976 by Bill Joy, a University
of California at Berkley student who later went on to co-found Sun
Microsystems. vi derives its name from the word ―visual,‖ because
it was intended to allow editing on a video terminal with a moving
cursor.
The vi editor is the original editor used on Unix systems. It
uses the console graphics mode to emulate a text-editing window,
allowing you to visually see the lines of your file, move around
within the file, and insert, edit, and replace text.
Most Linux distributions don't include real vi; rather, they ship
with an enhanced replacement called vim (which is short for ―vi
improved‖) written by Bram Moolenaar. vim is a substantial
improvement over traditional Unix vi and is usually symbolically
linked (or aliased) to the name ―vi‖ on Linux systems. In the
discussions that follow, we will assume that we have a program
called ―vi‖ that is really vim.

152
Operating System Concepts and Basic Linux Commands

5.12.1 Starting And Stopping vi:


To start vi, we simply type the following:

[aditi@localhost/aditi]$vi

And a screen like this should appear:

To exit, we enter the following command (note that the colon


character is part of the command):
:q
The shell prompt should return. If, for some reason, vi will not quit
(usually because we made a change to a file that has not yet been
saved), we can tell vi that we really mean it by adding an
exclamation point to the command:
:q!

If you get ―lost‖ in vi, try pressing the Esc key twice to find your
way [Link] running vim instead of vi. If that works, consider
adding alias vi='vim' to your shell file.

153
Shital Vivek Ghate

5.12.2 Editing Modes:


Let's start up vi again, this time passing to it the name of a non
existing file. This means we are creating a new file with vi:
[aditi@localhost/aditi]$vi [Link]

If all goes well, we should get a screen like this:

The leading tilde characters (―~‖) indicate that no text exists


on that line. This shows that we have an empty file. Do not type
anything yet! The second most important thing to learn about
vi(after learning how to exit) is that vi is a modal editor. When vi
starts up, it begins in command mode. In this mode, almost every
key is a command, so if we were to start typing, vi would basically
go crazy and make a big mess.

5.12.3 Insert Mode(Input mode):


In order to add some text to our file, we must first enter insert
mode by pressing the ―i‖ key. We should see the following at the
bottom of the screen if vim is running in its usual enhanced mode.

--INSERT--

Now we can enter some text. Try this:

This is the first day of my college.

154
Operating System Concepts and Basic Linux Commands

To exit insert mode and return to command mode, press the Esc
key.

5.12.4 Saving Our Work:


To save the change we just made to our file, we must enter an ex
command while in command mode. This is easily done by pressing
the ―:‖ key. After doing this, a colon character should appear at the
bottom of the screen:
:
To write our modified file, we follow the colon with a ―w‖ then
Enter:
:w
The file will be written to the hard drive and we should get a
confirmation message at the bottom of the screen, like this:
"[Link]" [New] 1L, 36C written
Following table shows the saving and quitting commands.

5.12.5 Moving The Cursor Around:


While in command mode, vi offers a large number of movement
commands, some of which are given in following table:

155
Shital Vivek Ghate

5.12.6 Adding new text in existing file:


Following keys are used to append text to the file in Input mode:

If we move the cursor to the end of the line and type ―a‖, the
cursor will move past the end of the line and vi will enter insert
mode. This will allow us to add some more text:

This is the first day of my college. It is cool.


Now press the Esc key to exit insert mode.
Move the cursor to the beginning of the line using the ―0‖ (zero)
command.

156
Operating System Concepts and Basic Linux Commands

Now we type ―A‖ and add the following lines of text:

Again, press the Esc key to exit insert mode.

Following keys are used to open new line in file in Input mode:

Now place the cursor on ―Line 3‖ then press the o key.

A new line was opened below the third line and we entered
insert mode. Exit insert mode by pressing the Esc key. Press the u
key to undo our change.
Press the O key to open the line above the cursor:

157
Shital Vivek Ghate

5.12.7 Deleting Text:


For text deletion many commands are used, they are given in
following table.

Place the cursor on the word ―It‖ on the first line of our text.
Press the x key repeatedly until the rest of the sentence is deleted.
Next, press the u key repeatedly until the deletion is undone.
Again, move the cursor to the word ―It‖ and press dW to
delete the word:

158
Operating System Concepts and Basic Linux Commands

5.12.8 Cutting, Copying And Pasting Text:


The d command not only deletes text, it also ―cuts‖ text. Each time
we use the d command the deletion is copied into a paste buffer
(think clipboard) that we can later recall with the p command to
paste the contents of the buffer after the cursor or the P command
to paste the contents before the cursor.
The y command is used to ―yank‖ (copy) text in much the
same way the d command is used to cut text. Here are some
examples combining the y command with various movement
commands:

Place the cursor on the first line of the text and type yy to copy
the current line. Next, move the cursor to the last line (G) and type
p to paste the line below the current line:

159
Shital Vivek Ghate

Just as before, the u command will undo our change. With the
cursor still positioned on the last line of the file, type P to paste the
text above the current line:

5.13 Compressing files (gzip, gunzip commands):


The gzip program is used to compress one or more files. When
executed, it replaces the original file with a compressed version of
the original. The corresponding gunzip program is used to restore
compressed files to their original, uncompressed form. Here is an
example

In this example, we create a text file named [Link] from a


directory listing. Next, we run gzip, which replaces the original file
with a compressed version named [Link]. In the directory

160
Operating System Concepts and Basic Linux Commands

listing of first.*, we see that the original file has been replaced with
the compressed version, and that the compressed version about
one-fifth the size of the original. We can also see that the
compressed file has the same permissions and time stamp as the
original. Next, we run the gunzip program to uncompress the file.
Afterward, we can see that the compressed version of the file has
been replaced with the original, again with the permissions and
time stamp preserved.

bzip2
The bzip2 program, by Julian Seward, is similar to gzip, but uses a
different compression algorithm that achieves higher levels of
compression at the cost of compression speed. In most regards, it
works in the same fashion as gzip. A file compressed with bzip2 is
denoted with the extension .bz2:

As we can see, bzip2can be used the same way as gzip.

5.14 Archiving Files( tar):


Archiving is the process of gathering up many files and bundling
them together into a single large file. Archiving is often done as a

161
Shital Vivek Ghate

part of system backups. It is also used when old data is moved


from a system to some type of long-term storage.

tar:
The tar program is the classic tool for archiving files. Its name,
short for tape archive, reveals its roots as a tool for making backup
tapes .While it is still used for that traditional task, it is equally
adept on other storage devices as well. We often see filenames that
end with the extension .tar or .tgz which indicate a ―plain‖ tar
archive and a gzipped archive, respectively. A tar archive can
consist of a group of separate files, one or more directory
hierarchies, or a mixture of both. The command syntax is:

tar mode[options] pathname...

where mode is one of the following operating modes :

For creating archive of directory ‗newone‘ which contains many


file issue the following command:

[aditi@localhost ~]$ tar cf [Link] newone

This command creates a tar archive named [Link] that


contains the entire dectory hierarchy. We can see that the mode
and the f option, which is used to specify the name of the tar
archive, may be joined together, and do not require a leading dash.
Note, however, that the mode must always be specified first,
before any other option.
162
Operating System Concepts and Basic Linux Commands

To list the contents of the archive, we can do this:

[aditi@localhost ~]$ tar tf [Link]

For a more detailed listing, we can add the v(verbose) option:

[aditi@localhost ~]$ tar tvf [Link]

Now, let‘s extract the newone in a new location. We will do this by


creating a new directory named foo, and changing the directory
and extracting the tar archive:

If we examine the contents of ~/foo/newone, we see that the


archive was successfully installed, creating a precise reproduction
of the original files. There is one caveat, however: unless you are
operating as the superuser, files and directories extracted from
archives take on the ownership of the user performing the
restoration, rather than the original owner.

5.15 Managing disk space: df, du


A quick way to get a summary of the available and used disk space
on your Linux system is to type in the df command. The command
df stands for "disk file system". With the -h option (df -h) it shows
the disk space in "human readable" form, which in this case means,
it gives you the units along with the numbers.
The output of the df command is a table with six columns. The
first column contains the file system path, which can be a reference
to a hard disk or another storage device, or a file system connected
through the network. The second column shows the capacity of

163
Shital Vivek Ghate

that file system. The third column shows the used space, the fourth
column shows the available space and the fifth column shows the
used space percentage and the last column shows the path on
which that file system is mounted. The mount point is the place in
the directory tree where you can find and access the file system.
The df tool simply reports the amount of free space on each
partition — how large they are, etc. It also provides information on
non-local file systems (such as mounted NFS or Samba shares). In
its most basic form, df provides the following:

However, you can add options to df to show the file system


type and show the sizes in an easier to understand format:

While df provides an overview of entire partitions, the du tool


will summarize the size of a given directory, broken down by
subdirectories: The du command shows the disk space used by
the files and directories in the current directory. Again the -h
option (df -h) makes the output easier to comprehend. By default,
the du command lists all subdirectories to show how much disk
space each has occupied. This can be avoided with the -s option (df
-h -s). This only shows a summary. Namely the combined disk
space used by all subdirectories. If you want to show the disk
usage of a directory (folder) other than the current directory, you
simply put that directory name as the last argument. For example:
du -h -s images, where "images" would be a subdirectory of the
current directory.

164
Operating System Concepts and Basic Linux Commands

Of course, to summarize the directory and all subdirectories


and display size values in a human-readable format, use:

5.16 Changing Your Password


To set or change a password, the passwd command is used. The
command syntax looks like this:

passwd [user]

To change your password, just enter the passwd command. You


will be prompted for your old password and your new password:

The passwd command will try to enforce use of ―strong‖


passwords. This means the it will refuse to accept passwords that
are too short, too similar to previous passwords, are dictionary
words, or too easily guessed:

165
Shital Vivek Ghate

If you have superuser privileges, you can specify a user name


as an argument to the passwd command to set the password for
another user. There are other options available to the superuser to
allow account locking, password expiration, etc.

5.17 File access permissions:


Every file on Linux has three types of permissions, read, write and
execute. The command chmod allows to decide who can and
cannot read, write and use a file. To assign these three types of
permissions, the following three symbols are used.

To specify then user to whom you are grant these permissions,


use these three symbols.

166
Operating System Concepts and Basic Linux Commands

Normally when a file is created, it is created with read, write


and execute permissions to the user and only read and execute
permissions for group members and other system users. Write
permission is not given normally to the group and other users. If it
is required, the user can assign permissions by system commands.
The currently existing permissions of files and directories can be
determined by using long listing of directories (ls –l) command.

For example:
User is logged in with name ‗funny‘

The explanation of the permissions for the prog1 and sample1


files and directory dir1 is given in following table:

The initial character describes the file or directory.


‗-‗ (dash) symbolizes an ordinary file and ‗d‘ symbolizes a
directory. There are nine possible permissions which can be given

167
Shital Vivek Ghate

or refused in combination. They are written as string of nine


characters.

For example: rwx r-x r-x


In this example user(owner) has all the three permissions read (r) ,
write(w), execute(x) and group and other system users have read( r
) and execute (x) permissions.

5.18 Granting access to files: (chmod command)


You can change existing permissions by executing chmod
command. This command uses two methods:
1. Symbolic method 2. Octal method

Symbolic method:
Syntax of symbolic method is given below:

chmod who + permissions file(s)

where:
chmod -name of command.
who -one of three user groups ( u, g, o or a) u=user/owner,
g=group,
o=others a=all
+ -means grant permissions
- -means deny permissions
Permissions –any combination of three (r, w, x)
Files -file or directory name

168
Operating System Concepts and Basic Linux Commands

For example:
$chmod u+r prog1
This command added read permission to the owner for file prog1.
$chmod u-w prog1
This command denies write permission to the owner for file prog1.
If you omit the scope( u, g or o) the permissions given apply to all
the three groups.

Octal method:
This format requires to specify permissions using three octal
numbers, ranging from 0 to 7. the octal code can be one to three
digit long. the first, second and third digits set the user, group and
other user permissions respectively. The integer used in each
position dictates the permissions for the files. Following table
shows the digits with meaning.

For example:
$chmod 750 sample1

This examples changes access permissions for the file sample1 in


the current working directory. The 7 gives permissions for the
owner of the file to read, write and execute that file, the 5 gives
permission for group members to read and execute that file and 0
denies permissions for other users to read, write or execute that
file.

169
Shital Vivek Ghate

5.19 Creating group account


The command groupadd is used to create a new group.
The groupadd command creates a new group account using the
values specified on the command line plus the default values from
the system. The new group will be entered into the system files as
needed.

Syntax:
groupadd [options] group
Options:
-f, --force

This option causes the command to simply exit with success status
if the specified group already exists. When used with -g, and the
specified GID already exists, another (unique) GID is chosen (i.e. -
g is turned off).

-g, --gid GID


The numerical value of the group's ID. This value must be unique,
unless the -o option is used. The value must be non-negative. The
default is to use the smallest ID value greater than 999 and greater
than every other group. Values between 0 and 999 are typically
reserved for system accounts.

-h, --help
Display help message and exit.

5.20. Sudo command:


The administrator can configure sudo to allow an ordinary user to
execute commands as a different user (usually the superuser) in a
very controlled way. In particular, a user may be restricted to one
or more specific commands and no others. The sudo provides
superuser privileges. The use of sudo does not require access to the
superuser's password. To authenticate using sudo, the user uses
his/her own password.
Let's say, for example, that sudo has been configured to allow
us to run a fictitious
170
Operating System Concepts and Basic Linux Commands

backup program called ―backup_script‖, which requires superuser


privileges. With sudoit would be done like this:

After entering the command, we are prompted for our


password (not the superuser's) and once the authentication is
complete, the specified command is carried out.

5.21 chown – Change File Owner And Group


The chown command is used to change the owner and group
owner of a file or directory. Superuser privileges are required to
use this command. The syntax of chown looks like this:

chown [owner][:[group]] file

chown can change the file owner and/or the file group owner
depending on the first argument of the command. Here are some
examples:
chown Argument Examples:

Let's say that we have two users: adi1, who has access to
super-user privileges and aditi, who does not. User adi1 wants to
copy a file from her home directory to the home directory of user

171
Shital Vivek Ghate

aditi. Since user adi1 wants aditi to be able to edit the file, adi1
changes the ownership of the copied file from adi1 to aditi:

Here we see user adi1 copy the file from his directory to the
home directory of user aditi. Next, adi1 changes the ownership of
the file from root (a result of using sudo) aditi. Using the trailing
colon in the first argument, adi1 also changed the group ownership
of the file to the login group of aditi, which happens to be group
aditi. Notice that after the first use of sudo, adi1 was not prompted
for her password? This is because sudo, in most configurations,
―trusts‖ you for several minutes until its timer runs out.

5.22 Communication commands:

5.22.1 who
This command displays a list of all the people, or users, who are
currently using the UNIX machine. The first column of the output
lists the usernames of the people who are logged in.

You can see that there are three users, atha, yash, and sanu.
The second column lists the terminals they are logged in to, and
the final column lists the time they logged in. The output varies
from system to system.

172
Operating System Concepts and Basic Linux Commands

5.22.2 who am i
You can use the who command to gather information about
yourself when you execute it as follows:

This tells me the following information:


l My username is atha.
2 I am logged in to the terminal pts/0.
3 I logged in at 8:49 on Dec 9.

The arguments am and i change the behavior of the who


command to list information about you only. In UNIX, most
commands accept arguments that modify their behavior.

5.22.3 mesg command:


Syntax:
mesg [y] [n]

The mesg command stops the incoming messages that were


initiated through other UNIX communication commands.
This command allows users to set the permissions on their
terminal so that read and write access is denied to all users apart
from themselves i.e., the owner of the terminal. Mesg has two
arguments: ‗y‘ and ‗n‘ (yes/no). mesg used without any arguments
simply returns the state of the current setting i.e.

y -message can be sent to the terminal, or


n -message cannot be sent to the terminal
we have to specify a single argument at time for example:

$mesg n
This command disabled all the incoming messages.

173
Shital Vivek Ghate

$mesg y
This command allows all the incoming messages from other users
through communication commands.

5.22.4 write command:


Syntax:
write <username> [ttyname]

This is two way communication command. Here <username>


is the recipient user, and [ttyname], is the name of the terminal that
appears in the /dev directory. With the use of write command it is
possible to write a message to the other user‘s terminal and that
recipient user can also write back to the sender‘s terminal and thus
a two way communications link can be established between two
users.
In this command a username must be specified, and an
optional terminal code can be included, if required, for example
when a user is logged in on more than one terminal and you want
to contact to a specific terminal.
When write is invoked, it reads from the standard input, so
you can simply type in your message, in the required format. You
can end the session by pressing <ctrl-d>, and the message will be
dispatched. The sender will be returned to the shell prompt. If the
person whom you were contacting was in the middle of another
task e.g. in an editing session etc., they will have to escape to the
shell level to write back. To check how many users are currently
logged in you can use the ‗who‘ command.

For example:

174
Operating System Concepts and Basic Linux Commands

In the above example we used both the options terminal name


and username.
If we tried to the user who is not logged in we receive a
following error message:

When you receive a message from a user , Linux displays a


one-line message indicating the name of the person who is
contacting you, the terminal from which the message originated,
and the date and time the message was sent. A small beep also
accompanies the message to attract your attention:

5.22.5 talk command:


The talk command is similar to write command except that it
reserves a separate area of screen for both the sender and recipient
users. The syntax is exactly the same as the write command.

Syntax:
talk <username> [ttyname]

When talk is activated with valid username or with a valid


terminal name or both, the current screen is cleared, and the
sender‘s screen is divided into two areas, separated by a single
line. The recipient user‘s screen, when he or she responds to the
sender, will also be divided into two separate parts. The top half of
the screen is where the sender will type a message to be sent to the
recipient. Assume we had typed the following command:
175
Shital Vivek Ghate

$ talk aditi
This command displays the following screen:

Fig 5.1: a typical screen when talk is invoked

A two screened window is opened, and talk contact the


recipient user, in this case user aditi. When talk is initially invoked,
a message is displayed on the recipient‘s screen.

This message tells the recipient that somebody is trying to


make contact, and that the recipient can respond with the command
‗talk aditi‘. It is necessary for both the sender and recipient to
each invoke the talk command.
When the connection with the recipient user has been
established i.e. the recipient user has invoked talk with the
sender‘s recipient name, talk display the message ‗Connection
established‘ and both users can proceed to type their messages.
Following figure illustrate a typical conversation after a connection
has established between the sender and recipient party.

176
Operating System Concepts and Basic Linux Commands

Fig.5.2: typical conversation using talk command.

To a conversation, the <ctrl-c> (interrupt) or <ctrl-d> key can be


used to close the connection. Both the sender and recipient will
then receive a message indicating that the connection has been
terminated.

5.22.6 wall command:


Wall stands for write all. It writes a message on a recipient‘s
terminal, reading the message from the standard input (keyboard).
Wall delivers the message to all the users on the system. Once the
command has read the message from the standard input, it
proceeds to write it simultaneously on every user‘s terminal.
The super-user normally uses the wall command to deliver
important system messages and can override any mesg n setting (
since the super user can write to any terminal at any time whatever
protection setting are in effect).

For example:

177

You might also like