0% found this document useful (0 votes)
10 views109 pages

Introduction to UNIX Programming Basics

The document outlines the UNIX Programming course (BCS515C), detailing its syllabus, assessment methods, and expected outcomes for students. It covers fundamental UNIX concepts, commands, and architecture, including the history and features of UNIX, as well as the structure of UNIX commands. Additionally, it discusses the POSIX standards aimed at ensuring compatibility across different UNIX systems.

Uploaded by

ashwinikamath
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)
10 views109 pages

Introduction to UNIX Programming Basics

The document outlines the UNIX Programming course (BCS515C), detailing its syllabus, assessment methods, and expected outcomes for students. It covers fundamental UNIX concepts, commands, and architecture, including the history and features of UNIX, as well as the structure of UNIX commands. Additionally, it discusses the POSIX standards aimed at ensuring compatibility across different UNIX systems.

Uploaded by

ashwinikamath
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

UNIX PROGRAMMING (BCS515C)

Module 1
Introduction
Text Books

1. Sumitabha Das., Unix Concepts and Applications.,


4thEdition., Tata McGraw Hill
2. W. Richard Stevens: Advanced Programming in the
UNIX Environment, 2nd Edition, Pearson Education,
2005
Course outcome (Course Skill Set)

At the end of the course, the student will be able to:


● Demonstrate the basics of Unix concepts and commands.
● Demonstrate the UNIX file system.
● Apply comands to reflect changes in file system.
● Demonstrate IPC and process management.
● Develop an application/service over a Unix system.
Assessment Details

•The weightage of Continuous Internal Evaluation (CIE) is


50% and for Semester End Exam (SEE) is 50%.
•The minimum passing mark for the CIE is 40% of the
maximum marks (20 marks out of 50) and for the SEE
minimum passing mark is 35% of the maximum marks (18
out of 50 marks).
Continuous Internal Evaluation:
● For the Assignment component of the CIE, there are 25
marks and for the Internal Assessment Test component,
there are 25 marks.
● Any two assignment methods mentioned in the
22OB2.4, if an assignment is project-based then only one
assignment for the course shall be planned.
● For the course, CIE marks will be based on a scaled-
down sum of two tests and other methods of
assessment.
Syllabus 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.
The OPERATING SYSTEM (OS)
• An operating system (OS) is an interface between hardware and
user. It manages hardware and software resource.
• It takes the form of a set of software routines that allow users and
application programs to access system resources (e.g. the CPU,
memory, disks, modems, printers, network cards etc.) in a safe,
efficient and abstract way.
The following actions are common to most OS
• The OS allocates memory for the program and loads the program
to the allocated memory
• It loads the CPU registers with control information related to
the program.
• The instructions provided in the program are executed by CPU.
• The program can access the hardware through OS.
• After the execution of the program OS cleans up the memory
and registers and make them available for next program
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 8
chikmagalur
History
• In the late 1960s, researchers from General Electric, MIT and Bell Labs
launched a joint project to develop an ambitious multi-user,
multi- tasking OS for mainframe computers known as MULTICS
(Multiplexed Information and Computing System).
• MULTICS failed. but it did inspire Ken Thompson, who was a
researcher at Bell Labs, to have a go at writing a simpler operating
system.
• He wrote a simpler version of MULTICS on a PDP7 in assembler
and called his attempt UNICS (Uniplexed Information and
Computing System)
• Because memory and CPU power were at a premium in those days,
UNICS (eventually shortened to UNIX) used short commands to
minimize the space needed to store them and the time needed
to decode them - hence the tradition of short.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 9
chikmagalur
• The limitation of UNIX was not portable.
• In order to overcome the limitation , Ken Thompson
started to work on the development of system
using higher level language called B Language.
• As B language did not yield expected results, Dennis
Ritchie developed higher level language called C.
• Ken Thompson then teamed up with Dennis Ritchie, the
author of the first C compiler in 1973.
• They rewrote the UNIX kernel in C - this was a big
step forwards in terms of the system's portability -
and released the fifth Edition of UNIX to universities in
1974.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 10
chikmagalur
UNIX ARCHITECTURE

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 11
chikmagalur
Kernel:
• Kernel is the core of 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.
• The kernel manages system memory, processes,
decides priorities.
Shell:
• Shell interface between Kernel and User. It functions
as command interpreter i,e it receives and interprets
the command from user and interacts with the
hardware.
• There is only one kernel running on the system, there
could be several shells in action- one for each user who
is logged in.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 12
chikmagalur
Files and Process:
• File is an array of bytes and it contain virtually anything.
• Unix considers even the directories and devices as members
of file system.
• The default file type is text and behavior of system is mainly
controlled by text files.

• The second entity is the process, which is the name given to


a file when it is executed as a program. Process is simply a
time image of an executable file.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 13
chikmagalur
System Calls:
• There are thousands of commands in the Unix system, they
all use functions called system calls.

• User programs that need to access the hardware use the


