Você está na página 1de 156

Basic Linux Commands

SYSTEM ORGANISATION
The functioning of Linux is manned in three levels 1. Tools and applications 2. Shell 3 Kernel 3. Tools and application programs which speak our language.(Programming language ) Kernel interacts with actual hardware in machine language. Th shell The h ll, or the th command di interpreter t t as it is i called, ll d i is the mediator, which interprets the commands that we give and then conveys g y them to the kernel which ultimately executes them.
2

Kernel Shell Relationship


User Other Application pp System SW UNIX Commands

Hardware Database Packages Kernel Compilers

Internet Tools

SHELL AS A INTERPRETOR
The shell is an ordinary program which interprets the commands that are typed at the terminal, and translates them into commands that are understood by the kernel. These programs are located in individual files in one of the system directories, such as /bin, /etc, or /usr/bin. The process of command interpretation by the shell can be explained as follows : reads the command searches h f for and dl locates the h fil file with i h that h name i in the h directories containing utilities. loads the utility y into memory y executes the utility
4

TYPES OF SHELLS
The Linux based shells in the freely available distributions are: Bash B h is Bash i an acronym f for Bourne B Again A i Shell Sh ll. It is i 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.

CONTINUED. CONTINUED
Tcsh Tcsh stands for Tom oms s C shell also known as the TC shell, and an enhancement of the C shell. The symbolic link available for Tcsh shell on Linux is csh csh. ASH It is i usually ll suitable it bl on machines hi th that th have very li limited it d memory.

LINUX COMMANDS
Remember a few things that apply to all Linux commands All Li Linux commands d should h ld always l b be entered t di in small case letters (case sensitive). Between the command name and the options p that may be available with the command there must always be a space or a tab. The option is usually preceded by a minus( minus(-) ) sign. sign Two or more options available with a command can usually be combined.
7

HOW TO LOGIN TO THE 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 by a command prompt. The command prompt is a $(dollar) if you are operating in Bash shell, or a % if in C shell.
8

CHANGE YOUR PASSWORD


The passwd command is used to change passwords. $ passwd d Current password:****** New password:******* Retype New password:******** The system does 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.

DISPLAYING MANUAL PAGES


The man command displays pages from the Linux reference manual that is installed along g with the Linux OS. Example : To get detailed information about the ls command, you can use the th f following ll i command. d $ man ls

10

FILE COMMANDS

11

IDENTIFYING THE CURRENT DIRECTORY PATH


The pwd (print working directory) command is used to display 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

The . and ..Directories


Linux adopts a widely used convention . (a single period) stands for the current directory while . . (a double period) indicates the parent directory. Whenever you create any sub-directory, these invisible directories are created automatically by the system. Y You cant t remove these th special i l di directories, t i nor can you write it into them. They provide a shorthand notation for referring to the current and parent directories.

13

CHANGING THE CURRENT DIRECTORY


The cd (change directory) command changes the current directory to the directory specified. cd d (also ( l chdir hdi in i some shells) h ll ) cd cd / cd .. cd ../.. change h di directory t changes to user's home directory changes directory to the system system's s root goes up one directory level goes up two directory levels

cd /full/path/name/from/root changes directory to absolute path named (note the leading slash)

14

CREATING A DIRECTORY
The mkdir (make directory) command is used to create directories. Syntax mkdir [options] directory Common Options -p create the intermediate (parent) directories directories, as needed -m mode access permissions. Examples $ mkdir /home/frank/data if your present working directory is /home/frank the following would be equivalent: $ mkdir data
15

REMOVING A DIRECTORY
The rmdir (remove directory) command removes the directory specified. Syntax S t rmdir directory Examples p To remove the empty directory /home/frank/data while in /home/frank use: $ rmdir data or $ rmdir /home/frank/data

16

LISTING THE CONTENTS OF A DIRECTORY


The ls command is used to display the names of the files and sub-directories in a directory. Syntax y ls [options] [argument]

17

Options p of ls
Options

-a lists all files, including those beginning with a dot (. ) -l long listing: lists the mode, mode link information, information owner, owner size, last modification (time). -r We can reverse the order of the presentation. -F indicates type of entry with a trailing symbol: 9 * / 9 / 9 Nothing executable directories ordinary file

-R Recursively it will search for the particular file.

18

DISPLAYING CONTENTS
The cat (concatenate) command displays the contents of the specified file. Command/Syntax cat [options] file more (or less or pg) [options] file What it will do concatenate (list) a file page through a text file

19

DISPLAYING CONTENTS
Th The cat t command d can be b used dt to vertically ti ll concatenate t t the th contents of more than one file.

Example:

$ cat data1 data2 A sample p file another sample file $-

20

COPYING FILES
The cp (copy) command duplicates the contents of the source file into a target file. Syntax cp [options] <source file/s><destination directory/file> Examples $ cp old_filename new_filename

21

COPYING FILES
Common Options -i i interactive (prompt and wait for confirmation before proceeding) -r recursively copy a directory Complete path names can be specified with the cp command to copy files across directories directories.

22

