0% found this document useful (0 votes)
56 views

Chapter 01 - Getting Started

The document provides an introduction to the UNIX operating system. It discusses how UNIX divides labor between the kernel and shell. The kernel interacts with hardware and provides services to programs through system calls, while the shell interacts with users and performs command interpretation. It also describes some basic UNIX commands like ls, cat, and man along with concepts like files, permissions, environment variables and redirection.

Uploaded by

apurba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Chapter 01 - Getting Started

The document provides an introduction to the UNIX operating system. It discusses how UNIX divides labor between the kernel and shell. The kernel interacts with hardware and provides services to programs through system calls, while the shell interacts with users and performs command interpretation. It also describes some basic UNIX commands like ls, cat, and man along with concepts like files, permissions, environment variables and redirection.

Uploaded by

apurba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 01 - Getting Started

1. Computer hardware is a bare machine with no intelligence of its own. An


operating system like UNIX is a special software which provides it with basic
intelligence. This intelligence is used to provide services for both the programs
and users.

Services for programs include:

o Scheduling CPU time.

o Allocating main memory.

o Accessing hardware devices such as hard-drive, printers etc.

Services for users include:

o File management (copying and deleting files, creating directories).

o Determining other users currently logged-in on system or local network.

o Sending e-mail messages, backing up files etc.

2. A program generally can't read a file by itself. Instead, it places a request to


access the file to the operating system. The operating system, in turn, performs
the job and makes the file data available to the program.

3. The program called user's login shell starts executing at the terminal as soon as
she logs in. Some of the popular shell programs available on a UNIX system are:

o sh: Bourne shell.

o csh: C shell.

o ksh: KornShell.

o bash: Bourne Again SHell.

4. The output produced by executing ls command is saved in a file named list in


the current directory.
The user's shell sees the redirection operator ( >) when interpreting the command-
line. It opens the named file following the redirection operator (here, list). Next,
the command ls is run, which reads the directory contents. The shell has
manipulated things such that the output of the ls command doesn't go to the
terminal, but is written to the file opened by the shell on its behalf.
5. The shell programming syntax requires that there should be no spaces on either
side of the = operator.
When entering x =10, x is treated as a command by the shell. As (in the default
installation) there's no command named x, the shell shows an error message
stating that the command couldn't be found.
A similar error occurs in case x= 10 is entered, where 10 is treated as a command.
The shell tries to locate and execute it but fails to do so. The x= part is seen as a
variable assignment valid only for the scope of 10 command.

6. Two empty files named README and readme are created in the current directory.

(Note: When running on a case-insensitive file-system (the default installation of


macOS), only one file named README is created. If either of the two files exists
already, it is replaced by an empty file.)

7. (i). who: Lists all the users currently logged in. It produces a four-column output
showing user's login name, terminal name, date & time of the last login, and
hostname if not local.
tty: Prints the file name of the terminal connected to the standard input.
(ii). ps: Lists all the currently running processes corresponding to the logged-in
user.
echo $$: Prints the integer PID (Process ID) corresponding to the running shell
process.

8. [Control-d], logout and exit. exit command is guaranteed to always work.

9. A user's ID is used to uniquely identify her in the operating system.

It is used by associating it with any file or process created by the user.

10. Directories, devices, terminals and printers are all represented as files in the UNIX
file-system.

11. Some of the duties of a systems administrator are:

o Backing up files.

o Restoring the system to a usable state in case of a crash.

o Startup, shutdown and maintenance of the system.


o Creating and administering user accounts.

o Updating system date.

12. AT&T and University of California, Berkeley (UCB).

1.
The UNIX system divides the labor between two agencies:

o Kernel

and

o Shell

The kernel interacts with the machine's hardware. It is responsible for all sorts of
system management activities. It also provides services to the user programs in
the form of system calls.

The shell interacts with the user. It provides the user interface to the computer. It
performs the job of command interpretation and gets a command-line executed
with the help of the kernel.

2. A file is simply an array of bytes stored on disk and can practically contain
anything. A file getting executed as a program is called a process.

UNIX systems predominantly use text files as they are easy to edit. UNIX started
as an operating system targeted towards engineering community, and engineers
often used to tinker with configuration files and share it with their peers. Using a
text file format makes it easy to do so.

3.

o Multiprogramming: Ability to keep multiple programs in the system


memory at the same time.

o Multiuser: Ability to support multiple users at the same time and allowing
them to use a single installation of operating system simultaneously.

o Multitasking: Ability to execute multiple tasks concurrently by a user and


even switch between them.
4. System calls are special routines written in C programming language and built
into the operating system kernel. They act as an interface to the kernel for user
programs. System calls are standardized across UNIX, and any UNIX operating
system, by definition, offers the same set of system calls.

