basiclinuxinfo
TRANSCRIPT
-
7/27/2019 BasicLinuxInfo
1/156
1
-
7/27/2019 BasicLinuxInfo
2/156
SYSTEM ORGANISATION
The functioning of Linux is manned in three levels
1. Tools and applications2. Shell
. erne
Tools and application programs which speak our
language.(Programming language )
Kernel interacts with actual hardware in machine
language.
e s e , or e comman n erpre er as s ca e , sthe mediator, which interprets the commands that we
ive and then conve s them to the kernel which
2
ultimately executes them.
-
7/27/2019 BasicLinuxInfo
3/156
Kernel Shell Relationship
Other
A lication
UNIX
Commands
User
System SW
Database Compilers
Hardware
Kernel
Internet Tools
3
-
7/27/2019 BasicLinuxInfo
4/156
Theshellis an ordinary program which interprets the commands
that are typed at the terminal, andtranslates them into commandsthat are understood by the kernel.
system directories, such as/bin, /etc, or /usr/bin.
Theprocess of command interpretationby the shell can be
explained as follows :
reads the command
searc es or an ocates t e e w t t at name n t edirectories containing utilities.
loads the utilit into memor
4
executes the utility
-
7/27/2019 BasicLinuxInfo
5/156
The Linux based shells in the freely available distributions are:Bash
as s an acronym or ourne ga n e . s an
enhancements to the Bourne shell and is the default shell for most
Linux systems. In Red Hat Linux, sh is the symbolic link for the
Bash shell which is stored under the /bin directory.
Pdksh
Pdksh stands for Public Domain Korn Shell and is an
enhancement of the Korn shell. On Linux systems, ksh is the
symbolic link to the Pdksh shell.
5
-
7/27/2019 BasicLinuxInfo
6/156
.
Tcsh
Tcsh stands for Toms C shell also known as the TC shell
and an enhancement of the C shell. The symbolic link
.
ASH
s usua y su a e on mac nes a ave very m e
memory.
6
-
7/27/2019 BasicLinuxInfo
7/156
LINUX COMMANDS
Remembera few things that apply to all Linuxcommands
nux comman s s ou a ways e en ere n
small case letters (case sensitive).
Between the command name and the o tions that
may be available with the command there must
always be a space or a tab. The option is
- . Two or more options available with a command
can usually be combined.
7
-
7/27/2019 BasicLinuxInfo
8/156
SYSTEM?
When you try to access your system, Linux will display a
prompt that looks something like this:
Login: perfect
Password: *****
Once the correct login name and password have been
supplied, you find some welcome messages from the suppliers
of the Linux version installed on the host machine, followed bya command prompt. The command prompt is a $(dollar) if you
8
-
7/27/2019 BasicLinuxInfo
9/156
The passwd command is used to change passwords.
passw
Current password:******
New password:*******
Retype New password:********
The systemdoes not let you choose the old password, nor
can the password be wholly alphabetic or wholly numeric.
It is desirable that password contains at least two alphabetic
characters and at least one numeric or special character within
the first eight characters.
9
-
7/27/2019 BasicLinuxInfo
10/156
The man command displays pages from the Linux
reference manual that is installed alon with the Linux
OS.
To get detailed information about the ls command, you
can use e o owng comman .
$ man ls
10
-
7/27/2019 BasicLinuxInfo
11/156
FILE COMMANDS
11
-
7/27/2019 BasicLinuxInfo
12/156
DIRECTORY PATH
The pwd (print working directory) command is used todisplay the full path name of the current directory.
Example:
$ pwd/home/perfect
$ _
/home/perfect is the directory in which the user is
currently working.
12
-
7/27/2019 BasicLinuxInfo
13/156
. ..
Linux adopts a widely used convention . (a single period)stands for the current directory while . . (a double period)
Whenever you create any sub-directory, these invisible
directories are created automatically by the system.ou can remove ese spec a rec ores, nor can you wr e
into them.
They provide a shorthand notation for referring to the current
and parent directories.
13
-
7/27/2019 BasicLinuxInfo
14/156
DIRECTORY
The cd (change directory) command changes the currentdirectory to the directory specified.
c a so c r n some s e s c ange rec ory
cd changes to user's home directory
'
cd .. goes up one directory level
cd ../.. goes up two directory levels
cd /full/path/name/from/root changes directory to absolute
path named (note the leading slash)
14
-
7/27/2019 BasicLinuxInfo
15/156
The mkdir(make directory) command is used to createdirectories.
S ntaxmkdir[options] directory
Common Options
,
-m mode access permissions.
Examples
if your present working directory is /home/frank the followingwould be equivalent:
15
-
7/27/2019 BasicLinuxInfo
16/156
The rmdir(remove directory) command removes thedirectory specified.
yn ax
rmdirdirectory
Exam les
To remove the empty directory /home/frank/data while in
/home/frank use:
or
$ rmdir /home/frank/data
16
-
7/27/2019 BasicLinuxInfo
17/156
DIRECTORY
The ls command is used to display the names of the files
and sub-directories in a directory.
S ntax
ls [options] [argument]
17
-
7/27/2019 BasicLinuxInfo
18/156
O tions of ls Options
-a lists all files, including those beginning with a dot
(.)
- , , ,
size, lastmodification (time).
-rWe can reverse the order of the presentation. -F indicates type o entry with a trailing symbol:
9 * executable
9
9Nothing ordinary file
-R Recursively itwill search for the particular file.
18
-
7/27/2019 BasicLinuxInfo
19/156
specified file.
What it will doCommand/Syntax
cat [options] file concatenate (list) a file
more (or less or pg)[options] file
page through a text file
19
-
7/27/2019 BasicLinuxInfo
20/156
e ca comman can e use o ver ca y conca ena e e
contents of more than one file.
Example: $ cat data1 data2
A sample fileanother sample file$ -
20
-
7/27/2019 BasicLinuxInfo
21/156
-
7/27/2019 BasicLinuxInfo
22/156
Common Options
-i interactive rom tand wait for confirmation before
proceeding)
-
Complete path names can be specified with the cp
.
22
-
7/27/2019 BasicLinuxInfo
23/156
The rm( remove) command is used to delete files or
directories.
Syntax
Example
.
23
-
7/27/2019 BasicLinuxInfo
24/156
Common Options
9 -i interactive (prompt and wait for confirmation
9 -r recursively remove a directory, first removing the
files and subdirectories beneath it
9 - -
If the file to be deleted is not located in the current or
,
given.
24
-
7/27/2019 BasicLinuxInfo
25/156
The mv command is used to move a file or directory fromone location to another or to change the name of a file or
directory.
Syntax
mv [options] old_filename new_filename
Examples
$ mv old_filename new_filename
25
-
7/27/2019 BasicLinuxInfo
26/156
MOVING AND RENAMING FILES
Common Options
-i interactive rom tand wait for confirmation before
proceeding)
-fdont prompt, even when copying over an existing
target file (overrides -i)
26
-
7/27/2019 BasicLinuxInfo
27/156
MULTIPLE FILES
Large files are sometimes impossible to edit with aneditor, and need to be split up.
$ split filename
What split does is that it creates a group of filesxaa,xab,xac and so on that could go up to xaz, and then
again from xba,..xbz. In this way you can have
* , .
27
-
7/27/2019 BasicLinuxInfo
28/156
MULTIPLE FILES
Split by default break up the file 1000 lines per file.$ split 72 data1.txt
Contains 72 lines per file.
We can even select our own primary filename by
specifying it as the last argument in the command line
$ split chap small
This creates files smallaa, smallab,smallac, etc.,
28
-
7/27/2019 BasicLinuxInfo
29/156
SIDE BY SIDE
The paste command takes two tables and combines them,side by side, wide table as output.
By default paste assumes a tab as the delimiter. Any other
delimiter must be specified.
Thed option specifies the delimiter that must be inserted.
29
-
7/27/2019 BasicLinuxInfo
30/156
aste COMMAND
Example :
$ paste d$: file1 file2 file3
.
of file two and add : to it.It returns to perform the same
function on all the lines of the file, till the end of file. The paste utility expands a tables width, increasing the
number of columns. The cat utility expands a tables length,
.
30
-
7/27/2019 BasicLinuxInfo
31/156
KNOWING FILE TYPES
Even though there are basically three types of files
(ordinary,directory, and device) we may often need to
. ,
may contain plain text, a C program or executable
code.$ file emp.lst
emp.lst : ascii text
progs directory,
$ file progs/*
31
-
7/27/2019 BasicLinuxInfo
32/156
The nl command is used to add line numbers to a file.e ne num ers recor s t e re atve pos ton o a ne n
a file. Example :
.
1. #include
2.int main().
4. Printf(\nHai);
5. Return 0;
.
Thei option is used to specify the value, other than thedefault 1, by which line numbers must be incremented.
32
-
7/27/2019 BasicLinuxInfo
33/156
e may requre o en er a num er o enames n e
command line. Example:
$ ls -l chap chap01 chap02 chap03 chap04 chap05
The above listing all files beginning with chap.
string(chap), the lengthy command line, using this
repeatedly, doesnt look elegant.
We can have a single pattern consisting of the stringchap, along with one or two special characters.
The shell does this throu h wildcard characters.
33
-
7/27/2019 BasicLinuxInfo
34/156
The shell offers the facility to perform an operation on a setof files without having to specify all the names of the files
on which the operation is to be performed.
This is made possible by the use of certain special
characters in the command in place of the actual file
names.
w car c arac ers.
34
-
7/27/2019 BasicLinuxInfo
35/156
The followin are the lists of wildcards available, with a
description Table. * -- Matches none or one character
or a string more than one
character.
-- .
[ ] -- Matches exactly one of a
s ecified setof characters.
Using this, the above example can be solved by as follows
$ ls l chap*
35
-
7/27/2019 BasicLinuxInfo
36/156
Examples:
COMMAND OUTPUT
$ ls a* a ab abcd ac abc abcde
$ ls a? ab ac
Suppose there are following files in present in workingdirectory:
$ ls a? No output
$ ls a* art
$ ls *.* No such file and directory$ ls [!abc]art part tart
$ ls [b-d u-z]* art art1 art2 art3
36
-
7/27/2019 BasicLinuxInfo
37/156
FILE ACCESS PERMISSIONS
37
-
7/27/2019 BasicLinuxInfo
38/156
Determining the FAPs for a file
e ccess ermss ons re er o e permss ons assoc a e
with a file with respect to owner, group and other users of
the file.
available for files and directories.
38
-
7/27/2019 BasicLinuxInfo
39/156
Access Denoted Action Action permitted on
file
read r Allows you todis la co t and
Allows you to list thecontents of the
compile the file directory.
write w Allows you to
edit, rename or
Allows you to create a
new file andmove the file toanother location
subdirectory withinthis directory.
execute x Allows you to Allows you to move toexecu e e eprovided the filealso has the readermission
e rec ory us ng ecd command.Todelete a file executeermission is re uired
39
in the directory.
-
7/27/2019 BasicLinuxInfo
40/156
PERMISSIONS
The access permissions associated with a file ordirectory can be changed using the chmod command.
Only the owner of a file can change the permissions
associated with it. Syntax:
chmod mode file/s
mode can be a symbolic or obsolute way.
40
-
7/27/2019 BasicLinuxInfo
41/156
..
Symbolic mode :
r- read w - write x - execute
- - -
- means remove
+ means grant
= =read and write)
Example :
- .
Here g means group - means read permission has
been removed.
41
-
7/27/2019 BasicLinuxInfo
42/156
ABSOLUTE MODE
The absolute mode of specifying FAPs uses a
series of digits to represent the actual permissions.
rea -- 4
write -- 2
execute -- 1
The figure below depicts a sample representation of
absolute numbers.
r w x r w - r - -7 6 4
42
-
7/27/2019 BasicLinuxInfo
43/156
.
Example :$ chmod 764 data1.C
It means file access permission has been changedby the command chmod read, write, execute
permissions for the user, read and write for the
group owner, and only read for other users
43
-
7/27/2019 BasicLinuxInfo
44/156
nux un ers an s e mpor ance o e secur y an
then sets up certain default permissions for the files youcreate.
nux uses e vara e ca e umas o ec e edefault permissions.
The current value ofumask can be easily determined byus ypng umas
$ umask022
The default permissions for all files should be 666.
The default permissions for all directories are 777.
44
-
7/27/2019 BasicLinuxInfo
45/156
LINK FILE
When a file is copied, both the original and copy occupyseparate space in the disk.
Linux allows a file to have more than one name, and yet
maintain a sin le co in the disk. Chan es to one file
are also reflected in the other.
,
it has more than one name.
45
-
7/27/2019 BasicLinuxInfo
46/156
we acc en a y e e e a e w a s nge n or a s nge
name there is no bringing it back as Linux has no fileundelete facility.
one e as or more n s, our e s sa e even one othe links gets deleted.
If one file is to be shared between several users insteado gvng eac user a separa e copy o e same e wecan create links of this file in each users directory. Thisavoids unnecessary duplication of the same file contents
.
The file is physically not removed until at least one linkis pointing to the file.
46
-
7/27/2019 BasicLinuxInfo
47/156
LINK
There are two types of links available in Linux
1. Hard link
. ym o c n
Hard Link
Hard links are special files that allow a single file tohave multiple names. You can have a hard link file
only for a file and not for directory. These Links are
known as hard links because the create a direct link
to an inode.$ ln data1.dat # by default hard link
47
-
7/27/2019 BasicLinuxInfo
48/156
Limitation of Hard link
9 We cant have two linked files in two file systems. Inother words if we have a file in the ext2 file s stemwe cant link this file to another file in the /home filesystem.
9 We cant l ink a director even within the same filesystem.
Unlike Hard Links, a symbolic link points to the filewhich actuall has the contents.
We can create a symbolic link withs option of ln.9 ln s data lndata
48
-
7/27/2019 BasicLinuxInfo
49/156
Hard l inks can be created only for fi les not for
directories. Symbolic link can be created for both.
ym o c n can wor across eren e sys ems.
But Hard link can not work across different file systems.
If it is a S mbolic link, whenever we do ls l it tells it is alinked file. But if we do Hard link it tells it is a ordinary
file.
sym o c ne e can no e use a er e e ng e
original file whereas a hard linked file can be used after
deleting the original file.
49
-
7/27/2019 BasicLinuxInfo
50/156
It recursively examines a directory tree to look for files
matching some criteria, and then takes some action on
t e seecte es.
#find / -name .bash_profile print
from the root directory.
The last section s ecifies the action rint to be taken
on the file.
50
-
7/27/2019 BasicLinuxInfo
51/156
FINDING UNUSED FILES
#find . -mtime -2 -print
This command searches files from the current directory,
, , .
-n less than n
+n more than n n exactly equal to n
find uses thesize operator to locate large files.
# find /home size +2048 size 8192 -print
51
-
7/27/2019 BasicLinuxInfo
52/156
COMPARING FILES
52
-
7/27/2019 BasicLinuxInfo
53/156
COMPARING TWO FILES
We require to know whether two files are identical so
that one of them can be deleted.
$ cmp chap01 chap02
Chap01 chap02 differ: char9, line1
The two files are compared byte by byte and the
the first line) is echoed on the screen.
53
-
7/27/2019 BasicLinuxInfo
54/156
COMPARING TWO FILES
The l option :
-l -- gives a detailed list of the byte number and differing
Bytes in octal for each character that differs in both files.
$ cmp l note1 note2
3 143 145
5 178 190
9 172 175
54
-
7/27/2019 BasicLinuxInfo
55/156
The comm command is used to give more information on
file differences.The command compares two sorted files
and generates a three-column output.
1st
column -- lists the lines that appear only in the firstfile.
2nd column lists lines that appear only in the second
file.
3rd column -- lists lines that appear in both files.
55
-
7/27/2019 BasicLinuxInfo
56/156
Example :
$ cat file1 $ cat file2 $ comm file1 file2apple grape apple
mango mango grape
orange pear mango
ear rose oran epear
rose
comm 12 file1 file2 #will dis la onl column 3
mangopear
56
-
7/27/2019 BasicLinuxInfo
57/156
diff COMMAND
The diffcommand is another file-comparison
command. It is used to compare two versions of the
same file.
The output is a list of editing actions that must be
added to the first file in order to generate the second
edited file.
y u , , ,
changing a line, and so on.
57
-
7/27/2019 BasicLinuxInfo
58/156
Example
$ diff file1 file2
0a1 # Append after line 0 of firstfile
>Command options # this line
2c3,4 # Change line 2 in first file
options of file # these two
> list of functions # lines
4d5 # Delete 4 of first file< in the chapter. # containing this line
58
If the two files are identical it displays nothing
-
7/27/2019 BasicLinuxInfo
59/156
There may be instances where the lines in a file are
duplicated.
The duplicates must be removed. The uniq command
.
A file must be sorted before using the uniq command.
59
-
7/27/2019 BasicLinuxInfo
60/156
COMMAND
Options :
-u outputs only those lines that are not repeated.
-d outputs only one copy of each repeated line.
-c generates output in default style but with each
line receded b a countof the number of itemsit occurred.
-n the first n fields together with any blanks beforeeach are i nored
+n the first n characters are ignored.
60
OTHER LINUX COMMANDS
-
7/27/2019 BasicLinuxInfo
61/156
OTHER LINUX COMMANDS
date command shows the date and time to the
nearest second
Fri May 16 08:25:53 IST 2002
clear
screen.
who
who command is used to report who are all logged
in, terminal name and what time they logged in.
61
-
7/27/2019 BasicLinuxInfo
62/156
PIPES AND FILTERS
62
STANDARD FILES
-
7/27/2019 BasicLinuxInfo
63/156
STANDARD FILES
A computer consists of mainly three parts, the input,
processing, and output devices.
Linux treats all input and output devices as special files.
These files are categorized as, Standard Input Files
Standard Out ut Files
Standard Error Files
63
STANDARD In ut Files
-
7/27/2019 BasicLinuxInfo
64/156
STANDARD In ut Files
Standard Input Files
In Linux when a user executes a command that
requires input, the shell interprets the command and
.
The keyboard is referred to as the standard input file.
,
input files
64
STANDARD OUTPUT FILES
-
7/27/2019 BasicLinuxInfo
65/156
STANDARD OUTPUT FILES
Standard Output File
for the output of any command that it executes. The
monitor is referred to as the standard output file. The file
descriptor 1, is assigned to the standard output file.
65
STANDARD ERROR FILES
-
7/27/2019 BasicLinuxInfo
66/156
STANDARD ERROR FILES
Standard Error File
Shell utilities display error messages on the monitor. The
descriptor 2, is assigned to the standard error file.
66
-
7/27/2019 BasicLinuxInfo
67/156
Redirection changes the assignments for the standard
input, standard output, and the standard error.
The three types of redirection are:
Input redirection Output redirection
Error redirection
67
INPUT REDIRECTION
-
7/27/2019 BasicLinuxInfo
68/156
INPUT REDIRECTION
The following example illustrates the usage of input
redirection:
Here, the less than symbol,
-
7/27/2019 BasicLinuxInfo
69/156
The following example illustates the usage of output
redirection:
Here, the greater than symbol, >, implies redirection of
output to the file, test2. The output of the cat command is
wr en o a s e, es .
However, if the file already exists, its contents are deleted
before the output is written to it.
If you want to append the output to the file, test2, thecommand is:
69
-
7/27/2019 BasicLinuxInfo
70/156
ERROR REDIRECTION
The following example illustrates the usage of error
redirection:cat test 2>erro -mes
Assume that the file, test, does not exist in the currentdirectory. So a user tries to execute this command, Linux
will enerate an error messa e since the execution isunsuccessful.
This error message will be written to the file named error-mes .
Unlike the file descriptors for the standard input andstandard output, the file descriptor for the standard errorfile 2 is com ulsor .
70
-
7/27/2019 BasicLinuxInfo
71/156
FILTERS
A filteris a program that takes its input from the
standard input file, processes( or filter) it, and sends its
The grep filter
The grep stands for globally search for regular
.
Syntax
grep regular_expression [filename]
regular_expression which has to be searched in the
file. file name is optional ,without it grep expectsstandard input.
71
-
7/27/2019 BasicLinuxInfo
72/156
rep In the grep command regular expressions are always given in
.
Example:
grep New[abc] Matches any one of a set of
.
grep New[a-c] Matches any one of a range of
characters.
grep ew a c e pa ern o owng mus occur athe beginning of each line.
grep New[^a-c] The pattern must not contain any
.
grep New[abc]$ The pattern preceding it must occur atthe end of each line.
72
grep ew. a c a c es any one c arac er.
-
7/27/2019 BasicLinuxInfo
73/156
The grep filter also has options that alter the output of
the command. These are : - ,
with its line number. The number is printed at the
beginning of the line.
-c s prn s ony a coun o e nes a ma c apattern.
-v This prints out all the lines that do not match the
pattern specified by the regular expressions.
Options must be specified before the regular expression.
O tions can also be combined.
73
-
7/27/2019 BasicLinuxInfo
74/156
wc The wc filter is used to count the number of lines, words,
and characters in a disk file or in the standard input.
Syntaxwc [-lwc] [filename/s]
Example:
$ wc test
The file test has 2 lines, 7 words, 9 characters Options :
-l -- Displays the number of lines
-w -- Displays the number of words -c -- Displays the number of characters
74
-
7/27/2019 BasicLinuxInfo
75/156
cut FILTER
The cut filter is useful when specific columns from the
output of certain commands(such as ls, who) need to be
Syntax
cut [options][filename/s]
-f -- Displays the specified
columns
- _ --
characters. -d--Specifies the column delimiter
75
-
7/27/2019 BasicLinuxInfo
76/156
cut FILTER
Example :
Here the cut command has been used to extract only
the names of the users that exist in the /etc/passwd file.
The field separator is a :.
76
-
7/27/2019 BasicLinuxInfo
77/156
tr
The tr filter can be used to translate one set of characters
to another. his filter can also be used to s ueeze re eated
occurrences of a character into one.
Thes option is used to squeeze several occurrences
of a character into one character.
Example:tr s < tem orar
Here tr command takes input from temporary file and soption of tr works on every record, squeezing repeatedspaces into a single space.
77
-
7/27/2019 BasicLinuxInfo
78/156
tr FILTER
$ tr : < /etc/passwd
character with a space, for the file /etc/passwd and
displays the output on the standard output.
78
-
7/27/2019 BasicLinuxInfo
79/156
sort FILTER
The sort filter arranges each line of the standard input
in ascending order. S ntax :
sort [options] [filename]if filename is not there, it will take the input from a
standard input file. Options : -r -- This sorts the input in the reverse order. -f -- It arranges in the order of the ascii values of
the characters.
-n -- Arranges the input in ASCII sequence,numbers would also be arranged accordingly.
79
-
7/27/2019 BasicLinuxInfo
80/156
sort
Example :
sort n12239121 d
Output:12121
9
80
-
7/27/2019 BasicLinuxInfo
81/156
SORTED ORDER
Specify the file name along with sort.
Example :
$ sort names # similar to sort < names>
The above command will display the lines in the file in
sorted order.
$ sort newnames oldnames
The above command will display the lines in the files,
newname and oldnames in a sorted order.
81
-
7/27/2019 BasicLinuxInfo
82/156
SORTED ORDER
The +pos1 pos2 Option:
In certain cases, a user may want to display each line in afile in the order of a specific column, and not in the usual
sort order. By default, sort assumes that the columns are
separated by a space or a
o sorta file on a column the osition of the column in thefile has to be specified. This is done as follows: The number of column separators (space or < Tab) that
have to be ignored to reach that column must be,
number. The column separator at which the sort filter has to stopmust be s ecified as os2, where os2 is the
82
appropriate number.
-
7/27/2019 BasicLinuxInfo
83/156
SORTED ORDER
For example, to specify sorting on the third column,
the user will have to specify +2 and 3.
$ sort +2 3 names
To sort the file, names, on the third column, within
which on the fourth column, the following command
can be used
$ sort +2 3 +3 4 names
83
-
7/27/2019 BasicLinuxInfo
84/156
-
7/27/2019 BasicLinuxInfo
85/156
COMMAND
Combination of Options :
+4n 5 -- Numeric sort on
the fifth column
-rn or nr -- Combination of r and n
nr or rn -- everse numerc sor on
the fifth column.
-fr or -- Sortin in the reverse
order of ascii characters
85
PIPES
-
7/27/2019 BasicLinuxInfo
86/156
Commands can be combined in such a way that
the standard output of one command can be sent
pipes.
Example :
$ ls > tempfile
$ more tempfile
by using pipes.$ ls | more
86
-
7/27/2019 BasicLinuxInfo
87/156
The following command will display the lines
containing #inlcude
$ ls l | grep #include text.*
To display the names of all ordinary files in the
current directory, the command will be,
$ ls l | grep - | tr s | cut d f9
87
-
7/27/2019 BasicLinuxInfo
88/156
ee
The intermediate output in a pipe is not saved on the
disk. The intermediate output can be saved in a file using
this tee command.
If the file already exists, the contents of the file are
overwritten. The -a option can be used to append the
new contents to an existing file.
88
-
7/27/2019 BasicLinuxInfo
89/156
Example :
$ cat temp | tee temp1 temp2,
temp, on the screen, and also writes to the files temp1 and
temp2
$ sort numbers | tee sort-numbers | more
The above command, the file, numbers is sorted, data is
-,
numbers, and stores the data in this file. It also sends thedata as input to the more utility.
89
-
7/27/2019 BasicLinuxInfo
90/156
The ps command is used to display the attributes of a
process.,
user at that terminal:
$ ps
PID TTY TIME CMD
476 tty03 00:00:01 login
659 tty03 00:00:01 sh
684 tty03 00:00:00 ps
90
OPTIONS OF
-
7/27/2019 BasicLinuxInfo
91/156
OPTIONS OF ps
The options:
a Lists out the processes of all users, but doesnt
display the system processes.
-u We can know the activities of a particular user.
-e Lists out all the system processes.
91
-
7/27/2019 BasicLinuxInfo
92/156
A multi-tasking system lets a user do more than one job at a
time. Since there can be only one job in the foreground, the.
The & is the shells operator used to run a process in the
background.
J ust terminate the command line with an &, the command
will run in the background:
sor o emp. s emp. s
The shell immediately returns a number the PID of theinvoked command.
92
no up :
-
7/27/2019 BasicLinuxInfo
93/156
no up :
The nohup command, when prefixed to a command,
permits execution of the process even after user has.
In the following command, no output filename was
specified, nohup sends the default output to a file
nohup.out
$ nohup sort emp.lst &
Sending output to nohup.out
93
no up :
-
7/27/2019 BasicLinuxInfo
94/156
no up :
When you use ps command after using nohup you will
notice something very significant:
$ ps u per
UID PID PPID STIME TTY TIME COMMAND
per 101 1 14:52:08 03 0:13 sort emp.lst
sh died on logging out, but not its child(sort). What the
kernel has done here is to reassign the PPID of the sort
process that has a PID1.
94
-
7/27/2019 BasicLinuxInfo
95/156
A PROCESS
Kill command is used to terminate process.
.
$ kil l 121 125 132
,simply kill the parent in order to kill all its children.
95
NUMBERS
-
7/27/2019 BasicLinuxInfo
96/156
NUMBERS
The system variable $! stores the PID of the last
background$ sort o emp.lst emp.lst &345
$ kill $!# kills the SORT command
Kill, by default uses the signal number 15 to Terminatethe process.
The process can be killed with signal number 9,sometimes known as the sure kill signal.
$ kill 9 121#kills process 121 with signal number 9
96
PRIORITY
-
7/27/2019 BasicLinuxInfo
97/156
PRIORITY
Processes in the Linux system are usually executed with
equal priori ty.,
operator to reduce the priority of jobs.
To run a job with a low priority, the command name
s ou e pre xe w nce:$nice wc l file1 &
Commands runs with a nice value of 20 in the Bash
shell.
$ nice n 15 wc l file1#Nice value becomes 35
97
OTHER UTILITIES
-
7/27/2019 BasicLinuxInfo
98/156
When we invoke bc (calculator) withoutarguments, the input has to be keyed in each lineterminated by pressing . After you have
finished your work use .$ bc12 + 517ibase=211001010202 # output in decimal, I.e., base 10
# To come out of calculator $ factor
7
2
98
-
7/27/2019 BasicLinuxInfo
99/156
The head command is used to display the first linesof the file. The user can specify the number of lines
to be displayed by using the n argument.
The default number of lines that are displayed is 10.$ head 2 dolls
The tail command, as the name suggests is used todisplay the last lines of a file.
The de ault number o lines that are displayed is
again 10.$ tail -4 dolls
99
v
-
7/27/2019 BasicLinuxInfo
100/156
v
A vi session begins by invoking the command vi with (or
without) a filename: When we o en a file with vi the cursor is ositioned at
the top left-hand corner of the screen. We are said to bein the command mode.
o enter text ou have to leave the command mode andenter the input mode.
We have to save our file or switch to editing another file.Commands can be entered in the last line of the screento act on text. This mode is known as ex mode or line
mode.
100
-
7/27/2019 BasicLinuxInfo
101/156
After opening the file, the user is in command mode, if
you press
i insert
r or R replace
you wan o sw c ac o comman mo e or ex mo efrom insert mode press esc key.
, .
This text goes into a place in memory known as buffer.
101
-
7/27/2019 BasicLinuxInfo
102/156
COMMAND FUNCTION
zz Writes the buffer to the
file and quits vi
:wq Writes the buffer to the
file and quits vi
:w fi le name Writes the buffer to file
and :q filename(new) and quits vi
:w! fi lename overwrites the existing file
and :q filename with the contents
of the buffer and quits vi
:q! Quits vi whether or not changesmade to the buffer were written
102
to a file.
-
7/27/2019 BasicLinuxInfo
103/156
o e a e o use oc comman s we mus remem er wothings:
1. All the block commands work in ex command.
2. Line numbers should be associated with the textbefore we issue any block commands.
To set the line numbers in the ex mode t e set number andpress enter.
:nd Deletes nth line
:m,n,d Deletes lines from m to n
:m,n,w>>filename Appends lines m to n to a file
:r filename Reads the contents of thefilename at current cursor
103
position.
SEARCH TEXT OR PATTERN IN vi
-
7/27/2019 BasicLinuxInfo
104/156
SEARCH TEXT OR PATTERN IN vi
vi is equipped with a mechanism to search strings in a
file.Command function
/Pattern searches forward in the bufferfor the next occurrence of theattern of text.
?Pattern searches backward in thebuffer for the first occurrenceof the attern of text.
n repeats the last search
N repeats the search commandin the o osite direction
104
,
-
7/27/2019 BasicLinuxInfo
105/156
,
If you want to delete a particular l ine, keep the
cursorin the corresponding line and press
If you want to delete 4 lines from the file, keep the
cursor in the first line and press 4dd.
an means w ou e e ng , v copes eseobjects into buffers without removing them from the
file.
For pasting, keep the cursor below in the line which
you want to have the copied buffer, and press p.( ornp)
105
-
7/27/2019 BasicLinuxInfo
106/156
SYSTEM ADMINISTRATION
106
-
7/27/2019 BasicLinuxInfo
107/156
The startup sequence begins when you turn on the
power. he boot ro ram checks for the existence and inte rit
of any hardware on the system. The program thendetermines which partition of the disk is active and bootsthe UNIX kernel.
The termbooting means taking a copy of the kernelprogram on the hard disk and placing it inmemory(RAM), where it can function.
The kernel then starts spawning further processes, the
most important of which is init. This process always hasthe PID 1.
107
-
7/27/2019 BasicLinuxInfo
108/156
init is responsible for keeping the system indifferent states, also called runlevels. Each run level
is normally a single digit(0 to 6). Normally thesystem wou e n any one o t ese run eve s:
0 System shutdown
1 System administration mode
2 Full multi user state
3 Network multiuser
5 Shut down same as 0
6 Shutdown and reboot mode
108
-
7/27/2019 BasicLinuxInfo
109/156
shutdown script to bring the system down.
File systems are unmounted, processes are killedand messages are broadcast to users with wall
command.
level to determine whether the system is to be placed
in single user mode, rebooted or simply powered
own.
By default this state is 0, which means a totalshutdown.
109
-
7/27/2019 BasicLinuxInfo
110/156
The user can also become a super user status with the su
command if she knows the root password. For example,
$ su
password: ********
# pwd
/home/local # prompt changes, but directory
The # prompt indicates that the user is a super user.
110
-
7/27/2019 BasicLinuxInfo
111/156
The super user can invoke the date command with a
numeric argument to set the system date. This argument is usually an eight-character string of the
formMMDDhhmm and optionally followed by a two digit
.# date 09180934
Thu Se 18 09:34:00 IST 1997
The system date can be set only by the administrator.
111
-
7/27/2019 BasicLinuxInfo
112/156
-
7/27/2019 BasicLinuxInfo
113/156
df (disk free) reports the amount of free space available on
the disk. The output always reports for each file systemseparate y:
# df
-/home (/dev/home ): 1448 blocks 382 i-nodes
Thet o tion includes the above out ut, as well as the total
amount of disk space in the file system.
113
-
7/27/2019 BasicLinuxInfo
114/156
The du (disk usage) command reports usage by a
recursive examination of the directory tree.s s ow u s s e usage o ome sa es per ec
# du /home/sale/perfect
534 /home/sale/perfect/data
1234 /home/sale/perfect/reports
By default, du lists the usage of each sub-directory of
its argument, and finally produces a summary.
114
-
7/27/2019 BasicLinuxInfo
115/156
Compress is quite fast and produces .z file.
$ compress file1.txtuncompress e1.txt.z
To view a compressed file, the zcat command can be
. . gzip command is comparatively slow but remarkably
efficient. The compressed files have the extension .gz.
$ gzip sales.dbf # produces sales.dbf.gz
$ gunzip sales.dbf.gz
115
CHANGING ANY PASSWORD
-
7/27/2019 BasicLinuxInfo
116/156
The passwd command is used by a user to change thepassword, the old password is first prompted for, and thenthe new one accepted.
However, when you invoke the command as a super user,the system behaves in a more lenient and helpful manner:
# asswd
INFO: Changing password for root
New password: ********
-
To change the particular user password,
#passwd perfect
116
dd was extensively used in copying file systems but today
-
7/27/2019 BasicLinuxInfo
117/156
dd was extensively used in copying file systems, but today
its role is mainly restricted to copying media, like floppies and
tapes.= ev o = emp s= cn =
The keywords are if=(input filename), of=(output filename)
and bs=(block size).
The above command copies a 1.44MB floppy to a temporary
file temp, using a block size of 147456, which is exactly one-
y . .
dd only uses raw divides, ie., those in /dev/rdsk or those filesin /dev beginning with r, like /dev/rfd0135ds18, or /dev/rct0
117
HANDLING DOS DISKETTES
-
7/27/2019 BasicLinuxInfo
118/156
It has now become quite common to see both DOS andUNIX systems on the desktop.Unix provides a family ofcommands with which one can read from and write ontofloppy diskettes.
mcopy - Copies files from or to from1.44MB floppy drive
mdir - Lists files in DOS diskette inDOS style
mt e - Concatenates files in DOSdiskette
mdel - Deletes file on DOSdiskette
118
DISKETTES & BACKUPS
-
7/27/2019 BasicLinuxInfo
119/156
Before you use a floppy for backup purposes, we need to
format it first. This is done with the fdformat with the raw
# fdformat /dev/fd0H1440
The two most popular programs for backups are cpio and
ar. The cpio command can be used to copy files to and from a
backup device. It uses standard input to take the list of
filenames, and then copies them with their contents and
headers, into a single archive that is written to the standardoutput.
119
c io COMMAND
-
7/27/2019 BasicLinuxInfo
120/156
Backing Up Files
-o option is used to create an archive, which can then beredirected to a device file. This is how ou co files in thecurrent directory to a 1.44MB floppy
# ls | cpio ov > /dev/fd0
A complete archive or selected files can be restored withthei key option. To restore all the files that were backed
operator(
-
7/27/2019 BasicLinuxInfo
121/156
tarnot only creates archives on tapes, but supports floppies aswell .
Exclusive features
- It doesnt use standard input to obtain its file list.taraccepts file and directory names as arguments.
- , . .,it operates recursively.
- It can create several versions of the same file in a.
- It can append to an archive without overwritingthe entire archive.
121
FILES
-
7/27/2019 BasicLinuxInfo
122/156
ac ng up es
taraccepts directory and filenames directly on the command
line.* .
The verbose option(-v) shows the number of blocks used byeach file.
.as high a value as the system permits. Higher block sizesspeed up I/O operations.
Restorin files
Files are restored with x(extract) option. When no file or
directory name is specified, it restores all files from the backupdevice.
122
# tar xvf /dev/fd0 18
fdisk IN LINUX
-
7/27/2019 BasicLinuxInfo
123/156
fdisk in Linux creates both partitions as well as filesystems.
# fdisk
The options:
-d delete a partition
-m print this menu
-n add a new partition
-p print the partition table
-q quit without saving changes-w write table to disk and exit
123
FILE SYSTEM MOUNTING
-
7/27/2019 BasicLinuxInfo
124/156
The interesting thing about a file system is that, once
created, it is logically separate entity with its own tree
.
These files systems unite to become a single file system at
.
The root file system becomes the main file system, and its
root director is also the director of the unified s stem.
This happens by aprocess
known asmounting
124
mount COMMAND
-
7/27/2019 BasicLinuxInfo
125/156
. ., ,
/mnt) must first be made available in the main file system.
/dev/fd0 loses its separate identity. It now becomes the
The type of fi le system is specified with thet option. If
system should be vfat filesystem.
125
-
7/27/2019 BasicLinuxInfo
126/156
Unmounting is achieved with the umount
command, which requires either the file system or
# umount /mnt/floppy
# umount /dev/fd0
You cant remove a directory unless you are placedin a directory above it, you cant unmounta file
s stemunless ou are laced above it. If ou tr to
do that, this is what you will see :
#umount /dev/fd0 busy : Device busy.
126
-
7/27/2019 BasicLinuxInfo
127/156
Almost everyday the administrator is required to
add or delete a user or group from the system.e comman o a e user s ,
# useradd
Password can be set for the user b the root b
giving the command,
# passwd
e comman o e e e e user s,
# userdel
127
RPM (Redhat Package Manager)
-
7/27/2019 BasicLinuxInfo
128/156
Open packaging system
UNIX systems .
Features include - installing, uninstalling,upgrading, querying, verifying all in one
line commands .
128
-
7/27/2019 BasicLinuxInfo
129/156
Installing
# rpm -ivh pkg- 1.0- 2.i386. rpm If r m ives an error sa in acka e alread installed
and still want to install the package, use -- replacepkgsi.e.,# rpm -ivh --replacepkgs rpm_name
Similarly use --replacefiles for conflicting files use --nodeps to install package with dependency conflicts
Uninstallingrpm pkgname // not the rpm ile name
use --nodeps to uninstall package with dependencyconflicts.
129
Print commands allows us to print files to standardou pu or o a ne prn er
-
7/27/2019 BasicLinuxInfo
130/156
ou pu or o a ne prn er.
Following are the commands to submit, cancel, and
check the status of a print job.
Command/Syntax What it will do
jobs
lpr(lp) [options] file print to defined printer
lprm (cancel) [options] remove a print job from the
print queue.
130
-
7/27/2019 BasicLinuxInfo
131/156
Shell Programming
131
-
7/27/2019 BasicLinuxInfo
132/156
USER DEFINED VARIABLES
The variable length can be of any reasonable length andmay cons u e a p a e s, g s, an un erscores. owever
-
7/27/2019 BasicLinuxInfo
133/156
may cons u e a p a e s, g s, an un erscores. oweverthe first character must be an alphabet or an underscore.
$ a=20$ echo a $ echo $a
a 20
$ str=Two words $ echo Enter our no
$ echo $str $ read noTwo words
,expr before the operation. For ex:
$ expr $a + $b `
133
.
Like C program shell program also takes command line.
-
7/27/2019 BasicLinuxInfo
134/156
$1 $2 $3 $4 $9 we represent command line
arguments like this in the program.-- o o argumen s
$$ -- pid of the current process
$0 -- Name of the shell program
$* -- List of all the command line arguments.$? -- exit status of the last executed command
Ex:vi tem
echo $# $3 $0 $$ $1
When we execute temp, ./temp hello how are you
134
are emp e o
CONDITIONAL STATEMENTS
Ifcondition1 # if conditions is TRUE=0
en
-
7/27/2019 BasicLinuxInfo
135/156
cmd list-1
elif condition2; thencmd list-2
else
cmd list-n
fi Ex: if [ $# -ge 2 ]
then
echo more than two arguments
elseecho no input
135
fi
LOOPING(for)
case param1 in
-
7/27/2019 BasicLinuxInfo
136/156
case param1 inpattern1) cmd-list1;;
pattern2) cmd-list2;;* e cm st;;
esac
forvar [ in list of vals]
o cmd-listdone
.do
cp $file new$file
136
LOOPING (while)
While [ codition ]
do
-
7/27/2019 BasicLinuxInfo
137/156
do
cmd-list
[ continue ]
done
x:while [ $# -gt 0 ]
do
echo $1
shift # $#=$# -1
137
LOOPING(until)
until loop executes till the conditions becomes false..
til [ diti ]
-
7/27/2019 BasicLinuxInfo
138/156
until [ condition ]
do -
[ break ]
[ continue ]
one Ex:
until [ $# -le 0 ]
do
echo $1shift
138
done
-
7/27/2019 BasicLinuxInfo
139/156
PROGRAMMING TOOLS
139
-
7/27/2019 BasicLinuxInfo
140/156
Linux software development perhaps this is because the
Linux s stem was ori inall develo ed in C.
The GNU C compiler, gcc, is one of the most versatile and
advanced compilers around.
Unlike the other C compilers, gcc supports all the modern C
standards currently in use such as the ANSI C standard
as well as many extensions specific to gcc itself.
140
QUICK OVERVIEW
Example:
.
i l d di h
-
7/27/2019 BasicLinuxInfo
141/156
#include
int main(){
printf(\nHello World!);
;
}
The gcc compiler must generate an object file from this
source code. The object file is essentially the machine code
equivalent of the C source.
141
,
printf() function, and code to return the value of 0.
cc continuation..
Linkerhas to link the object file to produce an executable.
The job of the linker is to take object files, merge them withd f lib i d d bl
-
7/27/2019 BasicLinuxInfo
142/156
code from libraries, and produce an executable.
First and foremost, the code for printf() must be linked in. Where does the code for printf() come from?
Answer: Libraries
,
including an index. When searching code for the printf(), the linker looks at the
.
It finds the object file containing the printf() function and
extracts that object file and links it to the executable.
142
cc BASIC USAGE
programmer would use the command:
-
7/27/2019 BasicLinuxInfo
143/156
p g
$ gcc o hello hello.cen execu ng s s nge gcc comman , rs gcc a o
compile your source file, hello.c, into an object file, hello.o.
Next, it had to link hello.o against the standard libraries
and produce an executable. If you want only to compile a source file into an object file
and fore o the linkin rocess use the c switch with cc
as in:
gcc c hello.c //produces the object file hello.o
143
e ave a program cons s ng o wo source es, oo.c anbar c
-
7/27/2019 BasicLinuxInfo
144/156
bar.c.
We would use one or more header files containing functionec ara ons s are e ween e wo programs.
To compile these two source files and link them together toproduce the executable baz, we would use the command:
$ gcc o baz foo.c bar.c This is roughly equivalent to the three commands:
$ cc c foo.c
$ gcc c bar.c
$ gcc o baz foo.o bar.o
144
-
7/27/2019 BasicLinuxInfo
145/156
An archive (or static library) is simply a collection of objectfiles stored as a single file.
hi i h d hi
-
7/27/2019 BasicLinuxInfo
146/156
You can create an archive using the ar command.Archive
files traditionally use a .a extension rather than the .oextension used by ordinary object files.
Heres how you would combine test1.o and test2.o into asingle libtest.a archive:
$ar cr libtest.a test1.o test2.o The cr flags tell ar to create the archive.
-with gcc.
146
LIBRARIES
an archive?
-
7/27/2019 BasicLinuxInfo
147/156
9
The most fundamental difference is that when a shared,not actually contain the code that is present in the sharedlibrary. Instead, the executable merely contains a
To create a shared library, you must compile the objects thatwill make up the library using the -fPIC option to the compiler,like this:
9 $gcc -c -fPIC test1.c
The -fPIC option tells the compiler that you are going to be
147
. .
Position-Independent Code (PIC)
9 PIC stands for position-independent code. The functions ina shared library may be loaded at different addresses in
-
7/27/2019 BasicLinuxInfo
148/156
a shared library may be loaded at different addresses in
different programs, so the code in the shared object mustno epen on e a ress or pos on a w c sloaded.
Combine the object files into a shared library, like this:
$ gcc -shared -fPIC -o libtest.so test1.o test2.o The -shared option tells the linker to produce a shared library
rather than an ordinary executable.
Shared libraries use the extension .so , which stands for
shared object. For Static archives and Shared libraries, the filename alwa s
148
begins with lib to indicate that the file is a library.
_ _
When you link a program with a shared library, the linker places only
the name of the shared library.
-
7/27/2019 BasicLinuxInfo
149/156
The system searches only /lib and /usr/lib , by default.
your s are rary es outs e t ese rectores, suppose t s
present in /usr/local/lib
$gcc -o app app.o -L.-ltest -Wl,-rpath,/usr/local/lib
Another solution to this problem is to set the LD_LIBRARY_PATH
environment variable.
rs w searc n _ _ vara e an en w
search in /lib and /usr/lib.
149
-
7/27/2019 BasicLinuxInfo
150/156
MAKEFILES
gcc o output hello.c hello1.c
-
7/27/2019 BasicLinuxInfo
151/156
gcc o output hello.c hello1.c
cc command in the above exam le would recom ile
each source file in turn before linking the executable. This
can be a large waste of time, especially if you only made
modifications to a single source file since lastcompilation.There would be no reason to recompile the
To overcome this problem, makefiles are used.
151
WHAT MAKE DOES
steps.
If lot of source files make up the final executable we
-
7/27/2019 BasicLinuxInfo
152/156
If lot of source files make up the final executable, we
chan e one and rebuild the executable without havin torecompile everything.
Example:
edime: main.o edit.o
gcc o edime main.o edit.omain.o: main.c
gcc c main.c
edit.o: edit.c
gcc c edit.c
152
-
7/27/2019 BasicLinuxInfo
153/156
FILES
The lines containing commands have to begin with
-
7/27/2019 BasicLinuxInfo
154/156
tabs(not spaces). And dont use a tab before any otherline.
We can place a hash mark(#) anywhere on a line to start
a comment. Everything after the hash mark is ignored.
If we put a backslash at the end of a line, it continues on
the next line.
154
When people use a filename or other string more than
i k fil th t d t i it t
-
7/27/2019 BasicLinuxInfo
155/156
once in a makefile, they tend to assign it to a macro.
Example:
OBJECTS = main.o edit.o
e me:
gcc o edime $(OBJECTS)
, . .
you specify $(OBJ ECTS).
155
-
7/27/2019 BasicLinuxInfo
156/156
156