SlideShare a Scribd company logo
18CS56- UNIX PROGRAMMING
Module-1
Introduction about Operating system
 An operating system is system software that manages
computer hardware and software resources and
provides common services for computer programs. It
acts as an interface between the user and the
computer hardware.
 Following are some of important functions of an
operating System.
• Memory Management
• Processor Management
• Device Management
cont..
• File Management
• Security
• Control over system performance
• Job accounting
• Error detecting aids
• Coordination between other software and users
UNIX OS
• Unix is a computer Operating System which is capable
of handling activities from multiple users at the same
time.
• The development of Unix started around 1969 at AT&T
Bell Labs by Ken Thompson and Dennis
• The main components of UNIX OS are kernel and shell.
The computer programs that allocate the system
resources and coordinate all the details of the
computer's internal is called the kernel.
Cont..
• Users communicate with the kernel through a program
known as the shell.
• The shell is a command line interpreter; it translates
commands entered by the user and converts them into
a language that is understood by the kernel.
History of UNIX
• The UNIX operating system was born in the late 1960s. It
originally began as a one man project led by Ken Thompson of
Bell Labs, and has since grown to become the most widely used
operating system.
• In the time since UNIX was first developed, it has gone through
many different generations and even mutations.
– Some differ substantially from the original version, like
Berkeley Software Distribution (BSD) or Linux.
– Others, still contain major portions that are based on the
original source code.
cont..
 There are various Unix variants available in the market.
Solaris Unix, AIX, HP Unix and BSD are a few examples.
Linux is also a flavor of Unix which is freely available.
UNIX vs LINUX
cont..
cont..
UNIX ARCHITECTURE
 The heart of Fig is the hardware. This is surrounded by
the operating system. UNIX architecture comprises of
two major components viz., the shell and the kernel.
Cont..
 The kernel interacts with the machine’s hardware and
the shell with the user.
 The heart of the operating system is often called the
kernel.
 The kernel normally contains essential features such as
the scheduler, file management etc. Users and
programs cannot communicate directly with the kernel
normally.
 However there is a mechanism by which we can
communicate – this is via system calls. Here is a basic
block diagram of a Unix system
Cont..
(i) Division of labor: kernel and shell
Kernel − The kernel is the heart of the operating system.
It is a collection of routines written in C.
 It is loaded into memory when the system is
booted and communicates directly with the
hardware. It interacts with the hardware and most of
the tasks like memory management, task scheduling
and file management.
Shell − The shell performs the role of command
interpreter. When you type in a command at your
terminal, the shell interprets the command and calls
the program that you want.
Cont..
 Even though there’s only one kernel running on the
system, there could be several shells in action, one for
each user who’s logged in.
 The shell uses standard syntax for all commands. C
Shell, Bourne Shell and Korn Shell are the most famous
shells which are available with most of the Unix
variants.
(ii) The File and Process
 All the data of Unix is organized into files. All files are
then organized into directories.
 These directories are further organized into a tree-like
structure called the file system.
Cont..
 A file is an array of bytes that stores information. It is
also related to another file in the sense that both
belong to a single hierarchical directory structure.
 A process is the second abstraction UNIX provides. It
can be treated as a time image of an executable file.
 Like files, processes also belong to a hierarchical
structure.
(iii) System Calls
• User programs that need to access the
hardware use the services of the kernel via use
of system calls.
Cont..
 These are similar to function calls, but remove control
from the user process All UNIX flavors use same system
calls. Eg:
• exit: exits a process
• write: writes to a file
• read: reads from a file
• fork: creates a new process
FEATURES OF UNIX OS
• Several features of UNIX have made it popular. Some
of them are:
• Portable : UNIX can be installed on many hardware
platforms. Its widespread use can be traced to the
decision to develop it using the C language.
• Multiuser: The UNIX design allows multiple users to
concurrently share hardware and software
• Multitasking: UNIX allows a user to run more than one
program at a time. In fact more than one program can
be running in the background while a user is working
foreground.
Cont..
• Networking: While UNIX was developed to be an
interactive, multiuser, multitasking system, networking
is also incorporated into the heart of the operating
system.
• Access to another system uses a standard
communications protocol known as Transmission
Control Protocol/Internet Protocol (TCP/IP).
• Organized File System: UNIX has a very organized file
and directory system that allows users to organize and
maintain files.
Cont..
• Device Independence: UNIX treats input/output
devices like ordinary files. The source or destination for
file input and output is easily controlled through a UNIX
design feature called redirection.
• Building block approach: UNIX uses a building-block
approach in the design of some of its tools and lets you
develop complex command routines by connecting
these tools. For example we can use pipe to connect ls
and wc and count number of files in your directory.
• Programming facilities
Cont..
• UNIX toolkit and documentation.
• Utilities: UNIX provides a rich library of utilities that can
be use to increase user productivity.
POSIX AND SINGLE UNIX SPECIFICATION
• POSIX (Portable Operating System Interface) is a set of
standard operating system interfaces based on
the Unix operating system.
• It is designed for achieving portability. It is a family of
standards specified by the IEEE Computer Society for
maintaining compatibility between operating systems.
• Beginning in 1998, a joint working group known as the
Austin Group began to develop the combined standard
that would be known as the Single UNIX Specification
Version 3 and as POSIX:2001 (formally: IEEE Std 1003.1-
2001).
Cont..
• It was released on January 30, 2002. The main two
interfaces are POSIX.1 and POSIX.2.
• POSIX.1 is the standard for an application program
interface in the C language. POSIX.2 is the
standard shell and utility interface.
• PSIX.4 is another important interface for thread
management.
Cont..
• Single Unix specification: In December 2008, the Austin
Group published a new major revision, known as
POSIX:2008 (formally: IEEE Std 1003.1-2008). This is the
core of the Single UNIX Specification, Version 4.
• The Single UNIX Specification is an industry standard
description of the C language program and user command
interfaces for a standard Unix operating system.
• The “write once, adopt everywhere” approach of this
development ensure that a program developed in one
POSIX compliant Unix operating system would run in a
somewhat different POSIX compliant Unix operating
system.
GENERAL FEATURES OF UNIX COMMANDS/ COMMAND
STRUCTURE
• A command is a program that tells the Unix system to
do something. It has the form:
Command name [options] [arguments]
• where command is the command name that can take a
set of optional options and one or more optional
arguments.
• An argument indicates on what the command is to
perform its action, usually a file or series of files. An
option modifies the command, changing the way it
performs.
Cont..
Commands:
• Commands are case sensitive.
 i.e command and Command are not the same.
 Commands, options and arguments have to be
separated by spaces or tabs to enable the shell to
interpret them as words.
 A contiguous string of spaces and tabs together is
called a whitespace.
Cont..
 The shell compresses multiple occurrences of whitespace into a
single whitespace.
• $ cp file1 file2
(Will copy file 1 to file 2)
 To change the name of a file, use the mv command. Following is