C programming is different and powerful in the UNIX environment as it was


created and implemented first on UNIX, and then the entire UNIX operating
system, including all the utilities were written in C. Thus, C language, and its
Standard Library is deeply integrated into the UNIX operating system. UNIX
system calls are implemented in C and are offered as C function interfaces.

Windows don't share the same system calls as UNIX. Neither does it include the C
Standard Library in its default installation.

5. The creators of the UNIX followed the small is beautiful philosophy. They


attempted not to cram many features into a single tool.

They believed that using simple tools that perform well-defined tasks can be
combined with shell operators to perform complicated tasks and filter data.

6. The statement is not precisely true. wc command can be used, not just with files,
but also with input coming from standard input or the output of some other
command. This is illustrated with the following examples:
Execute wc command without any argument. The command prompt won't return.
Enter some text and terminate the input by entering a newline, and immediately
following it by the end-of-file character using [Control-d]. The output will
represent the count of the characters in the entered text.
Pipe the output of a command such as ls to wc -l to count the number of non-
hidden files and directories in the current directory. Enter the command-line as:
ls | wc -l

7. Three major differences between UNIX commands and Windows programs are:

o Unlike Windows, commands in UNIX are case-sensitive.

o Windows commands work irrespective of the presence of whitespace


between the command and its options, unlike UNIX where whitespace is
mandatory.

o UNIX commands make use of POSIX systems calls whereas Windows


commands don't.
8. The program file named foo present in the current directory is not executed if the
current directory (represented by .) is not listed in the PATH environment
variable.

9. Any contiguous sequence of one or more space, tab and newline characters are


called whitespace.

If multiple contiguous whitespaces are present between the command and its
arguments, the shell automatically compresses it to a single space character.

10. By specifying the section number as an argument to the man command (as


depicted):
o man -s1 command
o man -s5 filename
o man -s2 system_call
11. Either one of -f or -r can be used as an argument when executing the
command /usr/xpg4/bin/tail.
The file argument can be repeated any number of times when executing the
command /usr/bin/ls.
12.
o Interrupt character represented by [Control-c] is used to interrupt a
long-running or an unresponsive program.
o eof character represented by [Control-d] can be used to terminate user
input or to log out from the current session.

13. By updating the PAGER environment variable to use the desired pager


say less by running PAGER=less on the shell prompt.

1. By running the following command-line:

date +"%d-%m-%y/%H:%M:%S"

2. An escape sequence is a sequence of characters that does not represent the


character itself, but a different character that's not possible to be entered directly
in the given context. It is generally a two character-sequence beginning with a
backslash (\) character.

Three escape sequences used by echo command, with their significance are:


o \n: Represents newline character.
o \b: Represents backspace character.
o \\: Represents backslash character.
3. (i). Supply -e option to echo command. The modified command-line is:
echo -e "Filename: \c"
(ii). Supply -n option to echo command. The modified command-line is:
echo -n "Filename: \c"

4. There are a couple of mistakes in the command-line:

o As per the syntax of printf command, it is invalid to supply a comma


character between arguments.
o fname needs to be evaluated as a variable and should be preceded
by $ character.

The corrected command-line is:

printf "Filename: %s\n" $fname


5. (i). Convert 192 to octal by executing the following bc internal commands:
obase=8
192
Convert 192 to hexadecimal by executing the following bc internal commands:
obase=16
192

(ii). Convert 192 to octal and hexadecimal by executing the following command-
line:

printf "The value of 192 in octal is %o and hexadecimal is %x\n" 192 192
bc command can be used to display the number in binary by setting obase=2.

6. Running script command spawns a new sub-shell process.

7. mailx command can obtain its arguments via shell variables or at runtime using
piping and redirection. It can work non-interactively and can be automated.

8. When a new email message is received, it is stored in a file referred to as mailbox.


Once the email message is read, it automatically moves to a file
named mbox which is generally stored in the user's home directory.

9. To hide the keyboard input from getting displayed on the terminal execute:

stty -echo

The above setting can be reversed by executing:

stty echo

10. A machine can be identified by checking its hostname by executing the


command-line:
uname -n

11. By examining the output of:

stty -a

and reading values corresponding to erase, kill and eof keys.

12. By setting [Control-c] corresponding to kill keyword using stty command as:


stty kill \^c

No, the setting doesn't persist between login sessions.

1. Two types of ordinary files are:

o Text file

o Binary file

A text file contains only printable characters from the ASCII character set. On the
other hand, a binary file can contain characters from the entire spectrum of the
ASCII character set.

