Unix _module 1 Notes
Unix _module 1 Notes
INTRODUCTION
UNIX COMPONENTS/ARCHITECTURE:
The kernel is the core of the operating system. It is a collection of routines written in C. It is
loaded into memory when the system is booted and communicates directly with the hardware.
User programs that need to access the hardware use the services of the kernel via use of
system calls and the kernel performs the job on behalf of the user. Kernel is also responsible
for managing system’s memory, schedules processes, decides their priorities.
FEATURES OF UNIX
Several features of UNIX have made it popular. Some of them are:
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).
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.
Personal environment originally unix designed as a multiuser environment, many user user
are installed UNIX on their personal computers this tends to personal unix system
environment
Timesharing environment
Commands are entered at shell prompt. The components of the command line are: the verb;
any options required by the command the command's arguments (if required).
For example, the general form of a UNIX command is:
$verb [-option(s)] [argument(s)]
BASIC COMMANDS SUCH AS echo, printf, ls, who, date, passwd, cal
date: displays the system date and time. If the system is local that is one in your own area-it
is the current time. If the system is remote, such as across the country the reply will contain
the time where the system is physically located.
The input for the date is the system itself. The date is actually maintained in the computer as
a part of OS. The date command sends its response to monitor.
$date
Figure 1.6.1 system date
$date -u
$who am i
Displays the same information, but only for the terminal session where the command was
issued, for example:
$who –u
Indicates how long it has been since there was any activity on the line. This is known as
Idle time. It also returns the process id for the user.
$who -uH
Displays "all" information, and headers above each column of data, for example:
passwd command: The change Password(PASSWD) COMMAND
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.
password command
passwd syntax
passwd [OPTION] [USER]
$passwd
Running passwd with no options will change the password of the account running the
command. You will first be prompted to enter the account's current password: (current) UNIX
password:
If it is correct, you will then be asked to enter a new password: Enter new UNIX password:
...and to enter the same password again, to verify it: Retype new UNIX password:
If the passwords match, the password will be changed.
The passwd command changes passwords for user accounts. A normal user can only change
the password for their own account, but the superuser can change the password for any
account. Passwd can also change or reset the account's validity period — how much time can
pass before the password expires and must be changed. Before a normal user can change their
own password, they must first enter their current password for verification. (The superuser
can bypass this step when changing another user's password.)
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
must be >=6 characters long,
must contain 2 out of 3 of upper-case letters,
lower-case letters,
non-letters (digits, punct)
may not be a dictionary word or too similar to your name
echo: Print message command
The echo command used to display a message.
To display the diagnostic messages on the terminal or to issue prompts for taking user
input
To evaluate shell variable.
Echo command
$ echo [SHORT-OPTION]... [STRING]...
Options
-n Do not output a trailing newline.
-e Enable interpretation of backslash escape sequences (see below for a list of these).
-E Disable interpretation of backslash escape sequences (this is the default).
--help Display a help message and exit.
version Output version information and exit.
If you specify the -e option, the following escape sequences are recognized:
\\ A literal backslash character ("\").
\a An alert (The BELL character).
\b Backspace.
\c Produce no further output after this.
\e The escape character; equivalent to pressing the escape key.
\f A form feed.
\n A newline.
\r A carriage return.
\t A horizontal tab.
\v A vertical tab.
Ex 1: $echo Hello world Output :Hello world Ex2:
$x=10
$echo “The value of x is $x”
Entering these two commands will output the following text:
Output :The value of x is 10.
Ex 3:
$echo -e “Here\bthe\bspaces\bare\bbackspaced”
Outputs the following text:
Output :Herthspacearbackspaced //one character before backslash is deleted. Ex 4:
$echo –e “Unix \n is \n a \n os”
Output :$Unix is a Os //prints in a new line Ex 5:
$echo –e “unix \t is \t a \t os”
Output :unix is a os //\t –prints after 8 spaces Ex 6:
$echo –e “unix \r is a os”
Output : is a os //moving cursor to beginning of line. //words before \r is deleted. Ex 7:
$echo –e “unix \c is a os”
Output :unix // cursor in same line.
//words after \c deleted. Ex 8:
$echo $SHELL
Output :/bin/bash //prints the shell name
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 note; ls -l note
tThe above command first it displays the line count, word cont and byte or character count
along with this it also display the details of note file.
When you learn to redirect the output of these commands you may even like to group them
together within parentheses .
Example :
$(wc note ; ls -l note) > newlist
The combined output of the two commands is now sent to the file newlist. Whitespace is
provided here only for better readability. You might reduce a few keystrokes like this
When a command line contains a semicolon, the shell understands that the command on each
side of it needs to be processed separately. The ; here is known as a metacharacter, and you'll
come across several metacharaters that have special meaning to the shell.
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 to you that the
command line isn't complete. This is easily shown with the echo command:
$echo “this is
> a three-line
>text message”
Output: this is a three-line text message
The bc command in Unix (short for Basic Calculator) is a command-line calculator that
supports arbitrary precision arithmetic and allows scripting for complex calculations.
bc Command
Simply type bc in the terminal and start entering expressions:
$ bc
5+3
8
Exit with Ctrl+D or type quit.
Script Command
The script command in Unix is used to record a terminal session. It captures everything
displayed on the terminal and saves it to a file, which can be useful for logging, debugging, or
documentation purposes.
To start recording a session:
$ script
• This starts recording everything you type and see on the terminal.
• To stop recording, type exit or press Ctrl+D.
The output is saved in a file named typescript (default).
tty command
The tty command in Ubuntu (and other Unix-like systems) is used to display the file name of
the terminal connected to the standard input.
Syntax
tty [options]
Common Uses
1. Check the current terminal device
$ tty
Example output:
/dev/pts/0
This indicates that the user is working on a pseudo-terminal (pts/0), which is typical for SSH
and terminal emulators.
stty
The stty (set terminal type) command in Ubuntu is used to change and print terminal line
settings. It allows users to configure input and output settings of the terminal, such as baud
rate, character size, control characters, and various other terminal behavior settings.
Basic Syntax:
$stty [options]
Common Uses of stty
1. Display current terminal settings
$stty -a
This lists all terminal settings.
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.
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.
$ 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.
THE TYPE COMMAND: knowing the type of a command and locating it.
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. It
works like this: type command where “command” is the name of the command you want to
examine. Here are some examples:
$type type
Output: type is a shell built-in
$type ls
Output: ls is aliased to ‘ls –color=tty’
$type cp
Output: cp is /bin/cp
Here we see the results for three different commands. Notice that the one for ls (taken from a
Fedora system) and how the ls command is actually an alias for the ls command with the “--
color=tty” option added. Now we know why the output from ls is displayed in color!
UNIX FILES
NAMING FILES
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.
Alphabetic characters and numerals
The period(.), the hyphen(-) and underscore(_)
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
Directory files
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
The filename
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 you create or remove a file the kernel automatically updates its corresponding
directory by adding or removing the enter i.e inode number associated with that file.
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
Your terminal is also treated as the standard output file (stdout) to which a command's
output is sent.
stdin and stdout will be discussed in more detail later
Two types of I/O: character and block
Usually only found under directories named /dev.
Command Function
cd Returns you to your login directory
cd ~ Also returns you to your login directory
cd / Takes you to the entire system’s root 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.
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
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
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 y using frontslashes to demarcate the various levels,you have a
mechanism of identifying a file uniquely.No two files in a UNIX system can have same
absolute pathnames.
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 path name i,e precede its name with complete path
$/bin/date
For example if you need to execute program less residing in /usr/local/bin you need to enter
the absolute path name
$/usr/local/bin/less
If both ‘-L‘ and ‘-P‘ options are used, option ‘L‘ is taken into priority. If no option is
specified at the prompt, pwd will avoid all symlinks, i.e., take option ‘-P‘ into account.