REMOVING FILES
The rm( remove) command is used to delete files or directories. Syntax rm [options] filename/s Example $ rm data1 data1.txt txt

23

REMOVING FILES
Common Options 9 -i interactive (prompt and wait for confirmation before proceeding) 9 -r recursively remove a directory, first removing the files and subdirectories beneath it 9 -f f don dont t prompt for confirmation (overrides -i) i) If the file to be deleted is not located in the current or sub directories directories, the complete path name has to be given.

24

MOVING AND RENAMING FILES


The mv command is used to move a file or directory from one 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

MOVING AND RENAMING FILES


Common Options -i i interactive (prompt and wait for confirmation before proceeding) -f dont prompt, even when copying over an existing target file (overrides -i)

26

SPLITTING A FILE INTO MULTIPLE FILES


Large files are sometimes impossible to edit with an editor, and need to be split up. $ split filename What split does is that it creates a group of files xaa,xab,xac and so on that could go up to xaz, and then again from xba,..xbz. In this way you can have 676(26*26) 676(26 26) files, files the last one having the name xzz. xzz

27

SPLITTING A FILE INTO MULTIPLE FILES


Split by default break up the file 1000 lines per file. $ split p 72 data1.txt Contains 72 lines per file. We can even select our own primary y filename by y specifying it as the last argument in the command line $ split chap small This creates files smallaa, smallab,smallac, etc.,

28

DISPLAYING THE CONTENTS 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. The d option specifies the delimiter that must be inserted.

29

paste COMMAND p
Example : $ paste d$: file1 file2 file3 This will take the first line of file1 add $ to it. it Take the first line 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, increasing the number of rows rows.

30

KNOWING FILE TYPES


