Final Unix Project
Final Unix Project
INDEX
3 11-14
Execute the following list of basic commands in UNIX:
(i) who (ii) echo (iii) cat
4 15-22
Execute the following list of basic commands in UNIX:
(i) rm (ii) mv (iii) wc (iv) cp
5 23-29
Execute the basic file attributes with all possible options:
(i) ls (ii) chmod
6 20-33
Execute basic commands using vi editor: a.
input mode commands
b. saving text and quitting
7 34-38
Execute basic commands using vi a.
navigation
b. editing text
c. searching pattern
8 Execute the following filters using regular expressions 39-41
with all possible options: (i) grep (ii) sed
9
Write a shell script to display current date and calendar 42
INTRODUCTION
What is UNIX?
UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows
which provides an easy to use environment. However, knowledge of UNIX is required for
operations which aren't covered by a graphical program, or for when there is no windows
interface available, for example, in a telnet session.
Types of UNIX
Here in the School, we use Solaris on our servers and workstations, and Fedora UNIX
on the servers and desktop PCs.
The UNIX operating system is made up of three parts; the kernel, the shell and the
programs.
The Kernel
The kernel of UNIX is the hub of the operating system: it allocates time and memory to
programs and handles the file store and communications in response to system calls.
As an illustration of the way that the shell and the kernel work together, suppose a user
types rm myfile (which has the effect of removing the file myfile). The shell searches the
filestore for the file containing the program rm, and then requests the kernel, through
system calls, to execute the program rm on myfile. When the process rm myfile has
finished running, the shell then returns the UNIX prompt % to the user, indicating that it is
waiting for further commands.
The Shell
The shell acts as an interface between the user and the kernel. When a user logs in, the
login program checks the username and password, and then starts another program called
the shell. The shell is a command line interpreter (CLI). It interprets the commands the
user types in and arranges for them to be carried out. The commands are themselves
programs: when they terminate, the shell gives the user another prompt (% on our
systems).
The adept user can customise his/her own shell, and users can use different shells on the
same machine. Staff and students in the school have the tcsh shell by default.
The tcsh shell has certain features to help the user inputting commands.
A file is a collection of data. They are created by users using text editors, running
compilers etc.
Examples of files:
All the files are grouped together in the directory structure. The file-system is arranged in
a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally
called root (written as a slash / )
In the diagram above, we see that the home directory of the undergraduate student
"ee51vn" contains two sub-directories (docs and pics) and a file called report.doc.
An UNIX Terminal window will then appear with a % prompt, waiting for you to start
entering commands.
UNIX Commands
The UNIX system is command-based i.e. things happen because of the commands that you
key in. All UNIX commands are seldom more than four characters long.
• Internal Commands: Commands which are built into the shell. For all the shell built-in
commands, execution of the same is fast in the sense that the shell doesn’t have to
search the given path for them in the PATH variable, and also no process needs to be
spawned for executing it.
Examples: source, cd, fg etc.
• External Commands: Commands which aren’t built into the shell. When an external
command has to be executed, the shell looks for its path given in the PATH variable,
and also a new process has to be spawned and the command gets executed. They are
usually located in /bin or /usr/bin. For example, when you execute the “cat” command,
which usually is at /usr/bin, the executable /usr/bin/cat gets executed. Examples: ls, cat
etc.
PROGRAM-1
OBJECTIVE: Execute the following list of basic commands in UNIX:
(i) pwd (ii) mkdir (iii) cd
(i) Pwd
• pwd stands for Print Working Directory.
• It prints the path of the working directory, starting from the root. pwd is shell built-in
command (pwd) or an actual binary(/bin/pwd).
$PWD is an environment variable which stores the path of the current directory. This
command has two flags.
(ii) mkdir
• mkdir command in Linux allows the user to create directories (also referred to as folders in some
operating systems).
• This command can create multiple directories at once as well as set the permissions for the
directories.
• It is important to note that the user executing this command must have enough permissions to
create a directory in the parent directory, or he/she may recieve a ‘permission denied’ error.
Options: -
I. --version: It displays the version number, some information regarding the license and exits.
II. --help: It displays the help related information and exits.
III. -v or --verbose: It displays a message for every directory created.
IV. -p: A flag which enables the command to create parent directories as necessary. If the
directories exist, no error is specified.
V. -m: This option is used to set the file modes, i.e., permissions, etc. for the created directories.
The syntax of the mode is the same as the chmod command.
We are in the /home/user directory where we have created a directory named “BCA”.
(iii) cd
The command accepts only two options that are rarely used.
In its simplest form, when used without any argument, cd will take you to your home
directory.
Here, the user has changed the directory to BCA which we can notice on executing the
pwd command, the shell displays the current working directory.
PROGRAM-2
OBJECTIVE: Execute the following list of basic commands in UNIX:
(i) who
• The standard Unix command who displays a list of users who are currently
logged into the computer.
• The who command is related to the command w, which provides the same
information but also displays the following information:
1) Time of last system boot
2) Current run level of the system 3) List of logged in users and more.
• The who command displays the following information for each user currently
logged in to the system if no option is provided:
1) Login name of the users
2) Terminal line numbers
3) Login time of the users in to system
4) Remote host name of the user
HP user@Sony-Vaio MINGW64 ~
$ who
HP user@Sony-Vaio MINGW64 ~
(ii) echo
• echo command in linux is used to display line of text/string that are passed as an
argument .
• This is a built in command that is mostly used in shell scripts and batch files to
output status text to the screen or a file.
Options: -
(iii) cat
Options: -
Tag Description
-e equivalent to -vE
-t equivalent to -vT
-u (ignored)
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB. display this help and
exit
PROGRAM-3
OBJECTIVE: Execute the following list of basic commands in UNIX:
(i) rm
Options: -
TAG DESCRIPTION
prompt once before removing more than three files, or when removing
recursively. Less intrusive than -i, while still giving protection against most
-I mistakes.
-- prompt according to WHEN: never, once (-I), or always (-i). Without interactive[=WHEN] WHEN,
prompt always.
(ii) mv
mv stands for move. mv is used to move one or more files or directories from one place
to another in file system like UNIX. It has two distinct functions:
a) It renames a file or folder.
b) It moves group of files to different directory.
Options: -
Tag Description
-u, --update
move only when the SOURCE file is newer than the destination file or
when the destination file is missing
(iii)
wc
• wc stands for word count. As the name implies, it is mainly used for counting purpose.
• It is used to find out number of lines, word count, byte and characters count in the files
specified in the file arguments.
• By default, it displays four-columnar output.
• First column shows number of lines present in a file specified, second column shows
number of words present in the file, third column shows number of characters present in
file and fourth column itself is the file name which are given as argument.
Options: -
Tag Description
(iv) cp
• cp stands for copy. This command is used to copy files or group of files or
directory.
• It creates an exact image of a file on a disk with different file name. cp
command requires at least two filenames in its arguments.
cp command works on three principal modes of operation and these operations depend
upon number and type of arguments passed in cp command:
1. Two file names: If the command contains two file names, then it copies the
contents of 1st file to the 2nd file. If the 2nd file doesn’t exist, then first it
creates one and content is copied to it. But if it existed then it is simply
overwritten without any warning. So be careful when you choose destination file
name.
cp Src_file Dest_file
2. One or more arguments: If the command has one or more arguments, specifying
file names and following those arguments, an argument specifying directory
name then this command copies each source file to the destination directory with
the same name, created if not existed but if already existed then it will be
overwritten, so be careful!
3. Two directory names: If the command contains two directory names, cp copies
all files of the source directory to the destination directory, creating any files or
directories needed. This mode of operation requires an additional option,
typically R, to indicate the recursive copying of directories.
cp -R Src_directory Dest_directory
Options: -
Tag Description
copy only when the SOURCE file is newer than the destination file or when
-u, --update
the destination file is missing
-V, --version-
override the usual version control
control=WORD
PROGRAM-4
OBJECTIVE: Execute the basic file attributes with all possible options: (i) ls (ii) chmod
i. ls
Description
Tag
with -lt: sort by, and show, ctime (time of last modification of file status
-c information) with -l: show ctime and sort by name otherwise: sort by ctime
across -x, commas -m, horizontal -x, long -l, single-column 1,verbose -l,
--format=WORD vertical -C
-h, --human-readable with -l, print sizes in human readable format (e.g., 1K 234M 2G)
--
dereferencecommandlinesymlink- follow each command line symbolic link that points to a directory
to-dir
-k like --block-size=1K
-n, --numeric-uid-gid like -l, but list numeric user and group IDs
-N, --literal print raw entry names (don't treat e.g. control characters specially)
show non graphic characters as-is (default unless program is `ls' and output
--show-control-chars
is a terminal)
use quoting style WORD for entry names: literal, locale, shell, shellalways,
--quotingstyle=WORD
c, escape
sort by WORD instead of name: none -U, extension -X, size -S, time -t,
--sort=WORD
version -v
with -l, show time as WORD instead of modification time: atime-u, access
--time=WORD -u, use -u, ctime -c, or status -c; use specified time as sort key if -sort=time
with -l, show times using style STYLE: full-iso, long-iso, iso,locale,
+FORMAT. FORMAT is interpreted like `date'; if FORMAT is
-time-style=STYLE FORMAT1FORMAT2, FORMAT1 applies to non-recent files and
FORMAT2 to recent files; if STYLE is prefixed with `posix-', STYLE takes
effect only outside the POSIX locale
with -lt: sort by, and show, access time with -l: show access time and sort by
-u
name otherwise: sort by access time
ii. chmod chmod command is used to change the access mode of a file.
The operator is used to specify how the modes of a file should be adjusted. The
following operators are accepted:
Operator Description
+ Adds the specified modes to the
specified classes
s If found in the user triplet it sets the setuid bit. If found in the group
triplet, it sets the setgid bit. It also means that x flag is set. When
the setuid or setgid flags are set on an executable file, the file is
executed with the file’s owner and/or group privileges.
S Same as s but the x flag is not set. This flag is rarely used on
files.
T Same as t but the x flag is not set. This flag is useless on file
Numeric Method:
• no permissions = 0
• x (execute) = 1
• w (write) = 2
• r (read) = 4
PROGRAM-5
OBJECTIVE: Execute basic commands using vi editor:
a. input mode commands
b. saving text and quitting
• Vi Editor is the default editor that comes with the UNIX operating system.
• Using vi editor, we can edit an existing file or create a new file from scratch.
• We can also use this editor to just read a text file.
• Command Mode: When vi starts up, it is in Command Mode. This mode is where vi
interprets any characters we type as commands and thus does not display them in the
window. This mode allows us to move through a file, and to delete, copy, or paste a
piece of text.
To enter into Command Mode from any other mode, it requires pressing the [Esc]
key. If we press [Esc] when we are already in Command Mode, then vi will beep
or flash the screen.
• Insert mode: This mode enables you to insert text into the file. Everything that’s
typed in this mode is interpreted as input and finally, it is put in the file. The vi
always starts in command mode. To enter text, you must be in insert mode. To come
in insert mode, you simply type i. To get out of insert mode, press the Esc key,
which will put you back into command mode.
• Last Line Mode (Escape Mode): Line Mode is invoked by typing a colon [:], while
vi is in Command Mode. The cursor will jump to the last line of the screen and vi
will wait for a command. This mode enables you to perform tasks such as saving
files, executing commands.
S.No.
vi filename
Creates a new file if it already does not exist, otherwise opens an existing file.
vi -R filename
Opens an existing file in the read-only mode.
view filename
Opens an existing file in the read-only mode.
• The command to quit out of vi is :q. Once in the command mode, type colon, and 'q',
followed by return. If your file has been modified in any way, the editor will warn
you of this, and not let you quit. To ignore this message, the command to quit out of
vi without saving is :q!. This lets you exit vi without saving any of the changes.
• The command to save the contents of the editor is :w. You can combine the above
command with the quit command, or use :wq and return.
• The easiest way to save your changes and exit vi is with the ZZ command. When
you are in the command mode, type ZZ. The ZZ command works the same way as
the :wq command.
• If you want to specify/state any particular name for the file, you can do so by
specifying it after the :w. For example, if you wanted to save the file you were
working on as another filename called filename2, you would type :w filename2 and
return.
PROGRAM-6
OBJECTIVE: Execute basic commands using vi editor
a. navigation
b. editing text
c. searching pattern
a) Moving within a File
1
k
Moves the cursor up one line
2
j
Moves the cursor down one line
3
h
Moves the cursor to the left one-character position
4
l
Moves the cursor to the right one-character position
1
i
Inserts text before the current cursor location
2 b) Editing
I Files
Inserts text at the beginning of the current line
3
a
Inserts text after the current cursor location
4
A
Inserts text at the end of the current line
5 o
Creates a new line for text entry below the cursor location
6
O
Creates a new line for text entry above the cursor location
c) Searching Pattern
Character &Description
Sr.No.
^
Searches at the beginning of the line (Use at the beginning of a search expression).
2
.
Matches a single character.
3
*
Matches zero or more of the previous character.
4
$
End of the line (Use at the end of the search expression).
5
[
Starts a set of matching or non-matching expressions.
6
<
This is put in an expression escaped with the backslash to find the ending or the
beginning of a word.
7
>
This helps see the '<' character description above
PROGRAM-7
OBJECTIVE: Execute the following filters using regular expressions with all possible
options:
i. grep
• The grep filter searches a file for a particular pattern of characters, and displays
all lines that contain that pattern.
• The pattern that is searched in the file is referred to as the regular expression
(grep stands for globally search for regular expression and print out).
Options:
Options Description
This prints only a count of the lines that match a pattern
-c
This prints out all the lines that do not matches the pattern
-v
Print only the matched parts of a matching line, with each such part on a
-o separate output line.
ii. sed
• SED command in UNIX is stands for stream editor and it can perform lots of
function on file like, searching, find and replace, insertion or deletion. Though most
common use of SED command in UNIX is for substitution or for find and replace.
By using SED, you can edit files even without opening it, which is much quicker
way to find and replace something in file, than first opening that file in VI Editor
and then changing it.
• SED is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).
• SED command in UNIX supports regular expression which allows it perform
complex pattern matching.
PROGRAM-8
OBJECTIVE: Write a shell script to display current date and calendar
SOURCE CODE:
#shell script to display date and calendar echo -e "\n\nToday is %s\n"
here\n""$(cal)"