0% found this document useful (0 votes)
139 views39 pages

UNIX Module 1 notes

Module 1 of the UNIX System Programming course covers the introduction to UNIX components, its architecture, and basic file handling. It discusses the history of UNIX, its features such as multiuser and multitasking capabilities, and the importance of the kernel and shell. Additionally, it explains the POSIX standards and the command-based nature of UNIX, including command types and file organization.

Uploaded by

Khyathi Kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views39 pages

UNIX Module 1 notes

Module 1 of the UNIX System Programming course covers the introduction to UNIX components, its architecture, and basic file handling. It discusses the history of UNIX, its features such as multiuser and multitasking capabilities, and the importance of the kernel and shell. Additionally, it explains the POSIX standards and the command-based nature of UNIX, including command types and file organization.

Uploaded by

Khyathi Kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

UNIX System Programming(BCS515C) Module-1 Introduction, Unix files

Programming(BCS5151C)
MODULE-1
Module 1: Introduction: Unix Components/Architecture. Features of Unix. The UNIX Environment
and UNIX Structure, Posix and Single Unix specification. General features of Unix commands/
command structure. Command arguments and options. Basic Unix commands such as echo, printf,
ls, who, date,passwd, cal, Combining commands. Meaning of Internal and external commands. The
type command: knowing the type of a command and locating it. The root login. Becoming the super
user: su command.

Unix files: Naming files. Basic file types/categories. Organization of files. Hidden files. Standard
directories. Parent child relationship. The home directory and the HOME variable. Reaching required
files- the PATH variable, manipulating the PATH, Relative and absolute pathnames. Directory
commands – pwd, cd, mkdir, rmdir commands. The dot (.) and double dots (..) notations to represent
present and parent directories and their usage in relative pathnames. File related commands – cat,
mv, rm, cp, wc and od commands.

Text Book:
Unix Concepts and Applications, Sumitabha Das, Tata McGraw Hill , 4th Edition

 What is an operating system?


 An operating system is the software that manages the computer's hardware.
 Provides convenient and safe environment for running programs. It acts as an interface
between user programs and the hardware resources that these programs access.
 Like – processor, memory, hard disk, printer & so on.
 It is loaded into memory when a computer is booted and remains active as long as the
machine is up.
 The first operating system used for real work was GM-NAA I/O, produced in 1956 by
General Motors' Research division for its IBM 704.
 Most other early operating systems for IBM mainframes were also produced by customers.

 History of Unix OS
 Prior to Unix, many operating systems ran collections or “batches” of operations one at a
time.
 This single-user “batch-processing” approach did not take advantage of the potential
processing power and speed of computers.

Dept of CSE, Vemana I.T Page 1 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)

 The UNIX Operating System


 Like DOS and Windows, there's another operating system called UNIX.
 UNIX was earlier known to be UNICS, which stands for UNiplexed Information
Computing System.
 It has practically everything an operating system should have, and several features which
 other OS never had.
 It runs practically on every Hardware and provided inspiration to the Open Source
 movement.
 You interact with a UNIX system through a command interpreter called the shell.
 A command may already exist on the system or it could be one written by user.
 However, the power of UNIX lies in combining these commands in the same way the
English language lets you combine words to generate meaningful idea.

 History of Unix OS
 The Unix OS was developed (based on Multics & CTSS operating systems) by Ken
Thompson at the AT&T Bell Laboratories in 1969. He wanted to create an multi-user
operating system to run “space travel” game.
 Ken‟s philosophy was to create an operating system with commands or “utilities” that
would do one thing well (i.e. UNIX). Pipes could be used combine commands...
 The first versions of UNIX were written in “machine-dependent” program (such as PDP-7).
 Ken Thompson approach Dennis Ritchie developer of C program), and in 1973 they
compiled UNIX in C programming language to make operating system “portable” to other
computers systems.

 The UNIX Architecture

Dept of CSE, Vemana I.T Page 2 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)

 The entire UNIX system is supported by a handful of essentially simple and abstract
concepts.
 The UNIX architecture has three important agencies:
1. Division of labor: Kernel and shell
2. The file and process
3. The system calls

1. Division of labor: Kernel and shell


 The fertile ideas in the development of UNIX has two agencies – kernel and shell.
 The kernel interacts with the machine's hardware.
 The shell interacts with the user.

a) The Kernel
 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 programs (the applications) that need to access the hardware use the services
of the kernel, which performs the job on the user's behalf.

Dept of CSE, Vemana I.T Page 3 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 These programs access the kernel through a set of functions called system calls.
 Functions of kernel:
 It manages the system's memory, schedules processes, decides their priorities
and so on.
 Process control
 Resources management.
 The kernel has to do a lot of this work even if no user program is running.
 The kernel is also called as the operating system - a programs gateway to the
computer's resources.

b) The Shell
 Computers don't have any capability of translating commands into action.
 That requires a command interpreter, also called as the shell.
 Shell is actually interface between the user and the kernel.
 Most of the time, there's only one kernel running on the system, there could be
several shells running – one for each user logged in.
 The shell accepts commands from user, if require rebuilds a user command, and
finally communicates with the kernel to see that the command is executed.
 Example: $ echo VTU Belagavi
#Shell rebuilds echo command by removing multiple spaces
VTU Belagavi
 Types of shell - There are 3 types of Shell:
1.) Bourne shell ($) ---- > Bash ($)
2.) C (%) ----- > tesh (%)
3.) korn ($)