the basic syntax
$ mv old_file new_file
Cont..
Options
• Options are generally preceded by a hyphen (-), and for most
commands, more than one option can be strung together, in the
form:
command -[option][option][option]
• Example: $ ls –l
• There must not be any whitespaces between – and l.
• Options are also arguments, but given a special name because
they are predetermined. Options can be normally combined
with only one – sign. i.e., instead of using
• $ ls –l –a –t we can as well use,
• $ ls –lat
Cont..
Filename Arguments
• Many UNIX commands use a filename as argument so that the
command can take input from the file.
• If a command uses a filename as argument, it will usually be the
last argument, after all options.
Example: cp file1 file2 file3 dest_dir
rm file1 file2 file3
• The command with its options and arguments is known as the
command line, which is considered as complete after [Enter] key
is pressed, so that the entire line is fed to the shell as its input
for interpretation and execution.
Cont..
Exceptions
• Some commands in UNIX like pwd do not take any options and
arguments. Some commands like who may or may not be
specified with arguments.
• The ls command can run without arguments (ls), with only
options (ls –l), with only filenames (ls f1 f2), or using a
combination of both (ls –l f1 f2).
• Some commands compulsorily take options (cut). Some
commands like grep, sed can take an expression as an argument,
or a set of instructions as argument.
Cont..
Cut command:
The cut command extracts a given number of characters or
columns from a file.
Syntax: cut [options] [file]
Example: $ cut -c 5-10 file1
This command will extract characters 5 to 10 from each line.
grep command in Unix
The grep filter searches a file for a particular pattern of characters,
and displays all lines that contain that pattern. The pattern that
is searched in the file is referred to as the regular expression
grep [options] pattern [files]
Cont..
Example: $grep -i "UNix" geekfile.txt
The -i option enables to search for a string case insensitively in the
give file. It matches the words like “UNIX”, “Unix”, “unix”.
Sed Command
SED is a powerful text stream editor. Can do insertion, deletion,
search and replace(substitution).
Syntax:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Example:
$sed 's/unix/linux/' geekfile.txt
Replacing or substituting string : Sed command is mostly used to
replace the text in a file. The below simple sed command
replaces the word “unix” with “linux” in the file.
Understanding of some basic commands such as echo, printf, ls,
who, date, passwd, cal
cal: The calendar
• The cal command is a command line utility for displaying a
calendar in the terminal. It can be used to print a single month,
many months or an entire year. The syntax is::
cal [-mjy] [[month] year]
• Everything within rectangular box is optional.
Tag Description
-m Display monday as the first day of the week.
-j
Display julian dates (days one-based, numbered from
January 1).
-y Display a calendar for the current year.
cont..
To display feb 2015 calendar
$ cal 2 2015
To display complete year calendar.
$ cal –y
date: Displaying the system date
The date command displays the current date and time. It can also
be used to display or calculate a date in a format you specify
The syntax is:
date [OPTION]... [+%FORMAT]
• To Print current system date and time:
$date
output:
Sun Jan 8 21:38:15 IST 2017
cont..
• To print only the month:
$date +%m
output:
08
• To print only the month name:
$date +%h
output:
Aug
cont..
• echo: Displaying a Message
 echo is a fundamental command found in most operating
systems that offer a command line.
 It is frequently used in scripts, batch files, and as part of
individual commands; anywhere you may need to insert text.
 Many command shells such as bash, ksh and csh implement
echo as a built-in command.
Escape sequence used by echo and printf
cont..
cont..
Example-1:
To print string "Hello, World!" on console
$ echo "Hello, World!"
output:
Hello, World!
Example-2:
To print value of x, where x=10.
$ echo “$x”
output:
10
Example-3:
Use option ‘b‘ – backspace with backslash interpretor ‘-e‘
removes all the spaces in between.
$ echo -e 'Here bthe bspaces bare bbackspaced.'
output:
Herethespacesarebackspaced.
cont..
Example-4:
Use option ‘n‘ – New line with backspace interpretor ‘-e‘ treats
new line from where it is used.
$ echo -e 'Here nthe nspaces nare nnewlined.‘
output:
Here
the
spaces
are
newlined.
printf: An alternate to echo
It format and print data
printf FORMAT [ARGUMENT]...
cont..
$ printf "%dn" 5
5
$ printf "%fn" 5
5.000000
$ printf "There are %d customers with purchases over %d.n" 50 20000
There are 50 customers with purchases over 20000.
who: who are you
The who command prints information about all users who are currently
logged in.
Who
Displays the username, line, and time of all currently logged-in
sessions.
cont..
Passwd: Changing your password
The passwd command changes passwords for user accounts. A normal
user may only change the password for his/her own account, while the
superuser may change the password for any account.
passwd also changes the account or associated password validity
period.
Example-1:
Change your own password:
$ passwd
output:
$ passwd
Changing password for ubuntu.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
cont..
Ls: listing files
ls List information about the FILEs (the current directory by default).
Sort entries alphabetically with uppercase having precedence over
lower..
ls [OPTION]... [FILE]..
To list all files of current directory:
$ ls
cont..
cont..
Example:
To display all information about files/directories:
$ ls -l
Example output format of ls -l:
-rw-r--r-- 1 root root 209 Mar 30 17:41 printcap
To display hidden files:
$ ls –a
To display files recursively:
$ ls -R /etc/network
To display file inode number:
$ ls -i /etc/wgetrc
cont..
Combining Commands:
Instead of executing commands on separate lines, where each
command is processed and executed before the next could be
entered, UNIX allows you to specify more than one command in
the single command line.
 Each command has to be separated from the other by a ;
(semicolon).
 wc sample.txt ; ls –l sample.txt
You can even group several commands together so that their
combined output is redirected to a file.
(wc sample.txt ; ls –l sample.txt) > newfile
When a command line contains a semicolon, the shell
understands that the command on each side of it needs to be
processed separately. Here ; is known as a metacharacter.
cont..
A command line can overflow or be split into multiple
lines:
When a command overflows into the next line or needs to be split
into multiple lines, just press enter, so that the secondary prompt
(normally >) is displayed and you can enter the remaining part of
the command on the next line.
Ex:
$ echo “This is a two-line
>text message”
TYPE COMMAND AND LOCATING COMMANDS
You can find the location of an executable program using type
command:
$ type ls
ls is /bin/ls
This means that when you execute ls command, the shell locates
this file in /bin directory and makes arrangements to execute it.
cont..
The Path: Locating Commands
UNIX obtains the list of directories that has to be searched from
of an environment variable – PATH
If you evaluate the value of PATH, you’ll find a directory list
separated by colons:
$ echo $PATH
/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:.
INTERNAL AND EXTERNAL COMMANDS
Internal commands:
Some commands are implemented as part of the shell itself
rather than separate executable files.
Such commands that are built-in are called internal commands.
The type command itself is a shell built-in.
 Whether or not you are able to execute it depends on the shell
you use. Ex: cd, source, fg.
External commands:
 These are not built into the shell. These are executables
present in a separate file.
When an external command has to be executed, a new process
has to be spawned and the command gets executed.
Cont..
For example, when you execute the "cat" command, which
usually is at /usr/bin, the executable /usr/bin/cat gets executed.
Since ls is a file having an independent existence in the /bin
directory (or /usr/bin), it is called an external command.
Most commands are external in nature. Ex: ls, cat
$ type cd
cd is a shell builtin
$ type cat
cat is /bin/cat
For the internal commands, the type command will clearly say its
shell built-in, however for the external commands, it gives the
path of the command from where it is executed
Cont..
root: THE SYSTEM ADMINISTRATOR’S LOGIN
The UNIX system provides a special login name for the exclusive
use of the administrator called root. Its password is generally set
at the time of installation.
The prompt of root is #, unlike $ or %used by unprivileged users.
Once you log in as root, you are placed in root’s home directory.
Depending on the system, this directory could be / or /root.root’s
PATH list is different from other users.
It doesn’t contain the current directory, since it’s possible that
root might execute programs of other users’.
Cont..
su COMMAND: AQUIRING SUPERUSER STATUS
su can be used to change the ownership of a session to any user,
it is most commonly employed to change the ownership from an
ordinary user to the root (i.e., administrative) user, thereby
providing access to all parts of and all commands on the
computer or system.
For this reason, it is often referred (although somewhat
inaccurately) as the superuser command. It is also sometimes
called the switch user command
su is also used to change a login session's owner (i.e., the user
who originally created that session by logging on to the system)
without the owner having to first log out of that session.
Syntax
The syntax for the su command is:
su [option] [user] [shell_args]
Cont..
$ su
Password: ****** //root’s password
# pwd
/home/student //Prompt changes, but directory doesn’t.
Creating a User’s Environment:
su, when used with a -, recreates the user’s environment without
taking the root password.
su –student //No password required.
It will create a separate profile for student and runs a separate
sub shell.
Unix - User Administration
There are three types of accounts on a Unix system
Root account: A superuser can run any commands without any
restriction. This user should be assumed as a system
administrator.
System accounts: These accounts are usually needed for some
specific function on your system, and any modifications to them
could adversely affect the system. Eg: mail accounts and sshd
accounts.
User accounts:User accounts provide interactive access to the
system for users and groups of users.
General users are typically assigned to these accounts and
usually have limited access to critical system files and directories.
Cont..
Unix supports a concept of Group Account which logically groups
a number of accounts.
Every account would be a part of another group account. A Unix
group plays important role in handling file permissions and
process management.
groupadd: Adding a group
We need to create groups before creating any account otherwise
we can make use of the existing groups in our system.
following is the syntax to create a new group account −
 groupadd [-g gid [-o]] [-r] [-f] groupname