services of the kernel, which performs the job on users
behalf.
• These programs access the kernel through a set of functions
called system calls.
Ex:
• open() system call to access both file and device.
• write() system call to write a file.
FEATURES OF UNIX
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. Because C programs are easily moved from one hardware
environment to another, it is relatively simple to port it to different
environments.
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.
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).
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 15
chikmagalur
Organized File System: UNIX has a very organized file and
directory system that allows users to organize and
maintain files.
Device Independence: UNIX treats input/output devices
like ordinary files. Input or output to a program can be
from any device or file. The source or destination for file
input and output is easily controlled through a UNIX
design feature called redirection.
Utilities: UNIX provides a rich library of utilities that can be
use to increase user productivity.
Services: UNIX also includes the support utilities for system
administration and control.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 16
chikmagalur
THE UNIX ENVIRONMENTS
• As UNIX is a multiuser system, to facilitate several users
at a time, terminals are provided.
• A terminal is combination of a monitor and a keyboard.
• User sitting at a terminal will login to his account through
his user ID and does his job. Once he finishes his work, he
can logout.
• There will be multiple users working like this and all are
accessing same CPU.
• UNIX user can have his own workstation. That is, one
can have dedicated CPU with RAM, hard disk etc
for individual purpose. This is also known as desktop PC.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 17
chikmagalur
• Sometimes, many workstations can be connected to
server via network.
• In such situations each workstation provides a terminal
emulation facility, which will help a workstation to
behave as a simple terminal.
• Now, the workstation will share the resources (CPU,
memory etc) of the server for most of the tasks. Its own
resources are used only for running terminal emulation
software.
• When the workstation is acting like a terminal (that is,
when emulation software is running), then all files
created will be stored in a server and, the programs
being run will use server’s memory and CPU.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 18
chikmagalur
Logging in:
• A large set of networked systems is maintained by system
administrator. He is responsible for creating and maintaining user
accounts, maintaining file systems, taking data backups,
managing disk space etc.
• Every user of UNIX will have a separate account which has to be
accessed by giving appropriate login-id or username and password.
• The account of system administrator is called as root.
• A program called shell will be always running behind and it
facilitates logging process as well. It is a command interpreter
between the user and the UNIX kernel.
• For logging in, the prompt displayed would be
login:
or
username:
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 19
chikmagalur
• Then the user has to provide his login id. Immediately after
that, he will be asked to enter the password. And, typed
password will be hidden.
• For example, if the login id is root, we can see
login: root (press enter key)
password: ******* (invisible)
• If any one of the login id and password is entered
incorrectly, then following error message will be displayed.
login incorrect
login:
• If the entered values are correct, then one may see –
Last login: Thu Aug 22 15:38:33 on tty2
$_
• The message displayed here indicates the last time the user
logged in. The prompt displayed here is $ followed by a
cursor blinking (the underscore _).
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 20
chikmagalur
• There are various shells like Bourne Shell (bsh), Bourne
again shell (bash), Korn Shell (ksh), C Shell(csh) etc.,
the prompt is $ for any normal user.
• For the administrator, usually the prompt would be %.
Some shells shows the users current directory as
[home/root]
• On the other hand, the Linux systems will display some
informative prompts instead of % or $.
• The information at the prompt will be like machine
name, username and current directory.
• For example,
[root@localhost root]$

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 21
chikmagalur
Logging out:
• It is a regular practice to log out of the system when the user
finishes all the jobs. The procedure for logging out of the system
will be depending on the shell used.
• The appearance of the text ‘login:’ confirms that the user has
successfully logged out.
Following are the various possibilities:
• [ctrl + d] (pressing the keys ctrl and d together, and then press
enter key) login:
• logout ( the command logout and then press enter key) login:
• exit (the command exit and then press enter key)
login:
• The C shell uses the command logout and the [ctrl+d] and
exit are used by other shells.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 22
chikmagalur
POSIX AND SINGLE UNIX SPECIFICATION

• Because many versions of UNIX exists and each of them


provides its own set of application programming
interface(APIs) functions, it is difficult for system developers
to create applications that can be easily ported to
different versions of UNIX.
• To overcome this problem, the IEEE society formed a special
task force called POSIX in 1980s to create a set of standards for
OS interfacing.
• Several subgroups of the POSIX such as POPSIX.1,
POSIX.1b and POSIX.1c are concerned with the development
of a set of standards for system developers

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 23
chikmagalur
• First, AT&T created 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:
1. POSIX.1 – Specifies the C application program interface – the system
calls(Kernel).
2. 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.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 24
chikmagalur
UNIX COMMAND STRUCTURE
• To understand power of UNIX, user must know
syntax of important UNIX commands.
• The general syntax of UNIX command is
command [-option(s)] [argument(s)]
• 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.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 25
chikmagalur
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.
Example: $ ls -l note
• -l option list all the attributes of the file note
• -rwxrwxrwx 1 root root 811 Jan 27 12:20 note
$ ls -z note
• Message from ls, not from shell ls: illegal option –z
• Options can normally be combined with only one (-)
sign, i.e., instead of using
• $ ls -l -a -t -d we can use $ ls -latd

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 26
chikmagalur
Filename Arguments
• Many UNIX commands use a filename as argument so
the command can take input from the file and filename
will generally be its last argument.
• We can have commands with multiple filenames as
arguments.
• The command with its arguments and options is known
as the command line.
• The complete line is then fed to the shell(press Enter)
as its input for interpretation and execution.
Examples:
Multiple filenames as arguments
> $ rm chap01 chap02
> $ cp chap01 [Link]

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 27
chikmagalur
• 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 file1)

