0% found this document useful (0 votes)
68 views69 pages

IT OS Commads

The document discusses basic UNIX commands including commands for general use, directories, files, and dates. Some key commands covered are telnet for remote login, adduser for creating users, passwd for setting passwords, mkdir for creating directories, cd for changing directories, ls for listing files, cat for viewing and concatenating files, rm for deleting files, and cp for copying files. Formatting codes are also provided for customizing output with the date command.

Uploaded by

Jay Shah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views69 pages

IT OS Commads

The document discusses basic UNIX commands including commands for general use, directories, files, and dates. Some key commands covered are telnet for remote login, adduser for creating users, passwd for setting passwords, mkdir for creating directories, cd for changing directories, ls for listing files, cat for viewing and concatenating files, rm for deleting files, and cp for copying files. Formatting codes are also provided for customizing output with the date command.

Uploaded by

Jay Shah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 69

IT_140702 Operating systems

Practical 1 UNIX COMMANDS Aim: Study of Basic UNIX Commands. Software Required: Server Telnet Client, Linux

Knowledge Required: Shell commands, Shell scripting language Theory: In UNIX, All commands must be entered in small case letters. After entering a UNIX command; you must hit the key to initiate the command. Some commands require arguments (parameters) in order for the command to execute successfully. Other commands allow optional arguments. Several types of UNIX commands are as follows General Commands:Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 1 of 69

IT_140702 Operating systems

1) telnet DESCRIPTION: - The telnet command is used to communicate with another host using the TELNET protocol. SYNTAX: -telnet [host IP] EXAMPLE: -For CEIT Linux Server Use following IP telnet 10.0.0.1 OR telnet 172.16.0.211 2) login DESCRIPTION: - login is used when signing onto a system. SYNTAX: - login [name] EXAMPLE: - login: os07it001 Password: **** 3) adduser DESCRIPTION:-Create a new user or update default new user information. SYNTAX: - $ adduser [username] EXAMPLE: - $adduser Gaurav OUTPUT: - New user added successfully.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 2 of 69

IT_140702 Operating systems

4) passwd DESCRIPTION: - passwd is used to update a user authentication token. After you create a new user with adduser command, passwd is used to set new password. SYNTAX: - passwd [username] EXAMPLE: - $ adduser gaurav $ passwd gaurav Changing password for user gaurav New Password: **** passwd: all authentication tokens updated successfully. 5) man DESCRIPTION: - It provides help on various commands. SYNTAX: - $man [Command Name] EXAMPLE: - $man telnet 6) logname DESCRIPTION: - Print users login name. SYNTAX: - $logname OUTPUT: - os07it001
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 3 of 69

IT_140702 Operating systems

7) uname DESCRIPTION: - Print system information SYNTAX: - $uname OUTPUT: - Linux 8) who DESCRIPTION: - The "who" command lets you display the users that are currently . logged into your UNIX computer system SYNTAX: - $who The general format for output is: [login name] [device name of the respective terminal] [date and time of login] [IP of the respective terminal] OUTPUT: -neha pts/0 Jun 17 14:25 [172.16.0.34] chandni pts/1 Jun 17 15:25 [172.16.0.35] 9) who am i DESCRIPTION:-Shows who is logged in. SYNTAX: - $who am i OUTPUT: - neha pts/0 Jun 17 14:25 [172.16.0.34]
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 4 of 69

IT_140702 Operating systems

10) tty DESCRIPTION:-Print the file name of the terminal connected to standard input. SYNTAX: - $ tty OUTPUT: - /dev/pts/ 11) date DESCRIPTION: - date command displays the time and date. The super-user can use it to set the system clock. SYNTAX: - $date OUTPUT: - Fri Jul 27 14:12:06 EDT 2007 Formatting of Data:To format a date provide a string beginning with +. EXAMPLE: - $date +%b OUTPUT: - Jan [%b option only display current month] Other Formats specifiers are as follows:Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 5 of 69

IT_140702 Operating systems

Specifier

Description Day

Values/Example

%a %A

weekday, abbreviated

Wed

weekday, full Wednesday day of the month (dd), zero padded day of the month (dd) day of year, zero padded day of week starting with Monday (1), i.e. mtwtfss

%d

17

%e

17

%j

000-366

%u

Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

Page 6 of 69

IT_140702 Operating systems

%w

day of week starting with Sunday (0), i.e. smtwtfs

%U

week number Sunday as first 053 day of week Week week number Monday as 053 first day of week week of the year 053

%W

%V

Month %m
Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

mm month

06
Page 7 of 69

IT_140702 Operating systems

%h %b

Mon

Jun

Mon, locale's Jun abbreviated locale's full month, June variable length Year

%B

%y %Y

yy two digit year ccyy year 2-digit year corresponding to the %V week number 4-digit year corresponding

0099 2009

%g

%G
Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

Page 8 of 69

IT_140702 Operating systems

to the %V week number Century %C cc century Date %D mm/dd/yy 06/17/09 0099

