unix notes.docx
unix notes.docx
1. Unix Components/Architecture
2. Features of Unix
3. The UNIX Environment and UNIX Structure, Posix and Single Unix specification
4. General features of Unix commands/ command structure. Command arguments and options
5. Basic Unix commands such as echo, printf, ls, who, date,passwd, cal, Combining commands
⮚ Unix Components/Architecture
An operating system is the software that manages the computer's hardware and provides a
convenient and safe environment for running programs.
It acts as an interface between user programs and the hardware resources that these programs access
like – processor, memory, hard disk, printer & so on.
It is loaded into memory when a computer is booted and remains active as long as the machine is up.
Like DOS and Windows, there's another operating system called UNIX.
It runs practically on every Hardware and provided inspiration to the Open Source movement.
You interact with a UNIX system through a command interpreter called the shell.
Unix Components/Architecture
The Shell
✔ Computers don't have any capability of translating commands into action.
✔ That requires a command interpreter, also called as the shell.
✔ Shell is acts interface between the user and the kernel.
✔ Most of the time, there's only one kernel running on the system, there could be
several shells running – one for each user logged in.
✔ The shell accepts commands from user, if require rebuilds a user command,
andfinally communicates with the kernel to see that the command is executed.
✔ Example:
$echoVTU Belagavi #Shell rebuilds echo command by removing multiple spaces
VTU Belagavi
● The following figure shows the kernel-shell Relationship:
Two simple entities support the UNIX – the file and the process.
The File
A file is just an array of bytes and can contain virtually anything.
Every file in UNIX is part of the one file structure provided by UNIX.
The Process
The process is the name given to the file when it is executed as a program (Process is
program under execution).
We can say process is the “time image” of an executable file.
UNIX provides tools to control processes move them between foreground and background and
killthem.
Though there are several commands that use functions called system calls to communicate with the
kernel.
All UNIX flavors have one thing in common – they use the same systemcalls. Eg: write, open
⮚ Features of Unix
1. A Multiuser System
⮚ UNIX is a multiprogramming system, it permits multiple programs to run and compete for the
attention of the CPU.
⮚ This can happen in two ways:
- Multiple users can run separate jobs
- A single user can also run multiplejobs
⮚ A single user system where the CPU, memory and hard disks are all dedicated to a single user.
⮚ In UNIX, the resources are shared between all users; UNIX is also a multiuser system.
2. A Multitasking System
⮚ A single user can also run multiple tasks concurrently.
⮚ UNIX is a multitasking system.
⮚ It is usual for a user to edit a file, print another one on the printer, send email to a friend and browse
www- all without leaving any of applications.
⮚ The kernel is designed to handle a user's multiple needs.
⮚ In a multitasking environment, a user sees one job running in the foreground; the rest run in the
background.
⮚ User can switch jobs between background and foreground, suspend, or even terminate them.
5. Pattern Matching
⮚ UNIX features very sophisticated pattern matching features.
⮚ Example: The * (zero or more occurrences of characters) is a special character used by system
toindicate that it can match a number of filenames.
6. Programming Facility
⮚
The UNIX shell is also a programming language; it was designed for programmer, not for end user.
⮚
It has all the necessary ingredients, like control structures, loops and variables, that establish
powerful programming language.
⮚ These features are used to design shell scripts – programs that can also invoke UNIX commands.
⮚ Many of the system's functions can be controlled and automated by using these shellscripts.
7. Documentation
⮚ The principal on-line help facility available is the man command, which remains the most
important references for commands and their configuration files.
⮚ Apart from the man documentation, there's a vast ocean of UNIX resources available on the Internet.
⮚ A variable that specifies how an operating system or another program runs, or the devices
that the operating system recognizes.
⮚ Set of dynamic values that can affect the way running processes will behave on a computer.
⮚ Basic Unix commands such as echo, printf, ls, who, date,passwd, cal, Combining
⮚ commands
$echo $SHELL
/usr/bin/bash
Constant Meaning
„a‟ Audible Alert (Bell)
„b‟ Back Space
„f‟ Form Feed
„n‟ New Line
„r‟ Carriage Return
„t‟ Horizontal Tab
„v‟ Vertical Tab
„\‟ Backslash
„\0n‟ ASCII character represented by the octal value n
The printf command is available on most modern UNIX systems, and is the one we can use
instead of echo. The command in the simplest form can be used in the same way as echo:
printf also accepts all escape sequences used by echo, but unlike echo, it
doesn‟t automatically insert newline unless the \n is used explicitly. printf also uses
formatted strings in the same way the C language function of the same name uses them:
The %s format string acts as a placeholder for the value of $SHELL, and printf replaces %s
with the value of $SHELL. %s is the standard format used for printing strings. printf uses
many of the formats used by C‟s printf function. Here are some of the commonly used ones:
%s – String
Example:
$ printf "The value of 255 is %o in octal and %x in hexadecimal\n" 255 255
With options it can provide information about the size, type of file, permissions, dates of file
creation, change and access.
Syntax
ls [options] [argument]
When no argument is used, the listing will be of the current directory. There are many very useful
options for the ls command. A listing of many of them follows. When using the command, stringthe
desired options together preceded by "-".
- a Lists all files, including those beginning with a dot (.).
- d Lists only names of directories, not the files in the directory
- F Indicates type of entry with a trailing symbol: executables with *, directories with /
and symbolic links
- R Recursive list
- u Sorts filenames by last access time
- t Sorts filenames by last modification time
- i Displays inode number
- l Long listing: lists the mode, link information, owner, size, last modification (time). If the file is
a symbolic link, an arrow (-->) precedes the pathname of the linked-to file.
- x multicolumnar output
$ls –ld helpdir progs
- drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir
- drwxr-xr-x 2 kumar metal 512 may 9 09:57
progs output in multiple columns(-x):
$ ls –x
Identifying Directories and executables(-F)
$ ls –Fx
Showing hidden files(-a)
$ ls -axF
Listing directory contents
$ ls -x
$ who
root tty7 2017-09-04 16:38 (:0)
root tty17 2017-09-04 16:38 (:0)
$_
The command can also be used with suitable format specifiers as arguments. Each symbol is
preceded by the + symbol, followed by the % operator, and a single character describing the
format. For instance, you can print only the month using the format +%m:
$date +%m
09
Or
the month name name:
$ date +%h
Aug
Or
You can combine them in one command:
$ date + “%h %m”
Aug 08
There are many other format specifiers, and the useful ones are listed below:
- d – The day of month (1 - 31)
- y – The last two digits of the year.
- H, M and S – The hour, minute and second, respectively.
- D – The date in the format mm/dd/yy
- T – The time in the format hh:mm:ss
6. Passwd: Changing your password
If account doesn’t have password or has one that is already known to others, should change
it immediately.
This can be done with passwd command:
$ passwd
passwd: changing password for Kumar
Enter login password: ******* Asks for old password
New password: ********
Re-enter new password: ********
passwd (SYSTEM): passwd successfully changed for Kumar
Passwd expects you to respond three times. First, it primpts for the old password. Next, it schecks
whether you have entered a valid password, and if you have, it then prompts for the new
password.
Enter the new password using the password naming rules.
Finally, passwd asks you to reenter the new password. Later, the new password is registered by the
system.
Rules for framing your own password:
- Don’t choose a password similar to the old one.
- Don’t use commonly used names like names of friends, relatives, pets and so forth.
- Use a mix of alphabetic or numeric characters.
- Don’t write a password in an easily accessible document.
- Change the password regularly.
cal command can be used to see the calendar of any specific month or a complete year.
Syntax:
cal [ [ month] year ]
Everything within the rectangular box in optional. So cal can be used without any arguments,
in which case it displays the calendar of the current month
$ cal
September 2017
Su Mo Tu We Th Fr Sa
12
345 6 789
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
The syntax show that cal can be used with arguments, the month is optional but year is not.
To see the calendar of month August 2017, we need to use two arguments as shownbelow,
$ cal 8 2017
August 2017
Su Mo Tu We Th Fr Sa
1 2 345
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
You can‟t hold the calendar of a year in a single screen page; it scrolls off rapidly before you
can use [ctrl-s] to make it pause. To make cal pause using pager using the | symbol to connect
them.
$cal 2017 | more
8. Combining commands
UNIX allows you to specify more than one command in the single command
line. Example:
$ wc note; ls -l note #Two commands combined here using ; (semicolon)
2 3 16 note
-rw-rw-r-- 1 mahesh mahesh 16 Jan 30 09:35 note
In the above example, wc command returns number of lines, words and characters in that file, and
ls –l command returns list all the attributes of the file
A command line can overflow or be split into multiple lines
This is
a three-line text
message
⮚ When the shell execute command(file) from its own set of built-in commands that are
not stored as separate files in /bin directory, it is called internal command.
⮚ They can be executed any time and are independent.
⮚ If the command (file) has an independence existence in the /bin directory, it is called
external command.
⮚ External commands are loaded when the user requests for them. It will have an individual
process.
Examples:
$ type echo # echo is an internal
command echo is shell built-in
$ type ls # ls is an external
command ls is /bin/ls
⮚ If the command exists both as an internal and external one, shell execute
internalcommand only.
⮚ Internal commands will have top priority compare to external command of samename.
⮚ The type command is used to describe how its argument would be translated if used as commands. It
is also used to find out whether it is built-in or external binary file.
⮚ The type command is shell builtin.
Syntax:
type [Options] command names
● If you want to know the location of executable program (or command), use type command-
Example:
$ type date
date is /bin/date
$ type echo
echo is a shell builtin
● When you execute date command, the shell locates this file in the /bin directory and
makes arrangements to execute it.
⮚ The unix system provides a special login name for the exclusive use of the administrator;
it is called root. This account doesn‟t need to be separately created but comes
with every system. Its password is generally set at the time of installation of the system
and has to be used on logging in:
Becoming the super at login
time login: root
Password: ********* [Enter]
#-
/sbin:/bin:/usr/sbin:/usr/bin:/usr/dt/bin
#pwd
/home/abc
● Though the current directory doesn‟t change, the # prompt indicates that abc now
has powers of a superuser. To be in root‟s home directory on superuser login, use su
–l.
● User‟s often rush to the administrator with the complaint that a program has
stopped running. The administrator first tries running it in a simulated environment.
Su command when used with a – (minus), recreates the user‟s environment without
the login-password route:
$su – abc
⮚ This sequence executes abc’s .profile and temporarily creates abc’s environment.
su runs in a separate sub-shell, so this mode is terminated by hitting [ctrl-d] or
using exit.
1. Naming files.
2. Basic file types/categories.
3. Parent child relationship.
4. The home directory and the HOME variable.
5. Reachingrequiredfiles- the PATHvariable, manipulatingthe PATH, Relativeandabsolutepathnames.
6. Directory commands – pwd, cd, mkdir, rmdir commands.
7. The dot (.) and double dots (..) notations to represent present and parent directories and their usage in
relative path names.
8. File related commands – cat, mv, rm, cp, wc and od commands.
The File
⮚ The file is the container for storing information.
⮚ Neither a file's size nor its name is stored in file.
⮚ All file attributes such as file type, permissions, links, owner, group owner etc are kept in a
separate area of the hard disk, not directly accessible to humans, but only to kernel.
⮚ UNIX treats directories and devices as files as well.
⮚ All physical devices like hard disk, memory, CD-ROM, printer and modem are treated as files.
File may or may not have extensions, and consist of any ASCII character expect the / &
NULL character.
Users are permitted to use control characters or other unprintable characters in a filename.
1. Ordinary file – also called as regular file. It contains only data as a stream of characters.
2. Directory file – it contains files and other sub-directories.
3. Device file – all devices and peripherals are represented by files.
Directory File
A directory contains no data but keeps some details of the files and subdirectories that it contains.
A directory file contains an entry for every file and subdirectories that it houses. If you have 20
files in a directory, there will be 20 entries in the directory.
Each entry has two components-
- the filename
- a unique identification number for the file or directory (called as inode number).
When any file is created or removed, the kernel automatically updates its corresponding directory
by adding or removing the entry (inode number & filename) associated with that file.
Device File
Installing software from CD-ROM, printing files and backing up data files to tape.
All of these activities are performed by reading or writing the file representing the device.
Advantage of device file is that some of the commands used to access an ordinary file also work
with device file.
Device filenames are generally found in a single directory structure, /dev.
The kernel identifies a device from its attributes and then uses them to operate the device.
The file system in UNIX is a collection of all of these files (ordinary, directory and device
files) organized in a Hierarchical (an inverted tree) structure as shown in below figure,
.
⮚ The feature of UNIX file system is that there is a top, which serves as the reference point for all files
⮚ This top is called root and is represented by a / (Front slash).
⮚ The root is actually a directory.
⮚ The root directory (/) has a number of subdirectories under it.
⮚ The subdirectories in turn have more subdirectories and other files under them.
⮚ Every file apart from root, must have a parent, and it should be possible to trace the ultimate
parentage of a file to root.
⮚ In parent-child relationship, the parent is always a directory.
⮚ The home directory is the parent of mthomas. / is the parent of home and the grandparent of
mthomas.
When you logon to the system, UNIX places you in a directory called home directory.
If a user login using the login name kumar, user will land up in a directory that could have the
path name /home/kumar.
PATH
A shell variable that contains a colon-delimited list of directories that the shell will lok through to
locatea command invoked by user.
The PATH generally includes /bin and /usr/bin for nonprivileged users and /bin and /usr/sbin for
the superuser.
Use echo to evaluate this variable in which directory list separated by colons:
$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/usr/ccs/bin:/usr/local/java/bin:.
There are six directories in this colon separated list.
When you issue a command , the shell searches this list in the sequence specified to locate and execute it.
Absolute Pathnames:
⮚ It shows a file’s location with reference to the top, i.e., root
⮚ It is simply a sequence of directory names separated by
slashes e.g.: /home/kumar
⮚ Suppose we are placed in /usr and want to access the file login.sql which is present in
/home/kumar, we can give the pathname of the file as below:
cat /home/kumar/login.sql
⮚ If the first character of a pathname is /, the file’s location must be determined wrt root(the first
/).such a pathname is called an absolute pathname.
⮚ If you know the location of a particular command, you van precede its name with the complete path.
⮚ Since date reside in/bin (or /usr/bin), you can use the absolute pathname:
$ /bin/date
Thu Sep 1 09:39:55 IST 2020
2.6The dot (.) and double dots (..) notations to represent present and parent directories
and their usage in relative path names.
Relative path is defined as the path related to the present working directly(pwd). It starts at
your current directory and never starts with a /.
A pathname which specifies the location of a file using the symbols. and ..
$ pwd
/home/kumar/progs/data/text
$ cd .. // moves one level up
$ pwd
/home/kumar/progs/data
To move to /home , we can use the relative path name as follows:
$ pwd
/home/kumar/pis
$ cd ../.. // moves two levels up
$ pwd
/home
Any time user can know the current working directory using pwd command.
$ pwd
/home/kumar
Like HOME, pwd displays the absolute path.
User can move around the UNIX file system using cd (change directory) command.
When used with the argument, it changes the current cd can also be used without arguments:
directory to the directoryspecified as argument, $ pwd
progs: /home/kumar/progs
$cd
$ pwd
$ pwd
/home/kumar
$ cd progs /home/kumar
$ pwd
/home/kumar/progs
Here we are using the relative pathname of progs cd without argument changes the working directory
directory. The same can be done with the absolute to home directory.
pathname also.
$cd /home/sharma
$cd /home/kumar/progs $ pwd
$ pwd /home/sharma
/home/kumar/progs $cd
$cd /bin
/home/kumar
$ pwd
/bin
mkdir: MAKING DIRECTORIES
$mkdir patch
You can create a number of subdirectories with one mkdir command:
$mkdir patch dba doc
This creates three subdirectories – progs, cprogs and javaprogs under progs.
The order of specifying arguments is important. You cannot create subdirectories before creation
of parent directory.
For instance following command doesn‘t work
The rmdir (remove directory) command removes the directories. You have to do this toremove
progs:
$rmdir progs
If progs is empty directory then it will be removed form system.
Following command used with mkdir fails with rmdir
First it removes cprogs and javaprogs form progs directory and then it removes progs fro system.
2.8 File related commands – cat, mv, rm, cp, wc and od commands.
cat: DISPLAYING AND CREATING FILES
cat command is used to display the contents of a small file on the terminal.
$ cat cprogram.c # include
<stdioh> void main ()
{
Printf(―hello‖);
}
As like other files cat accepts more than one filename as arguments
The contents of the second files are shown immediately after the first file without any
header information. So cat concatenates two files- hence its name
cat OPTIONS
cat without any option it will display text files. Nonprinting ASCII characters can be
displayed with –v option.
Numbering Lines (-n)
-n option numbers lines. This numbering option helps programmer in debugging programs.
cat is also useful for creating a file. Enter the command cat, followed by > character and
the filename.
When the command line is terminated with [Enter], the prompt vanishes. Cat now waits to take
input from the user. Enter few lines; press [ctrl-d] to signify the end of input to the system
To display the file contents of new use file name with cat command.
$ cat new
This is a new file which contains some text, just to Add some
contents to the file new
mv: RENAMING FILES
The mv command renames (moves) files. The main two functions are:
1. It renames a file(or directory)
2. It moves a group of files to different directory
It doesn't create a copy of the file; it merely renames it. No additional space is consumed on
disk during renaming.
Eg : To rename the file csb as csa we can use the following command
$ mv csb csa
If the destination file doesn‘t exist in the current directory, it will be created. Or else it will
just rename the specified file in mv command.
A group of files can be moved to a directory.
Eg : Moves three files ch1,ch2,ch3 to the directory module
$ mv rename newname
mv replaces the filename in the existing directory entry with the new name. It doesn't create
a copy of the file; it renames it.
Group of files can be moved to a directory
$ rm *
Removes all files from that directory
rm options
Interactive Deletion (-i) : Ask the user confirmation before removing each file:
$ rm -i ch1 ch2
rm: remove ch1 (yes/no)? ? y
A y removes the file (ch1) any other response like n or any other key leave the
file undeleted.
Recursive deletion (-r or -R): It performs a recursive search for all directories and files
within these subdirectories. At each stage it deletes everything it finds.
$ rm -r * Works as rmdir
It deletes all files in the current directory and all its subdirectories.
The cp command copies a file or a group of files. It creates an exact image of the file on
the disk with a different name. The syntax takes two filename to be specified in the
command line.
When both are ordinary files, first file is copied to second.
$ cp csa csb
If the destination file (csb) doesn‘t exist, it will first be created before copying takes place.
If not it will simply be overwritten without any warning from the system.
Example to show two ways of copying files to the cs directory:
cp command can be used to copy more than one file with a single invocation of
the command. In this case the last filename must be a directory.
Eg: To copy the file ch1,chh2, ch3 to the module , use cp as
$ cp ch1 ch2 ch3 module
The files will have the same name in module. If the files are already resident in module,
they will be overwritten. In the above diagram module directoryshould already exist and
cp doesn‘t able create a directory.
UNIX system uses * as a shorthand for multiple filenames.
Eg:
$ cp ch* usp #Copies all the files beginning with ch
cp options
Interactive Copying(-i) : The –i option warns the user before overwriting the
destination file, If unit 1 exists, cp prompts for response
$ cp -i ch1 unit1
$ cp: overwrite unit1 (yes/no)? Y
A y at this prompt overwrites the file, any other response leaves it uncopied.
Copying directory structure (-R) :
It performs recursive behavior command can descend a directory and examine all files
in its subdirectories.
-R : behaves recursively to copy an entire directory structure
$ cp -R usp newusp
$ cp -R class newclass
Character: smallest unit of information, and includes a space, tab and newline
wc offers 3 options to make a specific count. –l option counts only number of lines, - w and –
c options count words and characters, respectively.
$ wc -l ofile
4 ofile
$ wc -w ofile
20 ofile
$ wc -c ofile
97 ofile
Multiple filenames, wc produces a line for each file, as well as a total count.
Each line displays 16 bytes of data in octal, preceded by the offset in the file of the
first byte in the line.
$ od –b file
0000000 164 150 151 163 040 146 151 154 145 040 151 163 040 141 156 040
0000020 145 170 141 155 160 154 145 040 146 157 162 040 157 144 040 143
0000040 157 155 155 141 156 144 012 136 144 040 165 163 145 144 040 141
0000060 163 040 141 156 040 151 156 164 145 162 162 165 160 164 040 153
0000100 145 171
-c character option
Now it shows the printable characters and its corresponding ASCIIoctal representation
$ od –bc file
od -bc ofile
0000000 164 150 151 163 040 146 151 154 145 040 151 163 040 141 156 040
T h i s f i l e i s a n 0000020 145
170 141 155 160 154 145 040 146 157 162 040 157 144 040 143
e x a m p l e f o r o d c 0000040 157
155 155 141 156 144 012 136 144 040 165 163 145 144 040 141
o m m a n d \n ^ d u s e d a 0000060 163
040 141 156 040 151 156 164 145 162 162 165 160 164 040 153
s a n i n t e r r u p t k 0000100145
171
e y
It lists seven attributes of all files in the current directory and theyare:
For example,
$ ls -l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 2 kumar metal 19555 may 10 15:45 chap02
drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir
drwxr-xr-x 3 kumar metal 512 may 09 11:05 progs
⮚ Directories are easily identified in the listing by the first character of the first column, which
here shows d.
⮚ The significance of the attributes of a directory differs a good deal from an ordinary file.
⮚ To see the attributes of a directory rather than the files contained in it, use ls –ld with the
directory name. Note that simply using ls –d will not list all subdirectories in the current
directory. Strange though it may seem, ls has no option to list onlydirectories.
2. Changing file permissions: the relative and absolute permissions changing methods.
File Ownership
When you create a file, you become its owner. Every owner is attached to a group owner.
Several users may belong to a single group, but the privileges of the group are set by the owner
of the fileand not by the group members.
When the system administrator creates a user account, he has to assign these parameters to the
user:
File Permissions
UNIX follows a three-tiered file protection system that determines a file‘s access rights.
It is displayed in the following format: Filetype owner (rwx) groupowner (rwx) others (rwx).
For Example:
-rwxr-xr- - 1 kumar metal 20500 may 10 19:21 chap02
rwx r-x r- -
owner/user group owner others
The first group(rwx) has all three permissions. The file is readable, writable and executable by the
owner ofthe file.
The second group(r-x) has a hyphen in the middle slot, which indicates the absence of write
permission bythe group owner of the file.
The third group(r- -) has the write and execute bits absent. This set of permissions is applicable to
others.
You can set different permissions for the three categories of users – owner, group and others.
A file or a directory is created with a default set of permissions, which can be determined by
umask.
Let us assume that the file permission for the created file is -rw-r-- r--. Using chmod
command, we canchange the file permissions and allow the owner to execute his file.
Relative Permissions
chmod only changes the permissions specified in the command line and leaves the other
permissionsunchanged.
Its syntax is:
chmod category operation permission filename(s)
Initially,
-rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart
The command assigns (+) execute (x) permission to the user (u), other permissions remain
unchanged.
$chmod ugo+x xstart or chmod a+x xstart or chmod +x xstart
$ls –l xstart
Let initially,
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart
$chmod go-r xstart
Then, it becomes
$ls –l xstart
-rwx—x--x 1 kumar metal 1906 sep 23:38 xstart
Absolute Permissions
Here, we need not to know the current file permissions. We can set all nine permissions
explicitly.
A string of three octal digits is used as an expression.
The permission can be represented by one octal digit for each category. For each
category, we addoctal digits.
If we represent the permissions of each category by one octal digit, this is how the
permission can berepresented:
Read permission – 4 (octal 100)
Write permission – 2 (octal 010)
Execute permission – 1 (octal 001)
Octal Permissions Significance
0 --- no permissions
1 --x execute only
2 -w- write only
3 -wx write and execute
4 r-- read only
5 r-x read and execute
We have three categories and three permissions for each category, so three octal digits can
describe a file‘s permissions completely. The most significant digit represents user and the least
one represents others. chmod can use this three-digit string as the expression.
Using relative permission, we have,
$chmod a+rw xstart
Using absolute permission, we have,
$chmod 666 xstart
-rw-rw-rw- 1 kumar metal 1906 sep 10 23:38 xstart
will assign all permissions to the owner, read and write permissions for the group and only
execute permission to the others.
777 signify all permissions for all categories, but still we can prevent a file from being deleted.
000 signifies absence of all permissions for all categories, but still we can delete a file.
It is the directory permissions that determine whether a file can be deleted or not.
Only owner can change the file permissions. User cannot change other user‘s file‘s permissions.
This is simply useless but still the user can delete this file.
The UNIX system by default, never allows this situation as you can never have a secure system.
Hence,directory permissions also play a very vital role here .
4. Directory Permissions
It is possible that a file cannot be accessed even though it has read permission, and can be
removed even when it is write protected. The default permissions of a directory are,
rwxr-xr-x (755)
Example:
$mkdir c_progs
$ls –ld c_progs
drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs
If a directory has write permission for group and others also, be assured that every user can
remove every file in the directory. As a rule, you must not make directories universally writable
unless you have definitereasons to do so.
Usually, on BSD and AT&T systems, there are two commands meant to change the ownership of
a file ordirectory. Let kumar be the owner and metal be the group owner. If sharma copies a file
of kumar, then sharma will become its owner and he can manipulate the attributes.
chown changing file owner and chgrp changing group owner
chown
Once ownership of the file has been given away to sharma, the user file permissions that
previously applied to Kumar now apply to sharma. Thus, Kumar can no longer edit note since
there is no write privilege for group and others. He cannot get back the ownership either. But he
can copy the file to his own directory, in which case he becomes the owner of the copy.
chgrp
This command changes the file‘s group owner. No super user permission is required.
#ls –l dept.lst
-rw-r—r-- 1 kumar metal 139 jun 8 16:43 dept.lst
THE SHELL
⮚ The shell sits between you and the operating system, acting as a command interpreter.
⮚ It reads your terminal input and translates the commands into actions taken by the system.
⮚ When you log into the system you are given a default shell.
⮚ When the shell starts up it reads its startup files and may set environment variables,
command search paths, and command aliases, and executes any commands specified in
these files.
⮚ The original shell was the Bourne shell, sh. Every Unix platform will either have the
Bourneshell, or a Bourne compatible shell available.
⮚ Even though the shell appears not to be doing anything meaningful when there is no
activity at the terminal, it swings into action the moment you key in something.
The following activities are typically performed by the shell in its interpretive cycle:
⮚ The shell issues the prompt and waits for you to enter a command.
⮚ After a command is entered, the shell scans the command line for meta characters and
expands abbreviations (like the * in rm *) to recreate a simplified command line.
⮚ It then passes on the command line to the kernel for execution.
⮚ The shell waits for the command to complete and normally can’t do any work while
thecommand is running.
⮚ After the command execution is complete, the prompt reappears and the shell returns to
its waiting role to start the next cycle. You are free to enter another
command.
1. Wild-Cards
⮚ A pattern is framed using ordinary characters and a meta character (like *) using
well-defined rules.
⮚ The pattern can then be used as an argument to the command, and the shell will expand
itsuitably before the command is executed.
⮚ The meta characters that are used to construct the generalized pattern for matching
filenamesbelong to a category called wild-cards.
Wild-card Matches
* Any number of characters including none
? A single character
[ijk] A single character – either an i, j or k
[x-z] A single character that is within the ASCII range of characters x and z
[!ijk] A single character that is not an i, j or k (Not in C shell)
[!x-z] A single character that is not within the ASCII range of the characters x and z (Not in C
Shell)
{pat1,pat2...} Pat1, pat2, etc. (Not in Bourne shell)
The * and ?
⮚ The metacharacter *, is one of the characters of the shell’s wild card set.
⮚ It matches any number of characters (including none).
⮚ To list all files that begin with chap.
$ ls chap*
chap chap01 chap02 chap03 chap04 chap15 chap16 chap17 chapx chapy chapz
⮚ chap* matches the string chap. When the shell encounters this command line, it identifies the *
immediately as a wild card.
⮚ It then looks in the current directory and recreates the command line as below from the
filenames that match the pattern chap*:
ls chap chap01 chap02 chap03 chap04 chap15 chap16 chap17 chapx chapy chapz
⮚ Both * and ? operate with some restrictions. for example, the * doesn’t match all
filesbeginning with a . (dot) or the / of a pathname.
⮚ If you wish to list all hidden filenames in your directory having at least three characters
afterthe dot, the dot must be matched explicitly.
$ ls .???*
.bash_profile .exrc .netscape .profile
⮚ However, if the filename contains a dot anywhere but at the beginning, it need not be
matched explicitly.
⮚ Similarly, these characters don’t match the / in a pathname. So, you cannot use, $cd
/usr?local to change to /usr/local.
⮚ The character class comprises a set of characters enclosed by the rectangular brackets, [ and ],
butit matches a single character in the class.
⮚ The pattern [abd] is character class, and it matches a single character – an a,b or d.
Examples:
$ ls chap0[124]
chap01 chap02 chap04
$ls chap[x-z]
chapx chapy chapz
*.[!co] - To match all filenames with a single-character extension but not the .c or .o files
[!a-zA-Z]* - To match all filenames that don’t begin with an alphabetic character
⮚ A filename can contain a whitespace character also. Hence to remove a file named
MyDocumend.doc, which has a space embedded, a similar reasoning should be
followed:
$rm My\ Document.doc
⮚ Quoting is enclosing the wild-card, or even the entire pattern, within quotes.
Anythingwithin these quotes (barring a few exceptions) are left alone by the shell and
not interpreted.
⮚ When a command argument is enclosed in quotes, the meanings of all enclosed
specialcharacters are turned off.
Examples:
Standard input
The standard input can represent three input sources:
Standard error:
⮚ A file is opened by referring to its pathname, but subsequent read and write
operationsidentify the file by a unique number called a file descriptor.
⮚ The kernel maintains a table of file descriptors for every process running in the system.
⮚ The first three slots are generally allocated to the three standard streams
as,0 – Standard input
1 – Standard output
2 – Standard error
These descriptors are implicitly prefixed to the redirection symbols.
⮚ The shell can connect these streams using a special operator, the | (pipe) and avoid creation of the
diskfile.
⮚ The pipe is the third source and destination of standard input and standard
outputExamples
$ ls -l | wc –l Displays number of file in current directory
In a pipeline, all programs run simultaneously. A pipe has a built in mechanism to control the flow
of thestream.
Pipe is both being read and written, the reader and writer have to act in unison.
If one operates faster than the other, then the appropriate driver has to readjust the flow.
grep scans its input for a pattern displays lines containing the pattern, the line numbers or
filenameswhere the pattern occurs. The command uses the following syntax:
$grep options pattern filename(s)
grep searches for pattern in one or more filename(s), or the standard input if no filename is specified.
The first argument (except the options) is the pattern and the remaining arguments are filenames.
Examples:
grep silently returns the prompt because no pattern as “president” found in file emp.lst.
$ grep president emp.lst #No quoting necessary here
$ _#No pattern (president) found
when grep is used with multiple filenames, it displays the filenames along with the output.
Option Significance
-i Ignores case for matching
-v Doesn't display lines matching expression
-n Displays line numbers along with lines
-c Displays count of number of occurrences
-l Displays list of filenames only
-e exp Matches multiple patterns
-f filename Takes patterns from file, one per line
-E Treats patterns as an ERE
-F Matches multiple fixed strings
When you look for a name but are not sure of the case, use the -i (ignore) option.
The -v option selects all the lines except those containing the pattern.
It can play an inverse role by selecting lines that does not containing the pattern.
The -n(number) option displays the line numbers containing the pattern, along with thelines.
here, first column displays the line number in emp.lst where pattern is found
With the -e option, you can match the three agarwals by using the grep like this:
$ grep -e “Agarwal” -e “aggarwal” -e “agrawal” emp.lst
2476|anil aggarwal |manager |sales |01/05/59|5000 3564|sudhir
Agarwal |executive |personnel |06/07/47|7500 0110|v. k.
agrawal |g. m. |marketing |31/12/40|9000
You can place all the patterns in a separate file, one pattern per
line.Grep uses -f option to take patterns from a file:
$ grep -f patterns.lst emp.lst
Like the shell's wild-cards which matches similar filenames with a single expression,
grepuses an expression of a different type to match a group of similar patterns.
Unlike shell's wild-cards, grep uses following set of meta-characters to design an
expressionthat matches different patterns.
If an expression uses any of these meta-characters, it is termed as Regular Expression (RE).
The below table shows the BASIC REGULAR EXPRESSION (BRE) character set-
A RE lets you specify a group of characters enclosed within a pair of rectangular brackets, [ ], in
whichcase the match is performed for a single character in the group.
The *
Here, it indicates that the previous character can occur many times, or not at all.
egrep is another alternative to use all the ERE characters without -E option. This
ERE
uses someadditional characters set shown in below table-
Expression Significance
ch+ Matches one or more occurrences of character ch
ch? Matches zero or one occurrence of character ch
exp1 | exp2 Matches exp1 or exp2
GIF | JPEG Matches GIF or JPEG
(x1|x2)x3 Matches x1x3 or x2x3
(hard|soft)ware Matches hardware or software
The + and ?
+ - Matches one or more occurrences of the previous character
? - Matches zero or one occurrence of the previous character.