Unit 2 Linux
Unit 2 Linux
LINUX PROGRAMMING
UNIT 2
A directory is a file the solo job of which is to store the file names and the related
information. All the files, whether ordinary, special, or directory, are contained in
directories.
Unix uses a hierarchical structure for organizing files and directories. This structure
is often referred to as a directory tree. The tree has a single root node, the slash
character (/), and all other directories are contained below it.
Home Directory
The directory in which you find yourself when you first login is called your home
directory.
You will be doing much of your work in your home directory and subdirectories that
you'll be creating to organize your files.
You can go in your home directory anytime using the following command −
$cd ~
$
Here ~ indicates the home directory. Suppose you have to go in any other user's
home directory, use the following command −
$cd ~username
$
To go in your last directory, you can use the following command −
$cd -
$
Absolute/Relative Pathnames
Directories are arranged in a hierarchy with root (/) at the top. The position of any file
within the hierarchy is described by its pathname.
Elements of a pathname are separated by a /. A pathname is absolute, if it is
described in relation to root, thus absolute pathnames always begin with a /.
Following are some examples of absolute filenames.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
A pathname can also be relative to your current working directory. Relative
pathnames never begin with /. Relative to user amrood's home directory, some
pathnames might look like this −
chem/notes
personal/res
To determine where you are within the filesystem hierarchy at any time, enter the
command pwd to print the current working directory −
$pwd
/user0/home/amrood
Listing Directories
To list the files in a directory, you can use the following syntax −
$ls dirname
Following is the example to list all the files contained in /usr/local directory −
$ls /usr/local
Creating Directories
We will now understand how to create directories. Directories are created by the
following command −
$mkdir dirname
Here, directory is the absolute or relative pathname of the directory you want to
create. For example, the command −
$mkdir mydir
$
Creates the directory mydir 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 on the command line, mkdir creates each of the
directories. For example, −
$mkdir docs pub
$
Creates the directories docs and pub under the current directory.
Removing Directories
Directories can be deleted using the rmdir command as follows −
$rmdir dirname
$
Note − To remove a directory, make sure it is empty which means there should not
be any file or sub-directory inside this directory.
You can remove multiple directories at a time as follows −
$rmdir dirname1 dirname2 dirname3
$
The above command removes the directories dirname1, dirname2, and dirname3, if
they are empty. The rmdir command produces no output if it is successful.
Changing Directories
You can use the cd command to do more than just change to a home directory. You
can use it to change to any directory by specifying a valid absolute or relative path.
The syntax is as given below −
$cd dirname
$
Here, dirname 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. From this directory, you can cd to the
directory /usr/home/amrood using the following relative path −
$cd ../../home/amrood
$
Renaming Directories
The mv (move) command can also be used to rename a directory. The syntax is as
follows −
$mv olddir newdir
$
You can rename a directory mydir to yourdir as follows −
$mv mydir yourdir
$
Changing Permissions
To change the file or the directory permissions, you use the chmod (change mode)
command. There are two ways to use chmod — the symbolic mode and the absolute
mode.
Using chmod in Symbolic Mode
The easiest way for a beginner to modify file or directory permissions is to use the
symbolic mode. With symbolic permissions you can add, delete, or specify the
permission set you want by using the operators in the following table.
2
-
Removes the designated permission(s) from a file or directory.
3
=
Sets the designated permission(s).
Here's an example using testfile. Running ls -1 on the testfile shows that the file's
permissions are as follows −
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on the testfile,
followed by ls –l, so you can see the permission changes −
$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Here's how you can combine these commands on a single line −
$chmod o+wx,u-x,g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
0 No permission ---
1 Execute permission --x
Here's an example using the testfile. Running ls -1 on the testfile shows that the file's
permissions are as follows −
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on the testfile,
followed by ls –l, so you can see the permission changes −
$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile
$chmod 743 testfile
$ls -l testfile
-rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod 043 testfile
$ls -l testfile
----r---wx 1 amrood users 1024 Nov 2 00:10 testfile
Changing Ownership
The chown command changes the ownership of a file. The basic syntax is as follows
−
$ chown user filelist
The value of the user can be either the name of a user on the system or the user id
(uid) of a user on the system.
The following example will help you understand the concept −
$ chown amrood testfile
1 vi filename
Creates a new file if it already does not exist, otherwise opens
an existing file.
2 vi -R filename
Opens an existing file in the read-only mode.
3 view filename
Opens an existing file in the read-only mode.
Following is an example to create a new file testfile if it already does not exist in the
current working directory −
$vi testfile
The above command will generate the following output −
|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]
You will notice a tilde (~) on each line following the cursor. A tilde represents an
unused line. If a line does not begin with a tilde and appears to be blank, there is a
space, tab, newline, or some other non-viewable character present.
You now have one open file to start working on. Before proceeding further, let us
understand a few important concepts.
Operation Modes
While working with the vi editor, we usually come across the following two modes −
Command mode − This mode enables you to perform administrative tasks
such as saving the files, executing the commands, moving the cursor, cutting
(yanking) and pasting the lines or words, as well as finding and replacing. In
this mode, whatever you type is interpreted as a command.
Insert mode − This mode enables you to insert text into the file. Everything
that's typed in this mode is interpreted as input and placed in the file.
vi always starts in the command mode. To enter text, you must be in the insert mode
for which simply type i. To come out of the insert mode, press the Esc key, which will
take you back to the command mode.
Hint − If you are not sure which mode you are in, press the Esc key twice; this will
take you to the command mode. You open a file using the vi editor. Start by typing
some characters and then come to the command mode to understand the difference.
Getting Out of vi
The command to quit out of vi is :q. Once in the command mode, type colon, and 'q',
followed by return. If your file has been modified in any way, the editor will warn you
of this, and not let you quit. To ignore this message, the command to quit out of vi
without saving is :q!. This lets you exit vi without saving any of the changes.
The command to save the contents of the editor is :w. You can combine the above
command with the quit command, or use :wq and return.
The easiest way to save your changes and exit vi is with the ZZ command. When
you are in the command mode, type ZZ. The ZZ command works the same way as
the :wq command.
If you want to specify/state any particular name for the file, you can do so by
specifying it after the :w. For example, if you wanted to save the file you were working
on as another filename called filename2, you would type :w filename2 and return.
1 K
Moves the cursor up one line
2 J
Moves the cursor down one line
3 H
Moves the cursor to the left one character position
4 L
Moves the cursor to the right one character position
1
0 or |
Positions the cursor at the beginning of a line
2
$
Positions the cursor at the end of a line
3
W
Positions the cursor to the next word
4
B
Positions the cursor to the previous word
5
(
Positions the cursor to the beginning of the current sentence
6
)
Positions the cursor to the beginning of the next sentence
7
E
Moves to the end of the blank delimited word
8
{
Moves a paragraph back
9
}
Moves a paragraph forward
10
[[
Moves a section back
11
]]
Moves a section forward
12
n|
Moves to the column n in the current line
13
1G
Moves to the first line of the file
14
G
Moves to the last line of the file
15
nG
Moves to the nth line of the file
16
:n
Moves to the nth line of the file
17
Fc
Moves forward to c
18
Fc
Moves back to c
19
H
Moves to the top of the screen
20
nH
Moves to the nth line from the top of the screen
21
M
Moves to the middle of the screen
22
L
Move to the bottom of the screen
23
nL
Moves to the nth line from the bottom of the screen
24
:x
Colon followed by a number would position the cursor on the line number
represented by x
Control Commands
The following commands can be used with the Control Key to performs functions as
given in the table below −
Given below is the list of control commands.
Sr.No. Command & Description
1
CTRL+d
Moves forward 1/2 screen
2
CTRL+f
Moves forward one full screen
3
CTRL+u
Moves backward 1/2 screen
4
CTRL+b
Moves backward one full screen
5
CTRL+e
Moves the screen up one line
6
CTRL+y
Moves the screen down one line
7
CTRL+u
Moves the screen up 1/2 page
8
CTRL+d
Moves the screen down 1/2 page
9
CTRL+b
Moves the screen up one page
10
CTRL+f
Moves the screen down one page
11
CTRL+I
Redraws the screen
Editing Files
To edit the file, you need to be in the insert mode. There are many ways to enter the
insert mode from the command mode −
1 I
Inserts text before the current cursor location
2 I
Inserts text at the beginning of the current line
3 A
Inserts text after the current cursor location
4 A
Inserts text at the end of the current line
5 O
Creates a new line for text entry below the cursor location
6 O
Creates a new line for text entry above the cursor location
Deleting Characters
Here is a list of important commands, which can be used to delete characters and
lines in an open file −
2 X
Deletes the character before the cursor location
3 Dw
Deletes from the current cursor location to the next word
4 d^
Deletes from the current cursor position to the beginning of the line
5 d$
Deletes from the current cursor position to the end of the line
6 D
Deletes from the cursor position to the end of the current line
7 Dd
Deletes the line the cursor is on
Change Commands
You also have the capability to change characters, words, or lines in vi without
deleting them. Here are the relevant commands −
1 Cc
Removes the contents of the line, leaving you in insert mode.
2 Cw
Changes the word the cursor is on from the cursor to the
lowercase w end of the word.
3 R
Replaces the character under the cursor. vi returns to the command
mode after the replacement is entered.
4 R
Overwrites multiple characters beginning with the character currently
under the cursor. You must use Esc to stop the overwriting.
5 S
Replaces the current character with the character you type.
Afterward, you are left in the insert mode.
6 S
Deletes the line the cursor is on and replaces it with the new text.
After the new text is entered, vi remains in the insert mode.
1 Yy
Copies the current line.
2 Yw
Copies the current word from the character the lowercase
w cursor is on, until the end of the word.
3 P
Puts the copied text after the cursor.
4 P
Puts the yanked text before the cursor.
Advanced Commands
There are some advanced commands that simplify day-to-day editing and allow for
more efficient use of vi −
Given below is the list advanced commands.
Sr.No. Command & Description
1
J
Joins the current line with the next one. A count of j commands join many
lines.
2
<<
Shifts the current line to the left by one shift width.
3
>>
Shifts the current line to the right by one shift width.
4
~
Switches the case of the character under the cursor.
5
^G
Press Ctrl and G keys at the same time to show the current filename and
the status.
6
U
Restores the current line to the state it was in before the cursor entered
the line.
7
U
This helps undo the last change that was done in the file. Typing 'u' again
will re-do the change.
8
J
Joins the current line with the next one. A count joins that many lines.
9
:f
Displays the current position in the file in % and the file name, the total
number of file.
10
:f filename
Renames the current file to filename.
11
:w filename
Writes to file filename.
12
:e filename
Opens another file with filename.
13
:cd dirname
Changes the current working directory to dirname.
14
:e #
Toggles between two open files.
15
:n
In case you open multiple files using vi, use :n to go to the next file in
the series.
16
:p
In case you open multiple files using vi, use :p to go to the previous file
in the series.
17
:N
In case you open multiple files using vi, use :N to go to the previous file
in the series.
18
:r file
Reads file and inserts it after the current line.
19
:nr file
Reads file and inserts it after the line n.
1 ^
Searches at the beginning of the line (Use at the beginning
of a search expression).
2 .
Matches a single character.
3 *
Matches zero or more of the previous character.
4 $
End of the line (Use at the end of the search expression).
5 [
Starts a set of matching or non-matching expressions.
6 <
This is put in an expression escaped with the backslash to
find the ending or the beginning of a word.
7 >
This helps see the '<' character description above.
The character search searches within one line to find a character entered after the
command. The f and F commands search for a character on the current line
only. f searches forwards and F searches backwards and the cursor moves to the
position of the found character.
The t and T commands search for a character on the current line only, but for t, the
cursor moves to the position before the character, and T searches the line backwards
to the position after the character.
Set Commands
You can change the look and feel of your vi screen using the
following :set commands. Once you are in the command mode, type :set followed by
any of the following commands.
1 :set ic
Ignores the case when searching
2 :set ai
Sets autoindent
3 :set noai
Unsets autoindent
4 :set nu
Displays lines with line numbers on the left side
5 :set sw
Sets the width of a software tabstop. For example, you would set a
shift width of 4 with this command — :set sw = 4
6 :set ws
If wrapscan is set, and the word is not found at the bottom of the file,
it will try searching for it at the beginning
7 :set wm
If this option has a value greater than zero, the editor will automatically
"word wrap". For example, to set the wrap margin to two characters,
you would type this: :set wm = 2
8 :set ro
Changes file type to "read only"
9 :set term
Prints terminal type
10 :set bf
Discards control characters from input
Running Commands
The vi has the capability to run commands from within the editor. To run a command,
you only need to go to the command mode and type :! command.
For example, if you want to check whether a file exists before you try to save your file
with that filename, you can type :! ls and you will see the output of ls on the screen.
You can press any key (or the command's escape sequence) to return to your vi
session.
Replacing Text
The substitution command (:s/) enables you to quickly replace words or groups of
words within your files. Following is the syntax to replace text −
:s/search/replace/g
The g stands for globally. The result of this command is that all occurrences on the
cursor's line are changed.
Important Points to Note
The following points will add to your success with vi −
You must be in command mode to use the commands. (Press Esc twice at any
time to ensure that you are in command mode.)
You must be careful with the commands. These are case-sensitive.
You must be in insert mode to enter text.
The diff command analyzes line by line and displays a list of changes between two
files. As a special case, diff compares a copy of standard input to itself. This article
describes “How to use diff command in Linux.
Use the diff command to compare both the files as shown below –
linux@linux:~$ diff /home/linux/Desktop/file.txt
/home/linux/Desktop/file1.txt
file1.txt
Use the diff command to compare both files. The command should be like this-
2a3
> Oh yeah, I also need to buy grated cheese.
From the output, 2a3 means “After line 2 in the first file, a line needs to be added: line
3 from the second file”.
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you
to find out whether the two files are identical or not.
When cmp is used for comparison between two files, it reports the location
of the first mismatch to the screen if difference is found and if no difference
is found i.e the files compared are identical.
cmp displays no message and simply returns the prompt if the the files
compared are identical.
Syntax:
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
The syntax of cmp command is quite simple to understand. If we are comparing two files
then obviously we will need their names as arguments (i.e as FILE1 & FILE2 in syntax). In
addition to this, the optional SKIP1 and SKIP2 specify the number of bytes to skip at the
beginning of each file which is zero by default and OPTION refers to the options
compatible with this command about which we will discuss later on.
cmp Example : As explained that the cmp command reports the byte and line number if a
difference is found. Now let’s find out the same with the help of an example. Suppose
there are two files which you want to compare one is file1.txt and other is file2.txt :
2. -i [bytes-to-be-skipped] : Now, this option when used with cmp command helps
to skip a particular number of initial bytes from both the files and then after skipping
it compares the files. This can be done by specifying the number of bytes as argument to
the -i command line option.
//...cmp command used with -i option...//
3. -i [bytes to be skipped from first file] : [bytes to be skipped from second file] :This
option is very much similar to the above -i [bytes to be skipped] option but with the
difference that now it allows us to input the number of bytes we want to skip from
both the files separately.
//...cmp command used with -i option...//
4. -l option : This option makes the cmp command print byte position and byte value for
all differing bytes.
//...cmp command used with -l option...//
The first column in the output represents the position (byte number) of differing bytes.
The second column represents the byte value of the differing byte in the first file, while
the third column represents the byte value of the differing byte in the second file.
5. -s option : This allows you to suppress the output normally produced by cmp
command i.e it compares two files without writing any messages. This gives an exit value
of 0 if the files are identical, a value of 1 if different, or a value of 2 if an error message
occurs.
6. -n [number of bytes to be compared] option :This option allows you to limit the
number of bytes you want to compare ,like if there is only need to compare at most 25 or
50 bytes.
Example: Let us suppose there are two sorted files file1.txt and file2.txt and now
we will use comm command to compare these two.
// displaying contents of file1 //
$cat file1.txt
Apaar
Ayush Rajput
Deepak
Hemant
Note : The options 4 to 8 are rarely used but options 1 to 3 are very useful in terms
of the desired output user wants.
Using comm with options
1. Using -1 ,-2 and -3 options : The use of these three options can be easily
explained with the help of example :
//suppress first column using -1//
$comm -1 file1.txt file2.txt
Apaar
Hemant
Lucky
Pranjal Thakral
Note that you can also suppress multiple columns using these options
together as:
//...suppressing multiple columns...//
3. Using – -nocheck-order option : In case if you don’t want to check whether the
input files are sorted or not, use this option. This can be explained with the help of
an example.
//displaying contents of unsorted f1.txt//
$cat f1.txt
Parnjal
Kartik
$cat f2.txt
Apaar
Kartik
EXAMPLE:
The Metacharacters
Unix Shell provides various metacharacters which have special meaning while using
them in any Shell Script and causes termination of a word unless quoted.
For example, ? matches with a single character while listing files in a directory and
an * matches more than one character. Here is a list of most of the shell special
characters (also called metacharacters) −
* ? [ ] ' " \ $ ; & ( ) | ^ < > new-line space tab
A character may be quoted (i.e., made to stand for itself) by preceding it with a \.
Example
Following example shows how to print a * or a ? −
#!/bin/sh
echo Hello; Word
Upon execution, you will receive the following result −
Hello
./test.sh: line 2: Word: command not found
#!/bin/sh
#!/bin/sh
1
Single quote
All special characters between these quotes lose their special meaning.
2
Double quote
Most special characters between these quotes lose their special
meaning with these exceptions −
$
`
\$
\'
\"
\\
3
Backslash
Any character immediately following the backslash loses its special
meaning.
4
Back quote
Anything in between back quotes would be treated as a command and
would be executed.
VAR=ZARA
echo "$VAR owes <-\$1500.**>; [ as of (`date +%m/%d`) ]"
Upon execution, you will receive the following result −
ZARA owes <-$1500.**>; [ as of (07/02) ]
Double quotes take away the special meaning of all characters except the following
−
$ for parameter substitution
Backquotes for command substitution
\$ to enable literal dollar signs
\` to enable literal backquotes
\" to enable embedded double quotes
\\ to enable embedded backslashes
All other \ characters are literal (not special)
Characters within single quotes are quoted just as if a backslash is in front of each
character. This helps the echo command display properly.
If a single quote appears within a string to be output, you should not put the whole
string within single quotes instead you should precede that using a backslash (\) as
follows −
echo 'It\'s Shell Programming'
The Backquotes
Putting any Shell command in between backquotes executes the command.
Syntax
Here is the simple syntax to put any Shell command in between backquotes −
var=`command`
Example
The date command is executed in the following example and the produced result is
stored in DATA variable.
DATE=`date`