Examples:
> $ pwd
• pwd prints the current working directory
/root

> $ who
• who lists currently logged in users
root tty7 2022-01-30 09:08

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 28
chikmagalur
Basic UNIX Commands

echo
• The basic job of echo command is to display the string
passed to it as an argument. For example,
$ echo hello
hello
• The echo command can be used to evaluate a variable
given to it as an argument. For example,
$ x=5
$ echo $x
5
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 29
chikmagalur
• The output of echo command can be used to create
a new file as well. For example,
$echo Hello World > file1
$cat file1
Hello World
• Here, in the first line, echo command is used to
create file1 with the content Hello World.
• Then, cat command is used to display the contents of
file1.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 30
chikmagalur
printf
• The printf command can be used as an alternative to echo
command.
Examples
$printf “Hello World”
Hello World$
• To have a newline character, we can use the escape sequence \n
$printf “Hello World\n”
Hello World
$
• printf can be used without double quotes. But, printf terminates
when a space is encountered.
$printf Hello World
Hello$
Note that the term World didn’t get printed here.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 31
chikmagalur
• Like printf() function of C language, in UNIX also, printf
command can use format specifiers like %s, %d, %f, %o, %x etc.
• And, the values of variables can be displayed along with printf
command.
• For example,
$printf "Current shell is %s \n" $SHELL
Current shell is /bin/bash
• The format specifiers can be used to convert the numbers as
below
$printf "Value of 138 is %x in Hexa and %o in Octal\n" 138 138
Value of 138 is 8a in Hexa and 212 in Octal

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 32
chikmagalur
ls
• The ls command lists all files in the directory that
match the name. If name is left blank, it will list all of
the files in the current directory.
Examples
$ls
README
chap01
chap02
chap03
helpdir
progs
• Notice the listed files it has started displaying in
alphabetical order and with upper case filenames first.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 33
chikmagalur
$ls chap*
Chap01
Chap02
Chap03
• Since the files containing the first 3 chapters have similar
filenames, unix let us use special symbol(*) to access
them.

$ls -l chap*
-rw-r--r-- 1 root users 5609 Apr 23 09:30 chap01
-rw-r--r-- 1 root users 26129 Apr 23 09:30 chap02
-rw-r--r-- 1 root users 37385 Apr 23 09:30 chap03
• ls -l command display the 7 attributes of the filename
provided.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 34
chikmagalur
who: To know users of the system
• UNIX system can be used by multiple users at a time. One user may
needs to know the list of other users who are using the system
currently. The who command is used for this purpose. This command
displays name of the users (login ID used to log in), name of the
terminal and date and time of login and machine names from where
user have logged in.
• For example –
$who
root Console Sept 04 10:12 (:0)
John pts/01 Sept 04 11:11 ([Link])
Jay pts/02 Sept 04 12:35 ([Link])
ram pts/03 Sept 04 14:08 ([Link])

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 35
chikmagalur
$who -Hu
Name LINE TIME IDLE PID COMMENTS
root Console Sept 04 10:12 11040 (:0)
John tty01 Sept 04 11:11 0:33 11200 ([Link])
Jay tty02 Sept 04 12:35 0:48 12678 ([Link])

• The option -H provides the header for the


columns and -u shows the idle time of the user
and the process id.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 36
chikmagalur
date
• The date command in UNIX is used to display the current date and
time of the system.
• The UNIX system maintains an internal clock that runs continuously.
When the system is shutdown, the battery backup keeps the clock
ticking. This clock actually stores the number of seconds elapsed
since 1st January 1970.
• A 32-bit counter store these seconds and it is expected to overflow
sometime in 2038.
For example
$date
Mon Aug 3 11:35:32 IST 2022
• It will not allow the user to modify the current system date. When the
system administrator uses the same command, apart from displaying
the date, it allows to modify the date and time.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 37
chikmagalur
• Suitable format specifiers can be used as an argument to
date command to get the date/month/year etc, in
required format.
• The format specifier is preceded by a + symbol, followed
by the % operator and a single character describing the
format.
• Following are some of the formats –
• To display only the month, one can use +%m as a
specifier.
For example,
$date +%m
10 //indicates October.

• To display month name, use +%h


$date +%h
Oct
21-10-2022
Ashwini Kamath, Asst. Prof., ISE, AIT
chikmagalur
38
• +%d for day of month (1 – 31)
• +%y for last two digits of the year
• +%H, +%M and +%S indicates hour, minute
and second respectively.
• One can combine more than one option by
enclosing them in double quotes, and keeping
+ symbol outside the quote. For example,
combination of month and month name
$date +”%h %m”
Oct 10

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 39
chikmagalur
• cal
• This command is used to display the calendar of
current month.
$ cal
October 2022
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
$

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 40
chikmagalur
• Various arguments can be given to cal command as listed
below –
• $cal May 2017 : Displays May month calendar of the year
2017
• For example,
$cal May 2017
May 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 31