2. The File and Process


 Two simple entities support the UNIX – the file and the process.
 “Files have places and processes have life”.
 The File:
 A file is just an array of bytes and can contain virtually anything.
 Every file in UNIX is part of the one file structure provided by UNIX.
 UNIX considers directories and devices as members of the file system.

 The Process:

Dept of CSE, Vemana I.T Page 4 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 The process is the name given to the file when it is executed as a program (Process
is program under execution).
 We can say process is an “time image” of an executable file.
 We also treat process as a living organism which have parents, children and are born
and die.

3. The System Calls


 The UNIX system-comprising the kernel, shell and applications-is written in C.
 Though there are several commands that use functions called system calls to communicate
with the kernel.
 All UNIX flavors have one thing in common – they use the same system calls.
 System Calls and Utilities:
 A system call is a function that is called from within a program to request a
service from the operating system kernel.
 A system call is just what its name implies - a request for the operating system to do
something on behalf of the user's program.
 The system calls are functions used in the kernel itself. To the programmer, the system
call appears as a normal C function call.
 UNIX system calls are used to manage the file system, control processes, and to
provide inter-process communication. The UNIX system interface consists of
about 80 system calls.

 Features Of UNIX
UNIX is an operating system, so it has all the features an operating system is expected to have:
 A Multiuser System
 A Multitasking System
 The building-block approach
 The UNIX toolkit
 Pattern Matching
 Programming Facility
 Documentation
 Portable
 Networking
 Organization of File System

Dept of CSE, Vemana I.T Page 5 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 Device Independence
 Utilities
 Services

1. A Multiuser System:
 UNIX is a multiprogramming system; it permits multiple programs to run and
compete for the attention of the CPU.
 This can happen in two ways:
 Multiple users can run separate jobs
 A single user can also run multiple jobs
2. A Multitasking System:
 A single user can also run multiple tasks concurrently.
 UNIX is a multitasking system.
 It is usual for a user to edit a file, print another one on the printer, send email to a
friend and browse www - all without leaving any of applications.
 The kernel is designed to handle a user's multiple needs.
 In a multitasking environment, a user sees one job running in the foreground; the
rest run in the background.
 User can switch jobs between background and foreground, suspend, or even
terminate them.

3. The Building-block Approach:


 The designer never attempted to pack too many features into a few tools.
 Instead, they felt “small is beautiful”, and developed a few hundred commands each
of which performed one simple job.
 UNIX offers the | (filters) to combine various simple tools to carry out complex jobs.
 Example:
$ cat note #cat displays the file contents
WELCOME TO HIT
$ cat note | wc #wc counts number of lines, words & characters in the
file
1 3 15

4. The UNIX Toolkit:


 Kernel itself doesn't do much useful task for users.

Dept of CSE, Vemana I.T Page 6 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 UNIX offers facility to add and remove many applications as and when required.
 Tools include general purpose tools, text manipulation tools, compilers, interpreters,
networked applications and system administration tools.

5. Pattern Matching:
 UNIX features very sophisticated pattern matching features.
 Example: The * (zero or more occurrences of characters) is a special character used
by system to indicate that it can match a number of filenames.
 Ex: ls chap* , ls chap+ , ls chap?

6. Programming Facility:
 The UNIX shell is also a programming language it was designed for programmer,
not for end user.
 It has all the necessary ingredients, like control structures, loops and variables, that
establish powerful programming language.
 These features are used to design shell scripts – programs that can also invoke UNIX
commands.
 Many of the system's functions can be controlled and automated by using these shell
scripts.

7. Documentation:
 The principal on-line help facility available is the man command, which remains the
most important references for commands and their configuration files.
 Apart from the man documentation, there's a vast ocean of UNIX resources
available on the Internet.
8. Portable: The programs that are written in C are easily moved from one hardware to
other hardware. It needs only standard compile.
9. Networking: Networking allows the users at one location to log into systems at other
sites. Once access is gained to a remote system user operate just as though they were on
their own system. TCP/IP protocol is used for communication
10. Organized File System: UNIX has a very organized file & directory system that allows
users to organize and maintain files.
11. Device Independence: UNIX treats input / output devices like ordinary files.
12. Utilities: UNIX provides rich library packages. A user can develop programs / products in
an efficient manner.

Dept of CSE, Vemana I.T Page 7 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
13. Services: It also includes the support utilities for system administration & control System
Administrator is responsible in maintaining system resources like Disk, security access.

 Unix Environments
1. Personal environment - Linux, Apple‟s System X (Unix kernel).
Allows single user to use Unix on personal computer.

2. Time-Sharing Environment - Many users connected to one computer.


In time sharing environment, many users are connected to one or more computers. All the
computing must be done by the central computers.

The central computer has to perform following jobs :


- Control the shared resources.
- Manage the shared data & printing
- Computing / Processing

3. Client/Server Environment - Computing split between a central computer (server)


and users‟ computers (clients).
A client server computing environment splits the computing function between a
central computer & user computers.

Dept of CSE, Vemana I.T Page 8 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
In client / server environment, the users microcomputers or workstations are called
client. The central computer, which may be powerful microcomputer, minicomputer
or a central mainframe is called server.

 POSIX And The Single UNIX Specification:


 Dennis Ritchie's decision to rewrite UNIX in C didn't make UNIX portable.
 UNIX fragmentation and absence of single standard affected the development of
