You are on page 1of 6

UNIX Introduction

UNIX is a open system (source code) operating system.


Operatiing System :: Collection of system programs.It acts as a interface between
Hardware and the user.Lets look at the below diagram for more information...

UNIX is a Character User Interface (CUI )OS


Different types of OS ::

CUI GUI
Character User Interface Graphical User Interface
eg., MS-DOS,UNIX eg., LINUX , WINDOWS , MACINTOSH

UNIX File System::

A file is the basic structure used to store information on the UNIX system.The UNIX file system
provides a logical method for organizing,storing,retreiving,manipulating and information.

A file can store manuscripts and other word processing documents, instructions or programs for
the computer itself, an organized database of business information, a bitmap description of a
screen image, or any other kind of information stored as sequence of bytes on a computer.

With in UNIX, there are three different types of files ::

Regular Files
Directory Files
Special Files
Explanation ::

Regular Files

As a user, the information that you work with will be stored as an ordinary file.Ordinary
files/Regualr file aggregates of characters that are treated as a unit by the UNIX system.

A Regualr file can contain normal ASCII characters such as text for manuscripts or
programs.Ordinary file can be created, changed, or deletedas you wish.

Directory Files

Directory is a file that holds other files and contain information about the locations and attributes
of these other files.For example a directory includes a list of all the files and sub directories that it
contains, as well as their addresses, characteristics, file types and other attributes.

Basic Commands
# system adimistrator prompt
$ user working prompt

$pwd :: It displays present working directory. (If you login with dwhlabs)

/home/dwhlabs

$logname :: It displays the current username.


dwhlabs

$clear :: It clears the screen.

$date :: It displays the current system date and time.

Day Month date hour:min:sec standard name year


FRI MAY 23 10:50:30 IST 2008

$banner :: It prints a message in large letters.

$banner "tecno"

Administrator Commands

# system adimistrator prompt


$ user working prompt

#useradd :: To add the user terminals.

#useradd user1

#password :: To give the password to a particular user terminal.

#password user1
Enter password ::
Retype password ::

System Run Levels

#init :: To change the system run levels.

#init 0 : To shut down the system


#init 1or s: To bring the system to single user mode.
#init 2 : To bring the system multi user mode with no resource shared.
#init 3 : To bring the system multi user mode with resource shared.
#init 6 : Halt and reboot the system to the default run level.

ls command options

$ls :: This command is used to display list of files and directories.


$ls -x :: It displays width wise
$ls | pg :: It displays list of files and directoriews page wise & width wise.
$ls -a :: It displays files and directories including . and .. hidden files
$ls -f :: It displays files, diectories,executable files, symbolic files
a1
a2
sample\
letter*
notes@

\ directory
* executable files
- symbolic link file
$ls -r ::It displays files and directories in reverse order.(descending order)
$ls -R:: Itr displays files and directories recursively.
$ls -t :: It displays files and directories based on date and time of creation.
$ls -i :: It displays files and directories along with inode number.
$ls -l :: It displays files and directories in long list format.
file type permission link uname group sizeinbytes date filename
- rw-r 1 tec group 560 nov 3 1:30 sample
d rwxr-wr 2 tec group 430 mar 7 7.30 student

Different types of files ::

- ordinary file
d directory file
b special block files
c special character file
l symboliclink file

Wild card characters * , ? , [] ,-, . ,^,$

wild card character Description

* It matches one or more characters


? It matches any single character
[] It matches any single character in the given list
- It matches any character in that range
. It matches any single character except enter key character.
^ It displays line which starts with that character
$ It displays line which ends with that character.
Examples ::

* wild card

$ ls t* It displays all files starting with 't' character.


$ ls *s It displays all files ending with 's'
$ ls b*k It displays all files starting with b and ending with k
? wild card
$ ls t?? It displays all files starting with 't' character and also file length must be 3
characters..
$ ls ?ram It displays all files starting with any character and also the length of the file must
be 4 characters.
- wild card
$ ls [a-z]ra It displays all files starting with any character between a to z and ends
with 'ra'.The length of the file must be 3 characters.

Filter Commands