• $cal 2022: Displays the calendar for all months of 2022.


Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 41
chikmagalur
$ cal 2022
2022

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 42
chikmagalur
passwd
• The passwd command is used to change the password of a user
account.
• A normal user can run passwd to change their own password, and a
system administrator (the superuser) can use passwd to change
another user's password, or define how that account's password
can be used or changed.
• Running passwd with no options will change the password of the
account running the command.
• It will prompt the user to enter the account's current password
$passwd
(current) UNIX password:
• If it is correct, you will then be asked to enter a new password
Enter new UNIX password:
Retype new UNIX password:

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 44
chikmagalur
• Before a normal user can change their own password, they must
first enter their current password for verification.
• After the current password has been verified, passwd checks to
see if the user is allowed to change their password at this time.
If not, passwd refuses to continue, and exits.
• Otherwise, the user is then prompted twice for a replacement
password. Both entries must match for passwd to continue.
Rules for giving passwords
a) must be >=6 characters long,
b) must contain 2 out of 3 of
upper-case letters,
lower-case letters,
non-letters (digits, punct)
c) may not be a dictionary word or too similar to your name

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 45
chikmagalur
man
• The man command is used to know more about Unix commands and
using Unix online manual pages
• man is the system's manual viewer; it can be used to display manual
pages, scroll up and down, search for occurrences of specific text, and
other useful functions.
• Each argument given to man is normally the name of a program, utility
or function.
• The manual page associated with each of these arguments is then
found and displayed.
• In general 8 section of the UNIX manual are available.
• Man searches the manual from section 1. If it locates a keyword in one
section it will not continue the search even if the keyword is present in
another section.
• When a keyword is present in multiple section user can provide the
section number as an argument with the man command.
$man 4 passwd
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 46
chikmagalur
Section Numbers
• The section numbers of the manual are listed below. While
reading documentation, if you see a command name
followed by a number in parentheses, the number refers to
one of these sections.
• For example, man is the documentation of man found in
section number 1. Some commands may have documentation
in more than one section, so the numbers after the command
name may direct you to the correct section to find a specific
type of information.
• The section numbers, and the topics they cover, are as
follows: Section number Description

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 47
chikmagalur
Organization of the man documentation
1 Executable programs or shell commands

2 System calls (functions provided by the kernel)


3 Library calls (functions within program libraries)

4 Special files (usually found in /dev)


5 File formats and conventions eg /etc/passwd

6 Games

7 Miscellaneous (including macro packages and conventions),

8 System administration commands (usually only for root)

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 48
chikmagalur
Examples

• To know how man command is used we can type


$man man
• When man command is used with -k option, it searches a
summary database and prints a one line description of the
command.
$man –k awk
awk awk(1) -pattern scanning and processing language
nawk nawk(1) -pattern scanning and processing language

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 49
chikmagalur
apropos
• searches the manual pages for a keyword or
regular expression. Each manual page has a
short description included with it.
• appros command emulates man –k command.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 50
chikmagalur
whatis

• whatis command displays short manual page


description.
• man command uses –f option to emulate the
whatis behavior.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 51
chikmagalur
COMBINING COMMANDS
• UNIX allows you to specify more than one command in the
command line.
• Each command has to be separated from the other by a ;
(semicolon).
Example
$wc ex; ls -l ex
• The above command first it displays the line count, word count and
byte or character count along with this it also display the details of
ex file.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 52
chikmagalur
• When you learn to redirect the output of these
commands you may even like to group them together
within parentheses .
$ wc ex ; ls -l ex >newfile
1 2 12 ex
$ cat newfile
-rw-r--r-- 1 webmaster webmaster 12 Oct 19 11:08 ex

$ (wc ex ; ls -l ex) > newfile1


$ cat newfile1
1 2 12 ex
-rw-r--r-- 1 webmaster webmaster 12 Oct 19 11:08 ex
$

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 53
chikmagalur
A command line can overflow or be split into multiple lines
• A command is often keyed in though the terminal width is restricted to
80 characters, that doesn't prevent you from entering a command, or a
sequence of them, in one line even though the total width may exceed
80 characters.
• The command simply overflows to the next line though it is still in a
single logical line.
• Sometimes, you'll find it necessary or desirable to split a long command
line into multiple lines.
• In that case, the shell issues a secondary prompt, usually >, to indicate
that the command line isn't complete. This is easily shown with the
echo command:
$ echo "This is
> a three line
> text message"

This is
a three line
text message
$ 21-10-2022 Ashwini Kamath, Asst. Prof., ISE, AIT
46
chikmagalur
INTERNAL AND EXTERNAL COMMANDS
• UNIX commands are classified into two types
1. Internal Commands - Ex: echo
2. External Commands - Ex: ls, cat
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.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 55
chikmagalur
External Command:
• External commands are not built into the shell.
• 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.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 56
chikmagalur
• How to find out whether a command is internal or
external?
type command:
$ 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.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 57
chikmagalur
• type - Display information about command type.
• The type command is a shell built-in that displays the
kind of command the shell will execute, given a
particular command name.
$type type
type is a shell built-in

$type ls
ls is /bin/ls

$type cp
cp is /bin/cp
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 58
chikmagalur
THE 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 explicitly created but
comes with every system.
• Its password is set at the time of installation of the
system.
login: root
password:*******
#_
• The prompt of root is #. Once login with the root and
its password its directory will be / or /root
Ashwini Kamath, Asst. Prof., ISE, AIT
21-10-2022 59
chikmagalur
• On modern system most of the administrative
commands are resident in /sbin and /usr/sbin, but in
older system commands resides in /etc