Following example creates a developers group with default
values, which is very much acceptable for most of the
administrators.
$ groupadd developers
Cont..
S.No. Option & Description
1 -g GID
The numerical value of the group's ID
2 -o
This option permits to add group with non-unique GID
3 -r
This flag instructs groupadd to add a system account
4 -f
This option causes to just exit with success status, if the specified group
already exists. With -g, if the specified GID already exists, other (unique) GID
is chosen
5 Groupname
Actual group name to be created
Cont..
Modify a Group
To modify a group, use the groupmod
To change the developers_2 group name to developer, type −
$ groupmod -n developer developer_2
Delete a Group
To delete an existing group, all you need is the groupdel
command and the group name. To delete the financial group,
the command is −
$ groupdel developer
Unix files
File is a container for storing information. It is a sequence of
characters. UNIX files doesn’t contain the eof(end-of-file)
mark.
 All file attributes are kept in a separate are of the hard disk,
only accessible to kernel.
Basic file types/categories
Ordinary (Regular) File
A large majority of the files found on UNIX system is ordinary
files. Ordinary files contain ASCII (human-readable) text,
executable program binaries, program data, and more..
An ordinary file itself can be divided into two:
Text File
Binary File
Cont..
A text file contains only printable characters and you can
view and edit them. All C and Java program sources, shell
scripts are text files.
Every line of a text file is terminated with the newline
character.
A binary file, on the other hand, contains both printable and
nonprintable characters that cover the entire ASCII range. The
object code and executables that you produce by compiling C
programs are binary files.
Sound and video files are also binary files.
Cont..
Directory File
A directory contains no data, but keeps details of the
files and subdirectories that it contains.
A directory file contains one entry for every file and
subdirectory that it houses. Each entry has two components
namely, the filename
A unique identification number of the file or directory
(called the inode number).
When you create or remove a file, the kernel automatically
updates its corresponding directory by adding or removing
the entry (filename and inode number) associated with the file.
Cont..
Device File
All the operations on the devices are performed by reading
or writing the file representing the device.
 It is advantageous to treat devices as files as some of the
commands used to access an ordinary file can be used with
device files as well.
 Device filenames are found in a single directory structure,
/dev. A device file is not really a stream of characters. It is the
attributes of the file that entirely govern the operation of the
device.
The kernel identifies a device from its attributes and uses
them to operate the device.
Cont..
Special Files:
Links: A link is a tool used for having multiple filenames that
reference a single file on a physical disk. They appear in a file
system just like an ordinary file or a directory.
(Domain) sockets: a special file type, similar to TCP/IP
sockets, providing inter−process networking protected by the
file system's access control.
Named pipes: act more or less like sockets and form a way
for processes to communicate with each other, without using
network socket semantics.
Naming files
On a UNIX system, a filename can consist of up to 255
characters.
 Files may or may not have extensions and can consist of