portable applications.
 First, AT&T creates the System V Interface Definition (SVID).
 Later, X/Open – a consortium of vendors and users, created the X/Open Portability
Guide (XPG).
 Products of this specification were UNIX95, UNIX98 and UNIX03.
 Yet another group of standards, the POSIX (Portable Operating System for Computer
Environment) were developed by IEEE (Institute of Electrical and Electronics
Engineers).
 Two of the most important standards from POSIX are:
 POSIX.1 – Specifies the C application program interface – the system calls
(Kernel).
 POSIX.2 – Deals with the Shell and utilities.
 In 2001, a joint initiative of X/Open and IEEE resulted in the unification of two
standards.
 This is the Single UNIX Specification, Version 3 (SUSV3).
 The “Write once, adopt everywhere” approach to this development means that once
software has been developed on any POSIX machine it can be easily ported to another
POSIX compliant machine with minimum or no modification.

Dept of CSE, Vemana I.T Page 9 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)

 Locating Commands
 The UNIX is command-based system - i.e., things happens because the user enters
commands in.
 UNIX commands are seldom more than four characters long.
 All UNIX commands are single words like – cat, ls, pwd, date, mkdir, rmdir, cd, grep etc.
 The command names are all in lowercase.
Example: $ LS
bash: LS: command not found
 All UNIX commands are files containing programs, mainly written in C.
 All UNIX commands (files) are stored in directories (folders).
 If you want to know the location of executable program (or command), use type command
-
Example: $ type date
date is /bin/date
 When you execute date command, the shell locates this file in the /bin directory and makes
arrangements to execute it.

 The PATH
 The sequence of directories that the shell searches to look for a command is
specified in its own PATH variable.
 Example: $ echo $PATH
/bin: /usr/bin: /usr/local/bin: /usr/ccs/bin: /usr/local/java/bin:.

 External Commands And Internal Commands


1. External Commands:
 If the command (file) has an independence existence in the /bin directory, it is called
external command.
 Existence of the command can be seen as file
 Example: ls, ps, cat, who
$ type ls # ls is an external command ls is /bin/ls
 External commands are not built into the shell.

Dept of CSE, Vemana I.T Page 10 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 These are executable 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.
 For example, when you execute the "cat" command, which usually is at /usr/bin, the
executable /usr/bin/cat gets executed.

2. Internal commands:
 When the shell executes command(file) from its own set of built-in commands
that are not stored as separate files in /bin directory, it is called internal
command.
 Internal commands are something which is built into the shell.
 For the shell built in commands, the execution speed is really high.
 It is because no process needs to be spawned for executing it.
 For example, when using the "cd" command, no process is created.
 The current directory simply gets changed on executing it.
 If the command exists both as an internal and external one, shell executes internal
command only.
 Internal commands will have top priority compared to external command of same
name.
 Example: echo, exit, kill, cd
$ type echo # echo is an internal command echo is shell built-in

 Command Structure
 To understand power of UNIX, user must know syntax of important UNIX commands.
 The general syntax of UNIX command is – command arguments.
 Example: cat note

command argument
 Commands and arguments have to be separated by spaces or tabs to enable the system to
interpret them as words.
 UNIX arguments range from the simple to the complex.
 Arguments may consist of options, expressions, instructions and filenames etc.
 General Command Format

Dept of CSE, Vemana I.T Page 11 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)

 Options
 Options are special type of arguments mostly used with a minus (-) sign.
 An option is normally preceded by a minus (-) sign to distinguish it from filenames or other
arguments:
command –optionname
 Example: $ ls -l note # -l option list all the attributes of the file note
-rwxrwxrwx 1 mahesh mahesh 811 Jan 27 12:20 note
 Options can normally be combined with only one (-) sign, i.e., instead of using $ ls -l -a -t -
d you might as well use $ ls -latd.
 Example: $ ls -z note # Message from ls, not from shell ls:
illegal option –z

 Filename Arguments
 Many UNIX commands use a filename as argument so the command can take input from
the file.
 If a command uses a filename as argument, it will generally be its last argument.
 It's also quite common to see many commands working with multiple filenames as
arguments.
 The command with its arguments and options is known as the command line.
 This line can be considered complete only after the user has hit [Enter].
 The complete line is then fed to the shell as its input for interpretation and execution.
 Examples:
o $ ls -lat chap01 chap02 chap03 # Multiple filenames as arguments
o $ rm chap01 chap02
o $ cp chap01 chap01.bak

Dept of CSE, Vemana I.T Page 12 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 Exceptions
 There are some commands that don't accept any arguments.
 There are also some commands that may or may not be specified with arguments.
 The ls command can run without arguments (ls), with only options (ls -l) and also with only
filenames like: (ls chap01 chap02).
 Examples:
o $ pwd # pwd prints the current working directory /root
o $ who # who lists currently logged in users
o mahesh tty7 2013-01-30 09:08
o mahesh pts/1 2013-01-30 10:20 (:0)

 Flexibility Of Command Usage


 The UNIX system provides certain degree of flexibility in the usage of commands.
 A command can often be entered in more than one way.
 Shell allow the following type of command usage
o Combining Commands.
o A command line can overflow or Be split into multiple lines.
o Entering a command before previous command has finished.

1. Combining Commands
 UNIX allows you to specify more than one command in the single command line.
 Example: $ ( wc note; ls -l note ) #Two commands combined here using ; &
parenthesis
2 3 16 note
-rw-rw-r-- 1 mahesh mahesh 16 Jan 30 09:35 note
$ ls | wc #Two commands combined here using filter
115 166 1227