• root’s path list is also different from the one used by


other users
/sbin:/bin:/usr/sbin:/usr/bin:/usr/dt/bin

• For the super user its possible that he may execute


programs and scripts written by other users. That’s
why path doesn’t include the current directory.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 60
chikmagalur
Becoming the super user: su command
• Any user can acquire super user status with the
su command if she knows the root password.
• For example the user ram ( with home directory
/home/ram) becomes a super user as follows
$su
passwd:*******
#pwd
/home/ram
• Prompt changes but directory doesn’t.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 61
chikmagalur
Creating user’s environment:
• Users often rush to the administrator with a complaint
that a program has stopped running.
• The administrator first tries running it in a simulated
environment.
• su, when used with a -, recreates the user’s
environment without taking the login-password route.
su – ram
• This sequence executes ram’s profile and temporarily
creates ram’s environment.
• su runs a separate sub shell, so this mode is terminated
by hitting [Ctrl-d] or using exit.

Ashwini Kamath, Asst. Prof., ISE, AIT


21-10-2022 62
chikmagalur
UNIX PROGRAMMING (18CS56)

Module 1
UNIX Files
UNIX FILES
• UNIX system has thousands of files. If you
write a program, you add one more file to the
system.
• When you compile it you add some more.
Files grow rapidly, and if they are not
organized properly, you will find it difficult to
locate them.
• So UNIX has a file system (UFS) to manage or
organizes its own files in directory.
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 2
chikmagalur
NAMING FILES OR WHAT’S IN A (FILE) NAME
• UNIX permits file names to use most characters, but avoid spaces, tabs
and characters that have a special meaning to the shell, such as:
& ; () |? \ ' " ` [] {} <> $ - ! /
• It is recommended that only the following characters be used in filenames.
1. Alphabetic characters and numerals
2. The period(.), the hyphen(-) and underscore(_)
3. Case Sensitivity: uppercase and lowercase are not the same!
These are three different files: NOVEMBER November november
• Length: can be up to 256 characters
• Extensions: may be used to identify types of files
 libc.a - archive, library file
 program.c - C language source file
 alpha2.f - Fortran source file
 xwd2ps.o - Object/executable code
 mygames.Z - Compressed file
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 3
chikmagalur
BASIC FILE TYPES/CATEGORIES FILES
• File is a collection of records. So, files are
divided into three categories
1. Ordinary file
2. Directory file
3. Device file

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 4
chikmagalur
[Link] Files or regular files
• It contains data as a stream of characters. An ordinary files itself
divided into 2 types
Text file:
• Contains only printable characters, and you can often view the
contents and make sense out of them.
• All C and Java files are example of text file.
• A text file contains lines of characters where every line is terminated
with the newline character, also known as linefeed (LF) when you press
Enter while inserting text, LF character is every appended to every line.
• We won’t see this character but there is command (od) which can
make it visible.
Binary file: it contains both printable and unprintable characters that
cover the entire ASCII range(0 to 255), most UNIX commands are
example of binary files.

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 5
chikmagalur
2. Directory files
• It 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 sub directory
that it houses.
• Each entry has two components
1. The filename
2. A unique Identification number for the file or directory( called the
inode number)
• A directory contains the filename but not the contents of file.
• When we create or remove a file the kernel automatically updates
its corresponding directory by adding or removing the entry i.e
inode number associated with that file.

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 6
chikmagalur
3. Device files
• Used to represent a real physical device such as a printer,
tape drive or terminal, used for Input/Ouput (I/O)
operations
• Unix considers any device attached to the system to be a
file - including your terminal:
• By default, a command treats your terminal as the standard
input file (stdin) from which to read its input
• Our terminal is also treated as the standard output file
(stdout) to which a command's output is sent.
• stdin and stdout
• Two types of I/O: character and block
• Usually only found under directories named /dev
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 7
chikmagalur
PARENT CHILD RELATIONSHIP/ ORGANIZATION OF FILES
• All files in UNIX are related to one another. The file system in UNIX
is a collection of all ordinary, directory and device files and
organized in a hierarchical structure as shown in below fig.

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 8
chikmagalur
• The implicit feature of every UNIX file system is that there is a
top which serves as reference point for all files.
• This top is called root & is represented by a /. Root is actually a
directory.
• The root directory has a number of sub directories under it.
• These sub directories in turn have more sub directories and
others files under them.
• For instance bin and usr are two directories directly under root,
while a second bin and kumar are sub directories under usr.
• Every file apart from root must have a parent.
• Thus the home directory is the parent of kumar , while / is the
parent of home and grandparent of kumar.
• If you create a file [Link] under the kumar directory ,then
kumar will be the parent of this file.
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 9
chikmagalur
The first group contains the files that are made available
during system installation
• /bin and /usr/bin: these are the directories where all the
commonly used UNIX commands are found.
• /sbin and /usr/sbin: If there is command we can’t execute
then system administrator can execute, it would be
probably in one of these directories.
• /etc: this directory contains the configuration files of the
system. we can change a very important aspect of system
functioning by editing a text file in this directory. Our login
name and password are stored in files /etc/passwd and
etc/shadow
• /dev: This directory contains all device files. These files
don’t occupy space on disk. There could be more sub
directories like pts, dsk and rdsk in this directory
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 10
chikmagalur
• /lib and /usr/lib: Contains shared library files and
sometimes other kernel-related files.
• /usr and /include: contains the standard header files
used by C programs. The statement
#include<stdio.h> used in most C programs refers
to the file stdio.h in this directory.
• /usr/share/man: this is where the man pages are
stored. There are separate subdirectories here(like
man1,man2 etc) that contains the pages for each
section. For instance, the man page of ls can be
found in /usr/share/man/man1

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 11
chikmagalur
User also work with their own files, they write
programs, send and receive mail and also create
temporary files. These files are available in the
second group shown below
• /tmp: the directory where users are allowed to
create temporary files. These files are wiped away
regularly by the system
• /var: The variable part of the file system. Contains all
your print jobs and your outgoing and incoming mail.
• /home: On many systems users are housed here.
ram would have his home directory in /home/ram

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 12
chikmagalur
The home directory and the HOME variable