practically any ASCII character except the / and the Null
character.
You are permitted to use control characters or other
nonprintable characters in a filename.
However, you should avoid using these characters while
naming a file.
It is recommended that only the following characters be used
in filenames:
-Alphabets and numerals.
-The period (.), hyphen (-) and underscore (_).
Cont..
UNIX imposes no restrictions on the extension. In all cases, it
is the application that imposes that restriction.
Eg. A C Compiler expects C program filenames to end with .c,
Oracle requires SQL scripts to have .sql extension.
Hidden Files: have names that begin with a dot (.) For
example:
.cshrc .login .mailrc .mwmrc
Uniqueness: as children in a family, no two files with the
same parent directory can have the same name. Files located
in separate directories can have identical names.
Cont..
A file can have as many dots embedded in its name. A filename
can also begin with or end with a dot.
UNIX is case sensitive; cap01, Chap01 and CHAP01 are three
different filenames that can coexist in the same directory.
Reserved Filenames:
/ - the root directory (slash)
. - current directory (period)
.. - parent directory (double period)
~ - your home directory (tilde)
The HOME variable and home directory
When you log onto the system, UNIX automatically places you in
a directory called the home directory.
The shell variable HOME indicates the home directory of the
user.
E.g.,$ echo $HOME/home/kumar
Cont..
Unix File Organization or Parent Child Relationship in
INUX
Unix organizes files in a treelike hierarchical structure, with the
root directory, indicated by a forward slash (/), at the top of the
tree.
The root directory has a number of subdirectories under it.
These subdirectories in turn have more subdirectories and other
files under them.
For instance, bin and usr are two directories under root. Every
file apart from root must have a parent. In the parent child
relationship, the parent is always a directory.
Cont..
Cont..
The unix file system can be grouped into two categories:
The files available at installation:
/bin and /usr/bin: Common programs, shared by the system, the
system administrator and the users.
/sbin and /usr/sbin- Programs for use by the system and the
system administrator
/dev -Contains references to all the CPU peripheral hardware,
which are represented as files with special properties.
/etc-Most important system configuration files are in /etc, this
directory contains data similar to those in the Control Panel in
Windows
Cont..
/lib and /usr/lib-Library files, includes files for all kinds of
programs needed by the system and the users.
/usr/share/man-Man files
/usr- Programs, libraries, documentation etc. for all user-related
programs
The temporary files:
/tmp- Temporary space for use by the system, cleaned upon
reboot, so don't use this for saving any work!
/var-Storage for all variable files and temporary files created by
users, such as log files, the mail queue, the print spooler area,
space for temporary storage of files downloaded from the internet,
or to keep an image of a CD before burning it.
/home-Home directories of the common users.
Absolute and relative paths
Absolute Path Name
An absolute pathname traverses the file system hierarchy tree
from the very top, always starting at the topmost ROOT directory
of the file system hierarchy.
The topmost root directory is signaled by the leading “slash”
character (/) at the start of an absolute pathname.
Eg: /home/kumar/login.sql
No two files in a UNIX system can have identical absolute path
names. You can have two files with the same name, but in
different directories; their pathnames will also be different.
Thus, the files /home/kumar/progs/c2f.pl can coexist with
/home/kumar/safe/c2f.pl.
Cont..
Using absolute path names for a command
If you execute programs residing in some other directory that is
not in PATH, then you need to specify the absolute path name.
For example, to execute the command less residing in
/usr/local/bin you need to enter the absolute path name
/usr/local/bin/less
Relative pathname
A relative pathname uses the current directory as point of
reference and specifies the path relative to it.
Eg: progs/scripts
Cont..
Using . and .. in relative path name
. (Single dot) represents the current directory
.. (Double dot) represents the parent directory
cd .. ---Moves one level up
cd../.. ---Moves two levels up
$ pwd
/home/kumar/ progs
$ cd ..
$ pwd
/home/kumar
$ pwd
/home/kumar/ progs
$ cd ../..
$ pwd
/home
Cont..
A filename can begin with a dot
cp ../sharma/.profile
To refer to a file in the parent directory of the current directory,
use “../” followed by the name of the file.
Any command that used current directory as argument can also
work with a single dot.
Eg: cp ../sharma/.profile .
This copies the file .profile to the current directory (.)
Directory Commands
pwd - print working directory
At any time you can determine where you are in the file system
hierarchy with the pwd, print working directory, command,
E.g.,:
$ pwd
/home/student/src
cd - change directory
You can change to a new directory with the cd, change directory,
command. cd will accept both absolute and relative path names.
Syntax: cd [directory]
cd changes to user's home directory
cd / changes directory to the system's root
cd .. goes up one directory level
cd ../.. goes up two directory levels
Cont..
cd /full/path/name/from/root changes directory to
absolute path named
cd path/from/current/location changes directory to
path relative to current location
Examples
$ pwd
/home/kumar
$ cd progs //progs must be in current directory
$ pwd
/home/kumar/progs
Cont..
mkdir - make a directory
You extend your home hierarchy by making sub-directories
underneath it. This is done with the mkdir , make directory,
command.
You can specify either the full or relative path of the directory.
Syntax: mkdir directoryname
Examples
mkdir patch
Creates a directory patch under current directory
mkdir patch dbs doc
Creates three directories under current directory
Cont..
The system may refuse to create a directory due to the following
reasons:
1. The directory already exists.
2. There may be an ordinary file by the same name in the current
directory.
3. The permissions set for the current directory don’t permit the
creation of files and directories by the user.
rmdir - remove directory
A directory needs to be empty before you can remove it. If it’s
not, you need to remove the files first. Also, you can’t remove a
directory if it is your present working directory; you must first
change out of that directory.
Cont..
You cannot remove a subdirectory unless you are placed in a
directory which is hierarchically above the one you have chosen
to remove.
Examples
rmdir patch
Delete the directory called patch. Directory must be empty
rmdir pis pis/progs pis/data
Shows error as pisnis not empty. However rmdir silently deletes
the lower level subdirectories Progs and data. The correct order
for deleting subdirectory is:
rmdir pis/data pis/progs pis
The PATH environment variable
The PATH environment variable:
Environmental variables are used to provide information to the
programs you use.
A command runs in UNIX by executing a disk file. When you
specify a command like date , the system will locate the
associated file from a list of directories specified in the PATH
variable and then executes it.
The PATH variable normally includes the current directory also.
Whenever you enter any UNIX command, you are actually
specifying the name of an executable file located somewhere on
the system.
Cont..
The system goes through the following steps in order to
determine which program to execute:
1.Built in commands (such as cd and history) are executed within
the shell.
2. If an absolute path name (such as /bin/ls) or a relative path
name (such as ./myprog), the system executes the program
from the specified directory.
3. Otherwise the PATH variable is used.
FILE RELATED COMMANDS
cat: displaying and creating files
Cat command is used to display the contents of a small file on the
terminal.
Eg:
$ cat cprogram.c
# include <stdioh>
void main ()
{
Printf(“hello”);
}
As like other files cat accepts more than one filename as
arguments
$ cat ch1 ch2
It contains the contents of chapter1
It contains the contents of chapter2
Cont..
In this 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
Displaying Nonprinting Characters (-v)
Nonprinting ASCII characters can be displayed with –v option.
Numbering Lines (-n)
-n option numbers lines. This numbering option helps
programmer in debugging programs.
Using cat to create a file
Cat is also useful for creating a file. Enter the command cat,
followed by > character and the filename.
Eg:
$ cat > new
This is a new file which contains some text, just to
Add some contents to the file new
[ctrl-d]
$_
Cont..
cp: COPYING A File
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.
To copy a file ‘new’ from /home/user1 to your current directory,
use the following command:
cp /home/user1/new . - destination is the current
directory
Cont..
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
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.
Cont..
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
If the newclass/newuspdoesn’t exist, cp creates it along with the
associated subdirectories.
rm: deleting files
The rm command deletes one or more files.
Ex: Following command deletes three files:
$ rm mod1 mod2 mod3
Can remove two chapters from usp directory without having to cd
Ex: rm usp/marks ds/marks
Cont..
To remove all file in a directory use *
$ rm *
Removes all files from that directory
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.
Cont..
Forcing Removal (-f):
rm prompts for removal if a file is write-protected.
The –f option soverrides this minor protection and forces removal.
$rm -rf*
mv: renaming files
The mv command renames (moves) files. The main two functions
are:
It renames a file(or directory)
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.
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.
Cont..
A group of files can be moved to a directory.
Ex: Moves three files ch1,ch2,ch3 to the directory module
$mv ch1 ch2 ch3 module
Can also used to rename directory
$ 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
more : paging output
To view the file ch1, we can use more command along with the
filename, it is used for display
^d used as an interrupt key
^e indicates the end of file
It displays the contents of ch1 on the screen, one page at a time.
If the file contents is more it will show the filename and
percentage of the file that has been viewed:
----More--- (15%)
Cont..
Navigation
f or Spacebar: to scroll forward a page at a time
b to move back one page
The repeat features
The repeat factor: We can use the repeat factors with the
navigation keys Use the repeat factor as a command prefix simply
repeats the command that many times.
Use 10f for scrolling forward 10 pages and 5b for scrolling back 5
pages
wc: counting lines,words and characters
wc command performs Word counting including counting of lines
and characters in a specified file. It takes one or more filename as
arguments and displays a four columnar output.
$ wc ofile
4 20 97 ofile
Cont..
Line: Any group of characters not containing a newline
Word: group of characters not containing a space, tab or newline
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
Multiple filenames,
wc produces a line for each file, as well as a total count.
$ wc -c ofile file
Cont..
97 ofile
15 file
112 total
od: displaying data in octal
od command displays the contents of executable files in a ASCII
octal value.
-b option, displays this value for each character separately.
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
o0000000 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
Ad

Recommended

UNIX_module1.pptx
UNIX_module1.pptx
kushvithchinna900
 
Unix Operating System
Unix Operating System
MahakKasliwal
 
Unix final
Unix final
MahakKasliwal
 
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Sowmya Jyothi
 
CHAPTER 1 INTRODUCTION TO UNIX.pptx
CHAPTER 1 INTRODUCTION TO UNIX.pptx
MahiDivya
 
UNIT II-Programming in Linux
UNIT II-Programming in Linux
Dr.YNM
 
Summarized of UNIX Time Sharing System
Summarized of UNIX Time Sharing System
Shuya Osaki
 
Unix case-study
Unix case-study
NishantMishra126
 
UNIX_Module 1.pdf
UNIX_Module 1.pdf
krishnaraj714229
 
Unix seminar
Unix seminar
Ajeet Kushwaha
 
Introduction to unix
Introduction to unix
sudheer yathagiri
 
Introduction to Unix
Introduction to Unix
Nishant Munjal
 
Unix and shell programming | Unix File System | Unix File Permission | Blocks
Unix and shell programming | Unix File System | Unix File Permission | Blocks
LOKESH KUMAR
 
Chapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix Concepts
MeenalJabde
 
Unix / Linux Operating System introduction.
Unix / Linux Operating System introduction.
poongothai11
 
Unix Operating System
Unix Operating System
subhsikha
 
unix details file system, architecture, directory
unix details file system, architecture, directory
co3sem2020
 
Shells commands, file structure, directory structure.pptx
Shells commands, file structure, directory structure.pptx
SherinRappai
 
Unix operating system architecture with file structure
Unix operating system architecture with file structure
amol_chavan
 
Unix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basi
Priyadarshini648418
 
Spsl unit1
Spsl unit1
Sasidhar Kothuru
 
Operating systems unix
Operating systems unix
Achu dhan
 
Introduction To Unix.pptx
Introduction To Unix.pptx
ssuser140ea3
 
Unix - An Introduction
Unix - An Introduction
Deepanshu Gahlaut
 
Introduction to unix
Introduction to unix
Chandru Jangin
 
Unix Operaring System
Unix Operaring System
Mahnoor Shaukat
 