%x

locale's date representation 06/17/2009 (mm/dd/yy) %Y-%m-%d Hours

%F

%l hour (12 hour (Lowercas 9 clock) e L)


Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 9 of 69

IT_140702 Operating systems

%I hour (12 hour (Uppercas clock) zero 09 e I) padded %k hour (24 hour 21 clock) hour (24 hour clock) zero 21 padded locale's upper case AM or PM PM (blank in many locales) locale's lower case am or pm pm (really!) Minutes %M
Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

%H

%p

%P

MM minutes 19
Page 10 of 69

IT_140702 Operating systems

Seconds seconds since 00:00:00 1245273549 1970-01-01 UTC 0060 (The 60 is necessary to accommodate a leap second) 000000000 999999999

%s

%S

SS second

%N

nanoseconds

Time hours, minutes, seconds (12hour clock)

%r

09:19:09 PM

%R
Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

hours, minutes hh:mm e.g. 21:19


Page 11 of 69

IT_140702 Operating systems

(24 hour clock) hours, minutes, seconds (24hour clock) locale's time representation (%H:%M:%S) Date and Time %c locale's date and time Sat Nov 04 12:02:33 EST 1989

%T

21:19:09

%X

Time zone -zzzz RFC822 style numeric timezone

%z

-0500

Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

Page 12 of 69

IT_140702 Operating systems

%Z

time zone (e.g., EDT) nothing if no EST time zone is determinable

12) cal DESCRIPTION: - cal displays a simple calendar. If arguments are not specified, the current month is displayed. The options are as follows SYNTAX: - $ cal OUTPUT: - October 2008 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 13 of 69

IT_140702 Operating systems

26 27 28 29 30 31 Other options available are: -1 Display single month output. (This is the default value) -3 Display previous/current/next month output. -s Display Sunday as the first day of the week. (This is the default value) -m Display Monday as the first day of the week. -j Display Julian dates (days one-based, numbered from January 1). -y Display a calendar for the current year. 13) Echo DESCRIPTION:-Displays a line of text. It works like printf function in c EXAMPLE: - $echo Welcome to Linux world OUTPUT: - Welcome to Linux world Directory Commands:Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 14 of 69

IT_140702 Operating systems

14) mkdir DESCRIPTION: - Create the DIRECTORY, if it does not already exist. SYNTAX: - $mkdir [directory name] EXAMPLE: - $ mkdir neha NOTE: If you want to check whether directory is created or not use ls command. 15) cd DESCRIPTION: - cd is used to change directory. EXAMPLE: - $ cd neha 16) cd.. DESCRIPTION: - To come out of current working directory 17) pwd DESCRIPTION: - Use this command to find out what directory you are working in. EXAMPLE: - $pwd OUTPUT: - /home/oso7it001/neha. 18) rmdir DESCRIPTION: - Deletes a empty directory.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 15 of 69

IT_140702 Operating systems

EXAMPLE: - $rmdir neha The above command removes the directory neha. To check whether directory is deleted or not use ls command. You can also delete more than one directory in one shot. File Commands:19) touch DESCRIPTION: - Touch command is used to create empty file. Its size is 0 bytes. It will not allow user to write anything in file. Using touch command you can also create multiple files at a time. EXAMPLE: - $touch f1 f2 f3 f4 This command will create four empty file as stated above. 20) cat DESCRIPTION: - This is one of the most flexible UNIX commands. We can use to create, view and concatenate files. For our first example we create a threePrepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 16 of 69

IT_140702 Operating systems

item English-Spanish dictionary in a file called "dict." EXAMPLE:-$ cat >dict [Enter data into file] red rojo green verde blue azul <Control-D> Above, <control-D> stands for "hold the control key down, then tap 'd'". The symbol > tells the computer that what is typed is to be put into the file dict. To view a file we use cat in a different way: $ cat dict OUTPUT:red rojo green verde blue azul If we wish to add text to an existing file we do this: $ cat >>dict white blanco black negro <Control-D>
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 17 of 69

IT_140702 Operating systems

Now suppose that we have another file tmp that looks like this: $ cat tmp cat gato dog perro Then we can join dict and tmp like this: $ cat dict tmp >dict2 21) rm DESCRIPTION: - rm (short for remove) is a UNIX command used to delete files from a file system. EXAMPLE:-$ rm dict To check whether file is deleted or not use ls command. Common options that rm accepts include: -r, which removes directories, removing the contents recursively beforehand (so as not to leave files without a directory to reside in) ("recursive") -i, which asks for every deletion to be confirmed ("interactive")

Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

Page 18 of 69

IT_140702 Operating systems

-f, which ignores non-existent files and overrides any confirmation prompts ("force") -v, which shows what is being removed as it happens ("verbose") EXAMPLE:- $rm v dict OUTPUT: - removed dict To create a hidden file, file name should begin with. (dot) EXAMPLE:$ cat >.f1 When you give ls command hidden file would not be visible.

