Вы находитесь на странице: 1из 14

cd - change directory external command : tee (Saves one copy in a file and writes the other to standard i/p.

$who | tee user.txt ( displays the o/p of command who and also saves the o/p to the file user.txt) CREATING FILES command : $touch filename [enter] creates a file. Size of the file will be zero bytes running a cat command accessing the file making changes in a file modifying it command : $touch filename changes modification time and access time to current time command : $touch -a filename changes access time to current time Command : ls l (time displayed is last modification time) ls lu (time displayed is last access time) command : $ touch m filename (changes last modification time to current time) command : $touch a 0425120596 filename (Changes access time to a particular time instead of current time) 04/25/12/05/96 month/day/hour/minutes/year touch is used for creating empty files. command: $touch filename1 filename2 filename3 filename4 [enter] simple way of creating n number of empty files. Cat - concatenate Command: $cat > filename [enter] Cursor will go to next line. Without entering into files we can store text into the file. Type whatever you want. After end of typing press ctrl+d indicates EOF or end of file character. To view the typed content type Command : $cat filename [enter] Displays the content of file. Note : second time when cat > filename command is used it replaces previous data with new data. To retain previous data and to add new data to the file i.e appending the new data to existing data then type Command : $cat >> filename >> - appends new data to existing data without replacing it. CONCATENATING THE CONTENTS OF TWO FILES AND STORING IN THE THIRD FILE : Command : $cat filename1 filename2 > filename3 Creates filename3 and adds contents of filename1 and filename2 one after the other. But this replaces the contents of filename3 if it contains any. To retain previous contents of filename3 while adding contents of filename1 and filename2 use the following command Command : $cat filename1 filename2 >> filename3 >> - append output redirection operation Indulging in file play :

Copy : Command : $cp filename.a filename.b Contents of filename.a will be copied into filename.b. if filename.b does not exist it will be created. If filename.b exists and contains data cp command will overwrite the existing data. Command : $cp filename.a filename.b dirname filename.a, filename.b .. all files will be copied to directory dirname. Dirname must exist. Copying file from one location(dir) to another location(dir) from remote location Command : $cp /root/upendra/paddu/filename /root/tmp/filename.a Here, the file filename is copied from dir paddu to dir tmp and the name has changed to filename.a Filenames : examples ram, laxman, filename.1, sample.23, paddu.dd etc. the dot in filename is treated as another character. Absolute pathname : Eg : /root/upendra/paddu/filename, /root/tmp/filename.a The absolute pathname starts at root directory / Relative pathname : refers to the pathname starting from the directory in which you are in. pathname doesnt start with /. Remove command : removes the given file or files supplied to it. Works differently for different option or switches. Command : $rm filename (remove the file filename, this command doesnt work for directories) $rm i filename ( -i is a switch, removes file interactively i.e you are asked for confirmation before deleting the file) $rm r dir1 (recursively removes all contents of directory dir1 and also dir1 itself) $rm f filename, $rm f dir1 (removes files and directories forcibly, irrespective of whether you have write permission to them or not. f is applicable to files of which you are the owner. You cant forcibly delete somebody elses files using the f option of rm. If f is not provided we should first add write permission to the file using chmod and then delete it using rm) $rm rf dir1 (recursively and forceibly deletes all contents of the directory dir1) Renaming a file and directory : Command : $mv filename newname ( filename will be replaced with newname) Command : $mv olddir newdir ( olddir will be replaced with newdir) Moving a file : Command : $mv file1 file2 newdir (file1 and file2 are moved to new location newdir and are no longer present at their original locations) Note: diff b/w moving and copying : while moving the source file is erased from its original location and copies at the specified location. While copying the source file is retained in its orginal location and a copy is created in destined location. Listing files and directories : Command : $ls ( displays filenames and directories in alphabetical order)

Hidding files : $cat > .cricket {body of file -------------------------------------------------} Ctrl+d $ls (does not display the file .cricket) .cricket is there in the directory but it is treated as hidden file. Any file that begins with a . is treated as a hidden file. To view hidden files: Command : $ls a Note: . stands for current directory .. stands for parent of current directory These two entries automatically get created in the directory whenever the directory is created. Command : $ls r lists all files in the present directory including files present in any sub-directories. $ls -s - lists the files along with their sizes(in blocks not bytes) $ls i lists files along with their inode numbers. lc and lf are look-alikes of ls. lc displays files in columnar fashion. lf puts a * after all executable files and a / after all sub-directories present in current directory. Metacharacters : are characters that shell never takes at face value. They are representative of one or a group of characters. Symbol ? siginifies any single character. Command : $ls ?gain (displays all files ending with ain. ? can be any letter) o/p : gain pain main etc command : $ls p* (* indicates presence or absence of any number of characters, lists all files whose names begin with p) o/p pakde pommies command : $ls /dir/*x (lists all directories ending with x) command : $ls [aeiou]* - lists all files and directories that start with vowels a,e,i,o,u. the remaining letters can be anything. Command : $ls [!aeiou]* lists all files and directories that start with characters other than vowels. ! symbol complements the condition. Command : $ls [a-m][c-z][4-9]?? [ ] is always substituted by a single character.

Lists all 5 character filenames in the current directory whose first character is in the range a-m, second character is in the range c-z, third character is in the range 4-9 and fourth fifth are any valid characters. Command : $ls l (long listing) Total 22 File no of links ownername permissions -rwxr-x--x drwxr-xr-x 1 2 username username

groupname

size of file in bytes 24 24

group group

date,time File or when last dir name modified Sep 12 10:15 Myfile Sep 12 10:15 Mydir

Total 22 indicates no of disk blocks that the files in the current directory have occupied. Note: unix treats all entities files,directories and devices as files. File permission : signifies who all can access the file and for what purpose. If first character is - (its a file) If first character is d means its a directory Next three characters are file permissions for user or owner Next three characters file permissions for group Last three characters file permissions for others Owner the person who creates the file Group formed of a given number of users who may be working on the same data Others those outside the group Other possible file permissions : - Ordinary file d directory file c character special file b block special file l symbolic file s semaphore p named m shared memory file Three permissions: encoded numerically Permission r read w write x - execute weight 4 2 1

pipe

total weightage when all three permissions are available 4+2+1 =7 when everybody has all permissions weightage is 777 existing file permission can be changed by the user or owner of the file or by the superuser. Changing file permissions : two methods of mode changing Absolute mode: Command : $chmod 700 filename or dirname Briefing : owner 7 (r+w+x)

Group 0 (- - - ) Other 0 (- - - ) Symbolic mode: Command: $chmod [who] [+/-/=] [permission] filename Who can be user or owner (u)/group(g)/others(o) [if none is specified, all are assumed] + refers add permission, - refers remove permission and = instructs to add specified permission and take away all other permissions if present. Ex : $chmod +w filename or dirname (gives write permission to all) $chmod go-x filename or dirname (takes away execute permission from group and others) $chmod go+r,go-w file or dir (gives read permission to group and others and takes away their write permission ) $chmod go=r,u=rw file or dir (removes all existing permissions and replaces them with read permission for group and others and read and write permissions for owner of file or dir. Masking file permissions : umask stands for user file creation mask. Umask tells unix which of the three permissions are to be denied rather than granted. Determining current value of mask : Command : $umask o/p : 0022 0 indicates that what follows in octal number second zero permissions to be denied to owner third digit - permissions to be denied to group fourth digit - permissions to be denied to others note: by default permissions assumed by unix for a new file created are 666 since mask is 022 these values are subtracted and resulting value is 644 (rw-r--r--) permissions assumed for newly created dir are 777 since mask is 022 new values are 755 (rwxr-xr-x) execute permission for a dir is must to enter into it. Changing current value of umask : Command : $umask 242 Thereafter new files will have permission 666-242 i.e 424 and directories 777-242 i.e 535. In unix, directory is treated as a file contains names of the files present in the directory. For a directory Read permission allows listing of directory contents. No cd or removing or modifying files. Write permission this permission along is of no use without read and execute permission. Since no listing of contents is allowed and no entry into files is allowed. Execute permission cd is possible. Neither read nor modify or create files in the directory is allowed. Best combination of permissions to be granted to a directory 754(rwxr-xr--) Directory cd ls cp into pemissions for groups and others r-Error Works error -wError Error Error --x Works Error Error

cp from

Error Error Error

rwr-x -wx Rwx

Error Works Works Works

works works error works

Error Error Works Works

Error Works Error Works

Sticky bit : adding sticky bit to a directory : Adding sticky bit onto a directory no matter what a hacker does he can never delete any files in this directory. Only owner or superuser can delete files from this directory. Command : $chmod u+t dirname Check dir permissions. $ls l dirname o/p drwxrwxrwxt t at the end of permissions means sticky bit is set. (hereon all files within the directory will be guarded against an intruder trying to modify or remove them) Note : liberal directory permissions(777) command : $ls ld dirname, -l is for long listing and d is listing of a directory. Note : assign permission 754 to the directory and then add sticky bit.

Significance of sticky bit w.r.t file : Only super users can set sticky bit for a file. Moreover sticky bit can be set only for a binary executable file. When sticky bit is applied for a file, the file sticks around in memory even when its execution is over. This ensures that next time we have to execute it, it is not necessary to read it from disk, thereby saving read time. One good example of sticky bit: $ls l /usr/bin/vi o/p : -rwxxt (superuser sets a sticky bit for this file) this ensures that once executed there is no need to reload from disk during subsequent executions) note : vi stands for visual editor No of links : determines the number of different names the file is accessible. If links =2 not physically present at two locations, but can be referred to by either of the names. Note: unix has no file undelete facility. Adv of concept of links : If one of the links is deleted, still the file is safe as it has another link(name) Establishing one more link to a file : Command : $ln poem mypoem (one more link to the file poem is established in the form of the name mypoem) $ln reports impdir/results (file called reports in the root directory may have a link called results in the sub-directory). Reports and results can have diff. inode numbers. Physically present in one location but can be accessed by either of its two names. Even of reports is deleted as the other link is still intact the file is also intact and will remain so until all links to it are deleted) Check by the command : $ls l both files will be listed each showing the presence of 2links. Note : by default new file created has one link new directory created has two links (1 link will be in the directory in which it is created and other link will be contained by the directory itself in the form of . )

Directory related commands : Command : $pwd present working directory / denotes root directory of unix file system. Command : $mkdir dirname creates a directory named dirname Command: $mkdir -p allows to create multiple generations of directorie at one go eg. $mkdir p upendra/ram/laxman/paddu (first dir upendra is created,inside ram is created and inside laxman and inside paddu are created) To create a directory with permissions 754 irrespective of the unmask value : Command : $mkdir m 754 newdir Removing empty directories : Command : $rmdir upendra/ram/laxman/paddu ( deletes empty dir paddu) $rmdir p upendra/ram/laxman/paddu (after removing paddu, it removes laxman if empty and so on. Process stops when rmdir bumps into a non-empty parent directory. cd : command : $cd newdir enters into newdir $cd change over to current users home directory. cd can be used in two ways $cd absolute pathname (or full path name) $cd relative pathname (the path name starting from where you are now) A bit of mathematics : Command : $bc enters into calculator mode and the $ prompt disappears. $quit ends calculator mode Refer text pg 50 54. Miscellaneous commands : Command : $logname prints login name of user Command : $id prints user id and group id numbers Command : uname prints the name of the unix system we are using. Command : uname -x prints details like release num, version num, OEM number, type of microprocessor,type of bus, no. if cpus present etc. Note: to unix all devices are nothing but files. Each terminal has diff. files present in /dev file.To find out the name of your terminal file, say Command : $tty o/p : /dev/tty1a (all your transactions with the unix file are via the file /dev/tty1a. command : $who (gives a list of all the users on the system, their terminal names(or port line by which your terminal is connected to the host m/c) and the date and time at which they logged in) command : $who am i - displays only one user yourself) eg. Aa1 tty3a jun 10 09:15 (Loginname terminalnumber or serialport date time) command : $date - displays current date and time. o/p : sat Apr 20 04:40:10 IST 1996 IST Indian standard time

Modifying o/p of date by various switches : $date +DATE:%d-%m-%y %n TIME:%H:%M:%S format should be enclosed in single quotes and should start with a + sign. DATE: 17-06-96 TIME: 10:55:25 %d day %m month %y year %H hour %M minute %S second %n what follows is displayed on a new line

Hardware requirements for unix 80 MB hard disk Atleast 4MB of RAM on a 16 bit microprocessor. (80286 or preferably 80386/80486) Out of 80MB disk space almost 40MB is eaten away by actual unix OS files another 10-20 is used as swap space. Swap space is used when unix falls short of memory.the contents of memory which are not immediately required are stored in swap space.whenever required they are read back from the swap space. Per terminal requires 0.75 to 1MB of space in the host machine. Features of unix : 1. Multiuser capability 2. Multitasking capability editing of one program and execution of some other command are possible at the same time. 3. Communication (b/w users) 4. Security (by assigning passwords and usernames to individual users, at file level read,write and execute permissions and file encryption(encoding file into an unreadable format) 5. Portability Unix system organization : Kernel at the heart of unix is kernel which interacts with the actual h/w in m/c language. Shell or command interpreter mediator which interprets commands given by user and then conveys to kernel which executes them. Functions of kernel : 1. Manages files 2. Carries out all the data transfer b/w the file system and h/w 3. Manages memory Kernel program is stored in a file called unix Shell program is stored in a file called sh There may be several shell programs running but there will be only one kernel interacting with h/w. therefore at a particular instance only one program is executed by unix, others will wait. Different users interact through diff. shells but to one kernel.

Types of shells : 1. Bourne shell - used in the book 2. C shell Adv of c shell over bourne shell 1. Allows aliasing of commands. 3. Command history feature 4. Korn shell not so widely used. SYSTEM ADMINISTRATOR controls unix installation. He creates user accounts with unix so that user can login. Creation of account involves following activities: 1. Providing login name to the user 2. Providing initial password to the user 3. Providing the user in one of the groups 4. Providing a default working shell to the user 5. Providing a default working directory to the user Unix file system : All utilities,applications,data in unix is stored as files. Even directory is treated as a file which contains several other files.the file system begins with a directory called root. Denoted as slash(/). Braching from root there are several other directories called bin, lib, usr, ect, tmp and dev. Root directory also contains a directory unix which is unix kernel itself. All these are called sub-directories. Each of these directories contain several files and directories called sub-sub directories. / (root)

Unix bin lib dev usr tmp etc

User1 user2 user3 bin Usr contains all user related files.(home directories of all users). there are several files here each associated with a particular user.created by sys. Admin. Bin(/usr/bin) directory within unix contains additional unix command files(binary excutable files). Dev contains files that control i/o devices like terminals,printer, disk drives etc. for each device there is a separate file. Tmp all temporary files created by unix or by users. These files are automatically deleted when the system is shutdown and restarted. Bin contains all binary executable files for most of the unix commands. (unix commands can be either c programs of shell programs) Shell programs collection of several unix commands) Lib contains all library functions provided by unix for programmers. Etc binary executable files required by sys. Admin. Points to remember : 1. All unix commands must always be entered in small case letters. 2. Options available with a command are often known as switches. b/w command and option there should be a space and a - before the option. 3. ls l a is same as ls la

4. ctrl+d to return to login prompt or (exit to logout of unix session) grep globally search a regular expression and print it.

UNIX had a relatively small amount of code written in assembly language (this is called the kernel) and the remaining code for the operating system was written in a high level language called C. Cc c compiler Command prompt is $(dollar) if operating in bourne shell % if operating in c shell Password ageing after a specified period of time password ages (expires) and the next time you login the system requires you to change your password. This is password ageing. Login : aa1 Password : heman1(do not appear on the screen) Login incorrect (if any of the above are wrong) doesnt specify which one is wrong. This is security measure. 3-5 chances before the terminal gets disconnected. A msg is displayed to system admin telling him/her that several unsuccessful attempts were made on your login name.
Connecting terminal to host m/c : Through a 4/8/16 port controller card installed in the expansion slot on the mother board of the host m/c. one end of the cable plugged to the port on the controller card and another end to the serial port (9pin or 25pin) of the terminal. Partitions and File systems : File system is a group of files and relevant information regarding them. Unix requires a formatted hard disk. Disk space allotted to unix file system is made up of blocks.(typically 512bytes). Next step is to divide hard disk into partitions. Each partition is a logically independent disk that is accessed by its own device file. After partitioning the disk, a file system has to be created on each partition. Every file system has four components 1. the boot block : contains a program called bootstrap loader which is executed when we load the host m/c.(it loads kernel into memory). The bootstrapping program is read in from the boot block of only one file system(the main one called root file system) . for other file systems this block is simply kept blank. 2. The super node : describes the state of file system. How large it is, how many max. files it can accommodate, how many more files can be created etc. 3. Inode table : information related to all files(all entities in unix are treated as files) is stored in inode table on the disk. Each file has an inode entry(Each entry is made up of 64bytes) in the table. These details are stored a. Owner of the file b. Group to which owner belongs c. Type of file d. File access permissions e. Date and time of last access

f. Date and time of last modiication g. Number of links to the file h. Size of the file i. Addresses of blocks where the file is physically present. 4. Data blocks : these contain the actual file contents. An allocated data block can contain only one file in the file system. The changes made to super block and inode table are very frequent. Making these changes on hard disk takes precious cpu time.therefore a copy of inode table and super block gets loaded in RAM at start up time.therefore changes are recorded in ram copies of super block and inode tables every time changes occur. This is faster in ram.original super block and inode tables are updated after a fixed interval of time. Say 30 seconds. Command : sync (synchronises the inode table in memory with the one on disk) Command : $cmchk (to find out block size of your file system) o/p : BSIZE=1024 Unix identifies files by unique inode number Command : ls i filename 0/p filename inodenumber (eg : reports 12345) Storage of files : Each inode entry in the inode table consists of 13 addresses (0-12) Refer text book. Pg 66-68. Addresses 0 9 each store addresses that point to 1KB block on disk. So total addresses of 10KB file contents is stored. 10th address also contains an address of a 1KB block. (doesnt contain file contents) It contains 256 four byte slots which can store 256 more addresses each pointing to 1kb block on disk.therefore max. file size that can be addressed using 10th address entry is 256kb. This is called SINGLE INDIRECTION. 11th address contains address of a 1kb block which can store 256 addresses. Each of these 256 addresses can in turn store addresses of 1kb block each. Therefore total file content that can be stored is 256*256 KB = 64 MB. Like wise12th address can store 256*256*256 = 16GB of file contents. So total file content that can be stored is 10kb+256kb+64mb+16gb (more than sufficient for all practical purposes). Disk related commands : Checking disk free space: Command : $df - reports free as well as the used disk space for all the file systems installed on your machine. (reports no. of free disk blocks and free inodes for the file system) Command : $df -h displays space in human readable way. More information about disk usage Command : $df -ivt - reports available blocks, inodes and percentage of total available blocks and inodes. Command : $dfspace reports free disk space in terms of megabytes and percentage of total disk space. Command : $ /etc/dfspace (since dfspace is present in etc directory)

Disk usage the du command: df and dfspace report the disk space available in the file system as a whole where as du reports the disk space used by specified files,directories and sub-directories. Command : $du Command : $du h (reports disk space in human readable format) $du /dev reports no. of blocks occupied by directory /dev and sub-directories within /dev To report only blocks occupied by the directory /dev and not sub-directories within it Command : $du s /dev Command : $ulimit (user limit) presents current value of ulimit value. Implies that user cant create a file whose size is bigger than the ulimit value. User can reduce the value by saying Command : $ulimit 1 - here on no file can be created whose size is bigger than 512bytes. This change will be effective only for the current session and then the system will return to its default value when you logout. Note: user is not permitted to increase the ulimit value.user can only reduce the ulimit. A superuser can increase or reduce this value. More commands: Password : One person who can encroach on your data superuser. Command : $passwd If you forget your password administrator can give a new one, but cant tell you what your old one was. Passwords are stored in a file /etc/passwd Command : #cat /etc/passwd o/p of above command gives lots of useful information about users like login names(user names), encrypted password, user id, group id, default working directory and default working shell. Note : there is another passwd file in /bin directory. This gets executed when we change the password.thus /bin/passwd is an executable filewhich permits changing the password whereas /etc/passwd contains data about each user. Command : $cal (gives month, year and calender of current month) Command : $cal 2 1997 ( o/ps calendar of February 1997) Special command : cal j 1997 Command : $banner strange ways ( prints a message in large letters which looks like a banner) this prints strange in one line and ways in second line. To get both in same line command : $banner strange ways (but banner accommodates only 10characters including spaces in a single line,therefore ys are not displayed) File command: Command : $file * - recognizes several types of files(reads each file before determining its contents) O/p : cal.out: ascii text Ban: empty Clean: c program text

Etc: directory Story: English text Ss: commands text a.out : Iapx 386 not stripped File related commands (Operations on files) FILTERS : wc counts number of lines,words and characters in a specified file or files options : -l - no. of lines -w no. of words -c no. of characters Command : $wc lc file1 file2 o/p: file1 20 571 file2 30 804 wc also accepts i/p from keyboard (after entering i/p from keyboard terminate using ctrl+d) sort : 1. sorts the contents of files based on first character in each line in the file (if first character is same for two or more lines then second character in each line is compared so on) 2. merges multiple sorted files and stores results in specified o/p file. SORTING IS DONE BASED ON ASCII COLLATING SEQUENCE 1. Sorts spaces and tabs first 2. Then punctuation marks 3. Followed by numbers, uppercase and lower case letter in that order. Command : $ sort myfile sorts contents of file and displays the sorted o/p on screen $ sort file1 file2 file3 sorting contents of several files in one shot $ sort o resultfile file1 file2 file3 sorts the three files and stores o/p in resultfile $ sort u o resultfile file1 file2 file3 sorts only unique lines and stores in resultfile $ sort m file1 file2 merges already sorted files file1 and file2 $sort file1 - command helps in taking i/p from keyboard and then merges the contents of the file with the i/p from keyboard. $ sort r filename reverse sort $ sort sorts only the i/p from standard keyboard Sort is most fruitfully used for files which are databases whose information is organized in fields. Fields are group of characters separated by a predetermined delimiter such as space or tab etc or by a newline. Sort key fields used for sorting. They have starting and ending position specified by +pos1 and pos2. (these two are optional) If pos2 is not mentioned key is assumed to extend till the end of line. $sort +1 -2 filename +1 indicates sort key starts at the second field and -2 indicates that sort key ends before the third field Or $sort k 2 filename sorts based on second field( default delimiter is tab(Space)) $sort t: k 2 filename -t used to specify delimiter and sorts based on second field $sort t| k 3,3 k 2,2 filename sorting based on two fields. k 3,3 is primary key, k 2,2 is secondary key. 3,3 primary key starts at third field and ends on the same field. $sort t| k 5.7,5.10 filename starts sorting at 7th column of field five and ends sorting based on 10th column of field five.

$sort n filename sorting is done based on a numeric field Options : -tchar uses delimiter char to identify fields -b ignores leading spaces and tabs -c checks if files are already sorted.if sorted does nothing -d sorts in dictionary order -k n sorts on n th field (k key(posix option)) -k m,n starts sort on mth field and ends sort on nth field -k m.n starts sort on nth column of mth field -f folds lowercase to equivalent uppercase (Case insensitive) Cut : picks up a given number of character of field from the specified file Options : -c for cutting columns -f for cutting fields One of these is mandatory Cutting fields : $cut f 2,7 filename second and seventh fields of the file are displayed $cut f 2-7 filename fields 2 through 7 (2 to 7) of the file are displayed Cut command assumes by default the fields are separated by a tab character(Space)