C program source code, Perl script and ASCII text files are some examples of a
text file.

Executable program, PNG image and WAV audio files are some examples of a
binary file.

2. A device file helps in accessing the device by setting appropriate file attributes.

3. (i). This command won't work. Directory a and a/b need to exist before


attempting to create the directory a/b/c.

(ii). This command will work. Directory a will be created first followed by the
directory a/b.

(iii). This command won't work. The directory a/b/c does not exist yet.

(iv). This command will work partially. Directory a won't be removed as it's non-
empty. Directory a/b will be removed as it's empty.

(v). This command won't work for ordinary users. /bin is writable only by the root
user.
4. Either test already exists as a file or directory in the current directory, or the user
doesn't have write permission in the current directory.

5. Only ... and .... can be created either as a file or a directory. . and .. are special


names reserved for use by the operating system to refer to the current and the
parent directory.

6. The directory bar contains hidden files or directories which are not displayed


when running ls bar.

7. By referring to charlie's home directory as ~charlie.

8. cd ~charlie changes the current working directory to user charlie's home


directory. cd ~/charlie changes the current working directory to a directory
named charlie in the current user's home directory.

The former command will fail if there is no user named charlie. The latter
command will fail if no directory named charlie exists in the current user's home
directory.

9. To refer to update.sh file present in the current directory and not some other file
with the same name existing in one of the directories listed in PATH variable. This
measure is also required if the current working directory (represented by .) is not
listed in the PATH variable.

10. Numbers, followed by uppercase alphabets, followed by lowercase alphabets.

11. (i). Changes the current directory to the root directory /. It will work successfully if
the user has execute permission available for the root directory.

(ii). Creates a directory named bin under /home directory. The command will fail if


either the user doesn't have write permission available
for /home or /home/bin already exists as a directory or file.

(iii). This command won't work. It attempts to remove the parent directory
i.e. /home while being placed in the child directory /home/kumar, which the shell
won't permit.

(iv). This command will attempt to list the contents of the parent directory
i.e. /home. It will succeed only if the user has read permission available
for /home directory.
Chapter 05 - Handling Ordinary Files
1. A directory is technically a file containing the name and the inode number of files
and sub-directories that it contains. The entries are updated by the kernel on the
user's behalf when files and sub-directories are added, renamed or deleted.

The size of a directory file is usually small as it doesn't store the files and
subdirectories it contains, just their name and inode numbers.

2. The first command displays the contents of the file named foo present in the
current directory.

The second command returns a secondary prompt and lets the user type any
text. Upon encountering the end-of-file ([Control-d]) character, the entered text
is saved in a file named foo in the current directory.
Use of [Control-d] is required when entering the text from standard input to let
the shell know that the text entry is over.

3. (i). Yes, the command would work. The file foo would be copied into the directory
named bar.

(ii). No, the command won't work. An error message is displayed stating
that foo is a directory and is not copied.

4. The first command will delete the directory bar along with all the files and sub-
directories recursively, disregarding the write protection status of any files or sub-
directory and non-emptiness of any sub-directory.

The second command will work only when the directory bar is empty and its
parent directory (current directory here) is not write-protected.

5. (i). The file or directory named include present in the logged-in user's home


directory is moved to the current working directory.

(ii). The command recursively copies the contents of bar1 to bar2. If bar2 exists


and is a directory, recursively copy contents of bar1 into bar2, thus making a copy
of bar1 a sub-directory of bar2.

(iii). The command moves all the files and directories in the current directory into
a directory named bin in the parent directory.

6. The three possible reasons can be:


(i). Read permission is not available for the file hosts.

(ii). Write permission is not available for the file backup/hosts.bak.

(iii). Execute permission is not available for the directory backup.

7. By executing the following command-line:

cd ; mv a temp ; mv temp/a . ; rmdir temp

Individual commands in the above command-line are described below:

o cd: Changes the working directory to the user's home directory.


o mv a temp: Rename the directory a under home directory to temp.
o mv temp/a .: Move the directory hierarchy a under temp directory to the
home directory.
o rmdir temp: Remove the directory named temp.
8. Using the repeat factor, an internal command within more command can be made
to run multiple times.
Type /include to locate the first occurrence of the pattern. Continue pressing the
key n to repeat the search for further occurrences.

Using the repeat factor a command can be executed for a set number of times.
The dot command on the other hand simply repeats the last command once.

9. We can ensure that the file is indeed a Postscript file by executing file command


with the filename as an argument and checking for the file type information.

Postscript files are properly formatted for input to a Postscript printer.

10. (i). We can count the number of filenames by executing the command-line:

wc -l < foo