22) cp DESCRIPTION: - cp is the command entered in a UNIX shell to copy a file from one place to another. The original file remains unchanged, and the new file may have the same or a different name. To copy a file to another file:Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 19 of 69

IT_140702 Operating systems

SYNTAX: - $ cp [Source File] [Target File] To copy a file to a directory $ cp [Source File] ... [Target Directory] To copy a directory to a directory $ cp [Source Directory] ... [Target Directory] 23) mv DESCRIPTION: - mv (short for move) is a UNIX command that moves one or more files or directories from one place to another. The original file is deleted, and the new file may have the same or a different name. If possible (i.e. when the original and new files are on the same file system), mv will rename the file instead. Write permission is required on all directories being modified
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 20 of 69

IT_140702 Operating systems

SYNTAX: - mv [myfile] [mynewfilename] The above command renames the file. 24) ln DESCRIPTION: - ln is a standard UNIX command used to create links (link) to files Links allow more than one file name to refer to the same file SYNTAX: - ln [file_name] [link_name] 25) ls DESCRIPTION: - "ls" stands for list. It is used to list information about files and directories. EXAMPLES:$ ls DESCRIPTION: - This is the basic "ls" command, with no options. It provides a very basic listing of the files in your current working directory. Filenames
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 21 of 69

IT_140702 Operating systems

beginning with a decimal are considered hidden files, and they are not shown. OUTPUT: - Neha chandni f1 f2 $ls -a DESCRIPTION: -The -a option tells the ls command to report information about all files, including hidden files.

$ls -l DESCRIPTION: -The -l option tells the "ls" command to provide a long listing of It is used in UNIX long directory listings. It consists of 10 characters. The first character shows the file type. Next 9 characters are permissions, consisting of three groups: owner, group, others. Each group consists of three symbols: rwx (in this order), if some permission Is denied, then a dash "-" is used instead.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 22 of 69

IT_140702 Operating systems

EXAMPLE: - -rwxr--r-0123456789 Symbol in the position 0 ("-") is the type of the file. It is either "d" if the item is a directory or "l" if it is a link or "-" if the item is a regular file. Symbols in positions 1 to 3 ("rwx") are permissions for the owner of the file. Symbols in positions 4 to 6 ("r--") are permissions for the group. Symbols in positions 7 to 9 ("r--") are permissions for others. r Read access is allowed w Write access is allowed x Execute access is allowed Replaces "r", "w" or "x" if according access type is denied Examples: -rwxr-xr- File, x owner has read, write, execute permissions, group: only read and execute permissions,
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 23 of 69

IT_140702 Operating systems

others: only read and execute permissions. Directory, dr-x------ owner has read and execute access, group and others have no access EXAMPLE: - $ ls -l total 16 [total no of blocks occupied on the hard disk] Permis No Own sion of er own Na er me -rw2 chan rw-r-dni -rwrw-r--rwrw-r--rwrw-r-2 1 1 Gro Siz Time up e Of Nam Creat e ion chan 24 Jun dni 18 14:30 chan chan 24 Jun dni dni 18 14:30 chan chan 37 Jun dni dni 18 14:30 chan chan 0 Jun dni dni 18 File/Dire ctory file1 file2 file3 n3
Page 24 of 69

Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

IT_140702 Operating systems

Drwxr 2 wxr-x

11:12 chan chan 40 Jun Neha dni dni 93 18 14:30

26) chmod DESCRIPTION: - To change permissions on a UNIX file use the chmod chmod command is use To set/modify a file or directorys permissions of course, only the owner of a file may use chmod to alter a file's permissions. chmod has the following SYNTAX :- chmod [options] mode file(s) There are two types of modes available:1.

String modes:-

Reference Class Description U user the owner of the file G group users who are members of the
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 25 of 69

IT_140702 Operating systems

O A

file's group users who are not the owner of others the file or members of the group all three of the above, is the all same as ugo

The chmod program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted: Operator Description adds the specified modes to the + specified classes removes the specified modes from the specified classes the modes specified are to be made the = exact modes for the specified classes String mode examples:For example, the following command would be used to add the read and write permissions to the
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 26 of 69

IT_140702 Operating systems

user and group classes of a file or directory named sample $ chmod ug+rw sample $ ls -ld sample drw-rw---- 2 unixguy unixguy 96 Dec 8 12:53 sample
2.

Octal numbers

The chmod command also accepts three and four-digit octal numbers representing modes. See the article mentioned above for more information. Using a four-digit octal number to set the modes of a file or directory named sample would look something like this: Permission weight Read (r) Write (w) Execute 4 2 1

$ chmod 0664 sample Assuming that the setuid, setgid and sticky bits are not set, this is equivalent to $ chmod 664 sample
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 27 of 69

IT_140702 Operating systems