Introduction to unix (1).pptx
Introduction to unix (1).pptx
virat834293
 
Introduction to Unix-like systems (Part I-IV)
Introduction to Unix-like systems (Part I-IV)
hildenjohannes
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 

More Related Content

Similar to Module1-UNIX architecture; FEATURES OF UNIX OS (20)

UNIX_Module 1.pdf
UNIX_Module 1.pdf
krishnaraj714229
 
Unix seminar
Unix seminar
Ajeet Kushwaha
 
Introduction to unix
Introduction to unix
sudheer yathagiri
 
Introduction to Unix
Introduction to Unix
Nishant Munjal
 
Unix and shell programming | Unix File System | Unix File Permission | Blocks
Unix and shell programming | Unix File System | Unix File Permission | Blocks
LOKESH KUMAR
 
Chapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix Concepts
MeenalJabde
 
Unix / Linux Operating System introduction.
Unix / Linux Operating System introduction.
poongothai11
 
Unix Operating System
Unix Operating System
subhsikha
 
unix details file system, architecture, directory
unix details file system, architecture, directory
co3sem2020
 
Shells commands, file structure, directory structure.pptx
Shells commands, file structure, directory structure.pptx
SherinRappai
 
Unix operating system architecture with file structure
Unix operating system architecture with file structure
amol_chavan
 
Unix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basi
Priyadarshini648418
 
Spsl unit1
Spsl unit1
Sasidhar Kothuru
 
Operating systems unix
Operating systems unix
Achu dhan
 
Introduction To Unix.pptx
Introduction To Unix.pptx
ssuser140ea3
 
Unix - An Introduction
Unix - An Introduction
Deepanshu Gahlaut
 
Introduction to unix
Introduction to unix
Chandru Jangin
 
Unix Operaring System
Unix Operaring System
Mahnoor Shaukat
 
Introduction to unix (1).pptx
Introduction to unix (1).pptx
virat834293
 
Introduction to Unix-like systems (Part I-IV)
Introduction to Unix-like systems (Part I-IV)
hildenjohannes
 
Unix and shell programming | Unix File System | Unix File Permission | Blocks
Unix and shell programming | Unix File System | Unix File Permission | Blocks
LOKESH KUMAR
 
Chapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix Concepts
MeenalJabde
 
Unix / Linux Operating System introduction.
Unix / Linux Operating System introduction.
poongothai11
 
Unix Operating System
Unix Operating System
subhsikha
 
unix details file system, architecture, directory
unix details file system, architecture, directory
co3sem2020
 
Shells commands, file structure, directory structure.pptx
Shells commands, file structure, directory structure.pptx
SherinRappai
 
Unix operating system architecture with file structure
Unix operating system architecture with file structure
amol_chavan
 
Unix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basi
Priyadarshini648418
 
Operating systems unix
Operating systems unix
Achu dhan
 
Introduction To Unix.pptx
Introduction To Unix.pptx
ssuser140ea3
 
Introduction to unix (1).pptx
Introduction to unix (1).pptx
virat834293
 
Introduction to Unix-like systems (Part I-IV)
Introduction to Unix-like systems (Part I-IV)
hildenjohannes
 

Recently uploaded (20)

Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
nnnnnnnnnnnn7777777777777777777777777777777.pptx
nnnnnnnnnnnn7777777777777777777777777777777.pptx
gayathri venkataramani
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
nnnnnnnnnnnn7777777777777777777777777777777.pptx
nnnnnnnnnnnn7777777777777777777777777777777.pptx
gayathri venkataramani
 
Ad