• HOME DIRECTORY : When log on to the system, UNIX


automatically places you in a directory called the home
directory.
• It is created by the system when user account is opened.
• If you log in using the login name sharma , you will land
up in a directory that could have the pathname
/home/sharma
• The shell variable $HOME contains the home directory
path
$echo $HOME
/home/sharma
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 13
chikmagalur
• You will be doing much of your work in your home
directory and subdirectories.
• Home variable: it is also called environment
variable.
• Environment variables are a set of dynamic named
values that can affect the way running processes
will behave on a computer.
• Here $HOME is a environment variable it indicates
the home directory of the current user

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 14
chikmagalur
PATH Variable
• The PATH environment variable is a colon-delimited
list of directories that your shell searches through
when you enter a command.
• Program files (executables) are kept in many different
places on the Unix system. Your path tells the Unix
shell where to look on the system when you request
a particular program.
• To find out what your path is, at the Unix shell
prompt echo $PATH
• Your path will look something like the following.
/usr2/username/bin:/usr/local/bin:/usr/bin:.
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 15
chikmagalur
• /usr2/username/bin:/usr/local/bin:/usr/bin:.
• We can see our username in place of username.
• Using the above example path, if you enter the ls
command, your shell will look for the appropriate
executable file in the following order:
 first, it would look through the directory
/usr2/username/bin,
 then /usr/local/bin,
 then /usr/bin,
 and finally the local directory, indicated by the . (a
period)

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 16
chikmagalur
RELATIVE AND ABSOLUTE PATHNAMES

• The dot(.) and double dots(..) notation to represent


present and parent directories and their usage in relative
pathnames
RELATIVE PATHNAMES
• Pathname that does not begin with / specify locations
relative to the current directory.
• Relative pathnames uses either the current or parent
directory as reference and specifies path relative to it.
• A relative pathname uses one of these cryptic symbols.
. (a single dot)- this represents the current directory.
.. (two dots) this represents the parent directory
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 17
chikmagalur
Command Function

cd Returns you to your login directory

cd ~ Also returns you to your login directory

cd / Takes you to the entire system directory

cd /root Takes you to the home directory of the root or superuser, account
created at installation, you must be root user to access this directory.

cd /home Takes you to the home directory where user login directories are
usually stored

cd .. Moves you up one directory

cd ~otheruser Takes you to the otheruser’s login directory

cd /dir/subdirfoo Regardless of which directory you are in, the absolute path takes you
directly to subdirfoo, a subdirectory of dir.
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 18
chikmagalur
Ex .1: Assume the current directory is
/home/kumar/progs/data/text, using cd .. will move one level
up
$pwd
/home/kumar/progs/data/text
$ cd ..
$pwd
/home/kumar/progs/data
• Ex 2 : To move two levels up
$pwd
/home/kumar/progs
$ cd ../..
$pwd
/home
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 19
chikmagalur
Ex 3: My present location is /etc/samba and now I want to
change directory to /etc.
• Using relative path: $ cd ..
• Using absolute path: $cd /etc

Ex 4: My present location is /var/ftp/ and I want to change the


location to /var/log
• Using relative path: cd ../log
• Using absolute path: cd /var/log

Ex 5: My present location is /etc/lvm and I want to change my


location to /opt/oradba
• Using relative path: cd ../../opt/oradba
• Using absolute path: cd /opt/oradba
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 20
chikmagalur
ABSOLUTE PATHNAMES
• If the first character of a pathname is / the files
location must be determined with respect to root(/).
Such a pathname is called absolute pathname.
cat /home/kumar
• When you have more than one / in a pathname for
such / you have to descend one level in the file system.
• Thus Kumar is one level below home and two levels
below root.
• When you specify a file using forward slashes to show
the various levels, it will identifying a file uniquely.
• No two files in a UNIX system can have same absolute
pathnames.
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 21
chikmagalur
• When you specify the date command, the system has to
locate the file date from a list of directories specified in
the PATH variable and then execute it.
• However if you know the location of a command in prior,
for example date is usually located in /bin or /usr/bin .
Use absolute pathname i,e precede its name with
complete path
$/bin/date
• For example if you need to execute program prg residing
in /usr/local/bin you need to enter the absolute
pathname
$/usr/local/bin/prg

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 22
chikmagalur
DIRECTORY COMMANDS
pwd, cd, mkdir, rmdir COMMANDS
• pwd (Print Working Directory) (checking your current
directory)
• As the name suggests pwd command prints the current
working directory or simply the directory user is, at
present.
• It prints the current directory name with the complete
path starting from root (/).
• This command is built in shell command and is
available on most of the shell bash, Bourne shell,
ksh,csh, etc.
$pwd
/home/sharma

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 23
chikmagalur
cd(Change Directory): Changing The Current Directory
• The cd command, which stands for "change directory",
changes the shell's current working directory.
• The cd command is one of the commands you will use the
most at the command line in UNIX.
• It allows you to change your working directory. You use it to
move around within the hierarchy of your file system.