or $ chmod +r,-x, ug+w sample 27) umask DESCRIPTION: - Umask is use to decide the default permission.It stands for user file creation mask, mask implying which permission to mask or hide. The umask value tells UNIX which of three permission are to be denied rather than granted. $touch sample $ ls l sample -rw-rr 1 chandni chandni 24 Jun 18 14:30 sample. The current value of umask can be easily determined by just typing umask EXAMPLE: - $ umask OUTPUT: - 0022 Here, the first 0 indicate that what follows is an octal number. The three digits that follow the first zero refer to the permission to be denied to the owner, group and others. This means that for
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 28 of 69

IT_140702 Operating systems

the owner no permission is denied, whereas for both the group and others write permission (2) is denied. Whenever a file is created UNIX assumes that the permissions for this file should be 666.Burt since our umask value is 022, UNIX subtracts this value from the default system wide permission (666) resulting in a value (644).This value is then used as the permission for the file that you create. That is the reason why the permissions turned out to be 644 or rw-rrfor the sample file. Similarly system wide default permission for a directory are 777.This means that when we create a directory its permissions would be 777 022 i.e. 755.execute permission for a directory has a special significance. If a directory doesnt have an execute permission we can never enter into it. We can change the current umask. EXAMPLE:- $umask 242 Here onwards new file have permission 424 (666- 242).ans directory permission 535 (777242)
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 29 of 69

IT_140702 Operating systems

Miscellaneous Commands Filter Commands:28) head DESCRIPTION: - head command displays first 10 lines in the file. SYNTAX: - $ head [file name] EXAMPLE: - $head neha Note: It will display first 10 lines of the file. 29) tail DESCRIPTION: -tail command displays last 10 lines in the file. SYNTAX: - $ tail [file name] EXAMPLE: - $ tail neha Note: It will display last 10 lines of the file. For both head and tail command in order to display either first n or last n no of lines respectively, use OPTION n
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 30 of 69

IT_140702 Operating systems

EXAMPLE: - $ head -3 neha It will display first 3 lines of neha file. EXAMPLE: - $tail -3 neha It will display last 3 lines of neha file. 30) cut DESCRIPTION: -The external cut command displays selected columns or fields from each line of a file Cut can work in two modes: 1. column delimited selection (each column starts with certain fixed offset defined as range from-to) cut identifies both columns and fields. $ cat > file1 Neha lecturer os Chandni lecturer os Hinal lecturer Java The c option cutting columns. Example $ cut c 1 - 7, 19 -20 file1 OUTPUT:Neha os
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 31 of 69

IT_140702 Operating systems

Chandni os Hinal Java


2.

Separator-delimited selection (with column separator being a single character like blank, comma, colon, etc). In this mode cut uses a delimiter defined by -d option (as in example above). By default cut uses the value of delimiter stored in a shell variable called IFS (Input Field Separators) -typically TAB. The f option for cutting fields. Along with f option you have to user d (delimiter) for the field delimiter. $ cat > file2 Neha | lecturer | os Chandni | lecturer | os Hinal | lecturer |Java

EXAMPLE: - $ cut d | f 2, 3 file2 OUTPUT:- lecturer | os lecturer | os lecturer |Java


Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 32 of 69

IT_140702 Operating systems

31) paste DESCRIPTION: -It vertically merges lines of files. SYNTAX: - paste [OPTION]... [FILE]... EXAMPLE: - [Enter the Data of file 1] $cat >f1 Neha Patel [Enter the Data of file 1] $cat >f2 Chandni Shah $ paste f1 f2 OUTPUT: - Neha chandni Patel shah OPTIONS: - You can specify a delimiter of your choice with the d option EXAMPLE: - $ paste d \: f1 f2 OUTPUT: - Neha: Chandni Patel: Shah
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 33 of 69

IT_140702 Operating systems

32) sort DESCRIPTION:-Write lines consisting of the sequentially corresponding lines from each FILE, separated by Tabs, to standard output. With no FILE, Or when FILE is -, read standard input.

OPTIONS:1.-d, --delimiters=LIST reuse characters from LIST instead of Tabs 2.-s, --serial paste one file at a time instead of in parallel SYNTAX:sort [-b] [-d] [-f] [-i] [-m] [-M] [-n] [-r] [-u] [+fields] filename [-o outputfile] -b
Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

Ignores spaces at beginning of the


Page 34 of 69

IT_140702 Operating systems

line. -d Uses dictionary sort order and ignores the punctuation. -f Ignores caps -i Ignores nonprinting control characters. -m Merges two or more input files into one sorted output. -M Treats the first three letters in the line as a month (such as may.) -n Sorts by the beginning of the number at the beginning of the line. -r Sorts in reverse order -u If line is duplicated only display once +fields Sorts by fields , usually by tabs filename The name of the file that needs to be sorted. -o Sends the sorted output to a file. outputfile EXAMPLES:$ sort -r file.txt Would sort the file, file.txt in reverse order. Related commands
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 35 of 69

IT_140702 Operating systems