Module1-UNIX architecture; FEATURES OF UNIX OS

  • 2. Introduction about Operating system  An operating system is system software that manages computer hardware and software resources and provides common services for computer programs. It acts as an interface between the user and the computer hardware.  Following are some of important functions of an operating System. • Memory Management • Processor Management • Device Management
  • 3. cont.. • File Management • Security • Control over system performance • Job accounting • Error detecting aids • Coordination between other software and users
  • 4. UNIX OS • Unix is a computer Operating System which is capable of handling activities from multiple users at the same time. • The development of Unix started around 1969 at AT&T Bell Labs by Ken Thompson and Dennis • The main components of UNIX OS are kernel and shell. The computer programs that allocate the system resources and coordinate all the details of the computer's internal is called the kernel.
  • 5. Cont.. • Users communicate with the kernel through a program known as the shell. • The shell is a command line interpreter; it translates commands entered by the user and converts them into a language that is understood by the kernel.
  • 6. History of UNIX • The UNIX operating system was born in the late 1960s. It originally began as a one man project led by Ken Thompson of Bell Labs, and has since grown to become the most widely used operating system. • In the time since UNIX was first developed, it has gone through many different generations and even mutations. – Some differ substantially from the original version, like Berkeley Software Distribution (BSD) or Linux. – Others, still contain major portions that are based on the original source code.
  • 7. cont..  There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and BSD are a few examples. Linux is also a flavor of Unix which is freely available.
  • 11. UNIX ARCHITECTURE  The heart of Fig is the hardware. This is surrounded by the operating system. UNIX architecture comprises of two major components viz., the shell and the kernel.
  • 12. Cont..  The kernel interacts with the machine’s hardware and the shell with the user.  The heart of the operating system is often called the kernel.  The kernel normally contains essential features such as the scheduler, file management etc. Users and programs cannot communicate directly with the kernel normally.  However there is a mechanism by which we can communicate – this is via system calls. Here is a basic block diagram of a Unix system
  • 13. Cont.. (i) Division of labor: kernel and shell Kernel − The kernel is the heart of the operating system. It is a collection of routines written in C.  It is loaded into memory when the system is booted and communicates directly with the hardware. It interacts with the hardware and most of the tasks like memory management, task scheduling and file management. Shell − The shell performs the role of command interpreter. When you type in a command at your terminal, the shell interprets the command and calls the program that you want.
  • 14. Cont..  Even though there’s only one kernel running on the system, there could be several shells in action, one for each user who’s logged in.  The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are the most famous shells which are available with most of the Unix variants. (ii) The File and Process  All the data of Unix is organized into files. All files are then organized into directories.  These directories are further organized into a tree-like structure called the file system.
  • 15. Cont..  A file is an array of bytes that stores information. It is also related to another file in the sense that both belong to a single hierarchical directory structure.  A process is the second abstraction UNIX provides. It can be treated as a time image of an executable file.  Like files, processes also belong to a hierarchical structure. (iii) System Calls • User programs that need to access the hardware use the services of the kernel via use of system calls.
  • 16. Cont..  These are similar to function calls, but remove control from the user process All UNIX flavors use same system calls. Eg: • exit: exits a process • write: writes to a file • read: reads from a file • fork: creates a new process
  • 17. FEATURES OF UNIX OS • Several features of UNIX have made it popular. Some of them are: • Portable : UNIX can be installed on many hardware platforms. Its widespread use can be traced to the decision to develop it using the C language. • Multiuser: The UNIX design allows multiple users to concurrently share hardware and software • Multitasking: UNIX allows a user to run more than one program at a time. In fact more than one program can be running in the background while a user is working foreground.
  • 18. Cont.. • Networking: While UNIX was developed to be an interactive, multiuser, multitasking system, networking is also incorporated into the heart of the operating system. • Access to another system uses a standard communications protocol known as Transmission Control Protocol/Internet Protocol (TCP/IP). • Organized File System: UNIX has a very organized file and directory system that allows users to organize and maintain files.
  • 19. Cont.. • Device Independence: UNIX treats input/output devices like ordinary files. The source or destination for file input and output is easily controlled through a UNIX design feature called redirection. • Building block approach: UNIX uses a building-block approach in the design of some of its tools and lets you develop complex command routines by connecting these tools. For example we can use pipe to connect ls and wc and count number of files in your directory. • Programming facilities
  • 20. Cont.. • UNIX toolkit and documentation. • Utilities: UNIX provides a rich library of utilities that can be use to increase user productivity.
  • 21. POSIX AND SINGLE UNIX SPECIFICATION • POSIX (Portable Operating System Interface) is a set of standard operating system interfaces based on the Unix operating system. • It is designed for achieving portability. It is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. • Beginning in 1998, a joint working group known as the Austin Group began to develop the combined standard that would be known as the Single UNIX Specification Version 3 and as POSIX:2001 (formally: IEEE Std 1003.1- 2001).
  • 22. Cont.. • It was released on January 30, 2002. The main two interfaces are POSIX.1 and POSIX.2. • POSIX.1 is the standard for an application program interface in the C language. POSIX.2 is the standard shell and utility interface. • PSIX.4 is another important interface for thread management.
  • 23. Cont.. • Single Unix specification: In December 2008, the Austin Group published a new major revision, known as POSIX:2008 (formally: IEEE Std 1003.1-2008). This is the core of the Single UNIX Specification, Version 4. • The Single UNIX Specification is an industry standard description of the C language program and user command interfaces for a standard Unix operating system. • The “write once, adopt everywhere” approach of this development ensure that a program developed in one POSIX compliant Unix operating system would run in a somewhat different POSIX compliant Unix operating system.
  • 24. GENERAL FEATURES OF UNIX COMMANDS/ COMMAND STRUCTURE • A command is a program that tells the Unix system to do something. It has the form: Command name [options] [arguments] • where command is the command name that can take a set of optional options and one or more optional arguments. • An argument indicates on what the command is to perform its action, usually a file or series of files. An option modifies the command, changing the way it performs.
  • 25. Cont.. Commands: • Commands are case sensitive.  i.e command and Command are not the same.  Commands, options and arguments have to be separated by spaces or tabs to enable the shell to interpret them as words.  A contiguous string of spaces and tabs together is called a whitespace.
  • 26. Cont..  The shell compresses multiple occurrences of whitespace into a single whitespace. • $ cp file1 file2 (Will copy file 1 to file 2)  To change the name of a file, use the mv command. Following is the basic syntax $ mv old_file new_file
  • 27. Cont.. Options • Options are generally preceded by a hyphen (-), and for most commands, more than one option can be strung together, in the form: command -[option][option][option] • Example: $ ls –l • There must not be any whitespaces between – and l. • Options are also arguments, but given a special name because they are predetermined. Options can be normally combined with only one – sign. i.e., instead of using • $ ls –l –a –t we can as well use, • $ ls –lat
  • 28. Cont.. Filename Arguments • Many UNIX commands use a filename as argument so that the command can take input from the file. • If a command uses a filename as argument, it will usually be the last argument, after all options. Example: cp file1 file2 file3 dest_dir rm file1 file2 file3 • The command with its options and arguments is known as the command line, which is considered as complete after [Enter] key is pressed, so that the entire line is fed to the shell as its input for interpretation and execution.
  • 29. Cont.. Exceptions • Some commands in UNIX like pwd do not take any options and arguments. Some commands like who may or may not be specified with arguments. • The ls command can run without arguments (ls), with only options (ls –l), with only filenames (ls f1 f2), or using a combination of both (ls –l f1 f2). • Some commands compulsorily take options (cut). Some commands like grep, sed can take an expression as an argument, or a set of instructions as argument.
  • 30. Cont.. Cut command: The cut command extracts a given number of characters or columns from a file. Syntax: cut [options] [file] Example: $ cut -c 5-10 file1 This command will extract characters 5 to 10 from each line. grep command in Unix The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression grep [options] pattern [files]
  • 31. Cont.. Example: $grep -i "UNix" geekfile.txt The -i option enables to search for a string case insensitively in the give file. It matches the words like “UNIX”, “Unix”, “unix”. Sed Command SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution). Syntax: sed OPTIONS... [SCRIPT] [INPUTFILE...] Example: $sed 's/unix/linux/' geekfile.txt Replacing or substituting string : Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “unix” with “linux” in the file.
  • 32. Understanding of some basic commands such as echo, printf, ls, who, date, passwd, cal cal: The calendar • The cal command is a command line utility for displaying a calendar in the terminal. It can be used to print a single month, many months or an entire year. The syntax is:: cal [-mjy] [[month] year] • Everything within rectangular box is optional. Tag Description -m Display monday as the first day of the week. -j Display julian dates (days one-based, numbered from January 1). -y Display a calendar for the current year.
  • 33. cont.. To display feb 2015 calendar $ cal 2 2015 To display complete year calendar. $ cal –y date: Displaying the system date The date command displays the current date and time. It can also be used to display or calculate a date in a format you specify The syntax is: date [OPTION]... [+%FORMAT] • To Print current system date and time: $date output: Sun Jan 8 21:38:15 IST 2017
  • 34. cont.. • To print only the month: $date +%m output: 08 • To print only the month name: $date +%h output: Aug
  • 35. cont.. • echo: Displaying a Message  echo is a fundamental command found in most operating systems that offer a command line.  It is frequently used in scripts, batch files, and as part of individual commands; anywhere you may need to insert text.  Many command shells such as bash, ksh and csh implement echo as a built-in command. Escape sequence used by echo and printf
  • 37. cont.. Example-1: To print string "Hello, World!" on console $ echo "Hello, World!" output: Hello, World! Example-2: To print value of x, where x=10. $ echo “$x” output: 10 Example-3: Use option ‘b‘ – backspace with backslash interpretor ‘-e‘ removes all the spaces in between. $ echo -e 'Here bthe bspaces bare bbackspaced.' output: Herethespacesarebackspaced.
  • 38. cont.. Example-4: Use option ‘n‘ – New line with backspace interpretor ‘-e‘ treats new line from where it is used. $ echo -e 'Here nthe nspaces nare nnewlined.‘ output: Here the spaces are newlined. printf: An alternate to echo It format and print data printf FORMAT [ARGUMENT]...
  • 39. cont.. $ printf "%dn" 5 5 $ printf "%fn" 5 5.000000 $ printf "There are %d customers with purchases over %d.n" 50 20000 There are 50 customers with purchases over 20000. who: who are you The who command prints information about all users who are currently logged in. Who Displays the username, line, and time of all currently logged-in sessions.
  • 40. cont.. Passwd: Changing your password The passwd command changes passwords for user accounts. A normal user may only change the password for his/her own account, while the superuser may change the password for any account. passwd also changes the account or associated password validity period. Example-1: Change your own password: $ passwd output: $ passwd Changing password for ubuntu. (current) UNIX password: Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
  • 41. cont.. Ls: listing files ls List information about the FILEs (the current directory by default). Sort entries alphabetically with uppercase having precedence over lower.. ls [OPTION]... [FILE].. To list all files of current directory: $ ls
  • 43. cont.. Example: To display all information about files/directories: $ ls -l Example output format of ls -l: -rw-r--r-- 1 root root 209 Mar 30 17:41 printcap To display hidden files: $ ls –a To display files recursively: $ ls -R /etc/network To display file inode number: $ ls -i /etc/wgetrc
  • 44. cont.. Combining Commands: Instead of executing commands on separate lines, where each command is processed and executed before the next could be entered, UNIX allows you to specify more than one command in the single command line.  Each command has to be separated from the other by a ; (semicolon).  wc sample.txt ; ls –l sample.txt You can even group several commands together so that their combined output is redirected to a file. (wc sample.txt ; ls –l sample.txt) > newfile When a command line contains a semicolon, the shell understands that the command on each side of it needs to be processed separately. Here ; is known as a metacharacter.
  • 45. cont.. A command line can overflow or be split into multiple lines: When a command overflows into the next line or needs to be split into multiple lines, just press enter, so that the secondary prompt (normally >) is displayed and you can enter the remaining part of the command on the next line. Ex: $ echo “This is a two-line >text message” TYPE COMMAND AND LOCATING COMMANDS You can find the location of an executable program using type command: $ type ls ls is /bin/ls This means that when you execute ls command, the shell locates this file in /bin directory and makes arrangements to execute it.
  • 46. cont.. The Path: Locating Commands UNIX obtains the list of directories that has to be searched from of an environment variable – PATH If you evaluate the value of PATH, you’ll find a directory list separated by colons: $ echo $PATH /bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:.
  • 47. INTERNAL AND EXTERNAL COMMANDS Internal commands: Some commands are implemented as part of the shell itself rather than separate executable files. Such commands that are built-in are called internal commands. The type command itself is a shell built-in.  Whether or not you are able to execute it depends on the shell you use. Ex: cd, source, fg. External commands:  These are not built into the shell. These are executables present in a separate file. When an external command has to be executed, a new process has to be spawned and the command gets executed.
  • 48. Cont.. For example, when you execute the "cat" command, which usually is at /usr/bin, the executable /usr/bin/cat gets executed. Since ls is a file having an independent existence in the /bin directory (or /usr/bin), it is called an external command. Most commands are external in nature. Ex: ls, cat $ type cd cd is a shell builtin $ type cat cat is /bin/cat For the internal commands, the type command will clearly say its shell built-in, however for the external commands, it gives the path of the command from where it is executed
  • 49. Cont.. root: THE SYSTEM ADMINISTRATOR’S LOGIN The UNIX system provides a special login name for the exclusive use of the administrator called root. Its password is generally set at the time of installation. The prompt of root is #, unlike $ or %used by unprivileged users. Once you log in as root, you are placed in root’s home directory. Depending on the system, this directory could be / or /root.root’s PATH list is different from other users. It doesn’t contain the current directory, since it’s possible that root might execute programs of other users’.
  • 50. Cont.. su COMMAND: AQUIRING SUPERUSER STATUS su can be used to change the ownership of a session to any user, it is most commonly employed to change the ownership from an ordinary user to the root (i.e., administrative) user, thereby providing access to all parts of and all commands on the computer or system. For this reason, it is often referred (although somewhat inaccurately) as the superuser command. It is also sometimes called the switch user command su is also used to change a login session's owner (i.e., the user who originally created that session by logging on to the system) without the owner having to first log out of that session. Syntax The syntax for the su command is: su [option] [user] [shell_args]
  • 51. Cont.. $ su Password: ****** //root’s password # pwd /home/student //Prompt changes, but directory doesn’t. Creating a User’s Environment: su, when used with a -, recreates the user’s environment without taking the root password. su –student //No password required. It will create a separate profile for student and runs a separate sub shell.
  • 52. Unix - User Administration There are three types of accounts on a Unix system Root account: A superuser can run any commands without any restriction. This user should be assumed as a system administrator. System accounts: These accounts are usually needed for some specific function on your system, and any modifications to them could adversely affect the system. Eg: mail accounts and sshd accounts. User accounts:User accounts provide interactive access to the system for users and groups of users. General users are typically assigned to these accounts and usually have limited access to critical system files and directories.
  • 53. Cont.. Unix supports a concept of Group Account which logically groups a number of accounts. Every account would be a part of another group account. A Unix group plays important role in handling file permissions and process management. groupadd: Adding a group We need to create groups before creating any account otherwise we can make use of the existing groups in our system. following is the syntax to create a new group account −  groupadd [-g gid [-o]] [-r] [-f] groupname Following example creates a developers group with default values, which is very much acceptable for most of the administrators. $ groupadd developers
  • 54. Cont.. S.No. Option & Description 1 -g GID The numerical value of the group's ID 2 -o This option permits to add group with non-unique GID 3 -r This flag instructs groupadd to add a system account 4 -f This option causes to just exit with success status, if the specified group already exists. With -g, if the specified GID already exists, other (unique) GID is chosen 5 Groupname Actual group name to be created
  • 55. Cont.. Modify a Group To modify a group, use the groupmod To change the developers_2 group name to developer, type − $ groupmod -n developer developer_2 Delete a Group To delete an existing group, all you need is the groupdel command and the group name. To delete the financial group, the command is − $ groupdel developer
  • 56. Unix files File is a container for storing information. It is a sequence of characters. UNIX files doesn’t contain the eof(end-of-file) mark.  All file attributes are kept in a separate are of the hard disk, only accessible to kernel. Basic file types/categories Ordinary (Regular) File A large majority of the files found on UNIX system is ordinary files. Ordinary files contain ASCII (human-readable) text, executable program binaries, program data, and more.. An ordinary file itself can be divided into two: Text File Binary File
  • 57. Cont.. A text file contains only printable characters and you can view and edit them. All C and Java program sources, shell scripts are text files. Every line of a text file is terminated with the newline character. A binary file, on the other hand, contains both printable and nonprintable characters that cover the entire ASCII range. The object code and executables that you produce by compiling C programs are binary files. Sound and video files are also binary files.
  • 58. Cont.. Directory File A directory contains no data, but keeps details of the files and subdirectories that it contains. A directory file contains one entry for every file and subdirectory that it houses. Each entry has two components namely, the filename A unique identification number of the file or directory (called the inode number). When you create or remove a file, the kernel automatically updates its corresponding directory by adding or removing the entry (filename and inode number) associated with the file.
  • 59. Cont.. Device File All the operations on the devices are performed by reading or writing the file representing the device.  It is advantageous to treat devices as files as some of the commands used to access an ordinary file can be used with device files as well.  Device filenames are found in a single directory structure, /dev. A device file is not really a stream of characters. It is the attributes of the file that entirely govern the operation of the device. The kernel identifies a device from its attributes and uses them to operate the device.
  • 60. Cont.. Special Files: Links: A link is a tool used for having multiple filenames that reference a single file on a physical disk. They appear in a file system just like an ordinary file or a directory. (Domain) sockets: a special file type, similar to TCP/IP sockets, providing inter−process networking protected by the file system's access control. Named pipes: act more or less like sockets and form a way for processes to communicate with each other, without using network socket semantics.
  • 61. Naming files On a UNIX system, a filename can consist of up to 255 characters.  Files may or may not have extensions and can consist of practically any ASCII character except the / and the Null character. You are permitted to use control characters or other nonprintable characters in a filename. However, you should avoid using these characters while naming a file. It is recommended that only the following characters be used in filenames: -Alphabets and numerals. -The period (.), hyphen (-) and underscore (_).
  • 62. Cont.. UNIX imposes no restrictions on the extension. In all cases, it is the application that imposes that restriction. Eg. A C Compiler expects C program filenames to end with .c, Oracle requires SQL scripts to have .sql extension. Hidden Files: have names that begin with a dot (.) For example: .cshrc .login .mailrc .mwmrc Uniqueness: as children in a family, no two files with the same parent directory can have the same name. Files located in separate directories can have identical names.
  • 63. Cont.. A file can have as many dots embedded in its name. A filename can also begin with or end with a dot. UNIX is case sensitive; cap01, Chap01 and CHAP01 are three different filenames that can coexist in the same directory. Reserved Filenames: / - the root directory (slash) . - current directory (period) .. - parent directory (double period) ~ - your home directory (tilde) The HOME variable and home directory When you log onto the system, UNIX automatically places you in a directory called the home directory. The shell variable HOME indicates the home directory of the user. E.g.,$ echo $HOME/home/kumar
  • 64. Cont.. Unix File Organization or Parent Child Relationship in INUX Unix organizes files in a treelike hierarchical structure, with the root directory, indicated by a forward slash (/), at the top of the tree. The root directory has a number of subdirectories under it. These subdirectories in turn have more subdirectories and other files under them. For instance, bin and usr are two directories under root. Every file apart from root must have a parent. In the parent child relationship, the parent is always a directory.
  • 66. Cont.. The unix file system can be grouped into two categories: The files available at installation: /bin and /usr/bin: Common programs, shared by the system, the system administrator and the users. /sbin and /usr/sbin- Programs for use by the system and the system administrator /dev -Contains references to all the CPU peripheral hardware, which are represented as files with special properties. /etc-Most important system configuration files are in /etc, this directory contains data similar to those in the Control Panel in Windows
  • 67. Cont.. /lib and /usr/lib-Library files, includes files for all kinds of programs needed by the system and the users. /usr/share/man-Man files /usr- Programs, libraries, documentation etc. for all user-related programs The temporary files: /tmp- Temporary space for use by the system, cleaned upon reboot, so don't use this for saving any work! /var-Storage for all variable files and temporary files created by users, such as log files, the mail queue, the print spooler area, space for temporary storage of files downloaded from the internet, or to keep an image of a CD before burning it. /home-Home directories of the common users.
  • 68. Absolute and relative paths Absolute Path Name An absolute pathname traverses the file system hierarchy tree from the very top, always starting at the topmost ROOT directory of the file system hierarchy. The topmost root directory is signaled by the leading “slash” character (/) at the start of an absolute pathname. Eg: /home/kumar/login.sql No two files in a UNIX system can have identical absolute path names. You can have two files with the same name, but in different directories; their pathnames will also be different. Thus, the files /home/kumar/progs/c2f.pl can coexist with /home/kumar/safe/c2f.pl.
  • 69. Cont.. Using absolute path names for a command If you execute programs residing in some other directory that is not in PATH, then you need to specify the absolute path name. For example, to execute the command less residing in /usr/local/bin you need to enter the absolute path name /usr/local/bin/less Relative pathname A relative pathname uses the current directory as point of reference and specifies the path relative to it. Eg: progs/scripts
  • 70. Cont.. Using . and .. in relative path name . (Single dot) represents the current directory .. (Double dot) represents the parent directory cd .. ---Moves one level up cd../.. ---Moves two levels up $ pwd /home/kumar/ progs $ cd .. $ pwd /home/kumar $ pwd /home/kumar/ progs $ cd ../.. $ pwd /home
  • 71. Cont.. A filename can begin with a dot cp ../sharma/.profile To refer to a file in the parent directory of the current directory, use “../” followed by the name of the file. Any command that used current directory as argument can also work with a single dot. Eg: cp ../sharma/.profile . This copies the file .profile to the current directory (.)
  • 72. Directory Commands pwd - print working directory At any time you can determine where you are in the file system hierarchy with the pwd, print working directory, command, E.g.,: $ pwd /home/student/src cd - change directory You can change to a new directory with the cd, change directory, command. cd will accept both absolute and relative path names. Syntax: cd [directory] cd changes to user's home directory cd / changes directory to the system's root cd .. goes up one directory level cd ../.. goes up two directory levels
  • 73. Cont.. cd /full/path/name/from/root changes directory to absolute path named cd path/from/current/location changes directory to path relative to current location Examples $ pwd /home/kumar $ cd progs //progs must be in current directory $ pwd /home/kumar/progs
  • 74. Cont.. mkdir - make a directory You extend your home hierarchy by making sub-directories underneath it. This is done with the mkdir , make directory, command. You can specify either the full or relative path of the directory. Syntax: mkdir directoryname Examples mkdir patch Creates a directory patch under current directory mkdir patch dbs doc Creates three directories under current directory
  • 75. Cont.. The system may refuse to create a directory due to the following reasons: 1. The directory already exists. 2. There may be an ordinary file by the same name in the current directory. 3. The permissions set for the current directory don’t permit the creation of files and directories by the user. rmdir - remove directory A directory needs to be empty before you can remove it. If it’s not, you need to remove the files first. Also, you can’t remove a directory if it is your present working directory; you must first change out of that directory.
  • 76. Cont.. You cannot remove a subdirectory unless you are placed in a directory which is hierarchically above the one you have chosen to remove. Examples rmdir patch Delete the directory called patch. Directory must be empty rmdir pis pis/progs pis/data Shows error as pisnis not empty. However rmdir silently deletes the lower level subdirectories Progs and data. The correct order for deleting subdirectory is: rmdir pis/data pis/progs pis
  • 77. The PATH environment variable The PATH environment variable: Environmental variables are used to provide information to the programs you use. A command runs in UNIX by executing a disk file. When you specify a command like date , the system will locate the associated file from a list of directories specified in the PATH variable and then executes it. The PATH variable normally includes the current directory also. Whenever you enter any UNIX command, you are actually specifying the name of an executable file located somewhere on the system.
  • 78. Cont.. The system goes through the following steps in order to determine which program to execute: 1.Built in commands (such as cd and history) are executed within the shell. 2. If an absolute path name (such as /bin/ls) or a relative path name (such as ./myprog), the system executes the program from the specified directory. 3. Otherwise the PATH variable is used.
  • 79. FILE RELATED COMMANDS cat: displaying and creating files Cat command is used to display the contents of a small file on the terminal. Eg: $ cat cprogram.c # include <stdioh> void main () { Printf(“hello”); } As like other files cat accepts more than one filename as arguments $ cat ch1 ch2 It contains the contents of chapter1 It contains the contents of chapter2
  • 80. Cont.. In this 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 Displaying Nonprinting Characters (-v) Nonprinting ASCII characters can be displayed with –v option. Numbering Lines (-n) -n option numbers lines. This numbering option helps programmer in debugging programs. Using cat to create a file Cat is also useful for creating a file. Enter the command cat, followed by > character and the filename. Eg: $ cat > new This is a new file which contains some text, just to Add some contents to the file new [ctrl-d] $_
  • 81. Cont.. cp: COPYING A File 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. To copy a file ‘new’ from /home/user1 to your current directory, use the following command: cp /home/user1/new . - destination is the current directory
  • 82. Cont.. 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 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.
  • 83. Cont.. 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 If the newclass/newuspdoesn’t exist, cp creates it along with the associated subdirectories. rm: deleting files The rm command deletes one or more files. Ex: Following command deletes three files: $ rm mod1 mod2 mod3 Can remove two chapters from usp directory without having to cd Ex: rm usp/marks ds/marks
  • 84. Cont.. To remove all file in a directory use * $ rm * Removes all files from that directory 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.
  • 85. Cont.. Forcing Removal (-f): rm prompts for removal if a file is write-protected. The –f option soverrides this minor protection and forces removal. $rm -rf* mv: renaming files The mv command renames (moves) files. The main two functions are: It renames a file(or directory) 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. 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.
  • 86. Cont.. A group of files can be moved to a directory. Ex: Moves three files ch1,ch2,ch3 to the directory module $mv ch1 ch2 ch3 module Can also used to rename directory $ 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 more : paging output To view the file ch1, we can use more command along with the filename, it is used for display ^d used as an interrupt key ^e indicates the end of file It displays the contents of ch1 on the screen, one page at a time. If the file contents is more it will show the filename and percentage of the file that has been viewed: ----More--- (15%)
  • 87. Cont.. Navigation f or Spacebar: to scroll forward a page at a time b to move back one page The repeat features The repeat factor: We can use the repeat factors with the navigation keys Use the repeat factor as a command prefix simply repeats the command that many times. Use 10f for scrolling forward 10 pages and 5b for scrolling back 5 pages wc: counting lines,words and characters wc command performs Word counting including counting of lines and characters in a specified file. It takes one or more filename as arguments and displays a four columnar output. $ wc ofile 4 20 97 ofile
  • 88. Cont.. Line: Any group of characters not containing a newline Word: group of characters not containing a space, tab or newline 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 Multiple filenames, wc produces a line for each file, as well as a total count. $ wc -c ofile file
  • 89. Cont.. 97 ofile 15 file 112 total od: displaying data in octal od command displays the contents of executable files in a ASCII octal value. -b option, displays this value for each character separately. 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 o0000000 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