BCS515 - Unix Module 1
BCS515 - Unix Module 1
Programming
UNIT 1
Introduction
The Operating system
• Operating system allocates memory for the programs and loads the program to the
allocated memory.
• Operating system loads the CPU registers with control information related to the
program.
• The instructions provided in the program are executed by the CPU. The operating
system keeps track of the instruction that was last executed. This enables it to resume a
program if it had to be taken out of the CPU before it completed execution.
• If program need to access the hardware, it makes a call to the operating system rather
attempt to do the job itself.
Eg:-If program needs to read a file on disk, the operating system directs the disk
controller to open the file and make the data available to the program.
• After the program has completed execution the operating system cleans up the memory
and registers and makes them available for the next program.
Page 1
BCS515C-Unix System Programming -Module-1
Programming
1.1 The UNIX operating system:
Page 2
BCS515C-Unix System Programming -Module-1
Programming
As each vendor modified and enhanced Unix to create their own versions ,the original
unix lost its identity as a separate product. There was no Standard version of unix. Hence AT
& T Released SVR$(system V Release 4), a standard unix.
Even before the advent of SVR4, big things were happening in the U.s Defense
Department .They created the first communication network called ARPANET and it used
TCP/IP protocol and they tried to implement TCP/IP on BSD Unix. The incorporation of
TCP/IP into unix and its use as the basis of development were two key factors in the rapid
growth of the internet.
In the mean time ,Microsoft released a new operating system called Windows, which
used GUI(Graphical user interface) that uses mouse rather than complex commands to execute
a job. Windows was a big threat to UNIX, because it was a windowed system. So Unix badly
needed a windowed type interface for its survival hence the Massachusetts Institute of
Technology(MIT) introduced X Window-the first windowing system for unix.
Linus Torvalds-Father of Linux developed Linux. Linux is distributed free under
general public licence, which makes it mandatory for developers and sellers to make the source
code public. Linux is particularly strong in networking and internet features.
The most popular Linux flavors include Red Hat, Caldera, Mandrake, Fedora etc.
1.3 Unix Arcitecture /components
Page 3
BCS515C-Unix System Programming -Module-1
Programming
Division of labor: Kernel and shell
The main concept in the unix architecture is the division of labor between two agencies the
KERNEL and SHELL. The kernel interacts with hardware and shell interacts with user.
The kernel is the core of the operating system- a collection of routines mostly written in
C. It is loaded into memory when the system is booted and communicates directly with the
hardware. User applications (programs) that need to access the hardware, uses the services of
kernel. These programs access the kernel through a set of function called system calls.
Apart from providing support to user programs, the kernel also performs other tasks like
managing system’s memory, scheduling processes, decides their priorities etc. So the kernel is
often called the operating system- a program’s gateway to the computer’s resource.
The shell is the command interpreter, it translates command into action. It is the interface
between user and kernel. System contains only one kernel, but there may be several shells.
When user enters a command through the keyboard the shell thoroughly examines
keyboard input for special characters, if it finds any, it rebuilds simplified command line and
finally communicates with kernel to see that the command is executed.For eg,consider a echo
command which has lots of spaces between the arguments:
Eg: $echo Sun solaris
In the above example shell first rebuilds the command line i.e. it will compress all extra
spaces, then it will produces output.
Sun solaris
Page 4
BCS515C-Unix System Programming -Module-1
Programming
The system calls:
Unix system’s kernel, shell and applications are written in C. There are several
commands in the unix system, they uses system call functions to communicate with kernel.
Eg: Unix command writes into the file using write system call.Open system call can
be used to open the file.
Multi-user system:
Unix is a multi-user system i.e. multiple user can use the system at a time,
resources are shared between all users. In unix systems, computer breaks up a unit of
time into several segments. So at any point in time, the machine will be doing the job
of a single user. The moment the allocated time expires, the previous job will be
preempted and next user’s job is taken up. This process goes on until clock has turned
full circle and the first user’s job is taken up once again.Unix is a multi-programming
system, it permits multiple programs to run. This can happen in two ways:
Multi-tasking system:
A single user can run multiple tasks concurrently, in multitasking environment
a user sees one job running in the foreground, the rest running in the background. It is
possible to switch the job between background and foreground, suspend or even
terminate them.
Page 5
BCS515C-Unix System Programming -Module-1
Programming
Unix tool kit:
Unix contains set of tools like general purpose tools, text manipulation utilities
(called filters), compilers, interpreters, networked application and system
administration tools.
Pattern matching:
Unix contains pattern matching features, using this features it is possible to
match the different strings.
Eg: ‘*’
$ls chap*
Here ‘*’ is the special character, which matches the filename, which starts
with ‘chap’.
Programming facility:
Unix shell is also a programming language. It has control structures, variables,
loops that establish it as a powerful programming language. This features can be used
to design a shell scripts.
Documentation:
Unix contains ‘man’ pages for each command, it contains references for
commands and their configuration files.Apart from man page, we can even get the
command information in internet.there are several newsgroups on unix where we can
fire our queries to get the solution to a problem.
1.5 Unix Environments
Unix can be used in 3 different environments:
1) Personal Environment
2) Time sharing environment
3) Client-server environment
Personal environment : Even though unix is a multi user operating system ,it can be
installed in personal computers. This is called as personal environment.
Page 6
BCS515C-Unix System Programming -Module-1
Programming
Time sharing environment :
Employees in large companies often work in so called time sharing environment.In a time
sharing environment many users are connected to one or more computers.Their terminals are
often nonprogrammable.also in a time sharing environment the output devices and storage
devices are shared by all users .A typical time sharing environment has been shown shown in
above figure.
In time sharing environment,all of the computing must be done by the central
computer.centeral computer has many duties.It must control the shared resources .I t must
manage the shared data and printing and it must also do the printing. All the workload will be
given to central computer.Hence the system becomes slow.
Page 7
BCS515C-Unix System Programming -Module-1
Programming
Client-server Environment
server
clients
The client –server environment
A client server computing environment splits the computing function between a central
computer called server and users computers.In client –server environment users
microcomputers or workstations are called as clients.The client is a one which requests for
service. The central computer which is a powerful microcomputer,minicomputer or
supercomputer is called a server.Server is a one serves the request of a client.The users are
given personal computers or workstations so that some of the computations can be moved off
to clients.Here workload will be shared between server and clients and hence the system
becomes fast.
Page 8
BCS515C-Unix System Programming -Module-1
Programming
Unix Structure:
Note: For Diagram refer the Unix Architecture.
The main components of the Unix structure are:
1. Kernel.
2. Shell.
3. Application.
1. Kernel:
The kernel provides a bridge between the hardware and the user. It is a software
application that is central to the operating system. The kernel handles the files, memory,
devices, processes and the network for the operating system. It is the responsibility of
the kernel to make sure all the system and user tasks are performed correctly.
2. Shell:
The program between the user and the kernel is known as the shell. It translates the
many commands that are typed into the terminal session. These commands are known
as the shell script. There are two major types of shells in Unix. These are Bourne shell
and C Shell. The Bourne shell is the default shell for version 7 Unix.
The character $ is the default prompt for the Bourne shell. The C shell is a command
processor that is run in a text window. The character % is the default prompt for the C
shell.
3. Application:
The applications and utility layer in Unix includes the word processors, graphics
programs, database management programs, commands etc. The application programs
provide an application to the end users.
For example, a web browser is used to find information while gaming software is used
to play games. The requests for service and application communication systems used
in an application by a programmer is known as an application program interface (API).
Page 9
BCS515C-Unix System Programming -Module-1
Programming
• X/OPEN (open group) developed standard for UNIX i.e. X/OPEN portability guide
(XPG).
• In 2001, X/OPEN and IEEE unified these two standards and called it as SUSV3- Single
Unix
• Specification Version 3. This is based on “write once, adopt anywhere” approach, i.e.
once software has been developed on any POSIX compliant UNIX system, it can be
easily ported to another POSIX compliant UNIX system with minimum modification.
For ex:
$ ls –x chap*
In the above example ls is the command, –x is a option and chap* is a argument. The command
with its arguments and options is known as command line.
The first word in the command line is called as command, all subsequent words are actually
called as arguments. options are also arguments but given a special name because their list is
predetermined. There should be atleast one space to separate command and arguments. multiple
spaces are also allowed but shell compresses them to form a simplified command line.
Options:
There is a special type of argument, that is mostly used with a ‘-‘ sign. Such a
arguments are called options.
Eg: $ls –l
-l : long listing showing seven attributes of a file.
-l is an option to ls command. Options are also arguments but their list is
predetermined. Options can normally be combined with single ‘-‘ sign instead of using
$ls –l –a –t
Page 10
BCS515C-Unix System Programming -Module-1
Programming
i.e.
$ls –lat
File name arguments:
Many unix commands use a filename as argument, so the command can take input
from the file. It is even possible to give multiple filename as argument.
Eg:
$ls –l chap1 chap2
The command with its arguments and options is known as command line.
Exceptions:
There is exceptions to the general syntax of commands. There are commands (pwd)
that don’t accept any arguments. Some commands (EX: who) may or may not take
arguments. Some commands like
‘ls’ can run without arguments(ls), with only options (ls -l), with only filenames (ls chap1
chap2) or can be run with options and filenames (ls –l chap1 chap2).
1.8 Understanding some basic commands
echo: Displaying a message:
echo command can be used to
• To display a message (like echo “cseise”)
• To evaluate shell variables (echo $PAGER)
Originally echo was an external command, but now all shells have echo as built in command.
We can see escape sequence with echo, but we should use –e option.
Some escape sequences are:
\a Bell
\b Backspace
\c No newline
\f Form feed
\n New line
\r Carriage return
\t Tab
\v Vertical tab
\\ Backslash
\0n ASCII character represented octal value n
Page 11
BCS515C-Unix System Programming -Module-1
Programming
Eg:
$printf “Value of 255 is %o in octal and %x in hexadecimal\n” 255 255
Output: Value of 255 is 377 in octal and ff in hexadecimal.
$date
Tue Aug 28 1:58:03 IST 2018
Date displays 6 fields of output:
Page 12
BCS515C-Unix System Programming -Module-1
Programming
1. day 2.Month name 3. Date 4.time 5.time zone 6.year
To extract individual fields we can use following options with date
Options with date:
+%a – day
+%h – Month name
+%d – date
+%T –time
+%Z- Time Zone
+%Y-year
+%H-Hour
+%M- Minute
+%S - Second
Ex:
$ date +%h
Aug
$date +”%h %d “
Aug 28
ls: listing files:
It is possible to list the names of the files in the directory using ls command.
Eg: $ls
README
Chap01
Chap02
Chap03
Chap01
Chap02
Chap03
$ ls a.txt
The above command searches for file a.txt and if it is present it displays it otherwise displays
the message “file not found”.
Options used with ls
Page 13
BCS515C-Unix System Programming -Module-1
Programming
-a : Displays all files including hidden files,current directory(.) and parent
directory(..)
-d dirname : Searches for directory with name dirname,if found displays it,otherwise
produces a
message directory not found.
-i : displays file with inode number
-l : displays all files with seven attributes such as permissions,links,owner name
,group
name,size in bytes,date and time ,file name.
-r : displays files in reverse alphabetical order
-R : Displays recursive list of files
-t : Sorts file based on last modified time
-u : sorts files based on last access time
-x : multi columnar output
Unix has special symbols called meta-characters for creating and storing information in files.
‘>’ symbol can be used to save the information in the file.
6 6 42 list.txt
Output shows that the file ‘list’ contains 6lines, 6words and 42 characters.
It is possible to give output of one command as the input to the another command ‘|’
(pipe) it connects two commands to create pipeline.
Eg: $ls | wc
Unix maintains an account of all users, who are logged on to the system. ‘who’ command
displays listing
of these users.
$who
abc pts/1 Jan 1 20:25 (:0)
(heavens.co
xyz pts/10 Jan 1 14:49 m)
Page 14
BCS515C-Unix System Programming -Module-1
Programming
First column shows the username, second shows device name of user’s terminal, third, fourth,
fifth shows
date and time of logging in. User can login remotely to a unix system. Last column shows the
machine name from where user logged in. (:0) indicates user logged in from his own terminal.
To display the
-H : header.
-u : detailed list
Eg: $who –Hu
NAM COMMENT
E LINE TIME IDLE PID S
abc pts/1 Jan 13 07:51 0:48 11040 (:0)
xyz pts/2 Jan 13 07:56. 11052 heavens.com
First column shows name of the user, second shows terminal name, 3rd,4th,5th shows date and
time. 6th column shows system’s IDLE timing, i.e. from how long system is idle. A ‘.’ Against
xyz shows that activity has occurred in the last minute before the command was invoked.
‘abc’ seems to idling for the last 48minutes. The PID is the process ID, a number that
uniquely identifies a process. We can use the argument ‘am’’i’ with who to know who logged
in the system.
Eg: $who am i
abc pts/1 Jan 13 07:51 (:0)
passwd: changing password:
Eg: $passwd
Passwd: changing password for xyz
Enter login password: ******
New password: ***********
Re-enter new password: ***********
Passwd (SYSTEM) :passwd successfully changed for xyz
When user enters a password, the string is encrypted by the system. Encryption generates
string of random characters and stores in the file /etc/shadow
Page 15
BCS515C-Unix System Programming -Module-1
Programming
Change password regularly.
Syntax:
$cal [[month] year]
January 2013
Su Mo Tu We ThFr Sa
1 2 3 4 5
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
We can use cal with pager more or
less.
Eg:
$ (cal 2003; cal 2006; cal 2007) | more
We can use cal command with arguments.
Eg:
$cal 03 2006
Displays calendar of march 2006.
When ‘cal’ is used with arguments month is optional but year is mandatory. A single
argument to cal is interpreted as year.
Eg: $cal 2003
Displays the calendar of 2003
Combining commands:
Unix allows to specify more than one command in the command line. Each command
has to be separated from the other by a ; (semi-colon).
Page 16
BCS515C-Unix System Programming -Module-1
Programming
1.9 Meaning of Internal and External commands
Eg: $ls
ls is /bin/ls
‘ls’ command having an independent existence in the /bin directory (or /usr/bin). So
‘ls’ is a external command.
Internal commands doesn’t have independent existence, they are shell built-ins.
Shell is an external command with difference, i.e. shell possesses its own set of internal
commands. If a command exists both as an internal command of the shell as well as an external
one, the shell will give top priority to its internal command.
Eg: echo command, which is also found in /bin directory but rarely ever executed,
because the shell makes sure that the internal echo command takes precedence over the
external.
Command arguments can take the form of an expression (in grep), a set of
instructions (in sed), or a program (in awk and perl).
1. 10 The type command: knowing the type of a command and locating it.
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.
Synatx:
type [options] command names
Page 17
BCS515C-Unix System Programming -Module-1
Programming
Type command options:
-a: This option is used to find out whether it is an alias, keyword or function and it also
displays the path ofan executable. If available.
-t: This option will display a single word as an output. alias – if command is a shell alias
1. keyword – if command is a shell reserved word
2. builtin – if command is a shell builtin
3. function – if command is a shell function
4. file – if command is a disk file
Ex: type -t pwd
type -t cp
type -t ls
type -t while
Login : root
Password : ******[Enter}
#:
The prompt of root is #,unlike the $ or % used by nonprivileged users.Once you login as root,
you are placed in roots home directory.Depending on the system this directory could be / or
/root.roots PATH list is also different from the one used by other users:
Page 18
BCS515C-Unix System Programming -Module-1
Programming
su: Becoming the superuser
Any user can acquire superuser status with the su command if he/she knows the root password.
For example ,the user Juliet becomes a superuser in this way :
$ su
Password: *********
# pwd
/home/Juliet
Though the current directory doesn’t change ,the # prompt indicates that Juliet now has powers
of a superuser.To be in root’s home directory on superuser login use su –l.
Su – henry
This sequence executes henry’s profile and temporarily creates henry’s environment.This mode
can be terminated by hitting [ctrl-d] or using exit.
UNIX FILES
Introduction:
Files are the building blocks of any operating system. When you execute a command in UNIX,
the UNIX kernel fetches the corresponding executable file from a file system, loads its
instruction text to memory, and creates a process to execute the command on your behalf. In the
course of execution, a process may read from or write to files. All these operations involve files.
Thus, the design of an operating system always begins with an efficient file management
system.
Naming Files:
A filename can consists of upto 255 characters. Files may or may not have extensions, and can
cosists of practically any ASCII character except the / and the NULL character(ASCII value 0).
We are permitted to use control characters or other unprintable characters in a filename. The
following are the valid filenames in UNIX:
• .last_time
• list.
• ^V^B^D-++bcd
• -{}[]
• @#$%*abcd
• a.b.c.d.e
Page 19
BCS515C-Unix System Programming -Module-1
Programming
The third filename contains three control characters. These characters shoud be definitely be
avoided in framing filenames. Moreover, since the UNIX system has a special treatment for
characters like $, `, ?, *, & among others, it is recommended that only the following characters
be used in filenames.
2. Binary File:
A binary file, on the other hand, contains both printable and unprintable
characters that cover the entire ASCII range (0 to 255). Most UNIX commands are binary files,
and the object code and executables that you produce by compiling C programs are also binary
files. Picture, sound and video files are binary files as well. Displaying such files with a simple
cat command produces unreadable output and may even disturb your terminal's settings.
2. Directory File:
A directory contains no data, but keeps some details of the files and subdirectories that
it contains The UNIX file system is organized with a number of directories and subdirectories,
and you can also create them as and when you need. You often need to do that
Page 20
BCS515C-Unix System Programming -Module-1
Programming
to group a set of files pertaining to a specific application. This allows two or more files in
separate directories to have the same filename.
A directory file contains an entry for every file and subdirectory 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 the inode number),
If a directory bar contains an entry for a file foo, we commonly (and loosely) say that the
directory bar contains the file foo Though we'll often be using the phrase "contains the file
rather than "contains the filename, you must not interpret the statement literally A directory
contains the filename and not the file's contents.
3. Device File:
You'll also be printing files, installing software from CD-ROMs or backing up files to
tape. All of these activities are performed by reading or writing the file representing the device.
For instance, when you restore files from tape you read the file associated with the tape drive.
It is advantageous to treat devices as files as some of the commands used to access an ordinary
file also work with device files.
Device file names are generally found inside a single directory structure, /dev. A device file is
indeed special, it's not really a stream of characters. In fact, it doesn't contain anything at all.
Files in Unix System are organized into multi-level hierarchy structure known as a
directory tree. At the very top of the file system is a directory called “root” which is represented
by a “/”. All other files are “descendants” of root.
Page 21
BCS515C-Unix System Programming -Module-1
Programming
Directories or Files and their description:
• / : The slash / character alone denotes the root of the filesystem tree.
• /bin : Stands for “binaries” and contains certain fundamental utilities, such as ls or cp,
which are generally needed by all users.
• /boot : Contains all the files that are required for successful booting process.
• /dev : Stands for “devices”. Contains file representations of peripheral devices and
pseudo-devices.
• /etc : Contains system-wide configuration files and system databases. Originally also
contained “dangerous maintenance utilities” such as init,but these have typically been
moved to /sbin or elsewhere.
• /home : Contains the home directories for the users.
• /lib : Contains system libraries, and some critical files such as kernel modules or device
drivers.
• /media : Default mount point for removable devices, such as USB sticks, media players,
etc.
• /mnt : Stands for “mount”. Contains filesystem mount points. These are used, for
example, if the system uses multiple hard disks or hard disk partitions. It is also often used
for remote (network) filesystems, CD-ROM/DVD drives, and so on.
• /proc : procfs virtual filesystem showing information about processes as files.
• /root : The home directory for the superuser “root” – that is, the system administrator.
This account’s home directory is usually on the initial filesystem, and hence not in
/home (which may be a mount point for another filesystem) in case specific maintenance
needs to be performed, during which other filesystems are not available. Such a case
could occur, for example, if a hard disk drive suffers physical failures and cannot be
properly mounted.
• /tmp : A place for temporary files. Many systems clear this directory upon startup; it
might have tmpfs mounted atop it, in which case its contents do not survive a reboot, or
it might be explicitly cleared by a startup script at boot time.
• /usr : Originally the directory holding user home directories,its use has changed. It now
holds executables, libraries, and shared resources that are not system critical, like the X
Window System, KDE, Perl, etc. However, on some Unix systems, some user accounts
may still have a home directory that is a direct subdirectory of /usr, such as the default as
in Minix. (on modern systems, these user accounts are often related to server or system
use, and not directly used by a person).
• /usr/bin : This directory stores all binary programs distributed with the operating system
not residing in /bin, /sbin or (rarely) /etc.
• /usr/include : Stores the development headers used throughout the system. Header files
are mostly used by the #include directive in C/C++ programming language.
• /usr/lib : Stores the required libraries and data files for programs stored within /usr or
elsewhere.
• /var : A short for “variable.” A place for files that may change often – especially in size,
for example e-mail sent to users on the system, or process-ID lock files.
• /var/log : Contains system log files.
Page 22
BCS515C-Unix System Programming -Module-1
Programming
• /var/mail : The place where all the incoming mails are stored. Users (other than root)
can access their own mail only. Often, this directory is a symbolic link to
/var/spool/mail.
• /var/spool : Spool directory. Contains print jobs, mail spools and other queued tasks.
• /var/tmp : A place for temporary files which should be preserved between system
reboots.
Hidden Files:
To show all the hidden files in the directory, use ‘-a option’. Hidden files in Unix starts
with ‘.’ in its file name.It will show all the files including the ‘.’ (current directory) and ‘..’
(parent directory).
ls –a
ls –A: To show the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent directory).
Page 23
BCS515C-Unix System Programming -Module-1
Programming
• Top is represented by a /(front slash).
• Root directory(/) has several sub directories under it.
• These subdirectories in turn have more sub directories and other files under them.
o Ex: bin and usr are two directories directly under /, while cp and pwd are
subdirectories under bin.
• Every file must have a parent and it should be possible to trace the ultimate parentage
of a file to root.
• Thus, the home directory in the above figure is the parent for mthomas, while / is the
parent of home and the grand parent of mthomas.
• In the parent-child relationship, the parent is always a directory.
o Ex: login.sql is an ordinary file, it cannot have a directory under it.
Directory Commands:
1. pwd: Checking your current directory
• User can move around from one directory to another, but at any point of time, if
user wants to find out in which directory he is present then user can use Print
Working Directory(pwd) command.
Ex: pwd
/home/kumar/SantoshReddyP
Page 24
BCS515C-Unix System Programming -Module-1
Programming
$ pwd
/home/kumar/progs
• Here we are using the relative pathname of progs directory. The same can be done
with the absolute pathname also.
$cd /home/kumar/progs
$ pwd
/home/kumar/progs
$cd /bin
$ pwd
/bin
• cd can also be used without arguments:
$ pwd
/home/kumar/progs
$cd
$ pwd
/home/kumar
• cd without argument changes the working directory to home directory.
$cd /home/sharma
$ pwd
/home/sharma
$cd
/home/kumar
Page 25
BCS515C-Unix System Programming -Module-1
Programming
4. rmdir: Removing a Directory
• rmdir command removes the directory.
• The directory must be empty before using rmdir with the directory name.
o Ex: rmdir pis
• Multiple directories can be removed.
o Ex: $rmdir pis/data pis/progs pis
• Here first subdirectories are removed and at last pis is removed.
• A subdirectory cannot be removed unless the user is placed in a directory which is
hierarchically above that directory.
o Ex:$pwd
/home/kumar/pis/progs
$cd /home/kumar/pis
/home/kumar/pis
$pwd
/home/kumar/pis
$rmdir progs
• rmdir : Things to remember
o You can‘t remove a directory which is not empty
o You can‘t remove a directory which doesn‘t exist in system.
o You can‘t remove a directory if you don‘t have permission to do so.
Page 26
BCS515C-Unix System Programming -Module-1
Programming
/home/kumar/progs
$cd ../..
$pwd
/home
• The following command copies the file prog1.java present in javaprogs, which is
present is parent of current directory to current directory.
$pwd
/home/kumar/progs/cprogs
$cp ../javaprogs/prog1.java .
• Now prog1.java is copied to cprogs under progs directory.
cat options:
o Displaying Nonprinting Characters (-v)
cat without any option it will display text files. Nonprinting ASCII
characters can be displayed with –v option.
o Numbering Lines (-n)
-n option numbers lines. This numbering option helps programmer in
debugging programs.
Page 27
BCS515C-Unix System Programming -Module-1
Programming
$_
o 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
2. cp: Copying a file
o 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.
o When both are ordinary files, first file is copied to second.
$ cp csa csb
o 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.
o Example to show two ways of copying files to the cs directory:
$ cp ch1 cs/module1 ch1 copied to module1 under cs
$ cp ch1 cs ch1 retains its name under cs
o cp can also be used with the shorthand notation, .(dot), to signify the current
directory as the destination. To copy a file „new‟ from /home/user1 to your
current directory, use the following command:
$cp /home/user1/new new destination is a file
$cp /home/user1/new . destination is the current directory
o 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.
Ex: To copy the file ch1,chh2,ch3 to the module , use cp as
$ cp ch1 ch2 ch3 module
o 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 directory should
already exist and cp doesn‘t able create a directory.
o UNIX system uses * as a shorthand for multiple filenames.
Ex:
$ cp ch* usp Copies all the files beginning with ch
cp options
o 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
o A y at this prompt overwrites the file, any other response leaves it uncopied.
Page 28
BCS515C-Unix System Programming -Module-1
Programming
Copying directory structure (-R) :
o It performs recursive behavior command can descend a directory and examine
all files in its subdirectories.
o -R : behaves recursively to copy an entire directory structure
rm options
• Interactive Deletion (-i) : Ask the user confirmation before removing each file:
$ rm -i ch1 ch2 rm: remove ch1 (yes/no)? ? y rm: remove ch1 (yes/no)? ? n
[Enter]
• 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.
Page 29
BCS515C-Unix System Programming -Module-1
Programming
• It doesn't create a copy of the file; it merely renames it. No additional space is
consumed on disk during renaming.
Ex: 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.
Ex: Moves three files ch1,ch2,ch3 to the directory module
Page 30
BCS515C-Unix System Programming -Module-1
Programming
$ 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 ASCII octal
representation
$ od –bc file
Page 31