33) uniq DESCRIPTION:-Remove duplicated lines from a sorted file OPTIONS:1.-c, --count prefix lines by the number of occurrences 2.-d, --repeated only print duplicate lines 3.-D, --all-repeated[=delimitmethod] print all duplicate lines Delimit-method= {none (default), prepend, separate} Delimiting is done with blank lines. 4.-i, --ignore-case ignore differences in case when comparing
5.

-u, --uniqueQ only print unique lines


Page 36 of 69

Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

IT_140702 Operating systems

34) tr DESCRIPTION:-tr stands for Translating characters. UNIX tr command copies the standard input to the standard output with substitution or deletion of selected characters. In addition it can squeeze repeating characters into a singe character (with option -s). The format of the tr command is somewhat strange -- this is one of the few Unix commands that accepts input only from standard input. SYNTAX: - tr [options] [set1 [set2]] <inputfile Input characters in set1 are mapped to corresponding characters in set2. If length is unequal then set2 is extended to the length of set1 by repeating its last character as necessary. Excess characters in set2 are ignored. The set can be specified directly or as a complement.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 37 of 69

IT_140702 Operating systems

Utility performs classic alphabet1 to alphabet2 type of translation sometimes called 1:1 transliteration and as such is suitable for implementation of Caesar cipher. UNIX inherited tr from Multix as a derivative of PL/1 translate built-in, which in turn was a generalization of a TR command in System/360 architecture Sets can be specified by enumeration of characters like in tr '{}' '()' < infile > outfile) or using ranges like in tr 'A-Z' 'a-z' < infile > outfile. Special POSIX character classes can be used: alnum: alphanumeric characters alpha: alphabetic characters cntrl: control (non-printing) characters digit: numeric characters graph: graphic characters lower: lower-case alphabetic characters print: printable characters punct: punctuation characters space: whitespace characters upper: upper-case characters
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 38 of 69

IT_140702 Operating systems

xdigit: hexadecimal characters Typical usage of classes involves changing the case from upper to lower or vise versa like in the following example: cat >f1 HI HELLO NEHA CHANDNI $ tr '[: upper:]' '[: lower:]' < f1 OUTPUT:hi hello neha chandni Classes can be combined to form a more complex set, for example '[: lower:] [: upper:]' The tr utility accepts several options: -d delete characters in the first set from the output -d, --delete Delete specified set of characters defined in set1 but do not translate. For example: cat > f1 Hi; hello
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 39 of 69

IT_140702 Operating systems

134 / 12 EXAMPLE: - tr -d '; /' < f1 OUTPUT: - Hi hello 134 12 [: alnum:] -- All letters and digits [: alpha:] -- All letters [: blank:] -- All horizontal whitespace [: cntrl:] -- All control characters [: digit:] -- All digits [: graph:] -- All printable characters, not including space [: lower:] -- All lower case letters [: print:] -- All printable characters, including space [: punct:] -- All punctuation characters [: space:] -- All horizontal or vertical whitespace [: upper:] -- All upper case letters 35) grep The grep command allows you to search for a specified pattern in one or more files, printing any lines which contain the specified pattern.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 40 of 69

IT_140702 Operating systems

The syntax for the grep command is: grep [options] pattern [files] OPTIONS:-b -c -h -i -l -n -s -v -w Display the block number at the beginning of each line. Display the number of matched lines. Display the matched lines, but do not display the filenames. Ignore case sensitivity. Display the filenames, but do not display the matched lines. Display the matched lines and their line numbers. Silent mode. Display all lines that do NOT match. Match whole word.

EXAMPLES:grep -c tech file1 Exit Status


Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 41 of 69

IT_140702 Operating systems

This command returns the following exit values: 0 A match was found. 1 No match was found. A syntax error was found or a file was >1 inaccessible (even if matches were found). EXAMPLES:To use a pattern that contains some of the pattern-matching characters *, ^,?, [,], \(, \), \{, and \}, enter: grep ^[a-zA-Z]" pgm.s This displays every line in pgm.s whose first character is a letter. To display all lines that do not match a pattern, enter: grep -v "^#" pgm.s This displays every line in pgm.s whose first character is not a # (pound sign). To display all lines in the file1 file that match either the abc or xyz string, enter: grep -E "abc|xyz" file1 36) cmp
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 42 of 69

IT_140702 Operating systems

DESCRIPTION: - cmp is a command line utility for computer systems that use UNIX or a Unix-like operating system. It compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported. cmp may be qualified by the use of the following command-line switches, (the long versions are in brackets): 1)-b, --print-bytes Print differing bytes. 2)-i SKIP, --ignore-initial=SKIP Skip the first SKIP bytes of input. 3)-i SKIP1:SKIP2, --ignoreinitial=SKIP1:SKIP2 Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2. 4)-l, --verbose
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 43 of 69

IT_140702 Operating systems