Ex 1: When used with an argument it changes the current


directory to the directory specified as argument for example
assume ait is a directory under user directory sharma.
• To change from sharma directory to ait directory , issue the
command as follows
$pwd
/home/sharma
$cd ait
$pwd
/home/sharma/ait
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 24
chikmagalur
Ex 2: When cd used without arguments: cd when used without
arguments reverts to home directory
$pwd
/home/sharma/ait
$cd
$pwd
/home/sharma
Ex 3: If your present working directory is /home/sharma and you
need to switch to /bin directory directly, use absolute pathname i.e
/bin with cd command
$pwd
/home/kumar
$cd /bin
$pwd
/bin
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 25
chikmagalur
mkdir: "making directory".
• mkdir is used to create directories on a file system.
• If the specified DIRECTORY does not already exist, mkdir
creates it.
• More than one DIRECTORY may be specified when calling
mkdir.

Ex 1: To create a directory named ait, issue the following


command.
$mkdir ait
• ait directory is created under present working directory.
• Assume that pwd is /home/sharma , then ait directory is
created under sharma directory.
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 26
chikmagalur
Ex 2: To create three directories at a time, named
patch, dbs, doc, pass directory names as arguments.
$mkdir patch dbs doc

Ex 3:To create a directory tree:


• To create a directory named ait and create two
subdirectories named cse and ise under ait, issue the
command mkdir using ait is a parent directory.
$mkdir <parent directory> <sub-directories>

$mkdir ait ait/cse ait/ise

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 27
chikmagalur
Ex 4: Error while creating a directory tree
$mkdir ait/cse ait/ise
mkdir: Failed to make directory “ait/cse”; no such file or direcory
mkdir: Failed to make directory “ait/ise”; no such file or direcory
• Error is due to the fact that the parent directory named ait is not
created before creating sub directories cse and ise.

Ex 5: $mkdir test
mkdir: Failed to make directory “test”; permission denied
• This can happen due to:
a. The directory named test may already exist
b. There may be an ordinary file by the same name in the current
directory.
c. The permissions set for the current directory do not permit the
creation of files and directories by the user.
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 28
chikmagalur
• rmdir: Removing Directories
• The rmdir command removes the directory entry specified by
each directory argument, provided the directory is empty.
• You cant delete a directory with rmdir unless it is empty.
Ex:
$rmdir progs
• removes the directory named progs
• Arguments are processed in the order given.
• To remove both a parent directory and a subdirectory of that
parent, the subdirectory must be specified first, so the parent
directory is empty when rmdir tries to remove it.
• The reverse logic of mkdir is applied.

$rmdir <subdirectories> <parent directory>


