Lab Manual 2
User Manual: Pdf
Open the PDF directly: View PDF .
Page Count: 16
Download | |
Open PDF In Browser | View PDF |
Operating Systems Lab Manual 02 National University of Computer and Emerging Sciences Operating System Lab - 02 Lab Manual Objective This lab is all about running commands in Ubuntu Terminal and compiling C program in Ubuntu Table of Content Objective ...........................................................................................................................................1 Table of Content...............................................................................................................................1 Shell ...................................................................................................................................................2 Commands in Linux .........................................................................................................................2 Patterns and Wildcards....................................................................................................................9 Pipe in Linux..................................................................................................................................... 10 Compile C program in Linux .......................................................................................................... 10 Introduction to Shell Scripting ........................................................................................................ 11 Lab Activity ....................................................................................................................................... 12 1|Page Operating Systems Lab Manual 02 Shell Fortunately, or unfortunately, a computer can only understand binary language and humans can easily understand English language or equivalent high level language and therefore it is difficult to interpret and understand with the computer system. In order to ward off this difficulty every operating system has got an inbuilt interpreter(Shell). A shell accepts instructions or commands fed by user in user understandable language and translate it to binary language which a computer can easily understand. So in short a Shell is a language translator and in this lab is all about introducing Shell of the Linux and the commands that are most commonly used. Figure below will make the above paragraph more meaningful and reader can understand it better. User Input (Commands) Shell English to Binary Conversation Yes, Boss I understand you request and will execute it. Figure 1 Shell - A diagrammatic representation Commands in Linux From here the reader is exposed to the basic Linux commands. All the commands have to be tried in the terminal. Throughout the lab manuals Ubuntu will be used for explaining the concepts. To know how to start a Terminal please see Lab Manual 01 – Introduction to Terminal. The commands with their usage and example is given in the table below: NOTE: All Linux commands are case sensitive i.e. ‘cp’ is not equivalent to ‘CP’. Also, all the files and directories in linux are case sensitive so for example ‘/etc/hosts’ is not equivalent to ‘/etc/Hosts’ and so hosts and Hosts are two different files in the same directory. Command Switch Description Example Output BASIC COMMANDS Manual/Help for any command man 2|Page None Gives manual for the specified command man mkdir Opens manual in terminal, press ‘h’ for help or ‘q’ to quit and get back to terminal Operating Systems Lab Manual 02 Command Switch Description Date and Time Commands Example None Displays the system date -u, –utc, --universal Displays the universal date -u coordinated time Displays the date date –d specified by string “12/02/2014” date –s “20 JAN Sets the date specified by 2014 the string 18:00:00” Displays the day date +%d Displays the month date +%m Displays the year date +%y Displays the date in date +%D mm/dd/yyyy format Displays the hour in 24 date +%H hour format Displays the minute date +%M Displays the second date +%S Displays the time in HH:MM:SS in 24 hour date +%T format Displays the abbreviated date +%a weekday Displays the full weekday date +%A name Displays the abbreviated date +%b month Displays the full month date +%B name Displays the local system date +%c date and time Displays century date +%C Displays the time in HH:MM:SS in 12 hour format followed by AM or date +%r PM -d, --date -s +%d +%m +%y +%D +%H +%M +%S date +%T +%a +%A +%b +%B +%c +%C +%r date Managing Users and Groups in Linux (root user only) None Creates a new user useradd abc useradd profile or update 3|Page Output Sun Jan 19 22:11:00 MST 2014 Mon Jan 20 16:09:20 UTC 2014 Wed Dec 3 00:00:00 GMT 2014 Mon Jan 20 18:00:00 GMT 2014 20 01 2014 20/01/2014 18 36 40 15:20:20 Mon Monday Jan January Mon 20 Jan 2014 06:05:06 PM GMT 20 06:05:49 PM User Created Operating Systems Lab Manual 02 existing information user None Add a group to the system addgroup example None Creates a user account adduser that can be used for login username --ingroup Creates user account and adduser -add that user in a group ingroup sudo specified abc usermod -a -G Modify an existing user Usermod –a –G sudo abc Command Switch None Description Deletes the user from the system Deletes the group from the system Example deluser abc addgroup adduser --group deluser -Removes the user along deluser removehome with its home folder removehome directory abc --removeall-files Removes all the files and directories belong to the specified user None Change password of the passwd current logged in user or user specified Shutdown or Restart System deluser removeallfiles abc - passwd OR passwd abc passwd: password updated successfully The system will shutdown now for maintenance None Power off the computer shutdown now -f Restart system quickly shutdown -f now None shutdown 4|Page deluser --group example Adding group `example' (GID 1003) ... Done. Ask for password and some data along with confirmation and creates the account Same as adduserand also it adds the user to the group Add already existing user to already existing group Output Removing user `abc' ... Done. Removing group `example' ... Done. Removing files ... Removing user `abc' ... Done. Removing files Removing user ‘abc’ Done. Operating Systems Lab Manual 02 -k Sends warning message to user but does not shut down system -r Reboots after shutdown poweroff none Shutdowns computer shutdown –r now sudo poweroff reboot none Restarts computer sudo reboot shutdown -k now None None None None Files and Directories in Linux Recall that in Lab Manual 01, in generalized Linux file system. The basic unit is a file. It contains data about the file, essential metadata and non-essential metadata and some information. In Linux everything is a file. A directory is a special kind of the file. Even terminal window /dev/pts/4 or hard disk /dev/sdb is represented somewhere in the system as a file. Relative and Absolute Paths In Linux file system, when you type a path starting with a slash (/), then the root of the file tree is assumed. If you don't start your path with a slash, then the current directory is the assumed starting point. The screenshot below first shows the current directory /home/sumaiyah. From within this directory, you have to type cd /home instead of cd home to go to the /home directory. 5|Page Operating Systems Lab Manual 02 MANAGING FILES AND DIRECTORIES IN LINUX File Basics touch None Creates a file -t Creates a file with given timestamp touch file1 touch –t 130207111630 BigBattle file HelloWorld.c file None Determines file type file /dev/sda None Creates link of the file ln file1 link1 ln Creates shortcut link of the ln -s file1 slink1 -s file or directory ln -s dir1 dirslink1 Displaying Contents of a File Displays contents of file in cat none cat file1 the terminal Displays first 10 lines in none head file1 terminal head Displays first specified -[number] head -20 file1 number of lines in terminal Displays last 10 lines in none tail file1 terminal tail Displays last specified -[number] tail -17 file1 number of lines in terminal Copy, Move, Rename or Remove Files or Directory None Copies a file cp fileA fileB cp -r Copies a directory cp -r dir1 dir2 -i Copies files but prevents cp -I a.c b.c overwrites -p mv rm none none -r -rf 6|Page Preserve permissions and timestamps Moves/renames files and directories Removes a file Removes a directory For Removal, removes non-empty directories File Created File created HelloWorld.c: C source, ASCII text /dev/sda: block special (8/0) None None None None None None cp -P file* cp None mv fileA ~/fileB Mv dirA dirB rm file1 rm -r dir1 None None None None rm -rf dir1 None Operating Systems Lab Manual 02 Directory Basics pwd None None mkdir -p -v None -l ls -a -h 7|Page Determines the current Pwd path Creates a directory mkdir dir1 in current or sudo mkdir specified directory /home/dir1 mkdir Creates directory or p directories in tree dir1/subdir/subsubdir hierarchy manner Prints info about the mkdir dir1 directory being created Displays the content of current directory or specified directory ls ls /etc Displays the content in ls -l long format and with detail Displays the content along with hidden content ls -a of current or specified directory Displays the content in ls -h human readable form /home/alishah/Desktop None None None mkdir: created directory 'dir1' Operating Systems Lab Manual 02 -R Displays the content in recursive order (it list file and directories along with files and subdirectories of subdirectories and so on) ls -R File/Directory Permissions and Ownerships Every file created in file system has an owner and permissions associated with it. There are basically three kinds of user available in Linux 1. Owner (User who created the file/directory) 2. Group 3. Other Users/Groups Each of the above-mentioned user will have access permissions. Following are the three permissions associated with all the files. 1. Read (Denoted by r) 2. Write (Denoted by w) 3. Execute (Denoted by x) These permissions can be visualized by ‘ls -l ’ Let us examine ‘-rwxr-x---‘ the first ‘-‘ represent that it’s a file ‘d’ would represent that it’s a directory, the next 3 characters ‘rwx‘ are the rights for the owner, next three are the permissions of the group and last three characters are the permissions for the other users/group. The third column represents states the user who is the owner of the file. Now the question is: can I change the permission or ownership of a file or directory. The answer is ‘yes!’ 8|Page Operating Systems Lab Manual 02 Chmod can be issued in two different ways, First method is 4 2 1 code in digital electronics 4 2 1 r w X 1 or 0 1 or 0 1 or 0 This is really simple, if a user has to be assign with all permission (Read, Write and Execute), 1 has to be applied in all the permissions that are required: 1(r) + 1(w) + 1(x) = 1(4) + 1(2) + 1(1) = 7 so 7 is the number that will fetch all the permissions for that file or folder. 4 R Owner 2 1 w x 4 r Group 2 1 w x 4 R Other 2 1 w X Assuming that all the users get rwx permission so 4+2+1 = 7 will get mathematically 777. Below table shows the syntax and example of using chmod command and also how to change the owner of the file i.e. chown command. Command Switch Description None Changes permissions of a file chmod -v -R None chown -R 9|Page Output a diagnostic for every file processed Example chmod 700 file1 chmod -v 650 file1 Changes permissions files and chmod -R 760 directories recursively dir1 chown username Change the ownership of a file filename Change the ownership files and chown -R user directories recursively dir1 Output None mode of 'file1' changed from 0700 (rwx------) to 0650 (rw-r-x---) None None None Operating Systems Lab Manual 02 Other Useful Commands in Linux Command Switch Description Search pattern in a given None file Search given pattern in a -i file ignoring case Search given pattern in -r all the files in a directory recursively Search words only not -w grep strings Show match count for -c pattern Show line number for the -n matching pattern in file Prints match inverse, i.e. prints all those lines -v which do not contain the pattern. cal None whatis None whereis None 10 | P a g e Example grep ‘hellow’ file1.txt Grep -i ‘hEllow’ file1.txt grep -r ‘helllow’ dir1 Output grep –w hello cricket.txt grep -c hello cricket.txt grep -n hello /home/cricket.txt grep -v hello /home/cricket.txt Get the calender of the Cal current or specified month and year (only cal 9 2020 month will not do) cal 2020 Gives a brief description whatis ls of command Gives the path of the whereis ls Command ls (1) - list directory contents ls: /bin/ls /usr/share/man/man1/ls.1.gz Operating Systems Lab Manual 02 ifconfig -a finger None ps None -A kill None alias None unalias None df None du None mount None sudo None -i su 11 | P a g e None To know the status and configurations of ifconfig -a network interfaces To know about user finger username account in Linux Users Show snapshot of Ps running processes Show all the processes ps -A Kills the process with kill 1434 specified process id Renames a command alias l=’ls -al’ Undo renaming a unalias l command Shows detail of disk usage. df works by examining a directory Df entry, which generally are updated only when a file is closed. Estimates file space usage. Output the summary of disk usages Du of every file hierarchically i.e. recursively It is use to mound a file mount /dev/sda5 system that do not mound or itself mount /dev/usb Runs the command as sudo cp root/super ~/Desktop/file user/administrator /usr Login as root user sudo -i Change username or su username become a super user
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.5 Linearized : No Page Count : 16 Language : en-US Tagged PDF : Yes Author : k122211 Creator : Microsoft® Word 2016 Create Date : 2018:01:29 07:44:00+05:00 Modify Date : 2018:01:29 07:44:00+05:00 Producer : Microsoft® Word 2016EXIF Metadata provided by EXIF.tools