$ grep ::Gloabally Search a Regular expression and print it.


This command is used to search a particular pattern(single string ) in a file or directory and
regualr expression(pattern which uses wild card characters).

syntax - $grep pattern filename


eg. $grep dwhlabs dwh (It displays all the lines in a file which as dwhlabs pattern/string) -
Character Pattern
eg. $grep "dwhlabs website" dwh - character pattern
eg. $grep \< dwhlabs\> dwh (It displays all the line in a file which as a exact word 'dwhlabs') -
Word Pattern
eg. $grep ^UNIX$ language (It displays all the lines in the file which has a single word 'UNIX' ) -
Line Pattern

$grep options

$grep -i pattern filename (ignore case sensitive)

$grep -c pattern filename (It displays total count of lines)

$grep -n pattern filename (It displays along with the line number)

$grep -l pattern filename (It displays filename in the current directory)

$grep -v pattern filename (It displays unmatched pattern lines)

Question ?
Q)How to remove blank lines in a file?
A)$grep -v "^$" file1>tmp | mv tmp>file1

Explanation :: Piping concept is used to combine more than one cammand.Here we first identifing
the non blank files in file1 and redirecting the result to tmp (temporary file).This temporary file is
input to the next command. Now we are renaming the tmp into file1.Now check the file1 result
<You will not find any blank lines>

fgrep ::

This command is used to search multiple strings or one string but not regular expression.

syntax - $fgrep "pattern1


>pattern2
>....
>patternn " filename

eg.$fgrep "unix
>c++
>Data Warehouse" stud

egrep :: Extended grep

This command is used to search for single or multiple patterns and also regular expressions.
Filter Commands

$ cut :: This command is used to retreive the required fields or characters from a file.

100 Rakesh UNIX HYD


101 Ramani C++ CHE
102 Prabhu C BAN
103 Jyosh DWH CHE
syntax - $cut -f 1-3 filename
eg. $cut -f 0-2 employee (all the fields in the above file)

100 Rakesh UNIX


101 Ramani C++
102 Prabhu C
103 Jyosh DWH

eg. $cut -c 1-8 employee (first 8 characters)

100 Rake
101 Rama
102 Prab
103 Jyos
Delimeters

Default ( Tab )
: , ; * _
$cut options

$cut -f 0-2 employee


$cut -c 1-8 employee
$cut -d ',' 1-8 employee

$ sort :: This command is used to sort the file records in ascending or descending

$sort Options

$sort -r filename (reverse)


$sort -u filename (unique records)
$sort -n filename(show the number along sorting)

How to sort data in field wise ?

syntax- $sort -f +pos1 -pos2 filename


eg. $sort -f +1 -2 employee

$ uniq :: This command is used to filter the duplicate lines from a file
Note :: Always the file must be in sorting order.

syntax - $uniq filename


eg. $uniq employee

$uniq Options

eg. $uniq -u employee (displays non duplicated lines)


eg. $uniq -d employee ( displays duplicated lines)

Question ::

Q) How to remove duplicate lines in a file?


A) $uniq employee>temp
$mv temp employee
Filter Commands

$ tr :: Translate the character in string1 from stdin into those in string2 in stdout.

$ sed :: This command is used for editing files from a script or from the command line.

$ head :: Display first 'N' number of files.

$ tail :: Display last 'N' number of files.

$ cmp :: Compare two files andd list where differences occur.

$ diff :: Compare the two files and shows the difference.

$ wc :: Display word (or character or line ) count for file.


============

Shell Scripting Exercise


General
1 Write a script to open a given file?
2 Write a script to delete the blank lines from a file?
3 write a script to delete duplicate lines from a file?

if then else
4 Write a script to check the givn number is positive or negative?
5 Write a script to check the given number is even or odd number?
6 Write a script to accept two strings and check the given strings are equal or not?
7 Write a script to delete the given file?
8 Write a script to check the given user is logged in or not?
9 Write a script to check the given type of file?
10 Write a script to open the given file?

File watchers / File test commands


11 Write a script to check the given two files are equal or not?

case
12 Write a script to check the given type of character?
13 Write a script to create the menu creation?

You might also like