$rmdir ait/cse ait/ise ait
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 29
chikmagalur
ls : listing directory contents:
To obtain a list of all filenames in the current directory.
• Numerals first
• Uppercase next
• Lowercase Then
$ls
Output:
08_packets.html
calendar
[Link]
[Link]
helpdir
uskdsk06
• ls options: Output in multiple columns(-x):
$ls -x
08_packets.html calendar [Link] [Link]
helpdir progs usdsk07 usdsk07

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 30
chikmagalur
Identifying directories and executables(-F)
$ ls -Fx
08_packets.html calendar* [Link]*
[Link] [Link] helpdir/ progs/ usdsk07
• The * indicates the file contains the executable code
and / refers to directory
Showing Hidden files also(-a):
$ls axF
.profile .exrc .kshrc .xinitrc
08_packets.html calendar* [Link]* [Link]
[Link] helpdir/ progs/ usdsk07
• The hidden files are indicated by . (dot) displayed
before filename.
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 31
chikmagalur
Listing directory contents:
$ls -x helpdir progs
helpdir:
[Link] [Link]
progs:
[Link] [Link]
• If we specify two directories named helpdir
and progs , the contents of the directory i,e
filenames are listed out.

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 32
chikmagalur
Recursive listing(-R)
• The recursive option lists all sub-directories
and files in a directory tree structure.
$ls -xR
08_packets.html calendar [Link]
[Link] [Link] helpdir progs usdsk07
./helpdir
[Link] [Link]
./progs
[Link] [Link]

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 33
chikmagalur
FILE RELATED COMMANDS
Cat command : Displaying and creating files
• cat is one of the most well known commands of UNIX
system.
• Cat is useful for creating a file .
• Its mainly used to display the contents of a small file on the
terminal.
• Using cat to create a file:
• Enter the command cat, followed by > character and the
filename.
• Example: take a filename named foo
$ cat > foo
• > Symbol following command means that the output goes
to filename following it.
[ctrl+d] /* to terminate or to signify end of the input.
$
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 34
chikmagalur
Using cat to display a file
• Enter the cat command followed by filename
$ cat foo
• Display the content of the file foo on the terminal
screen.
Cat options (-v and -n)
• Displaying Non printing characters(-v)
• cat is normally used for displaying text files only.
If you have non-printing ASCII characters in your
input , you can see cat with -v option to display
these characters
• Numbering lines(-n): The -n option numbers lines.
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 35
chikmagalur
Cat with more than one filename as arguments:
cat filename1 filename2 ....
cat chap01 chap02
• The contents of second file are displayed
immediately after the first file without any
header information.

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 36
chikmagalur
cp: copying a file
• cp command copies a file or a group of [Link]
creates an exact image of the file on the disk with
the different name.
• The syntax requires at least two filenames to be
specified in the command line.
• When both are ordinary files, the first is copied to
second file.
cp <source file> <destination file>
$cp chap01 unit1
• if destination file i.e unit1 does not exist, first it
will be created before copying. If not it will be
simply overwritten without any warning.

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 37
chikmagalur
Copying a file to another directory
• ex: assume there is a file named chap01 and it has to be copied to
progs directory
cp chap01 progs
• output: chap01 is now copied to directory named progs with the
same name chap01.

Copying a file to another directory with different name


• ex: assume there is a file named chap01 and it has to be copied to
progs directory with chap01 file renamed as unit1
cp chap01 progs/unit1
• output: chap01 is now copied to directory named progs with the
same name unit1

Copy more than one file with a single command.


cp chap01 chap02 chap03 progs
• chap01, chap02, chap03 files are copied to directory named progs.

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 38
chikmagalur
Copy all files beginning with chap
• cp chap* progs

cp options:
• Interactive copying (-i): the -i option warns the user before
overwriting the destination file.
Ex: $ cp -i chap01 unit1
cp: overwrite unit1(yes/no)? y
• A y at this prompt will overwrite the file.

Copying directory structure(-R) : the -R command behaves recursively


to copy an entire directory structure say progs to newprogs.
Ex: say progs directory contains three files kernel, bash, korn. To
copy all three files under progs to newprogs directory
$ cp -R progs newprogs
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 39
chikmagalur
rm : deleting files
• The rm command deletes one or more files.
Ex 1: The following command deletes three files
chap01, chap02, chap03.
$ rm chap01 chap02 chap03

Ex 2: to delete files named chap01 and chap02 under


progs directory
$ rm progs/chap01 progs/chap02

Ex 3: to remove all file


$ rm*
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 40
chikmagalur
rm options:
Interactive deletion (-i): the i optin makes the command ask the user for
confirmation before removing each file.
$rm -i chap01 chap02 chap03
rm: remove chap01(yes/no)?y
rm: remove chap01(yes/no)?y
rm: remove chap01(yes/no)?y

Recursive deletion(-r or -R) deletes all subdirectories and files recursively.


Rm wont normally remove directories but when used with -r or -R
option it will.
$ rm -r *

• Forcing removal: rm prompts for removal, if a file is write protected.


The -f option overrides this minor protection and forces removal.
$ rm -rf * /*(deletes everything in the current directory and below)
Ashwini Kamath, Asst. Prof., ISE, AIT
07-11-2022 41
chikmagalur
mv: RENAMING FILES.
• The mv command renames or moves files. It has two
distinct functions:
i. It renames a file or directory
ii. it moves a group of files to a different directory

• To rename a file chap01 to man01


$ mv chap01 man01
• mv replace the filename in the existing directory entry with
the new name. No additional space is consumed on disk
during renaming.

• To rename a directory:
$ mv pts perdir
• pts directory is renamed as perdir

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 42
chikmagalur
• To move group of files to a directory
mv chap01 chap02 chap03 progs
• to move three files chap01, chap02, chap03 to
directory named progs

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 43
chikmagalur
wc command:
COUNTING LINES, WORDS, CHARACTERS
• wc command takes one or more filenames as arguments
and displays four columns output.
• First we will create a file named infile
$ cat > infile
I am the wc command
I count characters,words and lines [ctrl+D]
$wc infile
2 10 55 infile

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 44
chikmagalur
When two filenames are passed as wc argument
$ cat > infile1
Unix is a multitasking OS
Unix is a multiuser OS
[ctrl+D]
$wc infile infile1
2 10 55 infile
2 10 48 infile1
4 20 103 total
First line : number of lines, words and characters of infile
Second line: number of lines, words and characters of infile1
Third line: Total number of lines, words and characters of both.

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 45
chikmagalur
od Command: DISPLAYING DATA IN OCTAL.
$ cat odfile
White space includes
The ^G character rings a bell
$ od -b odfile
The -b option displays the octal values for each character.

Each line displays 16 bytes of data in octal , preceded by the offset


in the file of the first byte in the line.

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 46
chikmagalur
$od -bc odfile
• The -b and -c option combined
• Each line is now replaced with two. The octal values are
shown in first line and printable characters and escape
sequences are shown in second line

• The octal equivalent of characters are displayed : W- 127, i-


151, \t (tab)-011, \n(newline)-012, ^G(Bell character)- 007

Ashwini Kamath, Asst. Prof., ISE, AIT


07-11-2022 47
chikmagalur

You might also like