2. A Command Line Can Overflow or Be split into Multiple Lines


 A Command Line can Overflow or Be split into Multiple Lines
 UNIX terminal width is restricted to maximum 80 characters.
 Shell allows command line to overflow or be split into multiple lines.
 Example: $ echo “This is # $ first prompt
> a three-line # > Second prompt

Dept of CSE, Vemana I.T Page 13 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
> text message” #Command line ends here
This is
a three-line
text message
3. Entering a Command Before Previous Command Has Finished
 UNIX provides a full-duplex terminal which lets you type a command at any time,
and rest assured that the system will interpret it.
 When you run a long program, the prompt won't appear until program execution is
complete.
 Subsequent commands can be entered at the keyboard without waiting for prompt.
 The input remains stored in a buffer maintained by kernel for all keyboard input.
 The command is passed on to the shell for interpretation after the previous program
has completed.

 Basic Unix Commands


 cal: THE COMMAND:
 cal command can be used to see the calendar of any specific month or a
complete year.
 Syntax: cal [ [ month] year ]]
 Everything within the rectangular box is optional. So, cal can be used without
any arguments, in which case it displays the calendar of the current month.
 Example: $ cal September 2017
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
 The syntax show that cal can be used with arguments, the month is optional but
year is not.
 To see the calendar of month August 2017, we need to use two arguments as
shown below:
 Example1: $ cal 8 2017
August 2017
Su Mo Tu We Th Fr Sa

Dept of CSE, Vemana I.T Page 14 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

 Example2: bash-4.1$ cal


September 2020
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

 Example3: bash-4.1$ cal 10 2020


October 2020
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
bash-4.1$ cal 2020
Display calendar of 2020 year

 To make cal pause using pager using the “ | “ symbol to connect them.
 Example: $cal 2017 | more
date: DISPLAYING THE SYSTEM DATE
 One can display the current date with the date command, which shows the date
and time to the nearest second:
 Example: $ date
Mon Sep 4 16:40:02 IST 2017
 The command can also be used with suitable format specifiers as arguments.

Dept of CSE, Vemana I.T Page 15 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 Each symbol is preceded by the “ + “ symbol, followed by the “ % “ operator, and
a single character describing the format.
 Syntax: $ date +%format_specifier
 For instance, you can print only the month using the format +%m:
 Example:
$ date +%m
09
 Or can print the month name:
 Example:
$ date +%h
Sep
 Or You can combine them in one command:
 Example:
$ date + “%h %m”
Sep 09

 There are many other format specifiers, and the useful ones are listed below:
 d – The day of month (1 - 31)
 y – The last two digits of the year.
 H, M and S – The hour, minute and second, respectively.
 D – The date in the format mm/dd/yy
 T – The time in the format hh:mm:ss

 echo: Displaying the Message:


 echo command is used in shell scripts to display a message on the terminal, or to
issue a prompt for taking user input.
 Example:
$ echo "Enter your name:\c"
Enter your name:$_
$echo $SHELL
/usr/bin/bash

bash-4.1$ echo "ENTER YOUR NAME:"


ENTER YOUR NAME:
bash-4.1$

Dept of CSE, Vemana I.T Page 16 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
bash-4.1$ echo -e "ENTER YOUR NAME:\c"
ENTER YOUR NAME:bash-4.1$
bash-4.1$ echo -e "ENTER YOUR NAME:\t"
ENTER YOUR NAME:
bash-4.1$
bash-4.1$ echo -e "ENTER YOUR NAME:\n"
ENTER YOUR NAME:
bash-4.1$
 Echo can be used with different escape sequences.

 who: WHO ARE THE USERS?:


bash-4.1$ who
student tty1 2020-09-07 09:52 (:0)
student pts/0 2020-09-07 09:53 (:0.0)

bash-4.1$ who -H (H- Header)


NAME LINE TIME IDLE PID COMMENT
student tty1 2020-09-07 09:52 old 2133 (:0)
student pts/0 2020-09-07 09:53 . 2584 (:0.0)
bash-4.1$

bash-4.1$ who am i
student pts/0 2020-09-07 09:53 (:0.0)

Dept of CSE, Vemana I.T Page 17 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
bash-4.1$

bash-4.1$ ls
1.c ccittu.c~ Documents Music Pictures sample.odt te.cpp
text
array.c~ Desktop Downloads pcd Public s.doc Templates
text1.c
bash-4.1$

 ps: process status:


bash-4.1$ ps
PID TTY TIME CMD
2588 pts/0 00:00:00 bash
2968 pts/0 00:00:00 ps
bash-4.1$

 printf: AN ALTERNATIVE TO ECHO:


 The printf command is available on most modern UNIX systems
 it can be used instead of echo.
 The command in the simplest form can be used in the same way as echo:
$ printf "Enter your name\n"
Enter your name
$_
 printf also accepts all escape sequences used by echo, but unlike echo, it doesn‟t
automatically insert newline unless the \n is used explicitly.
 printf also uses formatted strings in the same way the C language function of the
same name uses them:
$ printf "My current shell is %s\n" $SHELL
My current shell is /bin/bash $_
 The %s format string acts as a placeholder for the value of $SHEL
 printf replaces %s with the value of $SHELL.
 %s is the standard format used for printing strings.
 printf uses many of the formats used by C‟s printf function.