Even though there are basically three types of files (ordinary,directory, and device) we may often need to know more about these files files. For instance instance, a regular file may contain plain text, a C program or executable code. $ file emp.lst emp.lst : ascii text When the command is used to apply to all files in the progs directory, $ file progs/*

31

nl COMMAND
The nl command is used to add line numbers to a file. Th line The li numbers b records d the h relative l i position i i of f a li line i in a file. Example : $ nl hawks.c hawks c 1. #include <stdio.h> 2.int main() 3{ 3.{ 4. Printf(\nHai); 5. Return 0; 6 } 6. The i option is used to specify the value, other than the default 1, by which line numbers must be incremented.

32

WILDCARD CHARACTERS
W We may require i t to enter t a number b of f filenames fil in i the th command line. Example: $ ls -l chap chap01 chap02 chap03 chap04 chap05 The above listing all files beginning with chap. Since the filenames used here have a common string(chap), the lengthy command line, using this repeatedly, doesnt look elegant. We can have a single pattern consisting of the string chap, along with one or two special characters. g wildcard characters. The shell does this through
33

WILDCARD CHARACTERS
The shell offers the facility to perform an operation on a set of 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. These special characters are called wildcard ildcard characters.

34

WILDCARD CHARACTERS
The following g 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 character character. -- Matches exactly one of a specified set of characters. Using this, the above example can be solved by as follows $ ls l chap*
35

WILDCARD CHARACTERS
Examples: COMMAND OUTPUT $ ls a* a ab abcd ac abc abcde $ ls a? ab ac $ ls a[bcd] ab ac Suppose there are following files in present in working directory: art part part1 part2 part3 mozart tart quartz $ ls a? No output $ ls a* art $ ls *.* No such file and directory $ ls [!abc]art part tart $ ls [ [b-dpu-z]* p ] part p p part1 p part2 p part3
36

FILE ACCESS PERMISSIONS

37

FILE ACCESS PERMISSIONS


Determining the FAPs for a file Fil A File Access P Permissions i i refer f t to th the permissions i i associated i t d with a file with respect to owner, group and other users of the file. The following table summarizes the access permissions available for files and directories.

38

FILE ACCESS PERMISSIONS


Access type read Denoted by r Action permitted on a file Allows you to p y, copt p and display, compile the file Allows you to edit, rename or move the file to another location Allows you to execute t the th file fil provided the file also has the read permission p Action permitted on a directory Allows you to list the contents of the directory. Allows you to create a new file and subdirectory within this directory. Allows you to move to th directory the di t using i th the cd command.To delete a file execute permission is required p q in the directory.
39

write

execute

CHANGING FILE ACCESS PERMISSIONS


The access permissions associated with a file or directory 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

Continuation..
Symbolic mode : r - read w - write x - execute u - users g - group o - others - means remove + means grant = means only those permissions (u u=rw rw means user read and write) Example : $ chmod g-r data1.C data1 C Here g means group - means read permission has been removed.

41

CHANGING FAPs THROUGH ABSOLUTE MODE


The absolute mode of specifying FAPs uses a series of digits to represent the actual permissions. read d write -- 4 -- 2

execute -- 1 The figure below depicts a sample representation of absolute numbers. r w x 7 r w 6 r 4 -

42

Continuation.
Example : $ chmod 764 data1.C

It means file access permission has been changed by the command chmod read, write, execute permissions for the user, read and write for the group owner, and only read for other users

43

MASKING FILE PERMISSIONS


Li Linux understands d t d the th importance i t of f file fil security it and d then sets up certain default permissions for the files you create. Linux Li uses th the variable i bl called ll d umask k to t decide d id th the default permissions. The current value of umask can be easily determined by j t typing just t i umask k $ umask 022 The default permissions for all files should be 666. The default permissions for all directories are 777.

44

LINK FILE
When a file is copied, both the original and copy occupy separate space in the disk. Linux allows a file to have more than one name, and yet maintain a ta a s single g e copy in the t e disk. d s Changes C a ges to o one e file e are also reflected in the other. The file is then said to have more than one link link, it means it has more than one name.

45

ADVANTAGES OF LINKING
If we accidentally id t ll d delete l t a fil file with ith a single i l li link k or a single i l name there is no bringing it back as Linux has no file undelete facility. If one fil file has h 2 or more li links, k our fil file i is safe f even if one of f the links gets deleted. If one file is to be shared between several users instead of f giving i i each h user a separate t copy of f the th same file fil we can create links of this file in each users directory. This avoids unnecessary duplication of the same file contents in different directories directories. The file is physically not removed until at least one link is pointing to the file.

46

HARD LINK AND SYMBOLIC LINK


There are two types of links available in Linux 1. Hard link 2 S 2. Symbolic mbolic Link Hard Link g file to Hard links are special files that allow a single have 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 they y create a direct link to an inode. $ ln data1.dat # by default hard link

47

SYMBOLIC LINK
Limitation of Hard link 9 We cant have two linked files in two file systems. In other words, if we have a file in the ext2 file system, we cant link this file to another file in the /home file system. 9 We can cant t link a directory even within the same file system. Unlike Hard Links, a symbolic link points to the file which c actua actually y has as t the e co contents te ts. We can create a symbolic link with s option of ln. 9 ln s data lndata

48

DIFFERENCE BETWEEN LINKS


Hard links can be created only for files not for directories. Symbolic link can be created for both. S Symbolic b li link li k can work k across different diff t file fil systems t . But Hard link can not work across different file systems. If it is a Symbolic y link, whenever we do ls l it tells it is a linked file. But if we do Hard link it tells it is a ordinary file. A symbolic b li li lined d fil file can not tb be used d after ft d deleting l ti th the original file whereas a hard linked file can be used after deleting the original file.
49

LOCATING FILES
It recursively examines a directory tree to look for files matching some criteria, and then takes some action on the h selected l d fil files. # find / -name .bash_profile print The path list ( / ) indicates that the search should start from the root directory. p the action (p (print) ) to be taken The last section specifies on the file.

50

FINDING UNUSED FILES


# find . -mtime -2 -print This command searches files from the current directory, which have been modified modified, say say, in less than 2 days days. -n less than n +n more than n n exactly equal to n find uses the size operator to locate large files. # find /home size size +2048 print print # find /home size +2048 size 8192 -print

51

COMPARING FILES

52

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 location of the first mismatch(in the ninth character of the first line) is echoed on the screen.

53

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 5 9 143 145 178 190 172 175

54

FINDING WHAT IS COMMON


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 first file. 2nd column lists lines that appear only in the second file. 3rd column -- lists lines that appear in both files.
55

FINDING WHAT IS COMMON


Example : $ cat file1 apple mango orange pear p $ cat file2 grape mango pear rose $ comm file1 file2 apple grape mango orange g pear rose #will display p y only y column 3

$ comm 12 file1 file2 mango pear

56

diff COMMAND
The diff command 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. Editing ma may incl include, de adding a line line, deleting a line line, changing a line, and so on.

57

diff COMMAND
Example $ diff file1 file2 0a1 file >Command options 2c3,4 <system calls -> options of file > list of functions 4d5 < in the chapter.

# Append after line 0 of first # this line # Change line 2 in first file # Replace this line # with # these two # lines # Delete 4 of first file # containing this line

If the two files are identical it displays nothing


58

uniq COMMAND

There may be instances where the lines in a file are duplicated. The duplicates must be removed. The uniq command is used to remove repeated lines from a file file. A file must be sorted before using the uniq command.

59

OPTIONS OF THE UNIQ COMMAND


Options : -u outputs only those lines that are not repeated. -d -c outputs only one copy of each repeated line.

generates output in default style but with each line preceded by a count of the number of items it occurred. -n the first n fields together with any blanks before each a eac are e ignored g o ed +n the first n characters are ignored.

60

OTHER LINUX COMMANDS


date command shows the date and time to the nearest second $ date Fri May 16 08:25:53 IST 2002 clear The clear command is used to clear the terminal screen. who who command is used to report who are all logged in, terminal name and what time they logged in.

61

PIPES AND FILTERS

62

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 g as, Standard Input Files Standard Output Files Standard Error Files

63

STANDARD Input p Files


Standard Input Files In Linux, , when a user executes a command that requires input, the shell interprets the command and assigns the keyboard as the default source of input input. The keyboard is referred to as the standard input file. The file descriptor 0, is assigned to the standard input files

64

STANDARD OUTPUT FILES

Standard Output File The shell assigns the monitor as the default destination 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

Standard Error File Shell utilities display error messages on the monitor. The monitor is thus also the standard error file. File descriptor 2, is assigned to the standard error file.

66

REDIRECTION
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
The following example illustrates the usage of input redirection: $ cat < test1 Here, the less than symbol, <, implies input redirection from the file, test1. The cat command will take each line of f the th file, fil t test1, t1 as input i t and d di display l it on th the monitor. it For better clarity, the above command can also be written using the file descriptor as: $ cat 0< test1 Here, 0 indicates input redirection.

68

OUTPUT REDIRECTION
The following example illustates the usage of output redirection: $ cat test1 > test2 Here, the greater than symbol, >, implies redirection of output to the file, test2. The output of the cat command is written itt t to a di disk k fil file, t test2. t2 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, the command is: $ cat test1 >> test2
69

ERROR REDIRECTION
The following example illustrates the usage of error redirection: $ cat test 2> 2 error error-mesg mesg Assume that the file, test, does not exist in the current directory. So a user tries to execute this command, Linux will generate an error message since the execution is unsuccessful. This error message will be written to the file named errormesg. esg Unlike the file descriptors for the standard input and standard output, the file descriptor for the standard error , is compulsory. p y file 2,
70

FILTERS
A filter is a program that takes its input from the standard input file, processes( or filter) it, and sends its output to the standard output file. The grep filter The grep stands for globally search for regular expression and print out out. Syntax grep regular_expression [filename] regular_expression which has to be searched in the file. file name is optional ,without it grep expects standard input.
71

grep FILTER
In the grep command regular expressions are always given in quotes. quotes Example: grep New[abc] Matches any one of a set of characters. characters grep New[a-c] Matches any one of a range of characters. grep ^New[abc] ^N [ b ] The Th pattern tt following f ll i it must t occur at t the beginning of each line. grep New[^a-c] The pattern must not contain any character in the set specified. specified grep New[abc]$ The pattern preceding it must occur at the end of each line. grep New.[abc] N [ b ] Matches M t h any one character. h t
72

OPTIONS OF THE GREP FILTER


The grep filter also has options that alter the output of the command. These are : -n This prints each line matching the pattern pattern, along with its line number. The number is printed at the beginning of the line. -c This Thi prints i t only l a count t of f the th lines li that th t match t ha pattern. -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. Options Opt o s can ca also a so be combined. co b ed
73

wc FILTER
The wc filter is used to count the number of lines, words, and characters in a disk file or in the standard input. Syntax wc [ [-lwc] ] [filename/s] [ ] Example: $ wc test 2
Options :

The file test has 2 lines, 7 words, 9 characters -l -- Displays the number of lines -w -- Displays the number of words -c -- Displays the number of characters

74

cut FILTER
The cut filter is useful when specific columns from the output of certain commands(such as ls, who) need to be extracted. Syntax cut [options][filename/s] Options -f<column number(s)> -- Displays the specified columns -c<character_number(S)>--Displays c<character number(S)> Displays the specified characters. -d<column_delimiter>--Specifies the column delimiter

75

cut FILTER
Example : $ cut d d : : f1 f1 /etc/passwd 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

tr FILTER
The tr filter can be used to translate one set of characters to another. This filter can also be used to squeeze repeated occurrences of a character into one. The s option is used to squeeze several occurrences of a character into one character. Example: $tr s $t s < temporary te po a y Here tr command takes input from temporary file and s option of tr works on every record, squeezing repeated spaces into a single space.
77

tr FILTER
$ tr : < /etc/passwd This command replaces all occurrences of the : : character with a space, for the file /etc/passwd and displays the output on the standard output.

78

sort FILTER
The sort filter arranges each line of the standard input in ascending order. Syntax : 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

sort FILTER
Example : sort n 12 23 9 121 <ctrl>d Output: 12 121 23 9

80

DISPLAYING DISK FILES IN SORTED ORDER


Specify the file name along with sort. Example p : $ 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

DISPLAYING DISK FILES IN SORTED ORDER


The +pos1 pos2 Option: In certain cases, a user may want to display each line in a file 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 <Tab> To sort a file on a column, the position of the column in the file 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 specified as +pos1 +pos1, where pos1 is the appropriate number. The column separator at which the sort filter has to stop p as pos2, p , where p pos2 is the must be specified appropriate number.
82

DISPLAYING DISK FILES IN 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

OPTIONS OF THE SORT COMMAND


The t Option : The sort filter assumes that the columns are separated by a space or a <Tab>. However, you can create a file with a different column separator. If the file, names, contains columns separated by a hyphen, and the file is to be sorted on the third column, then $ sort t - +2 3 names The o Option : The output of the sort filter always appears on the monitor. In case this output is to be saved on a disk file file, the o o option should be used. $ sort t +2 3 o newnames names

84

OPTIONS OF THE SORT COMMAND


Combination of Options : +4n 5 -rn or nr +4 5 +4nr 5 or +4 +4rn 5 5 fr or r r -fr -- Numeric sort on the fifth column -- Combination of r and n -- Reverse R numeric i sort t on the fifth column. -- Sorting in the reverse order of ascii characters

85

PIPES

Commands can be combined in such a way that the standard output of one command can be sent as standard input to another command by using pipes. Example : $ ls > tempfile $ more tempfile These two commands can be combined together by using pipes. $ ls | more

86

PIPES

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

t COMMAND tee
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

tee COMMAND
Example : $ cat temp | tee temp1 temp2 The above command displays the contents of the file file, 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 piped to the tee command command, which creates the file sortsort numbers, and stores the data in this file. It also sends the data as input to the more utility.

89

PROCESS STATUS
The ps command is used to display the attributes of a process. By default, default ps lists out the processes associated with a user at that terminal: $ ps PID 476 659 684 TTY tty03 tty03 tty03 TIME 00:00:01 00:00:01 00:00:00 CMD login sh ps

90

OPTIONS OF ps
The options: a Lists out the p 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

RUNNING JOBS IN BACKGROUND


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 rest of the jobs have to run in the background. The & is the shells operator used to run a process in the background. Just terminate the command line with an &, the command will run in the background: $ sort t o emp.lst l t emp.lst l t& The shell immediately returns a number the PID of the invoked command.
92

nohup h : LOG OUT SAFELY


The nohup command, when prefixed to a command, permits execution of the process even after user has logged out out. In the following command, no output filename was specified, nohup sends the default output to a file nohup.out $ nohup sort emp.lst & 586 Sending output to nohup.out

93

nohup h : LOG OUT SAFELY


When you use ps command after using nohup you will notice something very significant: $ ps u per UID PID PPID STIME TTY TIME per 101 1 14:52:08 03 0:13 COMMAND 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

PREMATURE TERMINATION OF A PROCESS


Kill command is used to terminate process. The command uses one or more PIDs as its arguments arguments. $ kill 121 125 132 If all of these processes have the same parent parent, you may simply kill the parent in order to kill all its children.

95

KILLING WITH SIGNAL 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 Terminate the 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

JOB EXECUTION WITH LOW PRIORITY


Processes in the Linux system are usually executed with equal priority. Linux offers the nice command command, which is used with the & operator to reduce the priority of jobs. To run a job with a low priority, the command name should h ld b be prefixed fi d with ith nice: i $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
When we invoke bc ( (calculator) ) without arguments, the input has to be keyed in each line terminated by pressing <enter>. After you have finished your work use <ctrl+d>. $ bc 12 + 5 17 ibase=2 11001010 202 # output in decimal, I.e., base 10 <ctrl+d> # To come out of calculator $ factor 28 # input given by the user 7 2 <ctrl +d>

98

HEAD AND TAIL COMMANDS


The head command is used to display the first lines of 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 to display the last lines of a file. The default f number of f lines that are displayed is again 10. $ tail -4 dolls

99

THE vi i EDITOR
A vi session begins by invoking the command vi with (or without) a filename: When we open a file with vi, the cursor is positioned at the top left-hand corner of the screen. We are said to be in the command mode. To enter text, you have to leave the command mode and enter the input mode. We have to save our file or switch to editing another file. Commands Co a ds can ca be entered e te ed in t the e last ast line eo of t the e sc screen ee to act on text. This mode is known as ex mode or line mode.

100

INPUT MODE
After opening the file, the user is in command mode, if you press i insert r or R replace If you want tt to switch it h back b k to t command d mode d or ex mode d from insert mode press esc key. After pressing i or r , you can type the content of the file file. This text goes into a place in memory known as buffer.

101

COMMANDS FOR QUITTING Vi


COMMAND zz :wq :w file name and :q q :w! filename and :q :q! FUNCTION Writes the buffer to the file and quits vi Writes the buffer to the file and quits vi Writes the buffer to file filename(new) ( ) and q quits vi overwrites the existing file filename with the contents of the buffer and quits vi Quits vi whether or not changes made to the buffer were written to a file.
102

BLOCK COMMANDS
T To be b able bl t to use bl block k commands d we must t remember b t two things: 1. All the block commands work in ex command mode. mode 2. Line numbers should be associated with the text before we issue any block commands. To set the line numbers, , in the ex mode, , type yp set number and press 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 the filename at current cursor position.
103

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 buffer for the next occurrence of the pattern of text. ?Pattern searches backward in the buffer for the first occurrence of t o the e patte pattern of o text. te t n repeats the last search N repeats the search command in the opposite pp direction
104

DELETE YANK AND PASTE DELETE,


If you want to delete a particular line, keep the cursor in the corresponding line and press dd.(command mode) If you want to delete 4 lines from the file, keep the cursor in the first line and press 4dd. Y Yank k means without ith t d deleting l ti , vi i copies i th these objects 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.( or np)
105

SYSTEM ADMINISTRATION

106

STARTING UP THE SYSTEM


The startup sequence begins when you turn on the power. The boot program checks for the existence and integrity of any hardware on the system. The program then determines which partition of the disk is active and boots the UNIX kernel. The term booting means taking a copy of the kernel program on the hard disk and placing it in memory(RAM), where it can function. The kernel then starts spawning further processes, the most important of which is init. This process always has the PID 1.
107

RUNLEVELS IN LINUX
init is responsible for keeping the system in different states, also called runlevels. Each run level is normally a single digit(0 to 6). Normally the system t would ld b be i in any one of f th these run l levels: l 0 System shutdown 1 System administration mode 2 Full multi user state 3 Network multiuser 4 User defined 5 Shut down same as 0 6 Shutdown and reboot mode

108

SHUT DOWN THE SYSTEM


The administrator almost invariably uses the shutdown script to bring the system down. File systems are unmounted, processes are killed and messages are broadcast to users with wall command. shutdown finally invokes init with the appropriate run level to determine whether the system is to be placed in single user mode, rebooted or simply powered down. By default this state is 0, which means a total shutdown.
109

ACQUIRING SUPER USER STATUS


The user can also become a super user status with the su command if she knows the root password. For example, the user can also use become a super user: $ su password: ******** # pwd /home/local # prompt changes, but directory # doesnt

The # prompt indicates that the user is a super user.

110

SETTING THE SYSTEM DATE


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 form MMDDhhmm and optionally followed by a two digit year string string. # date 09180934 Thu Sep p 18 09:34:00 IST 1997 The system date can be set only by the administrator.

111

SETTING LIMIT ON FILE SIZE


Faulty programs or processes can eat up disk space in no time. It is desirable to impose a restriction on the maximum size that a user can be permitted to create create. This limit is set by the ulimit statement of the shell. p in units of 512 bytes y is set The default ulimit expressed inside the kernel. Though an ordinary user can only reduce this default value, l th the super user can i increase it it. By default, Linux shows the ulimit value is unlimited.

112

MANAGING DISK SPACE


df (disk free) reports the amount of free space available on the disk. The output always reports for each file system separately: l # df / /home (/dev/root (/dev/home ): 11234 blocks 534 i i-nodes nodes ): 1448 blocks 382 i-nodes

The t option p includes the above output, p , as well as the total amount of disk space in the file system.

113

DISK USAGE
The du (disk usage) command reports usage by a recursive examination of the directory tree. This Thi i is h how du d lists li t the th usage of f /home/sales/perfect /h / l / f t # du /home/sale/perfect 156 /home/sale/perfect/forms 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

compress and gzip COMMAND


Compress is quite fast and produces .z file. $ compress file1.txt $ uncompress fil file1.txt.z 1 To view a compressed file, the zcat command can be used (uncompresses and displays). used.(uncompresses displays) 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


The passwd command is used by a user to change the password, the old password is first prompted for, and then the new one accepted. However, when you invoke the command as a super user, the system behaves in a more lenient and helpful manner: # passwd INFO: Changing password for root New password: ******** Re enter password: ******** Re-enter To change the particular user password, # passwd perfect
116

COPYING DISKETTES AND TAPES


dd was extensively y used in copying py g file systems, y but today y its role is mainly restricted to copying media, like floppies and tapes. # dd if=/dev/fd0 if /d /fd0 of=temp f t bs=147456 b 147456 cnt=1 t 1 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 onetenth the capacit capacity of a 1 1.44MB 44MB diskette. diskette dd only uses raw divides, ie., those in /dev/rdsk or those files in /dev beginning g g with r, like /dev/rfd0135ds18, or /dev/rct0
117

HANDLING DOS DISKETTES


It has now become quite common to see both DOS and UNIX systems on the desktop.Unix provides a family of commands with which one can read from and write onto floppy diskettes. mcopy Copies files from or to from 1.44MB floppy drive mdir Lists files in DOS diskette in DOS style mtype type Concatenates Co cate ates files es in DOS OS diskette mdel Deletes file on DOS diskette
118

FORMATTING FLOPPY DISKETTES & BACKUPS


Before you use a floppy for backup purposes, we need to format it first. This is done with the fdformat with the raw device name as argument. # fdformat /dev/fd0H1440 The two most popular programs for backups are cpio and tar. 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 standard output.
119

cpio p COMMAND
Backing Up Files -o option is used to create an archive, which can then be redirected to a device file. This is how you copy files in the current directory to a 1.44MB floppy # ls | cpio ov > /dev/fd0 Restoring Files A complete archive or selected files can be restored with the i key option. To restore all the files that were backed up with a previous cpio command the shells shell s redirection operator(<) must be used to take input from the device. # cpio iv < /dev/fd0

120

tar COMMAND
tar not only creates archives on tapes, but supports floppies as well . Exclusive features It doesnt use standard input to obtain its file list. tar accepts file and directory names as arguments. It copies one or more entire directory trees trees, i i.e., e it operates recursively. It can create several versions of the same file in a single archive. archive It can append to an archive without overwriting the entire archive.

121

BACKING UP & RESTORING FILES


Backing up p files tar accepts directory and filenames directly on the command line. # tar cvf cvf /dev/fd0 /home/perfect/* /home/perfect/ .sh sh The verbose option(-v) shows the number of blocks used by each file. Never use tar and cpio with their default block sizes. sizes Choose as high a value as the system permits. Higher block sizes speed up I/O operations. g files Restoring Files are restored with x(extract) option. When no file or directory name is specified, it restores all files from the backup device. # tar xvf /dev/fd0 18
122

fdisk IN LINUX
fdisk in Linux creates both partitions as well as file systems. # fdisk The options: -d delete a partition -l list known partition types -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


The interesting thing about a file system is that, once created, it is logically separate entity with its own tree structure and root directory directory. These files systems unite to become a single file system at the time of booting booting. The root file system becomes the main file system, and its root directory y is also the directory y of the unified system. y This happens by a process known as mounting

124

mount COMMAND
To mount(i.e., (i e attach) a file system system, an empty directory (say /mnt) must first be made available in the main file system. # mount t t ext2 /dev/fd0 /mnt/floppy

/dev/fd0 loses its separate identity. It now becomes the directory /mnt/floppy The type of file system is specified with the t option. If you are not specifying a file sytem it assumes the file system should be vfat filesystem.

125

UNMOUNTING FILE SYSTEMS


Unmounting is achieved with the umount command, which requires either the file system or the mount point as argument. # umount /mnt/floppy # umount /dev/fd0 You cant remove a directory unless you are placed in a directory above it, you cant unmount a file system unless you are placed above it. If you try to do that, this is what you will see : #umount /dev/fd0 busy : Device busy.

126

ADDING AND REMOVING USERS


Almost everyday the administrator is required to add or delete a user or group from the system. The Th command d to t add dd the th user i is , # useradd <username> Password can be set for the user by y the root by y giving the command, # passwd <username> The Th command dt to delete d l t the th user is, i # userdel <username>

127

RPM (Redhat Package Manager)


Open packaging system Works on Red hat Linux as well as other Linux & UNIX systems . Features include - installing, uninstalling, upgrading, querying, verifying all in one line commands .

128

INSTALLING AND INSTALLING


Installing # rpm -ivh pkg- 1.0- 2.i386. rpm If rpm gives an error saying package already installed and still want to install the package, use -- replacepkgs i.e., # rpm -ivh --replacepkgs rpm_name Similarly use --replacefiles for conflicting files use -nodeps to install package with dependency conflicts Uninstalling # rpm pkgname // ( not the rpm file f name ) use --nodeps to uninstall package with dependency conflicts.

129

PRINT COMMANDS
 Print commands allows us to print files to standard output t t or to t a line li printer. i t  Following are the commands to submit, cancel, and check the status of a print job. Command/Syntax lpq (lpstat) [options] lpr (lp) [options] lprm (cancel) [options] What it will do show the status of the print jobs file print to defined printer remove a print job from the print queue.

130

Shell Programming

131

SHELL PROGRAMMING
Why y we need shell p programming? g g 1. Customizing your work environment. 2. Automating your daily tasks. 3. Automating repetitive tasks. 4. Executing important system procedures like shutting down the system. 5. Performing g the same operation p on many y files. In the shell programming we have to add, 1. #!/bin/sh - It is a logical link to indicate the shell that it is a shell script. 2. Chmod +x filename without this we will not be able to execute to the shell script.

132

USER DEFINED VARIABLES


The variable length can be of any reasonable length and may constitute tit t alphabets, l h b t digits, di it and d underscores. d However H the first character must be an alphabet or an underscore. $ a=20 $ echo a $ echo $a a 20 $ str=Two words $ echo Enter y your no $ echo $str $ read no Two words To carry out the arithmetic operations on a variable variable, type expr before the operation. For ex: $ `expr $a + $b ` Note: Use the accent graves sign with expr expr.
133

COMMAND LINE ARGUMENTS


Like C program shell program also takes command line arguments. arguments $1 $2 $3 $4 $9 we represent command line arguments like this in the program. $# -- No N of f arguments t $$ -- 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 temp echo $# $3 $0 $$ $1 When we execute temp, ./temp hello how are you 4 are temp 1010 hello
134

CONDITIONAL STATEMENTS
If condition1 # if conditions is TRUE=0 then th cmd list-1 elif condition2; then cmd list-2 else cmd list-n fi Ex: if [ $# -ge 2 ] then echo more than two arguments else echo no input fi
135

CASE STATEMENTS & LOOPING(for)


case param1 in pattern1) cmd-list1;; pattern2) cmd-list2;; *) d def f cmdlist;; dli t esac for var [ in list of vals] do cmd-list done for file in hello1 hello1.c c temp do cp $file new$file done
136

LOOPING (while) ( )
While [ codition ] do cmd-list [ break ] [ continue ] done Ex: E while [ $# -gt 0 ] do echo $1 shift done

# $#=$# -1

137

LOOPING(until) ( )
until loop executes till the conditions becomes false. This is the difference between while & until. until until [ condition ] do cmd list cmd-list [ break ] [ continue ] done Ex: until [ $# -le 0 ] do echo $1 shift done
138

PROGRAMMING TOOLS

139

PROGRAMMING WITH gcc


The C programming language is by far the most used in Linux software development perhaps this is because the y was originally g y developed p in C. Linux system 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: // filename: hello.c hello c #include <stdio.h> int main() { printf(\nHello World!); return 0; } 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. It contains code to set up the main() calling stack, stack a call to printf() function, and code to return the value of 0.
141

gcc ( g (continuation..) )
Linker has to link the object file to produce an executable. The job of the linker is to take object files, merge them with 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 A library is essentially a collection of many object files, including an index. When searching code for the printf(), the linker looks at the index for each library library. It finds the object file containing the printf() function and extracts that object file and links it to the executable.

142

gcc BASIC USAGE g


To compile and link the program to the executable, the programmer would use the command: $ gcc o hello hello.c Wh When executing ti this thi single i l gcc command, d first fi t gcc h had dt to 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 forego the linking process, use the c c switch with gcc, as in: gcc c hello.c //produces the object file hello.o

143

USING MULTIPLE SOURCE FILES


W We have h a program consisting i ti of ft two source files, fil f foo.c and d bar.c. We would use one or more header files containing function d l ti declarations shared h db between t th the t two programs. To compile these two source files and link them together to produce the executable baz, we would use the command: $ gcc o baz foo.c bar.c This is roughly equivalent to the three commands: $g gcc c foo.c $ gcc c bar.c $ gcc o baz foo.o bar.o

144

SHARED LIBRARIES
Virtually all programs are linked against one or more libraries. Any program that uses a C function (such as printf or malloc ) will be linked against the C runtime library. In each of these cases, you must decide whether to link the library statically or dynamically. If you choose h t to li link k statically, t ti ll your programs will ill b be bi bigger and harder to upgrade, but probably easier to deploy. If you link dynamically, your programs will be smaller, easier to upgrade, but harder to deploy.

145

ARCHIVE
An archive (or static library) is simply a collection of object files stored as a single file. You can create an archive using the ar command.Archive files traditionally use a .a extension rather than the .o extension used by ordinary object files. Heres how you would combine test1.o and test2.o into a single libtest.a archive: $ar cr libtest.a test1.o test2.o The cr flags tell ar to create the archive. Now you can link with this archive using the -ltest ltest option with gcc.

146

DYNAMICALLY LINKED LIBRARIES


What is the main difference between shared library and an archive? 9 The most fundamental difference is that when a shared library is linked into a program program, the final executable does not actually contain the code that is present in the shared library. Instead, the executable merely contains a reference to the shared library. To create a shared library, you must compile the objects that will 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 using test test.o o as part of a shared object object.
147

CONTINUATION
Position-Independent Code (PIC) 9 PIC stands for position-independent code. The functions in a shared library may be loaded at different addresses in different programs, so the code in the shared object must not t depend d d on th the address dd ( (or position) iti ) at t which hi h it i is loaded. 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 always y begins with lib to indicate that the file is a library.
148

USING LD_LIBRARY_PATH LD LIBRARY PATH


When y you link a p program g with a shared library, y the linker p places only y the name of the shared library. The system searches only /lib and /usr/lib , by default. If your shared h d library lib li lies outside t id th these di directories, t i suppose it i is 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. First Fi t it will ill search hi in LD LD_LIBRARY_PATH LIBRARY PATH variable i bl and d then th it will ill search in /lib and /usr/lib.

149

PROS AND CONS


Advantages of Shared Library One O major j advantage d t of f a shared h d lib library i is th that t it saves space on the system where the program is installed. A related advantage to shared libraries is that users can upgrade the libraries with-out upgrading all the programs that depend on them. Disadvantages of Shared Library An upgrade to a shared library affects all programs that depend on it. It is very difficult to Install our libraries in /lib or /usr/lib , without the administrator privileges. LD_LIBRARY_PATH means an extra step for them. Because each user has to do this individually, this is a substantial additional burden.

150

MAKEFILES
Why do we need makefiles? gcc o output hello.c hello1.c gcc command in the above example, g p , would recompile p 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 last compilation.There would be no reason to recompile the other source files. To overcome this problem, makefiles are used.

151

WHAT MAKE DOES


The basic goal of make is to let you build a file in small steps. If lot of source files make up the final executable, we g one and rebuild the executable without having g to change recompile everything. Example: edime: main.o edit.o gcc o edime main.o edit.o main.o: main.c gcc c main.c g edit.o: edit.c gcc c edit.c

152

make(CONTINUATION)
The above example builds a program named edime from two source files named main.c and edit.c. The command to create a executable, $ make edime The steps it follows are: gcc c main.c gcc c edit.c gcc o edime main.o edit.o If we edit main.c and reissue the command, it rebuilds only the necessary files saving us some time: gcc c main.c gcc o edime main.o edit.o g
153

SOME SYNTAX RULES IN MAKE FILES


The lines containing commands have to begin with tabs(not spaces). And dont use a tab before any other line. 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

MACROS IN MAKEFILES
When people use a filename or other string more than once in a makefile, they tend to assign it to a macro. Example: OBJECTS = main.o edit.o edime: di $(OBJECTS) gcc o edime $(OBJECTS) When make runs, runs it simply plugs in main main.o o edit edit.o o wherever you specify $(OBJECTS).

155

Thank You!!!!

156

Você também pode gostar