Output byte numbers and values of all differing bytes. 5)-n LIMIT, --bytes=LIMIT Compare at most LIMIT bytes. 6)-s, --quiet, --silent Output nothing; yield exit status only. 7)-v, --version Output version info. 8)--help Outputs a help file. Return values:0 - files are identical 1 - files differ 2 - inaccessible or missing argument 37) comm DESCRIPTION: - comm reads two files as input, regarded as lines of text. comm outputs one file, which contains three columns. The first two columns contain lines unique to the first and second file, respectively. The last column contains lines common to
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 44 of 69

IT_140702 Operating systems

both. This functionally is similar to diff. EXAMPLE:File foo apple banana eggplant File bar apple banana banana zucchini comm foo bar apple banana banana eggplant zucchini Disk related Commands:Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 45 of 69

IT_140702 Operating systems

38) du DESCRIPTION: - du (abbreviated from disk usage) is a standard UNIX program used to estimate the file space usage; space used under a particular directory or files on a file system. du takes a single argument, specifying a pathname for du to work; if it is not specified, the current directory is used. The SUS mandates for du the following options: -a, display an entry for each file (and not directory) contained in the current directory -H, calculate disk usage for link references specified on the command line -k, show sizes as multiples of 1024 bytes, not 512-byte -L, calculate disk usage for link references anywhere -s, report only the sum of the usage in the current directory, not for each file -x, only traverse files and directories on the device on which the pathname argument is specified.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 46 of 69

IT_140702 Operating systems

-h, show filesize in human readable format. EXAMPLE:sum of directories in kilobytes $ du -sk * OUTPUT:152304 directory One 1856548 directory Two Sum of directories in human-readable format (Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte): $ du -sh * 149M directoryOne 1.8G directoryTwo disk usage of all subdirectories and files including hidden files within the current directory (sorted by filesize): $ du -sk . [A-z]* *|sort -n The weight of directories: $ du -d 1 -c -h 39) df
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 47 of 69

IT_140702 Operating systems

DESCRIPTION:-df (abbreviation for disk free) is a standard Unix computer program used to display the amount of available disk space for file systems on which the invoking user has appropriate read access. df is usually implemented by reading the mtab file OPTIONS:-k Use 1024-byte units, instead of the default 512-byte units, when writing space figures. -P Use a standard, portable, output format -t If XSI compliant, show allocated space as well The output with -P shall consist of one line of information for each specified file system. These lines shall be formatted as follows: <fs name>, <total space>, <space used>, <space free>, <percentage used>, <fs root>
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 48 of 69

IT_140702 Operating systems

In the following list, all quantities expressed in 512-byte units (1024-byte when -k is specified) shall be rounded up to the next higher unit. The fields are: <fs name> The name of the file system, in an implementation-defined format. <total space> The total size of the file system in 512-byte units. The exact meaning of this figure is implementation-defined, but should include <space used>, <space free>, plus any space reserved by the system not normally available to a user. <space used> The total amount of space allocated to existing files in the file system, in 512-byte units. <space free>

Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

Page 49 of 69

IT_140702 Operating systems

The total amount of space available within the file system for the creation of new files by unprivileged users, in 512-byte units. When this figure is less than or equal to zero, it shall not be possible to create any new files on the file system without first deleting others, unless the process has appropriate privileges. The figure written may be less than zero. <percentage used> The percentage of the normally available space that is currently allocated to all files on the file system. This shall be calculated using the fraction: <space used> / (<space used>+ <space free>) expressed as a percentage. This percentage may be greater than 100 if <space free> is less than zero. The percentage value shall be expressed as a positive integer, with any fractional result causing it to be rounded to the next highest integer.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 50 of 69

IT_140702 Operating systems

<fs root> The directory below which the file system hierarchy appears. Process related Commands:40) ps DESCRIPTION:-The "ps" command (active process) lets you check the status of processes that are running on your UNIX system. Note - the ps command may be slightly different on your system, depending on the type of system you have. The examples shown here work on systems derived from AT&T's original Unix System V ps command. SYNTAX: - ps [options] EXAMPLES:ps
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 51 of 69

IT_140702 Operating systems

The ps command by itself shows minimal information about the processes you are running. Without any arguments, this command will not show information about other processes running on the system. ps f The -f argument tells ps to supply full information about the processes it displays. In this example, ps displays full information about the processes you are running. ps e The -e argument tells the ps command to show every process running on the system.

ps -ef The -e and -f arguments are normally combined like this to show full information about every
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 52 of 69

IT_140702 Operating systems

process running on the system. This is probably the most often-used form of the ps command. ps -ef | more Because the output normally scrolls off the screen, the output of the ps -ef command is often piped into the more command. The more command lets you view one screenful of information at a time. ps -fu fred This command shows full information about the processes currently being run by the user named fred (the -u option lets you specify a username).

QUESTIONS:1) What is the difference between MV and CP command of UNIX?


Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 53 of 69

IT_140702 Operating systems

2) How can we obtain just day from the date command? 3) Explain different types of calculator functions. 4) Explain cut and paste commands with example. 5) What is difference between internal command and external command?

Practical 3 Aim: Shell Programming.

Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