Dept of CSE, Vemana I.T Page 18 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 Here are some of the commonly used ones:
o %s – String
o %30s – As above but printed in a space 30 characters wide
o %d – Decimal integer
o %o – Octal integer
o %x – Hexadecimal integer
o %f – Floating point number
 Example:
$ printf "The value of 255 is %o in octal and %x in
hexadecimal\n" 255 255
The value of 255 is 377 in octal and ff in hexadecimal

 bc: binary calculator:


bash-4.1$ bc
bc 1.06.95
2+3
5
4*5
20
3/2
1
scale=2
3/2
1.50
scale=3
4/3
1.333
ibase=2
100
4
111
7
obase=2
4

Dept of CSE, Vemana I.T Page 19 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
100
7
111

 Type Command
 All UNIX commands are files containing programs, mainly written in C.
 All UNIX commands(files) are stored in directories(folders).
 If you want to know the location of executable program (or command), use type
 Command.
 Example:
$ type date
date is /bin/date
 When you execute date command, the shell locates this file in the /bin directory and makes
arrangements to execute it.
 The PATH:
 The sequence of directories that the shell searches to look for a command is specified
in its own PATH variable.
 Example:
$ echo $PATH
/bin: /usr/bin: /usr/local/bin: /usr/ccs/bin: /usr/local/java/bin:

 Root login
 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:
o Becoming the super at login time
Login: root
Password: ********* [Enter]
#-
 The prompt of root is #Once you login as a root you are placed in root‟s home
directory. Depending on the system, this directory could be / or /root.

Dept of CSE, Vemana I.T Page 20 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 Administrative commands are resident in /sbin and /usr/sbin in modern
systems and in older system it resides in /etc.
 Roots PATH list includes detailed path, for example:
/sbin:/bin:/usr/sbin:/usr/bin:/usr/dt/bin
o Becoming the super user using su command
 su: Acquiring superuser status
 Any user can acquire superuser status with the su command if they know the
root password.
 For example, the user abc becomes a superuser in this way:
$ su
Password: *********** #Password of root user
#pwd
/home/abc
 Though the current directory doesn‟t change, the # prompt indicates that abc
now has powers of a superuser. To be in root‟s home directory on superuser
login, use su –l.

Chapter -2 of Module1
UNIX Files

 The File
 The file is the container for storing information.
 Neither a file's size nor its name is stored in file.

Dept of CSE, Vemana I.T Page 21 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 All file attributes such as file type, permissions, links, owner, group owner etc are kept in a
separate area of the hard disk, not directly accessible to humans, but only to kernel.
 The UNIX has divided files into three categories:
o Ordinary file – also called as regular file. It contains only data as a stream of
characters.
o Directory file – it contains files and other sub-directories.
o Device file – all devices and peripherals are represented by files

 What‟s In A (File) Name?


 A filename can consist up to 255 characters.
 File may or may not have extensions, and consist of any ASCII character expect the / &
NULL character.
 Users are permitted to use control characters or other unprintable characters in a filename.
 Examples - .last_time list. @#$%*abcd a.b.c.d.e
 But, it is recommended that only the following characters be used in filenames-
1. Alphabetic characters and numerals
2. the period(.), hyphen(-) and underscore(_)
 There are few restrictions like:
1. Limit the length of a filename to 14 character.
2. We can use dividers like .(dot),_(underscore),-(hypen).
3. Never start a filename with dot(.), the filename that start with .(dot) are hidden files
in UNIX.
4. Hidden files are created and used by the system.

 File Types
 The UNIX has divided files into three categories:
1. Ordinary file – also called as regular file. It contains only data as a stream of
characters.
2. Directory file – it contains files and other sub-directories.
3. Device file – all devices and peripherals are represented by files.
 Ordinary File – ordinary file itself can be divided into two types
a) Text File – it contains only printable characters, and you can often view the
contents and make sense out of them.
Example: C, C++, Java files are text files

Dept of CSE, Vemana I.T Page 22 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
b) Binary file – it contains both printable and unprintable characters that cover
entire ASCII range.
Examples: Most Unix commands, executable files, pictures, sound and
video files are binary.
 Directory File –
 a directory contains no data but keeps some details of the files and
subdirectories that it contains.
 A directory file contains an entry for every file and subdirectories that it
houses.
 If you have 20 files in a directory, there will be 20 entries in the directory.
 Each entry has two components-
a) The filename
b) A unique identification number for the file or directory (called as inode
number).
 Device File –
 All devices & peripherals are represented by files to read or to write a device.
 Installing software from CD-ROM, printing files and backing up data files to
tape.
 All of these activities are performed by reading or writing the file representing
the device.
 Advantage of device file is that some of the commands used to access an
ordinary file also work with device file.
 Device filenames are generally found in a single directory structure, /dev.

 Character Special (Device) File: Character device file is a physical device, that
reads or writes one character at a time.
 Ex: Terminal

 Block Special (Device) File – Block device file is a physical device, that reads or writes one
block at a time. Ex: Disk

 Symbolic Link (soft) file – It is a logical file that defines the location of another file.

 FIFO file – A first-in first-out is also known as a PIPE used in inter-processor communication.

Dept of CSE, Vemana I.T Page 23 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 SOCKET file – A socket file is special file that is used for network communication. A Unix
socket (or Inter-process communication socket) is a special file which allows for advanced
inter-process communication. A Unix Socket is used in a client-server application framework.

 Organization of files
 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.

 Hidden Files
 UNIX allows users to have files which are not listed, by default, by the ls command.
 These are called hidden files and are distinguishable from other files by the fact that their
