CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1 History of Unix Developed in AT&T Bell Labs by Ken Thomson as a single user OS in 1969 Initially written.
Post on 26-Mar-2015
213 Views
Preview:
Transcript
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1
History of Unix
Developed in AT&T Bell Labs by Ken Thomson as a single user OS in 1969
Initially written in assembly language
Developed as multi-user OS later
Rewritten in C in 1973
Licensed to university for educational purposes in 1974
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2
Unix Flavors AIX by IBM
Solaris by Sun Microsystems
HP-UX by Hewlett-Packard Company
IRIX by Silicon Graphics, Inc.
BSD by University of California
GNU/Linux by Open Source Movement
MacOS by Apple Computer, Inc.
System V by AT&T
POSIX by IEEE
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3
Features of Unix
Multi-user
Multi-tasking
Portable
Interactive
Hierarchical File System
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 4
Unix Architecture
Hardware Kernel
Shell & Unix Utilities
Other Application Program
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 5
Directory Structure
cat
ls
user2 user3user1
progrm1
progrm2
bin boot home etc usr dev lib
Desktop
start
/
Directory
File
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 6
File Basics
Everything on Unix is a file. File structure is hierarchical like an upside down tree.
File is just a sequence of bytes.
The meaning of the bytes depends solely on the programs that interpret the file.
The format of a file is determined by the programs that use it.
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 7
File Attributes
Name (such as “test.log”)
An Owner (such as “sandhya”)
Access Rights (such as read, write, execute)
Other attributes (such as date of creation)
Among these attributes, file names are stored in the directory but other attributes like file-size, permission, links, time-stamps are stored in the inode.
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 8
File Types
Every item in a UNIX file system belongs to one of the four possible types:
1. Ordinary/Regular files
2. Directory files
3. Device/Special files
4. Symbolic links
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 9
Ordinary File
Contains text, data, or program information
Cannot contain another file or directory
Can be thought of as one-dimensional array of bytes
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 10
Directory File
Contains directory(s) and/or file(s) within it.
Has one line for each item contained within the directory.
Each line in a directory file contains only the name of the item, and a numerical reference to the location of the item, called inode number.
Inode number is an index to a table known as the inode table. Inode stores all information about the file except its name.
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 11
Device File
Physical devices (printers, terminals etc) are represented as “files”.
The read() and write() functions used to read and write ordinary files can also be used to read from and write to these devices.
Two types of device files:1. Character Special2. Block Special
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 12
Symbolic Link
Links make the same file available in multiple directories at the same time Two types of Links:
1. Hard Link2. Soft Link or Symbolic Link
1. Hard Link A hard link is another name given to the existing file These names share the same inode UNIX command ln is used to create hard links:
ln file1 file2
2. Soft Link A soft link to a file has a separate inode than the file It stores the target file’s path in its inode UNIX command ln –s is used to create soft links:
ln –s file1 file3
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 13
Inode
Inode is a data structure containing useful information about an item in the Unix File System.
Inodes reside on disk and do not have names. Instead, they have indices (numbers) indicating their positions in the array of inodes as shown in the next slide.
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 14
Inode
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 15
Pathname
Every item in the file system with a name can be specified with a pathname.
Pathname represents the path to the entry from the root of the file system. By following this path, the system can find the inode of the referenced entry.
Pathnames can be absolute or relative.
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 16
Unix Users
Super User
Owner
Group
Others
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 17
Unix Users
Superuser Can also be referred to as a System Administrator Has an overall authority on Unix OS Responsible for OS maintenance, backup and recovery, user
management etc. Superuser login is root and prompt is #
Owner Is a user who creates a file For every Unix file there can be only one owner File owner can assign the file permissions to group and other
users
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 18
Unix Users
Group In Unix, groups can be formed based on area of work Superuser can create a group and assign members to it Owner of a file can decide what permissions to be given to
group members
Others User who is not a owner and does not belong to any specific
group is referred to as other user Owner of a file can decide what permissions to be given to
other users
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 19
Unix Commands
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 20
Basic Commands
$ pwdShows working i.e current directory
$ whoShows who is logged on
$ who am IShows the login name of the current user
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 21
Basic Commands
$ man command formats and displays online manual pages
The manual pages are divided into logical grouping of the commands called the sections. Sections are numbered from 1 through 9. For example, commands are 1, system calls are 2, library function are 3, file formats are 5, & management commands are 8.
If section is specified, man only looks in that section of the manual.
The command man 2 open displays the manual for the system call open.
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 22
Basic Commands
$ date: command prints or sets the system date and time
For example,$ date$ date –r TestFileDisplays the last modification time of the file “TestFile”
This command can be used with suitable format specifiers as argument. Each format is preceded by a + symbol, followed by the % operator, and a single character describing the format.
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 23
Basic Commands
Sequence Interpretation
%a abbreviated weekday name (Mon .. Sun)
%b or %h abbreviated month name (Jan .. Dec)
%d day of month (01 .. 31) %r time (12- hour) %T time (24- hour) %y last two digits of year (00 .. 99) %D date (mm/dd/yy)
e. g. $ date “+Today is %a %m %Y”
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 24
Basic Commands
echo “Hi $USER \n Welcome to Unix”
echo ‘Hi $USER \n Welcome to Unix’
echo Hi $USER \n Welcome to Unix
clear
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 25
Basic Commands
$ cal Shows the calendar of the current month/year
$ bc 2.3+5.4
Shows the mathematical calculations involving integers as well as floats
bc is a filter command
$ write user1 Lets to write a message to the other user; user1 in this
example
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 26
File Management Commands
$ ls command to list files and directories
Option Description-l list in long format
-C multicolumn output -F indicates type of file by /, * -R recursive listing of all subdirectories
encountered -a list all files including hidden files -i List all files along with i-node number
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 27
Wild Card Characters
* - zero or more characters - ex ls m* lists all the files starting with m
? – single Character -ex ls m? lists all the files starting with m and having one character after that
[]- any character from all the characters within [] ex ls [aeiou]* lists all the files starting with a or e or i or o or u
- Specifies range
! – works as not operator ex: ls[!x-z]* will list all the files not starting with any character from the range x-z
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 28
File Management Commands cat
To concatenate files To display contents of one or more ordinary files To create an ordinary file
$ cat file1 file2 … It displays contents of all files specified on the command line
one below the other
$ cat > file It creates a new file by accepting text from the standard input Press CTRL-d to save and exit the file
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 29
File Management Commands
cd [directory] changes working directory to the directory, if specified;
otherwise to the home directory
cd .. moves to the parent directory and cd. keeps you in the current directory
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 30
File Management Commands
cp command copies files and directories
$ cp –i file1 file2 Copies file1 to file2 -i - informs user before overwriting, if file2 exists
$ cp file1 file2 … dest_directory Copies multiple files in the specified existing directory
$ cp -r directory1 directory2 … dest_directory Recursively copies files from directory1, directory2 etc. to the
dest_directory
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 31
File Management Commands
mv command changes name of the file or moves the file to the specified destination path
$ mv file1 new-file Renames file1 as new-file
$ mv file1 file2 … dest_directory Moves multiple files to the specified existing directory
$ mv directory1 directory2 … dest_directory Moves one or more directory subtrees to an existing or new
dest_directory
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 32
File Management Commands
rm command is used for deleting unwanted files/directories
$ rm [-i] file … It is interactive removal (option –i) of specified files
$ rm -r directory … It is recursive deletion of all the files within the specified
directories and also the directories themselves
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 33
Creating Links
ln file1 file2 Creates a hard link to existing file file1 with the name file2 in
the current directory Both names point to the same inode
ln –s file1 file3 file3 is a soft / symbolic link to file1 here Removal of file1 affects file3
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 34
Finding Files and Directories
find command is used for finding files and directories in the specified file tree based upon certain criteria
Command Syntax:$ find path criteria actione.g.
$ find /usr -name “ymess*” –print
Finds files recursively in the directory in path The path can be absolute or relative Multiple directories can be written in the path In the criteria, wild cards are allowed
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 35
Finding files by name:
find /home/user1 -name “*.sh” -print
finds all files with .sh extension in /home/user1 and its subdirectories
Finding files by type:
find /usr –type d –print
finds all directories in /usr and its subdirectories
find /var –type f –print
finds all regular files in /var and its subdirectories
Finding Files and Directories
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 36
Finding Files and Directories
Find files and take some other action than printing
find . –name “*.sh” –exec cp {} {}.bak \;
Finds all files with the extension .sh, in the current directory and its subdirectories, and then prepares a copy of each file with .bak extension
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 37
File Comparison
comm compares two sorted files and produces result in three colums:Lines: Unique to file1 Unique to file2 Common
$ comm file1 file2 Anil 12 2000
Ajit 12 2000 Dinesh 27 1400
Sunil 12 2000
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 38
File Access Permissions
chmod command is used to change the permissions on a file for owner, group and otherschmod <permission><filename>
Symbolic Method: Code Meaning
a allu userg groupo other+ add- remove= assign
chmod u+x, g-w, o+r, o-wx sample
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 39
File Access Permissions
Absolute value Method: Code Meaning 4 Read 2 Write 1 Execute
$ chmod 754 sample
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 40
File Access Permissions
umask command changes initial permission of newly created file
The value of argument can be calculated by subtracting the mode you want as default from the current default mode
Assume that the current default mode is 0666 and you want it as 0644 then 666 – 644 = 022 will be the parameter which we have to pass with “umask” command
$umask 0 – sets default mode which is 0666
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 41
Pipes
It is a feature by which filters & other commands can be combined in such a way that the standard output of one filter or command can be sent as standard input to another filter
e.g. $ ls >temp $ more temp
Ls |
moreInput Output
| $ ls | more
pipe
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 42
Filters
wc: counts lines, words and characters $ wc -[wlc] [filename]
head: displays first ‘n’ lines, horizontal slicing $ head -[n] [filename]
tail: displays last ‘n’ lines, horizontal slicing$ tail -[n] [filename]
split: divides files horizontally$ split -[n] [filename]
We get m subfiles of size n (xaa, xab,…)
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 43
Filters
cut: cuts file vertically either column wise/field wise
$ cut -[cfd] [filename] -c columns/characters -f field number -d field delimiter/separator
$ cut -c2-5 sample cuts columns 2 to 5 from the file sample
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 44
vi Editor
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 45
The vi editor
The vi editor is a screen-based editor which lets a user create new files or edit existing files
A key concept in vi is combining a certain action with a movement
vi is extremely powerful in moving around within (or between) files
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 46
The vi editor
A vi session begins by invoking the command “vi” with a filename
$ vi [filename]
You can start vi without a filename, but when you want to save your work, you will have to tell vi which filename to save it into.
The last line in the screen is reserved for some commands that you can enter to act on the text. This line is also used by the system to display messages.
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 47
Modes of Operation
The three different modes of operations are: Command mode: This is the default mode where you can pass
the commands to act on the text, using most of the keys of the keyboard
You can switch to this mode using “Esc” key
Insert (Input) mode: To enter the text, you have to enter into input mode. Press key “i” to enter into insert mode from command mode
You can switch to command mode by pressing “Esc” key
ex mode or line mode: You have to save your file or switch to another file or make a global substitution in the file. You then have to use ex mode, where you can enter the instruction in the last line of the screen. To enter into this mode, press “Esc” key followed by “:”
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 48
Text Insertion Commands
Command Description
i inserts text before cursor position a Appends text after cursor position I inserts text at beginning of line A Appends text after end of line o opens line below current line to insert text O opens line above current line to insert text
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 49
Cursor Movement Commands
Command Description
h left by one characterl right by one characterk up by one linej down by one linew right by one wordb left by one word0 or ^ beginning of line$ end of line
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 50
Cursor Movement Commands
Command Description
ctrl+u move up half window
ctrl+b move up full windowctrl+d move down half
windowctrl+f move down full
windowctrl+e scroll downctrl+y scroll up
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 51
Text Search Commands
Command Description
/text searches and highlights the text downwards n moves between highlighted text * searches the identical text on which the
cursor was ?text searches the text upwards
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 52
Text Deletion Commands
Command Description
x character under cursorX character before cursor[n]dw delete n wordsd0 beginning to cursor positiond$ or D cursor position to end of line[n]dd n lines from current line[n]dd pp will paste deleted lines to current cursor
position
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 53
Text Yanking Commands
Command Description
y character
y0 beginning to cursor position y$ cursor position to end of line [n]yw copy n words [n]yy n lines from current line in to the buffer [n]yy p p will paste copied lines to
current cursor position
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 54
Text Manipulation Commands
Command Description
nc [space] overwrites next n characters with space
c0 overwrites the portion between beginning of line to cursor position
c$ overwrites the portion between cursor to end of line
Cw overwrites current word:%s/pattern1/pattern2/g globally replaces pattern1 with pattern2 on
the specified lines:%s/pattern1/pattern2/gc globally replaces pattern1 with
pattern2 on the specified lines interactively
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 55
Text Related Commands
Command Description
ab <abb> <longword> set abbreviation for a long word
una string unset abbreviation>> right shifting a line<< left shifting a lineR replace characters starting with
current character till Esc is pressed
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 56
File Related Commands
Command Description
ZZ or :wq save and exit:w save & continue editing:q! quit without saving:r filnam I insert contents of file
filename :[addr1,addr2]w filname write the lines between line
number addr1 and line number
addr2 in the file filename
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 57
File Related Commands
Command Description
1,$s/source/target/ substitute string source by string target from line number 1to las line
u undo last change on the lineU undo last changes on the lineCtrl-R redo the undone changese edit multiple files e# return to previous file
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 58
Customizing vi
set commands
Command Description
:set all displays all set option:set autoindent/ai does automatic indentation:set number/nu shows all line duly numbered:set showmatch helps to locate matching brackets:set tabstop=5 sets tab=5 for display:set ic ignore case while pattern matching
When the string “no” is prefixed to any option, it indicates that the option is inoperative
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 59
Summary
In this session, we have covered: Unix Overview Unix File System Unix Commands Vi editor
top related