Page 54 of 69

IT_140702 Operating systems

Software Required: Linux Server

Telnet Client facility,

Knowledge Required: Shell commands, Shell scripting language Theory / Logic: A shell is a command line interpreter. It takes commands and executes them. As such, it implements a programming language. The Bourne shell can be used to create shell scripts, i.e., programs that are interpreted/ executed by the shell. You can write shell scripts with the C shell; however, this is not covered here. Some Basic Points about the Shell Scripts: Shell scripts are simple text files created with an editor. Shell scripts are marked as executable Shell scripts should be located in your search path and ~/bin should be in your search path. You likely need to rehash if you're a Csh (tcsh) user (but not again when you login). Arguments are passed from the command line and referenced, for example, as $1.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 55 of 69

IT_140702 Operating systems

#! /bin/sh All Bourne Shell scripts should begin with the sequence #! /bin/sh From the man page for exec(2): On the first line of an interpreter script, following the "#! is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains "#! /bin/sh", then the con- tents of the file are executed as a shell script. You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. Long ago there was just one (the Bourne Shell) but these days there are many interpreters -- csh, ksh, bash, and others. Comments Comments are any text beginning with the pound (#) sign. A comment can start anywhere on a line and continue until the end of the line. Search Path All shell scripts should include a search path specification: PATH=/usr/ucb:/usr/bin:/bin; export PATH
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 56 of 69

IT_140702 Operating systems

A PATH specification is recommended -- often times a script will fail for some people because they have a different or incomplete search path. The Bourne Shell does not export environment variables to children unless explicitly instructed to do so by using the export command. VI EDITOR: vi (pronounced vee-EYE, short for "visual") provides basic text editing capabilities. Three aspects of vi make it appealing. First, vi is supplied with all UNIX systems. You can use vi at other universities or any businesses with UNIX systems. Second, vi uses a small amount of memory, which allows efficient operation when the network is busy. Third, because vi uses standard alphanumeric keys for commands, you can use it on virtually any terminal or workstation in existence without having to worry about unusual keyboard mappings. As a point of interest, vi is actually a special mode of another UNIX text editor called ex. Normally you do not need to use ex except in vi mode.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 57 of 69

IT_140702 Operating systems

To start vi, enter: vi filename RETURN Saving Your Work: vi provides several means of saving your changes. Besides saving your work before quitting, it's also a good idea to save your work periodically. Power failures or system crashes can cause you to lose work. From command mode, you type: :w RETURN to save your work ("w" is for "write"). Similarly, to quit vi use the command: :q RETURN The shell can execute commands; can have parameters and display messages just as if they were type from the keyboard. In addition, files may use variables, like programming languages. The files which are executed by the shell are called shell programs or shell scripts. To create a shell script, enter the command exactly as you would from the $ prompt into a file through an editor.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 58 of 69

IT_140702 Operating systems

A shell script is simply a file containing shell commands that you can execute like this: sh filename [arg1 arg2 ... argn] A shell script may also be executed by name if the file containing the shell commands has read and execute permission. If file ``do_it'' contains the shell commands and has such permissions, then the previous example is equivalent to: do_it [arg1 arg2 ... argn] In this case, executing a shell script works the same as executing a program. Remember that its first line should be #!/bin/sh to be sure the Bourne shell is the command interpreter that reads the script. Simple Shell Scripts The simplest shell script contains one or more complete commands. For example, if you wanted to know the number of files in your current directory, you could use ls -l | wc -l If you were to create a file called ``countf'' that contained this line (and with the correct read and execute permissions), you could then count the number of files simply by typing:
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 59 of 69

IT_140702 Operating systems

countf Any number of commands can be included in a file to create shell scripts of any complexity. For more than simple scripts, though, it is usually necessary to use shell variables and to make use of special shell programming commands. Shell Variables Shell variables are used for storing and manipulating strings of characters. A shell variable name begins with a letter and can contain letters, digits, and underscores, such as x x1 abc_xyz Shell variables can be assigned values like this: x=file1 x1=/usr/man/man1/sh.1 abc_xyz=4759300 Notice that there are no spaces before or after the equals-sign. The value will be substituted for the shell variable name if the name is preceded by a $. For example, echo $x1 would echo /usr/man/man1/sh.1
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 60 of 69

IT_140702 Operating systems

Several special shell variables are predefined. Some useful ones are $#, $*, $?, and $$. Arguments can be passed to a shell script. These arguments can be accessed inside the script by using the shell variables $1, $2,...,$n for positional parameter 1,2,...,n. The filename of the shell script itself is $0. The number of such arguments is $#. For example, if file do_it is a shell script and it is called by giving the command do_it xyz then $0 has the value do_it, $1 has the value xyz, and $# has the value 1. $* is a variable containing all the arguments (except for $0) and is often used for passing all the arguments to another program or script. $? is the exit status of the program most recently executed in the shell script. Its value is 0 for successful completion. This variable is useful for error handling (see section 10.6). $$ is the process id of the executing shell and is useful for creating unique filenames. For example, cat $1 $2 >> tempfile.$$
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 61 of 69

