Chapter 01 - Getting Started
Chapter 01 - Getting Started
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 csh: C shell.
o ksh: KornShell.
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.
10. Directories, devices, terminals and printers are all represented as files in the UNIX
file-system.
o Backing up files.
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 Multiuser: Ability to support multiple users at the same time and allowing
them to use a single installation of operating system simultaneously.
Windows don't share the same system calls as UNIX. Neither does it include the C
Standard Library in its default installation.
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:
If multiple contiguous whitespaces are present between the command and its
arguments, the shell automatically compresses it to a single space character.
date +"%d-%m-%y/%H:%M:%S"
(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.
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.
9. To hide the keyboard input from getting displayed on the terminal execute:
stty -echo
stty echo
stty -a
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.
(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.
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.
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.
(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.
(iii). The command moves all the files and directories in the current directory into
a directory named bin in the parent directory.
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.
10. (i). We can count the number of filenames by executing the command-line:
wc -l < foo
od -bc foo
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.
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:
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:
rm archive.tar.gz
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.
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 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.
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.
(i). kumar can edit the file as the group owners have the write permission
available.
(iv). kumar cannot change the ownership of the file foo as she is not the owner of
the file.
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.