filenames begin with a dot (.).
 Such a file is .profile which is executed every time you log in to the system.
 Hidden files are listed by adding the -a option to the ls command.
 For example:
Type: ls –a
 They are commonly used for storing user preferences or preserving the state of a utility, and
are frequently created implicitly by using various utilities.

 Standard Directories (UNIX Filesystem)


 Following are the directories that exist on the major versions of Unix
 / (root directory): This contains only the directories needed at the top level of
the file structure.
 /bin: This contains common programs, shared by the system, the system
administrator and the users.

Dept of CSE, Vemana I.T Page 24 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 /dev: This contains references to all the CPU peripheral hardware, which are
represented as files with special properties. These are device drivers.
 /etc: This contains system configuration files.
 /home: This contains home directories of the common users.
 /lib: This contains library files, including files for all kinds of programs needed
by system and users.
 /sbin: This contains programs for use by the system and the system
administrator.
 /tmp: This contains temporary space for use by the system. This space is
cleaned upon reboot. So, don't use this for saving any work.
 /usr: This contains programs, libraries, documentation etc. for all user-related
programs.
 /var: This contains storage for all variable files and temporary files created by
users, such as:
→ log files
→ mail queue or
→ print spooler area

 The PARENT-CHILD Relationship


 The files in UNIX are related to one another.
 The file system in UNIX is a collection of all of these files (ordinary, directory and device
files) organized in a hierarchical (an inverted tree) structure as shown in below figure.

Dept of CSE, Vemana I.T Page 25 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)

 The feature of UNIX file system is that there is a top, which serves as the reference point for
all files.
 This top is called root and is represented by a / (Front slash).
 The root is actually a directory.
 The root directory (/) has a number of subdirectories under it.
 The subdirectories in turn have more subdirectories and other files under them.
 Every file apart from root, must have a parent, and it should be possible to trace the ultimate
parentage of a file to root.
 In parent-child relationship, the parent is always a directory.

 The HOME Variable: HOME DIRECTORY


 When you logon to the system, UNIX places you in a directory called home directory.
 It is created by the system when the user account is created.
 If a user login using the login name vemana, user will land up in a directory that could have
the path name /home/vemana.
 The shell variable HOME knows the home directory.
$echo $HOME
/home/vemana

 The PATH variable


Dept of CSE, Vemana I.T Page 26 of 39
UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)

• This variable specifies the locations in which the shell should look for commands.

• Example:
$ echo $PATH
/bin: /usr/bin:

• 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 also includes the current directory.

• Whenever you enter any UNIX command, you are actually specifying the name of an
executable file located somewhere on the system.

• The system goes through the following steps in order to determine which program to
execute:
 Built in commands (such as cd and history) are executed within the shell.
 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.
 Otherwise, the PATH variable is used.

 Relative And Absolute Path


• Absolute Path:
 Absolute path name: The pathname begins with / & denotes the file location
with respect to the root (/) is called absolute path name.
 Ex: /home/student
 Directories are arranged in a hierarchy with root (/) at the top. The position of
any file within the hierarchy is described by its pathname.
 Elements of a pathname are separated by a /.
 A pathname is absolute, if it is described in relation to root, thus absolute
pathnames always begin with a /.
 Following are some examples of absolute filenames:
o /etc/passwd
o /users/student/pcd/binomial
o /dev/rdsk/Os3

 Relative Path:

Dept of CSE, Vemana I.T Page 27 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 A pathname can also be relative to your current working directory. Relative
pathnames never begin with /. Relative to user home directory, some pathnames
might look like this – cs/3cs ec/3ec.
 Using . and .. in relative path name –
- User can move from working directory /home/student/cs/3cs to home
directory /home/student using cd command like:
$pwd
/home/student/cs/3cs
$cd /home/student
$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/student/cs/3cs
$pwd
/home/student/cs/3cs
$cd ..
$pwd
/home/student/cs

 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/student/cs/3cs
$cd ../..
$pwd
/home

 Directory Command
 Any time user can know the current working directory using pwd command.
 Example:

Dept of CSE, Vemana I.T Page 28 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
bash-4.1$ pwd
/home/student
 Like HOME it displays the absolute path.
1. cd: Changing The Current Directory:
 User can move around the UNIX file system using cd (change directory) command.
 When used with the argument, it changes the current directory to the directory
specified as argument.
 Example:
bash-4.1$ cd pcd
bash-4.1$ pwd
/home/student/pcd
 Here we are using the relative pathname of pcd directory. The same can be done
with the absolute pathname also. Example:
bash-4.1$ cd /home/student/unix
bash-4.1$ pwd
/home/student/unix
bash-4.1$ pwd
/home/student
bash-4.1$ cd .
bash-4.1$ pwd
/home/student
bash-4.1$ cd ..
bash-4.1$ pwd
/home
bash-4.1$ cd /bin
bash-4.1$ pwd
/bin
 cd can also be used without arguments:
bash-4.1$ cd pcd
bash-4.1$ pwd
/home/student/pcd
bash-4.1$ cd
/home/student

Dept of CSE, Vemana I.T Page 29 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
2. mkdir: Making Directories:
 Directories are created with mkdir (make directory) command. The command is
followed by names of the directories to be created.
 Example:
bash-4.1$ mkdir vemana
bash-4.1$ ls
1.c demo.txt Documents pcd
roopa.txt vemana array.c Desktop DownloadsPictures
Templates ccittu.c directorycommand Music Public Unix
bash-4.1$ cd vemana
 You can create a number of subdirectories with one mkdir command:
bash-4.1$ mkdir cs is ec
bash-4.1$ ls
cs ec is
 For instance, the following command creates a directory tree:
bash-4.1$ mkdir cs/3cs cs/4cs cs/2cs
bash-4.1$ cd cs
bash-4.1$ ls
2cs 3cs 4cs
 The order of specifying arguments is important. You cannot create subdirectories
before creation of parent directory. For instance following command doesn„t work:
bash-4.1$ cd pcd
bash-4.1$ mkdir prog prog/prog1 prog/prog2
bash-4.1$ ls
binomial prog
bash-4.1$ mkdir file/text file/ppt file
mkdir: cannot create directory `file/text': No such file or
directory
mkdir: cannot create directory `file/ppt': No such file or
directory
bash-4.1$

3. rmdir: Removing A Directory:


 The rmdir (remove directory) command removes the directories.
 Example:

Dept of CSE, Vemana I.T Page 30 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
bash-4.1$ rmdir 3cs
bash-4.1$ ls
2cs 4cs
 If 3cs is empty directory then it will be removed form system:
bash-4.1$ rmdir cs
rmdir: failed to remove `cs': Directory not empty
 rmdir expect the arguments reverse of mkdir:
bash-4.1$ rmdir cs/2cs cs/4cs cs
bash-4.1$ ls
ec is
 First subdirectories need to be removed from the system then parent.
 Following command works with rmdir.

 System refuses to create a directory due to following reasons:


i. User doesn„t have permission to create directory. (i.e. directory write
protected).
ii. The directory already exists.
iii. There may be ordinary file by that name in the current directory.
 To remove your current directory 1st we should move to parent directory of current
directory. For Example:
bash-4.1$ cd ec
bash-4.1$ ls
bash-4.1$ rmdir ec
rmdir: failed to remove `ec': No such file or directory
bash-4.1$ cd ..
bash-4.1$ rmdir ec is
bash-4.1$ ls
bash-4.1$ rmdir vemana
rmdir: failed to remove `vemana': No such file or directory
bash-4.1$ cd ..
bash-4.1$ rmdir vemana
bash-4.1$ ls
1.c demo.txt Documents pcd roopa.txt array.c~ Desktop
DownloadsPictures Templates ccittu.c~ directorycommand
Music Public unix

Dept of CSE, Vemana I.T Page 31 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 The order of specifying arguments is important. You cannot delete parent directories
before deletion of subdirectories
 System refuses to delete a directory due to following reasons:
i. User doesn„t have permission to delete directory. (i.e. write protected
directory).
ii. The directory doesn„t exist in system.
iii. The directory is your present working directory.
 The dot (.) and double dots (..) notations in relative pathnames:
- User can move from working directory /home/kumar/progs/cprogs to home
directory /home/kumar using cd command like:
$pwd
/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

Dept of CSE, Vemana I.T Page 32 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
- The following command copies the file prog1.java present in javaprogs,
which is present is parent of current directory to current directory.
$pwd
/home/kumar/progs/cprogs
$cp ../javaprogs/prog1.java .
- Now prog1.java is copied to cprogs under progs directory.

 File Related Commands


1. cat command:
 This command can be used to display the content of a file on the terminal.
 Syntax: cat FILENAME
 Example:
$ cat F1
WELCOME TO UNIX // contents of F1
$ cat F2
WELCOME TO PERL // contents of F2
 This command can also accept more than one filename as arguments.
 Example:
$ cat F1 F2
WELCOME TO UNIX // contents of F1
WELCOME TO PERL // contents of F2

 Here, the content of the second file is shown immediately after the first file. So, this
command concatenates two files; hence its name (cat).
 This command can also be used to create a new file.
 Syntax: cat > FILENAME
 Example:
$ cat > F3
WELCOME TO SHELL // contents of F3
[ctrl-d] // Terminates F3
$ cat F3
WELCOME TO SHELL // contents of F3
 Using cat to redirect the contents of two files to another file :The following
command redirects the contents of the files named F1 and F2 to the file named all:
$cat F1 F2 > new

Dept of CSE, Vemana I.T Page 33 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 To view the contents of the file named new we will also use the cat command as we
did in the first example:
$cat new
WELCOME TO UNIX // contents of F1
WELCOME TO PERL // contents of F2
 appending the contents ( >> ):
cat F3 new >> new1
cat new1
WELCOME TO SHELL // contents of F3
WELCOME TO UNIX // contents of F1
WELCOME TO PERL // contents of F2
 cat Options:
1) Displaying Non-printing Characters (-v):
- By default, without any option, this command displays only printing ASCII
characters of the file.
- -v option can be used to display even non-printing ASCII characters of the file.
2) Numbering Lines (-n):
- -n option can be used to number the lines of the file.
- This option helps the programmer in debugging programs.

2. mv: Renaming Files:


 This command renames or moves files.
 This command can be used to rename a file in the current directory.
 Syntax: mv OldFileName NewFileName
 It doesn't create a copy of the file; it merely renames it.
 No additional space is consumed on disk during renaming.
 Example:
$ mv OldFile NewFile // renames the OldFile by NewFile
 This command can also be used to move a group of files to a directory.
 In this case, the last filename must be a directory.
 Example:
$ mv FILE1 FILE2 FILE3 module // moves 3 files to "module"
directory

3. rm: Deleting Files:

Dept of CSE, Vemana I.T Page 34 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 This command can be used to delete a file.
 Syntax: rm FileName
 Example:
$ rm FILE1 // deletes FILE1
$ rm FILE1 FILE2 FILE3 // deletes three files
$ rm *.pdf // deletes all files with extensions .pdf in the currant directory
 rm Options :
i. Interactive Deletion (-i):
o –i option can be used to warn the user before deleting the file.
o Example:
$ rm –i FILE1 FILE2 // delete FILE1 & FILE2 if "Y"
is entered
rm: remove FILE1 (yes/no)? Y
rm: remove FILE2 (yes/no)? Y

ii. Recursive Deletion (-r or -R):


o –R option can be used to recursively delete an entire directory
structure.
o Entire directory including all files in its subdirectories will be deleted.
o Example:
$ rm –r DIR1 // delete DIR1 and all its subdirectories & files
$ rm -r * // deletes all files in the current directory and all its
subdirectories.

iii. Forceful Deletion (-f):


o By default, this command cannot delete a file which is write-
protected.
o –f option can be used to delete even the write-protected file.
o Example:
rm -rf * // deletes all files in the current directory and all its
subdirectories.

4. cp: Copying A File:


 This command is used to copy a file(s) from one location to another location.
 It creates an exact image of the file on the disk with a different name.

Dept of CSE, Vemana I.T Page 35 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
 Syntax: cp SOURCE DESTINATION
 This command can be used to copy a file within current working directory.
 Example:
$ cp FILE1 FILE2 // copies contents of FILE1 to FILE2 in current working
directory
 Here,
i. If the destination file doesn„t exist, it will first be created before copying
takes place.
ii. If the destination file exists, it will be overwritten without any warning from
the system.
 This command can also be used to copy a file to the another directory.
 Example:
$ cp FILE1 part2/FILE2
 Here, this copies the file FILE1 from your current working directory to the file FILE2
in the subdirectory "part2".
 This command can also be used with (.)(dot) to signify the current directory as the
destination.
 Example:
$ cp part2/FILE2 . same as $ cp part2/FILE2 FILE2
 Here, this copies the file FILE2 in the subdirectory "part2" to your current working
directory.
 This command can also accept more than 2 filenames as arguments.
 In this case, the last filename must be a directory.
 Example:
$ cp FILE1 FILE2 FILE3 module // copies 3 files to "module" directory
 Here, "module" directory should already exist because cp cannot create a directory.
 This command can also be used with metacharcters (* or ?).
 Example:
cp *.pdf DIR1 //copies all the files with extensions .pdf to directory DIR1
 cp Options:
i. Interactive Copying (-i):
o –i option can be used to warn the user before overwriting the
destination file.
o Example:

Dept of CSE, Vemana I.T Page 36 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
$ cp –i FILE1 FILE2 // copies contents of FILE1 to FILE2 if
"Y" is entered
$ cp: overwrite FILE2 (yes/no)? Y
ii. Recursive Copying (-R):
o –R option can be used to recursively copy an entire directory structure
from one location to another location.
o Entire directory including all files in its subdirectories will be copied.
o Example:
cp –R DIR1 DIR2
o If directory DIR2 doesn‟t exist, cp creates it along with the associated
subdirectories.

5. wc: COUNTING LINES, WORDS AND CHARACTERS:


 This command can be used to get a count of the total number of lines, words, and
characters contained in a file.
 Syntax: $wc FILENAME
 Example:
$ cat F1
WELCOME // contents of P1.c
TO
UNIX
$ wc F1
3 3 15 F1
 This command can also accept more than one filename as arguments.
 Example:
$ wc F1 F2
3 3 15 F1
6 6 15 F2
3 15 F3
 wc Options:
o –l option can be used to count only number of lines
o –w option can be used to count only number of words
o –c option can be used to count only number of characters
 Example:
$ wc -l F1

Dept of CSE, Vemana I.T Page 37 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)
3 F1
$ wc -c F1
15 F1

6. od: DISPLAYING DATA IN OCTAL:


 od command displays the contents of executable files in a ASCII octal value.
 Example:
$ cat chap01
abcd efgh // content of file chap01
bcd
efgh
 Each line displays 16 bytes of data in octal, preceded by the offset in the file of the
first byte in the line.

 od Options:
i. byte (-b):
o -b option can be used to display octal value of each printable character.
o Each line displays 16 bytes of data in octal, preceded by the offset of
the first byte in the line.
o Example:
$ od –b chap01
offset <-------------- 16 bytes of data in octal ------------------->
0000000 141 142 143 144 040 145 146 147 148 040 040 040
040 040 040 012
0000020 141 142 143 144 040 145 146 147 148 040 040 040
040 040 040 012
ii. character (-c):
o -c option can be used to display the printable characters and its
corresponding octal value.
o Example:
$ od –bc chap01
0000000 141 142 143 144 040 145 146 147 148 040 040 040 040 040 040 012
a b c d e f g f \n
0000020 141 142 143 144 040 145 146 147 148 040 040 040 040 040 040 012
a b c d e f g f \n

Dept of CSE, Vemana I.T Page 38 of 39


UNIX System Programming(BCS515C) Module-1 Introduction, Unix files
Programming(BCS5151C)

Dept of CSE, Vemana I.T Page 39 of 39

You might also like