IT_140702 Operating systems

concatenates the files passed as parameters 1 and 2, appending them to a file called tempfile.31264 (assuming the process id is 31264). Flow Control Most programming languages provide constructs for looping and for testing conditions to know when to stop looping. The shell provides several such flow control constructs, including if, for, and while. if The if command performs a conditional branch. It takes the form if command-list1 then command-list2 else command-list3 fi A command-list is one or more commands. You can put more than one command on a line, but if you do so, separate them by semicolons. If the last command of command-list1 has exit status 0, then command-list2 is executed. But if the
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 62 of 69

IT_140702 Operating systems

exit status is nonzero, then command-list3 is executed. for The for command provides a looping construct of the form for shell-variable in word-list do command-list done The shell variable is set to the first word in word-list and then command-list is executed. The shell variable is then set to the next word in word-list and the process continues until wordlist is exhausted. A common use of for-loops is to perform several commands on all (or a subset) of the files in your directory. For example, to print all the files in your directory, you could use for i in * do echo printing file $i lpr $i done In this case, * would expand to a list of all filenames in your directory, i would be set to each filename in turn, and $i would then substitute the filename for i (in the echo and lpr commands).
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 63 of 69

IT_140702 Operating systems

while The while command provides a slightly different looping construct: while command-list1 do command-list2 done While the exit status of the last command in command-list1 is 0, command-list2 is executed. Test The test command can be used to compare two integers, to test if a file exists or is readable, to determine if two strings are equal, or to test several other conditions. For example, to test whether the value of shell variable x is equal to 5, use test $x -eq 5 If $x is equal to 5, test returns true. Other useful tests include test -s file (true if file exists and has a size larger than 0) test -w file (true if file exists and is writable) test -z string (true if the length of string is 0) test string1 != string2
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 64 of 69

IT_140702 Operating systems

(true if string1 and string2 are not identical) The test command is often used with the flowcontrol constructs described above. Here is an example of test used with the if command: if test "$1" = "" (or if ["$1" = ""] ) then echo usage: myname xxxx exit 1 fi This tests to see if the command line contains an argument ($1). If it does not ($1 is null), then echo prints a message. A complete list of test operators can be found in the man page for test. Shell Scripts: 1) To copy source file to destination file using CP Command 2) To copy source file to destination file without using CP Command 3) To append file f1 at the end of file f2. 4) ) To concate content of two files.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 65 of 69

IT_140702 Operating systems

5) Write a shell script to perform real number of calculation and store result to third variable. 6) Write a shell script that works as a calculator. 7) Checks it is a prime or not. 8) Finds factorial of the given number. 9) ) Finds reverse of a given number. 10) ) Finds out square root of a given number 11) Finds the given No is Armstrong or Not. 12) Fibonacci series of a given number 1 2 3 n 13) 1 + 2 + 3 + n 14) Write a shell script to compare two numbers and two strings. 15) Write a shell script that performs following options: Copy, Rename, Link, Remove, Exit 16) Checks whether entered string is a file or a directory. 17) Checks whether that file has write permission or not, if it has, then append data at the end of the file. 18) Finds no. of occurrence of a given word in file.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 66 of 69

IT_140702 Operating systems

19) Write a shell script to find out no. of vowels form the file. 20) Write a shell script to check validity of the user 21) Write a shell script which displays appropriate message according to login time. 22) Write a shell script to find out the biggest number from the given three numbers. Numbers are supplied as command line argument. Print error message if sufficient arguments are not supplied. 23) Write a shell script which will receive any number of filenames as arguments. The shell script should check whether every argument supplied is a file or a directory. If it is a directory lines present in it should be displayed. 24) Write a program to implement fork () system call. 25) Write a program to calculate standard deviation using thread.

Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

Page 67 of 69

IT_140702 Operating systems

Questions: 1) What is difference between commands and shell scripts? 2) State true or false: a. Using a for loop we can calculate factorial value of a number. b.A null variable A can be created using . c. Shell Meta character $# represents number of arguments supplied to the shell script. d.By using statement read v1 v2 v3 we can supply only one value. e. Shell variables are case sensitive. 3) How shell act by executing a shell script? 4) Is there any restriction on the length of a shell variable? 5) Can we redirect the echo stmts output to the file? Justify.
Prepared By: Neha Shah (I.T) Chandni Shah (I.T) Page 68 of 69

IT_140702 Operating systems

6) Which type of shell variable is declared for a=1235? 7) Which programs are compatible among of born shell and c shell? 8) What is the use of set command? 9) Point out errors: a. t = 562 b.cmd = 34 c. k=`expr a*b` d.3.14*$r*$r = area e. a=10 if [$a -gt 5] then echo a is large

Prepared By: Neha Shah (I.T) Chandni Shah (I.T)

Page 69 of 69

You might also like