Notes of Unix
Notes of Unix
Who developed the Linux? In 1991, Linus Torvalds studding Unix at the University, where he used special educational experimental purpose operating system called Minix (small version of Unix and used in Academic environment). But Minix had it's own limitations. Linus felt he could do better than the Minix. So he developed his own version of Minix, which is now know as Linux. Linux is Open Source From the start of the day. For more information on Linus Torvalds, please visit his home page. How to get Linux? Linux available for download over the net, this is useful if your internet connection is fast. Another way is order the CD-ROMs to save time, and the installation from CD-ROM is fast/automatic. Various Linux distributions are available. Following are important Linux distributions. Linux distributions. Website/Logo
Note: If you are in India then you can get Linux Distribution from the Leading Computer magazine such as PC Quest (Even PCQuest has got its own Linux flavor) or if you are in Pune, India please visit the our sponsor web site to obtained the Red Hat Linux or any other official Linux distribution. Note that you can also obtain your Linux distribution with Linux books which you purchase from Local book store.
2) pwd
Print name of current/working directory. Synopsis:pwd [options] Description:NOTE:- Your shell may have its own version of pwd which will supercede the version described here. Print the full filename of the current working directory.
3) w
It shows who is logged on. Synopsis:w [option] Description:-b, --boot -d, --dead -H, --heading -q, --count -r, --runlevel -u, --users :- Time of last system boot. :- Print dead processes. :- Print line of column headings. :- All login names and number of users logged on. :- Print current runlevel. :- Lists users logged in.
4) whoami
Print effective user id. Synopsis:Whoami Description:Print the user name associated with the current effective user id.
5) mkdir
Make directories. Synopsis:mkdir [option] filename Description:Create the Directory(ies) if they do not already exist. Option:-v, --verbose :-Print a message for each created directory.
6) date
Print or set the system date and time. Synopsis:date [option] Option:-u :- Print or set coordinated universal Time.
7) cal, ncal
Displays a calendar and the date of Easter. Synopsis:cal [-jy] [[month] year] ncal [-jJpwy] [-s country_code] [[month] year] ncal [-Jeo] [year] Description:cal displays a simple calendar in traditional format and ncal offers an alternative layout, more options and the date of Easter. The new format is a little cramped but it m makes a year fit on a 25*80 terminal. If arguments are not specified, the current month is displayed.
4 Options:-J -e -j -p -w :- Displays Julian calendar, if combined with the e option, display date of Easter according to the Julian calendar. :- Displays date of Easter. :- Displays Julian days. :- Print the country codes. :- Print the number of the week below each column.
8) banner
Prints large banner on printer. Synopsis:banner [-d] [-t] [-w width] message Description:banner Prints a large, high quality banner on the standard output. If the message is omitted, it prompts for and reads of its standard input. Options:-d :- Enable debug. -t :- Enable trace. -w width :- Change the output from a width of 132 to width, suitable for a narrow terminal. NOTE:- The w option is implemented by skipping some rows and columns. The smaller it gets, the grainier the output. Sometimes it runs letters together.
9) cp
Copy files and directories. Synopsis:cp [option] SOURCE DESTINATION Description:Copy source to Destination, or multiple Source(s) to Directory. Options:--backup[=control] :- Make a backup of each existing destination file. -b :- Like --backup but does not accept an argument. -f, --force :- If an existing destination file cant be opened, remove it and try again. -i, --interactive :- Prompts message before overwrite. -l, --link :- Link files instead of copying. -R, --recursive :- Copy directories recursively.
10) mv
Move(rename) files Synopsis:mv [option] SOURCE DESTINATION Options:-f :- Never prompts before overwriting. -i :- Prompts message before overwrite. -u, --update :- Move only older or brand new non-directories.
11) rmdir
Remove empty directories. Synopsis:rmdir [option] DIRECTORY Description:Remove the Directory(ies), if they are empty. Options:-p, --parents :- Remove Directory, then try to remove each Directory component of that path name. -v :- Output a diagnostic for every directory processed.
12) cat
Displaying and creating files. Synopsis:cat file_name Description:It is used to display the contents of a small file on the terminal. Option:-v Using cat to create a file:cat is also useful for creating a file. Enter the command cat, followed by > (the right chevron) character and the filename. Syntex:- $ cat > abc [message] <ctrl-d> // To terminate the file. NOTE:- The <ctrl-d> character is used to terminate input not only with cat, but with all commands that accept input from the keyboard. :- To display non-printing characters in our input.
13) rm
Deleting files. Synopsis:rm [option] file_name Description:Files can be deleted with rm(remove). Unlike its Dos counter part DEL, it can delete more than one file with a single instruction. Options:-i -r -f :- Prompts message before deleting. :- Delete current directories as well as files and sub-directories within the directory. :- Delete files and sub-directories forcedly if the files are write -protected.
14) wc
Counts lines, word and character in a file. Synopsis:wc [options] file_name Description:It counts lines, words and characters depending on the options used. It takes one or more filenames as its arguments and displays a four-columnar output. Options:-l -w -c :- Count no. of lines. :- Counts no. of words. :- Counts no. of characters.
15) cmp
Comparing two files. Synopsis:cmp filename1 filename2 Description:We may often require to know whether two files are identical so that one of them can be deleted. The two files are compared byte by byte, and the location of the mismatch is echoed to the screen. Option:-l :- Gives a detailed list of the byte number and the differing bytes in octal for each character that differs in both files.
16) chmod
Changing file permissions. Descriptions:The chmod command is used to set the three permissions for all three categories of Users(owner, group and others) of a file. It can be used only by the owner of the file, and Uses the following syntax:chmod category operation permission file_name chmod takes as its arguments an expression, which combines the category, the operation and the type of permission by making use of suitable abbreviations. The abbreviations used for the three elements expressions of the expression are shown below:
Abbreviations Used by chmod:Category u User g Group o Others a - all Operation + - Assign permission - - Remove permission = - Assign absolute permission Permission r Read permission w Write permission x Execute permission
Now, lets take an example, to assign executable permission to the owner of the file xstart, other permissions remaining the same. $ chmod u+x xstart $ ls l xstart -rwxr--r-- 1 kumar metal 1906 May 10 20:30 Let us see these command to the file xstart:1) $ chmod ugo+x xstart $ ls l xstart -rwxr-xr-x 1 kumar 1906 May 10 20:30 xstart $ chmod go-r xstart $ ls l xstart -rw-r--r-- 1 kumar 1906 May 10 $ chmod u-x,go+r xstart $ ls l xstart -rw-r--r-- 1 kumar 1906 May 10 $ chmod o+wx xstart $ ls l xstart -rw-r--rwx 1 kumar 1906 May 10 $ chmod ugo=r xstart
xstart
2)
20:30 xstart
3)
20:30 xstart
4)
20:30 xstart
5)
8 $ ls l xstart -r--r--r-- 1 kumar 1906 May 10 The Octal Notation:There is a short-hand notation available for changing the permissions. Chmod also takes a numeric argument that describes both the category, as well as Permissions. Read permission 4 Write permission 2 Execute permission 1 When theres more than one permission associated with a particular category, the respective numbers are added. For example:$ chmod 666 xstart $ ls l xstart -rw-rw-rw- 1 kumar 1906 May 10 20:30 xstart The 6 indicates read and write permissions(4+2). To restore the original permissions to the file, we need to remove the write permissions(2) from group and others: a) $ chmod 644 xstart $ ls l xstart -rw--r--r-- 1 kumar 1906 May 10 20:30 xstart b) $ chmod 761 xstart $ ls l xstart -rwxrw-r 1 kumar 1906 May 10 20:30 xstart
20:30 xstart
9 Common vi editor command list For this Purpose To insert new text To save file To save file with file name (save as) To quit the vi editor To quit without saving To save and quit vi editor To search for specified word in forward direction To continue with search To search for specified word in backward direction To copy the line where cursor is located To paste the text just deleted or copied at the cursor To delete entire line where cursor is located To delete word from cursor position Use this vi Command Syntax esc + i ( You have to press 'escape' key then 'i') esc + : + w (Press 'escape' key then 'colon' and finally 'w') esc + : + w "filename" esc + : + q esc + : + q! esc + : + wq esc + /word (Press 'escape' key, type /word-to-find, for e.g. to find word 'shri', type as /shri) N esc + ?word (Press 'escape' key, type word-to-find) esc + yy esc + p esc + dd esc + dw esc + :$s/word-to-find/word-to-replace/g For. e.g. :$s/mumbai/pune/g Here word "mumbai" is replace with "pune"
To Find all occurrence of given word and Replace then globally without confirmation
To Find all occurrence of given word and Replace then globally with confirmation To run shell command like ls, cp or date etc within vi
Answer to Variable sections exercise Q.1.How to Define variable x with value 10 and print it on screen. $ x=10 $ echo $x Q.2.How to Define variable xn with value Rani and print it on screen For Ans. Click here $ xn=Rani $ echo $xn
10 Q.3.How to print sum of two numbers, let's say 6 and 3 $ echo 6 + 3 This will print 6 + 3, not the sum 9, To do sum or math operations in shell use expr, syntax is as follows Syntax: expr op1 operator op2 Where, op1 and op2 are any Integer Number (Number without decimal point) and operator can be + Addition - Subtraction / Division % Modular, to find remainder For e.g. 20 / 3 = 6 , to find remainder 20 % 3 = 2, (Remember its integer calculation) \* Multiplication $ expr 6 + 3 Now It will print sum as 9 , But $ expr 6+3 will not work because space is required between number and operator (See Shell Arithmetic) Q.4.How to define two variable x=20, y=5 and then to print division of x and y (i.e. x/y) For Ans. Click here $x=20 $ y=5 $ expr x / y Q.5.Modify above and store division of x and y to variable called z For Ans. Click here $ x=20 $ y=5 $ z=`expr x / y` $ echo $z
11
Shell Programming :1) Wap in shell programming to check whether a no. is prime no. or not. echo Enter no. read a c=2 b=`expr $a - 1` while [ $c -le $b ] do a1=`expr $a % $c` if [ $a1 -eq 0 ] then echo Not prime no. fi c=`expr $c + 1` done if [ $c -eq $a ] then echo Prime no. fi
2) Wap to check whether a no. is Armstrong no. or not. echo Enter no. read a s=0 a1=`expr $a + 0` while [ $a -ne 0 ] do x=`expr $a % 10` s=`expr $s + $x \* $x \* $x` a=`expr $a / 10` done if [ $a1 -eq $s ] then echo Armstrong no. else echo Not Armstrong no. fi 3) Wap to generate a series of Armstrong no. between 1 to 1000. a=1 while [ $a -ne 1000 ] do s=0
12 a1=`expr $a + 0` while [ $a1 -ne 0] do x=`expr $a1 % 10` s=`expr $s + $x \* $x \* $x` a1=`expr $a1 / 10` done if [ $a -eq $s ] then echo $a fi a=`expr $a + 1` done
13 4) Wap to create a menu driven:1) Date. 2) List of directory. 3) To print Your name. 4) Exit. ax=1 clear while [ $ax -ne 0 ] do echo 1. Date echo 2. List of directory. echo 3. To print Your name. echo 4. Exit. echo -n Enter Your Choice:read a case $a in 1) date ;; 2) ls l ;; 3) echo Enter your name read x banner -w 35 $x ;; 4) exit 0 esac done