Unix Command Unit 1
Unix Command Unit 1
Like DOS and windows, there is another operating system called UNIX UNIX operating system arrived earlier than the previous two (i.e., DOS and Windows) operating system. UNIX is a giant operating system. UNIX runs on every hard ware and provides inspiration to open source movement. UNIX operating system is difficult to use, even when the user is an experienced professional. UNIX uses numerous symbols. UNIX operating system doesnt tell whether you are right or wrong and it doesnt give any warnings. Interaction with UNIX system is through a command interpreter called the shell. Commands in UNIX are just like English languages.
A BRIEF INTRODUCTION TO UNIX SYSTEMS: Logging in with username and password: UNIX is security conscious. It can be used only by those persons who maintain an account with the computer system. The list of account is maintained separately in the computer. System Administrator is a person who grants you the authority to use the system. To login in UNIX system there is a login prompt which indicates that the terminal is available for someone to login. This login prompt also indicates that the previous user has logged out. For example, consider that you have a user account by name kumar. Enter
Rashmi Shenoy K Dept Of CS&EPage 1
10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Rashmi Shenoy K Dept Of CS&EPage 3
There are currently two users- kumar and vipul. These are the user-ids or usernames which the user uses to login. The output includes the username (ie, kumar). The second column shows the name of the terminal the user is working on (ie, console). The date and time of the login are also shown in third, fourth and fifth column. The output of the who command doesnt include any headers to indicate what the various columns mean. 5] ps: Viewing Processes. Every command that you run gives rise to a process. To view all processs that you are responsible for creating, run the ps command. $ ps PID 344 TTY console TIME 01:00 CMD ksh
ps command generates a header followed by line containing the details of the ksh process. This process has a unique number (ie, 344 here in the example above) called the process-id, and when you log out, this process is killed.
To get the filenames having similar names, we can give ls command like: $ ls chap* chap01 cahp02 chap03 If you want to know more about the files, you can use ls command with option l, between the command and the file name.
$ ls l chap*
The argument beginning with a hyphen is known as an option. An option changes the default behaviour of the command, so if ls displays a columnar list of files, the l option makes to display some of the attributes also. 7] wc: Counting Number of Lines in a file The wc command gives the number of lines in a particular file. $ wc list 6 7 43 list
Here list is the filename given as argument to the wc command. The first column displays Number of lines in the file list, second column displays Number of words in the file list, and third column displays Number of the characters in the file list. 8] exit: Signing Off To suspend a session you have to use exit command. This command gives the login prompt which indicate that the user has logged out successfully and another user can be logged in. $ exit login:
CHAPTER 2
Unix and shell programming THE UNIX ARCHITECTURE AND COMMAND USAGE
THE UNIX ARCHITECTURE: The entire UNIX system is supported by a handful of essentially simple, though somewhat abstract concepts. The software architecture of UNIX has
1. 2. 3.
Division of Labor : Kernel and Shell The File and Process The System Calls
1] Division of Labor: Kernel and Shell UNIX architecture comprises of two major components-The shell and the kernel. The Kernel interacts with the machines hardware and the shell interacts with the user. The Kernel is the core of the operating system. It is a collection of routines mostly written in C. It is loaded ino the memory when the system is booted and communicates directly with the hardware. User programs that need to access the hardware use the services of the kernel and the kernel performs the job on behalf of the user. These programs access the kernel through a set of functions called system calls. Kernel manages the systems memory, schedules processes, decides their priorities and performs various other tasks. The shell performs the role of command interpreter. Even though there is only one kernel running on the system, there could be several shells in action, one for each user whos logged in. When you enter a command through keyboard, the shell thoroughly examines the keyboard input for special characters. If it finds any, it rebuilds a simplified command line, and finally communicates with the kernel to see that the command is executed. The kernel is represented by the file depending on the system. The shell is represented by sh(Bourne shell), csh(C shell), ksh(Korn shell) or bsh(Bash shell). One of these shells will be running when the user logs in. To know which shell is running right now, you have to type $ echo
2] The File and Process Two simple entities support the UNIX system the file and the process. A file is an array of bytes that stores information. It is also related to another file by being part of a single hierarchical structure. A process can be treated as a time image of an executable file. Like files, process also belongs to a hierarchical structure.
UNIX:A Multiuser System UNIX:A multitasking System The Building Block Approach The UNIX Toolkit Pattern Matching Programming Facility Documentation
1] UNIX: A Multiuser System UNIX is a multiprogramming system. It permits multiple programs to run in two ways:
Multiple users can run separate jobs. A single user can also run multiple jobs.
In UNIX, the resources are actually shared between all users. UNIX is also a multiuser system.
more, Brekeleys pager, which is now available universally as a superior alternative to the original pg command. less, the standard pager used on Linux systems, but also available for all UNIX platforms. Less is modeled on the vi editor and is more powerful than more because it provides vi-like navigational and search facilities.
On a man page that uses more as the pager, the user will see a prompt at the bottom-left of the screen which looks like: ---More(26%) Less shows : prompt At this prompt the user can press a key to perform navigation, or search for string. The key which the user presses is interpreted as one of the mans internal commands. Many UNIX utilities like vi and mail also have their own internal commands. To quit the pager and man, press q. NAVIGATION AND SEARCH There are numerous navigation commands. For example:
f or spacebar, displays by one screen text at a time. b, moves back one screen.
The man documentation is sometimes quite extensive, and the search facility lets the user to locate a page containing a keyword quite easily. For example: To call up the page containing the word clobber by using the string with the / (front slash): / clobber [Enter] You are taken to the page containing clobber. If that is not the page you are looking for, then you can repeat the search by pressing n.
If a command argument is enclosed in rectangular brackets, then it is optional. Otherwise, the argument is required. In the example above the wc man page shows all of its arguments enclosed in three such groups. This means that wc can be used without arguments. The ellipsis (a set of three dots) implies that there can be more instances of the preceding word. The expression [file] signifies that wc can be used with more than one filename as argument. The / character means that only one of the options shown on either side of the pipe can be used. In the example above the option c, -m and c can be used.
DESCRIPTION provides a detailed description. For example: DESCRIPTION The wc utility reads one or more input files and, by default, writes the number of newline characters, words and bytes contained in each input file to the standard
whatis: man command uses the f option to emulate whaiis behavior. The command lists one-liners for a command $whatis cp cp cp(1) -copy files
WHEN THINGS GO WRONG: Terminals and keyboards have no uniform behavioral pattern. Terminal settings directly impact the keyboard operation. If you observe a different behavior from that expected, when you press certain keystrokes, it means that the terminal settings are different. In such cases, you should know which keys to press to get the required behavior. Backspacing doesnt work: Backspacing doesnt work on UNIX system. Consider that you misspelled passwd as password, and when you press the backspace key to erase the last three characters, you saw like: $ password^H^H^H Backspace doesnt work here. So it is seen like ^H when you press backspace. To erase a character you should use [Ctrl-h] or [Delete] key instead of backspace. Killing a line: If a command line contains many mistakes, you can kill the line altogether without executing it. In such case, use [Ctrl-u]. The line kill character erases everything in the line and returns the cursor to the beginning of the line. Interrupting a command: Sometimes, program goes on running for an hour and
Function Erases text Interrupts a command Stops scrolling of screen output and unlocks keyboard Resumes scrolling of screen output and unlocks keyboard
Kills command line without executing it Kills running program but creates a core file containing the memory image of the program Suspends process and returns shell prompt; use fg to resume lob Alternative to [Enter] Alternative to [Enter]
CHAPTER 3
Rashmi Shenoy K Dept Of CS&EPage 23
1] Ordinary file: An Ordinary file is also called as regular file. This is the most common file type. All programs you type belong to this type. An ordinary file can be divided into two types:
Text file: A text file contains only printable characters. All C and Java program sources, shell and perl scripts are text files. A text file contains lines of characters where every line is terminated with the newline character, also known as linefeed (LF). When you press [Enter] while inserting text, the LF character is appended to every line.
The filename A unique identification number for the file or directory called inode number.
When you create or remove a file, the kernel automatically updates its corresponding directory by adding or removing the entry (filename or inode number) associated with the file. 3] Device file: All the operations on the devices are performed by reading or writing the file representing the device. It is advantageous to treat devices as files as some of the commands used to access an ordinary file can be used with device files as well. Device filenames are generally found inside a single directory structure, / dev. A device file is not really a stream of characters. It is the attributes of the file that entirely govern the operation of the device. The kernel identifies a device from its attributes and uses them to operate the device.
FILENAMES IN UNIX: In UNIX system, a filename can consist of up to 255 characters. Files may
Alphabetic characters and numerals The period (.), hyphen (-) and underscore (_)
UNIX imposes no rules for framing filename extensions. In all cases, it is the application that imposes restriction. Example: a C compiler expects C program filenames to end with .c, Oracle requires SQL scripts to have .sql extension. A file can have as many dots embedded in its name. a filename can also begin with or end with a dot. UNIX is case sensitive. For example: chap01, Chap01 and CHAP01 are three different filenames that can coexist in the same directory. THE PARENT-CHILD RELATIONSHIP: All files in UNIX are related to one another. The file system in UNIX is a collection of all of these related files organized in a hierarchical structure. The implicit feature of every UNIX file system is that there is a top, which serves as reference point for all files. This top is called root and is represented by a / (front slash). Root is actually a directory. The root directory (/) has a number of subdirectories under it. These subdirectories in turn have more subdirectories and other files under them. For example, bin and usr are two directories directly under /, while a second bin and kumar are subdirectories under usr. Every file, apart from root, must have a parent. Thus, home directory is the parent of kumar, while / is the parent of home, and grand parent of kumar. If you create a file login.sql under the kumar directory, then kumar will be the parent of this file.
/(root)
bin
dev
etc
home
lib
stand
tmp
usr
date
who
dsk
rdsk
kumar
sharma
unix
bin
include sbin
f0q18dt
login.sql
progs
safe
In parent-child relationship, the parent is always a directory. home and kumar are both directories as they are parent of atleast one file or directory. login.sql is simply an ordinary file, it cant have any directory under it. THE HOME VARIABLE - THE HOME DIRECTORY: When the user logs on to the system, UNIX automatically places the user in a directory called the home directory. It is created by the system when a user account is opened. For example: If you login with user name kumar, you will have the pathname /home/kumar. The user can change his home directory when he wants. The shell variable HOME knows the home directory. to know the home directory you have to type $ echo $HOME You will get the output like /home/kumar. This sequence is called as
The directory test may already exist There may be an ordinary file by that name in the current directory The permissions set for the current directory doesnt permit the creation of files and directories by the user.
4] rmdir: REMOVING DIRCTORIES The rmdir (remove directory) command removes directories. For example: To remove directory pis: $ rmdir pis It is possible to remove or delete more than one directory with one rmdir command. For example: The three directories and subdirectories which created using mkdir in above example can be removed by using rmdir with reversed set of argument: $ rmdir pis/data pis/progs pis When you delete a directory and its subdirectories, a reverse logic of mksir has to be applied. The following directory sequence used by mkdir is invalid in rmdir. For example: $ rmdir pis pis/progs pis/data rmdir:directory pis : Directory not empty
You cant delete a directory unless it is empty. You cant remove a subdirectory unless you are placed in a directory which is hierarchically above the one you have chosen to remove.
If you try to remove the directory progs by executing the command from the same directory itself: $ cd progs $ pwd /home/kumar/pis/progs $ rmdir /home/kumar/pis/progs rmdir: directory /home/kumar/pis/progs: Directory does not exist To remove this directory, the user must position himself in the directory above progs, ie, pis and then remove it from there $ cd /home/kumar/pis $ pwd /home/kumar/pis $ rmdir progs The mkdir and rmdir command work only in directories owned by the user.
ABSOLUTE PATHNAME: Many UNIX commands use file and directory names as arguments which are presumed to exist in the current directory. For example: $ cat login.sql will work only if the file login.sql exists in your
Rashmi Shenoy K Dept Of CS&EPage 32
. (a single dot)This represents the current directory .. (two dots)This represents the parent directory
Using .. to frame relative pathnames : Assume that you are placed in /home/kumar/progs/data/text, you can use .. as argument to cd command to move to the parent directory, /home/kumar/progs/ data $ pwd /home/kumar/progs/data/text $ cd.. $ pwd
1] Output in multiple columns (-x) -x option is used to display the output in multiple columns For example:
2] Identifying directories and executables (-F) The F option is used to identify directories and executable files. For example: $ ls F 08_packets.html TOC.sh * calendar* cptodos.sh* dept.lst emp.lst helpdir/ progs/ The use of two symbols, * and / as type indicators. The * indicates that the file contains executable code and the / indicates the directories. 3] Showing hidden files (-a) There are some hidden files in the system. To see the hidden files a option is used with ls command. For example: $ ls a ./
The contents of the directory are listed, consisting of Oracle documentation in the helpdir and a number of perl program files in progs. 5] Recursive listing (-R)
The list shows filenames in three sectionsthe one under the home directory and those under the subdirectories helpdir and progs. ./helpdir indicates that helpdir is a subdirectory under . THE UNIX FILE SYSTEM: The UNIX file system consists of two groups. The first group contains the files that are made available during system installation:
/bin and /usr/bin These are the directories where all the commonly used UNIX commands are found. /sbin and /usr/sbin There are some commands which the user cannot execute. The commands can be executed only by system administrator, such commands are found in this directory / etc This directory contains the configuration files of the system. The login name and password are stored in files /etc/passwd and /etc/shadow /dev This directory contains all device files. These files dont occupy space on disk. There could be more subdirectories like pts, dsk and rdsk in this directory. /lib and /usr/lib This directory contains all library files in binary form /usr/include This directory contains the standard header files used by C
/usr/share/man This is where the man page is stored. There are separate subdirectories that contain the pages for each section. For example, the man page of ls can be found in /usr/share/man/man1, where 1 in man1 represents Section 1 of the UNIX manual
The contents of these directories would change as more software and utilities are added to the system. Users work with their own file, write programs, send and receive mails and create temporary files. The second group contains
/tmp The directories where users are allowed to create temporary files. These files are wiped away regularly by the system. /var The variable part of the file system. It contains all outgoing and incoming mails /home On many systems users are housed here. For example, kumar would have his home directory in /home/kumar.