Introduction to UNIX Programming
Introduction to UNIX Programming
MODULE – 1
INTRODUCTION
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.
After the program has completed execution the operating system cleans up the
memory and registers and makes them available for the next program.
Modern operating system are MULTI-PROGRAMMING. They allow multiple
programs to be in memory.
The operating system creates a process for each program and then control the
switching of these processes. Eg for operating system: - DOS, WINDOWS, UNIX
etc.
Users can interact with Unix system through a command interpreter called shell. It
achieves unusual tasks with a few keystrokes.
Unix operating system doesn’t tell whether user is right or wrong and doesn’t warn the
consequences of their actions.
Page | 1
UNIX PROGRAMMING (18CS56) MODULE - 1
Unix is actually a very old operating system, until Unix came on the scene, operating
systems were designed with a particular machine in mind. They were written in low
level language. Programs designed for one system simply wouldn’t run on another, so
two persons named Ken Thompson and Dennis Ritchie, of AT & T, in 1969, started to
build a flexible operating system that would run on any hardware. they designed and
built an elegant file system, command interpreter(shell) and a set of utilities. But this
system was once again hardware dependent.
In 1973, Dennis Ritchie rewrote the entire system in C –a high level language and
it was portable. This was the first version of Unix.
The University of California Berkley(UCB), created a Unix of its own. They called
it BSD UNIX (Berkley software distribution). These versions became quite popular
worldwide. BSD Unix had a standard editor of the Unix system(vi) and a popular shell (c
shell). Berkley also created a better file system, a more versatile mail feature and a better
method of linking files. Later they also offered with their standard distribution a
networking protocol software(TCP/IP) that made the internet possible.
Sun used the BSD Unix as a foundation for developing their own brand of Unix
called SunOS. Today their version of UNIX is known as solaries. Others had their own
brands such as IBM had AIX, HP offered HP-UX.
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.
Page | 2
UNIX PROGRAMMING (18CS56) MODULE - 1
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
Page | 3
UNIX PROGRAMMING (18CS56) MODULE - 1
simplified command line and finally communicates with kernel to see that the
command is executed.
Two simple entities support the Unix system is the file and process.
A file is just an array of bytes and can contain virtually anything. It is also related
to another file by being part of a single hierarchical structure. So it is necessary
to locate a file within reference to a pre-determined place. Unix considers even
directories and devices as files. Files will be arranged in a hierarchical structure
in Unix system.
Unix is written in C. Though there are a thousand of commands in the system, they all
use a handful of functions called System Calls, to communicate with the kernel. All
versions of Unix use the same system calls.
FEATURES OF UNIX
Unix is an operating system, so it has all the features an operating system is expected
to have. The following sections present the major features of this operating system.
In Unix, the resources are actually shared between all users, therefore Unix is also a
multiuser system. Multiuser technology is the great socializer that has time for
everyone.
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.
Page | 4
UNIX PROGRAMMING (18CS56) MODULE - 1
5. Pattern Matching
Unix features sophisticated pattern matching features. The “*” is a special
character used by the system to indicate that it can match a number of
filenames. Apart from the *, there are several other special characters used in
Unix system. Unix features elaborate pattern matching schemes that use
several characters from this metacharacter set.
6. Programming Facility
The Unix shell is also a programming language. It has all the features of a
programming language such as control structures, loops and variables. These
features are used to design shell scripts.
Page | 5
UNIX PROGRAMMING (18CS56) MODULE - 1
7. Documentation
To know about Unix in detail there exist many online help facilities like man
command. It is the most important reference for commands and their
configuration files. The reference manuals are available online.
Example: $man cp
UNIX Environments
1. Personal Environment
2. Time sharing environment
3. Client-server environment
Personal Environment
Page | 6
UNIX PROGRAMMING (18CS56) MODULE - 1
All of this work tends to keep the central computer busy so, user has to wait more time
for get done their work so, it is nonproductive because of slow response.
Client-Server Environment
UNIX STRUCTURE
UNIX consists of four major components: The Kernel, The Shell, a standard set of
utilities and Application programs. These components are shown in the figure:
Page | 7
UNIX PROGRAMMING (18CS56) MODULE - 1
The kernel:
All the other components call on the kernel to perform these services for them.
The Shell:
To do any operations in the system, user must give the Shell a command. If the
command requires a utility, the shell requests that the kernel execute the utility.
There are two major parts of the shell. The first is Interpreter; the second part of
the shell is a programming capability that allows you to write a shell script.
Utilities:
A Utility is a Standard UNIX program that provides a support process for users.
There are literally hundreds of UNIX Utilities. Three common utilities are text
editors, search programs and sort programs.
For example: vi (text editor), the list (ls) utility display the files that reside on the
disk.
Applications:
Page | 8
UNIX PROGRAMMING (18CS56) MODULE - 1
Many standard utilities standard out as application years ago and proved so
useful that they are now part of the system.
POSIX- Portable Operating System standard for Computer Environment (specially for
UNIX) is the group of standards developed by IEEE for operating system.
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.
Command Structure:
Verb:
The Verb is the command name. The command indicates what action is
to taken.
Command name and options/arguments have to separate by spaces or
tabs to enable the system to interpret them as words.
Single/Multiple spaces can be given to separate the commands and
options/arguments.
The shell compresses these multiple spaces to a single space.
Page | 9
UNIX PROGRAMMING (18CS56) MODULE - 1
Options:
The options modify how the action is applied.
Options are usually one character preceded by a minus sign (-) or plus sign
(+).
Arguments:
The Arguments provide additional information to the command.
Some commands may accept single argument; some may accept multiple
arguments or so may not accept any arguments.
Exceptions:
There are 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).
echo command used in shell scripts to display messages on the terminal, or to issue
prompts for taking input from the user. It can also be used to display shell variables.
Originally echo was an external command, but now all shells have echo as built in
command.
Option Purpose
N Do not output the trailing new line
E Enable interpretation of the backslash escaped characters listed
below
E Disables interpretation of those sequences in Strings.
Help Display this help and exit
version Output version information and exit
Page | 10
UNIX PROGRAMMING (18CS56) MODULE - 1
Examples:
1. $echo
2. $echo $SHELL
[Displays the message and suppresses the new line character. To enable
the use of escape sequence in LINUX, the –e option must be used.]
printf can be used as alternative command to echo. The difference is echo command by
default inserts ‘\n’ character at the end, but printf does not insert newline at the end.
printf also accept all escape sequence used by echo. printf also uses formatted strings in
the same way the C language function of the same name.
Page | 11
UNIX PROGRAMMING (18CS56) MODULE - 1
Example:
%s String
%d Decimal integer
%o Octal integer
%x Hexadecimal integer
Example:
Page | 12
UNIX PROGRAMMING (18CS56) MODULE - 1
Example:
$ls
Output: [It displays all the files present in the current directory]
$ls p*
$ls –i
$ls –l
Output: [Displays all files with seven attributes such as permissions,links,owner name
,group name,size in bytes,date and time ,file name.]
Page | 13
UNIX PROGRAMMING (18CS56) MODULE - 1
Unix maintains an account of all users, who are logged on to the system. The ‘who’
command displays an informative listing of these users.
Example:
$who
Output:
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 ma chine name from where user logged in. (:0) indicates user
logged in from his own terminal.
$who –Hu
Output:
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.
Page | 14
UNIX PROGRAMMING (18CS56) MODULE - 1
The Unix system maintains an internal clock meant to run perpetually. When the
system is shut down a battery backup keeps the clock ticking. Date command is used to
display both date and time to the nearest second.
Example:
$date
Output:
The command can also be used with suitable format specifiers as arguments, each
format preceded by the + symbol, followed by the % operator, and single character
describing the format.
There are many other format specifiers and the useful ones are listed below:
Page | 15
UNIX PROGRAMMING (18CS56) MODULE - 1
If your account doesn’t have a password or has one that is already known to others, you
should change it immediately. This is done with the passwd command.
$passwd
passwd: changing password for kumar
Enter login password: ******
New password: ***********
Re-enter new password: ***********
passwd (SYSTEM): passwd successfully changed for kumar
passwd expects user to respond three times. First, it prompts for the old password. Next,
it checks whether user have entered a valid password, and if user have, it then prompts
for the new password. Enter the new password using the password naming rules
applicable to user system. Finally, passwd asks user to re-enter the new password. If
everything goes soomthly, the new password is registered by the system.
When user enters a password, the string is encrypted by the system. Encryption
generates string of random characters and stores in a file named shadow in the /etc
directory.
Using cal command it is possible to see the calendar of any specific month or a complete
year. The facility is totally accurate and takes into account the leap year adjustments
that took place in the year 1752.
Example:
$cal
Page | 16
UNIX PROGRAMMING (18CS56) MODULE - 1
When ‘cal’ is used with arguments month is optional but year is mandatory. A single
argument to cal is interpreted as year.
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).
Output will be line, word and character count of the file note and long listing of attributes
of file note.
Output of ‘wc note’ and ‘ls –l’ will be redirected to the file list
Internal Commands: The commands that are directly executed by the shell are known
as Internal commands.
External Commands: The commands that are executed by the kernel are called external
commands.
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.
Page | 17
UNIX PROGRAMMING (18CS56) MODULE - 1
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.
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.
Examples:
-a: This option is used to find out whether it is an alias, keyword or function and it also
displays the path of an executable.
-t: This option will display a single word as an output. alias – if command is a shell alias
Page | 18
UNIX PROGRAMMING (18CS56) MODULE - 1
type -t while
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:
Login: root
Password: ****** [Enter}
#:
The prompt of root is #, unlike the $ or % used by non-privileged users. Once user login
as root, you are placed in roots home directory. Depending on the system this directory
could be / or /[Link] PATH list is also different from the one used by other users:
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.
Creating user’s environment: Users 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, when used with a -, recreates the users’ environment
without taking the login-password route:
su – henry
Page | 19
UNIX PROGRAMMING (18CS56) MODULE - 1
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 user 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.
UNIX Looks at everything as a file and any UNIX System has thousands of files.
The file system in UNIX is one its simple and conceptually clean feature.
UNIX File system is a logical method of organizing and storing large amounts of
information in a way that makes it easy to manage.
The file is a container of storing information. user can treat it simply as a
sequence of characters. If you name a file foo and write three characters a,b,c into
it, then foo will contain only the string abc and nothing else.
UNIX treats directories and devices as files as well. A directory is simply a folder
where you store filenames and other directories.
All physical devices like the hard disk, memory, CD, ROM, Printer and modem are
treated as files.
Naming Files:
A filename can consist of up to 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
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.
Page | 20
UNIX PROGRAMMING (18CS56) MODULE - 1
A file can have as many dots embedded in its name; a.b.c.d.e is a perfectly valid
filename. A filename can also begin with a dot or end with one.
UNIX is case sensitive to case; Chap01, chap01. And CHAP01 are three different
filenames, and it’s possible for them to coexist in the same directory.
Ordinary File:
An ordinary file or regular file is the most common file type. All programs you write
belong to this type. An ordinary file itself can be divided into two types:
a) Text File
b) Binary File
A Text file contains only printable characters, and you can often view the contents and
make sense out of them. 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 line feed (LF). When you press (Enter) while inserting
text, the LF character is appended to every line. You won't see this character normally,
but there is a command (od) which can make it visible.
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.
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 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.
Page | 21
UNIX PROGRAMMING (18CS56) MODULE - 1
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.
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.
Every file has some attributes that are not stored in the file but elsewhere on disk. The
operation of a device is entirely governed by the attributed of its associated file. The
kernel identifies a device from its attributes and then uses them to operate the device.
Unix file system is a logical method of organizing and storing large amounts of
information in a way that makes it easy to manage. A file is a smallest unit in which
the information is stored. Unix file system has several important features. All data in
Unix is organized into files. All files are organized into directories. These directories are
organized into a tree-like structure called the file system.
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 | 22
UNIX PROGRAMMING (18CS56) MODULE - 1
Page | 23
UNIX PROGRAMMING (18CS56) MODULE - 1
• /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.
• /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 | 24
UNIX PROGRAMMING (18CS56) MODULE - 1
Page | 25
UNIX PROGRAMMING (18CS56) MODULE - 1
• The slashes act as a delimiter to the file and directory names except the first /(root).
Example: /usr/staff/joan/file3
Depending on the beginning point from where a path is defined, there are two types of
pathnames.
Example: /usr/staff/joan/file3
RELATIVE PATHNAME: A Relative pathname does not contain / as the first character
and starts from the current working directory.
Example: joan/file3
The command will work only when the directory joan is under the current working
directory and file3 exists in joan directory.
Relative pathname uses either current working directory or the parent directory as
reference, and specifies a path relative to it. A relative pathname uses one of these
symbols:
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 | 26
UNIX PROGRAMMING (18CS56) MODULE - 1
• When used with the argument, it changes the current directory to the
directory specified as argument, progs:
$ pwd
/home/kumar
$cd progs
$ 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
/home/kumar/progs
$cd
$ pwd
/home/kumar
$ pwd
/home/sharma
$cd
/home/kumar
Page | 27
UNIX PROGRAMMING (18CS56) MODULE - 1
• You can create a number of subdirectories with one mkdir command: $mkdir
patch dba doc
• For instance, the following command creates a directory tree:
$mkdir progs progs/cprogs progs/javaprogs
• 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.
$cd /home/kumar/pis
/home/kumar/pis
$pwd
/home/kumar/pis
$rmdir progs
Page | 28
UNIX PROGRAMMING (18CS56) MODULE - 1
/home/kumar/progs/cprogs
$cd /home/kumar
$pwd
/home/kumar
• Navigation becomes easy by using common ancestor.
• . (a single dot) - This represents the current directory
• .. (two dots) - This represents the parent directory
• Assume user is currently placed in /home/kumar/progs/cprogs
$pwd
/home/kumar/progs/cprogs
$cd ..
$pwd
/home/kumar/progs
• This method is compact and easy when ascending the directory hierarchy. The
command cd .. Translates to this ―change your current directory to parent of
current directory‖.
• The relative paths can also be used as: $pwd
/home/kumar/progs
$cd ../..
$pwd
/home
• The following command copies the file [Link] present in javaprogs, which is
present is parent of current directory to current directory.
$pwd
/home/kumar/progs/cprogs
$cp ../javaprogs/[Link] .
• Now [Link] is copied to cprogs under progs directory.
Page | 29
UNIX PROGRAMMING (18CS56) MODULE - 1
$ cat cprogram.c
# include
<stdioh>
void main ()
{
Print(“hello:);
o As like other files cat accepts more than one filename as arguments
$ cat ch1 ch2
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.
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
Page | 30
UNIX PROGRAMMING (18CS56) MODULE - 1
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
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
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:
Page | 31
UNIX PROGRAMMING (18CS56) MODULE - 1
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.
• Forcing Removal (-f): rm prompts for removal if a file is write-protected. The -f
option overrides this minor protection and forces removal.
rm -rf* #Deletes everything in the current directory and below
Page | 32
UNIX PROGRAMMING (18CS56) MODULE - 1
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
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
Page | 33
UNIX PROGRAMMING (18CS56) MODULE - 1
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 | 34