which outputs the total number of lines in the file foo.

(ii). By displaying the octal dump of the file's contents by executing:

od -bc foo

and checking for the presence of space character in the output.

11. DOS and UNIX text files differ by using different line ending characters. DOS uses
a combination of carriage return and line feed characters (\r\n), whereas, UNIX
uses line feed (\n) as the line ending character.
dos2unix command converts a text file from DOS format to UNIX format by
removing the extra carriage return characters from the places where the line
ending character combination occurs.
unix2dos command reverses the process by appending the carriage return
character before every occurrence of the line feed character.

12. The desired list can be created by executing the command-line:

comm -13 foo1 foo2

which will list the entries unique to the file foo2. The command will not work
properly when the files are unsorted, or the files contains more than one name
per line.

13. To add the two files to an archive and compress it, execute the command-line:

tar -cvf archive.tar foo.html bar.html ; gzip archive.tar

The compressed archive will be saved as archive.tar.gz. To delete the original files
execute:

rm foo.html bar.html

To reverse the process, uncompress and unarchive the files by executing the
command-line:

tar -xvf archive.tar.gz

This can be followed by deleting the compressed archive by running the


command:

rm archive.tar.gz

to completely reverse the process.

14. The three advantages of using zip over gzip are:

o zip files can be handled easily both in Windows and UNIX.

o zip combines archiving and compressing under a single command.

o zip command provides facility to add/append files to an existing


compressed archive.

To compress and archive a directory hierarchy to send via email execute:


zip -r archive.zip direcotry_path

The recipient can easily recreate the directory structure by executing:

unzip archive.zip

15. A command behaves recursively when it can descent a directory hierarchy and
execute over all the files, sub-directories and files thereof.

Examples of commands which support recursive operation are:

o cp -R: Recursively copies a directory hierarchy.


o ls -R: Recursively lists files in a directory hierarchy.
o rm -r: Recursively deletes files in a directory hierarchy.
o zip -r: Recursively compress and archives a directory hierarchy.

Chapter 06 - Basic File Attributes


1. Executing the command-line:

ls -lR / > listing

saves the listing of all the files and directories in the whole system in a file
named listing in the current directory.

2. The significance of the first four fields in ls -l output is as described below:

o First column: File type and the associated permissions for the file owner,
group owner and others.

o Second column: The number of links associated with the file.

o Third column: The username of the file owner.

o Fourth column: The name of the group that owns the file.

These attributes can be changed by the owner of the file as well as the system
administrator. (Depending on the release of UNIX i.e. AT&T or BSD. See answer
12 below).

Only the system administrator can change the group ownership of a file to a
group to which the owner doesn't belong.
3. (i). This command shows the attributes of the current directory in a long listing
format.

(ii). This command lists the contents of the parent directory in a long listing
format.

4. (i). If bar is an ordinary file, both the commands display bar as the output.

(ii). If bar is a directory that contains only a single file or directory also named bar,
than both the commands display bar as the output.

5. No. The group owner of a file can be changed to a group to which the file owner
is not a member of by the system administrator.

6. (i). By executing the command-line:

chmod u+rwx,o-rwx foo

(ii). By executing the command-line:

chmod 740 foo

In case of absolute assignment, we need to know the default permissions for the
file foo to preserve the group permissions.

7. The read permission may not be available for the source file. To copy the file, the
appropriate read permission is required which can be set either by the file owner
or the superuser.

8. (i). No one, including the owner of the file, can do anything with the file. The file
is technically inaccessible.

(ii). Anyone can read, write or execute the file. The file is publicly accessible.

9. kumar is the group owner of the file foo.

(i). kumar can edit the file as the group owners have the write permission
available.

(ii). Whether or not kumar can delete the file foo would depend on the group


ownership and permission set for the directory housing the file foo. kumar will be
able to delete the file foo if the parent directory belongs to group kumar, and has
the write permission available for the group.
(iii). kumar cannot change the permissions of the file foo as the user is not the
owner of the file.

(iv). kumar cannot change the ownership of the file foo as she is not the owner of
the file.

10. Assume that the file is named foo.

Using relative assignment:

(i). chmod a+rwx foo


(ii). chmod a-wx,o-r foo
(iii). chmod a-wx,u-r foo
(iv). chmod a-rwx foo

Using absolute assignment:

(i). chmod 777 foo


(ii). chmod 440 foo
(iii). chmod 044 foo
(iv). chmod 000 foo

11. No.

No.

12. On an AT&T system, the owner can change the owner and the group of a file. On
a BSD system, the owner of a file can be changed only by the system
administrator and the user can change the group of a file only to a group to
which she already belongs.

You might also like