Unix Intro
Unix Intro
What is UNIX?
• UNIX is an Operating System (OS).
• An operating system is a control program that helps the
user communicate with the computer hardware.
• The most popular operating systems: Windows -- from
Microsoft. (Windows is the “Big Mac” of operating systems
-- cheap and “billions served”.)
• UNIX was developed long before Windows, in 1969 by Ken
Thompson at AT&T Bell Labs in the US.
• Command Line Interpreter
• GUIs (Window systems) are now available
What is UNIX?
• UNIX is an operating system for experts,
used on high-end workstations, database
servers, and web servers.
• UNIX provides some powerful features:
– security - private and shared files
– multi-user support
– data sent to display, files, or printers in same
way
– interprocess communication
• Microsoft keeps trying to upgrade
Windows to try to replace UNIX as the “OS
for experts”.
UNIX Versions
• There are many different versions of
UNIX for different hardware:
– Linux for the PC, including
• Mandriva (running in CS Lab, some PG/faculty desktops, and
Linux CPU servers; used to be called Mandrake)
• Red Hat
• Fedora Core (free community version of Red Hat)
• Debian (freeware)
– Sun Microsystem’s Solaris
– Hewlett-Packard’s HP-UX
– IBM’s AIX
– SGI’s IRIX
Who Uses UNIX?
• Big companies. They especially use
UNIX servers, preferring its stability.
They can afford to hire employees with
UNIX experience.
– Computer manufacturers such as Sun,
SGI, IBM, and HP
– Computer chip manufacturers like
Motorola & Intel
– Software companies
– Banks
– Hong Kong Government
– Hospital Authority
– Universities
• Small companies that use Linux
– OS free
Most Important Feature of UNIX
Microsoft Office
Word Document
Unix File System
• The arrangement of disk blocks in Unix is as shown in the figure below.
• The super block contains global information about the entire disk. It also maintains free list
of inodes and data blocks which can be allocated by the kernal when creating a file.
•
• The I-node which contains a table for every file of the file system. All attributes of the file
and directory are stored in this area except the name of the file or directory itself.
• data blocks contains the actual data in the form of directories and files.
The super block contains the following information, to keep
track of the entire file system.
• Owner: Owner indicates who owns the file(s) corresponding to this inode.
• Type: Type indicates whether inode represents a file, a directory, a FIFO, a
character device or a block device. If the type value is 0 then the inode is free.
• Last modified time , Last accessed time & Last inode modified time: The times
represent when, the file has been modified, when it was last accessed, or when the
inode has been modified last. Whenever the contents of the file is changed, the
"inode modified time" also changes. Moreover it changes when there are changes
for the inode like permission change, creating a link etc.
• Access Permissions: Each file will be having nine access permissions for read, write
and execute, for the owner, group and others in rwx rwx rwx format.
• No of links to the file: In Unix we can create links to some files or directories. So
we need to have a count of how many links are pointing to the same inode, so that
if we delete one of the links the actual data is not gone.
• Size of the file: Size of the file represents the actual size of the file.
• Data blocks owned : In Unix, we have a kind of indexing to access the actual data
blocks that contains data.
Assignment of new inodes
Inode Data Block representation
The data blocks contain the actual data contained in the files or
directories. In Unix, a directory is a special file. A directory file
contains names of the subdirectories and files present in that
directory and its corresponding inode number.
Basic UNIX commands
options
• ls will list all the files in your home directory, this command
has many options.
• ls -l will list all the file names, permissions, group, etc in long
format.
• ls -a will list all the files including hidden files that start with . .
• ls -lt will list all files names based on the time of creation,
newer files bring first.
• ls –Fx will list files and directory names will be followed by
slash.
• ls –R will lists all the files and files in the all the directories,
recursively.
• ls -R | more will list all the files and files in all the directories,
one page at a time.
• Mkdir command.
• Cd command.
• Head command.
head filename by default will display the first 10 lines of a file.
If you want first 50 lines you can use head -50 filename or for
37 lines head -37 filename and so forth.
• Tail command.
tail filename by default will display the last 10 lines of a file.
If you want last 50 lines then you can use tail -50 filename.
• More command. more command will display a
page at a time and then wait for input which is
spacebar. For example if you have a file which
is 500 lines and you want to read it all. So you
can use
more filename
• Some examples:
mv oldfile newfile will rename oldfile to newfile.
mv -i oldfile newfile for confirmation prompt.
mv -f oldfile newfile will force the rename even if
target file exists.
mv * /usr/priya/ will move all the files in current
directory to /usr/priya directory.
• Wc command
wc command counts the characters, words or lines in a file depending
upon the option.
Options
• wc -l filename will print total number of lines in a file.
• wc -w filename will print total number of words in a file.
• wc -c filename will print total number of characters in a file.
• Rcp command.
rcp command will copy files between two unix systems and works just like
cp command (-p and -i options too).
For example you are on a unix system that is called Cheetah and want to
copy a file which is in current directory to a system that is called lion in
/usr/john/ directory then you can use rcp command
•
rcp filename lion:/usr/john
You will also need permissions between the two machines.
• Ln command.
Instead of copying you can also make links to
existing files using ln command.
If you want to create a link to a file called
coolfile in /usr/local/bin directory then you
can enter this command.
ln mycoolfile /usr/local/bin/coolfile
Some examples:
• ln -s fileone filetwo will create a symbolic link
and can exist across machines.
• ln -n option will not overwrite existing files.
• ln -f will force the link to occur.
• Rm command.
To delete files use rm command.
Options:
• rm oldfile will delete file named oldfile.
• rm -f option will remove write-protected files
without prompting.
• rm -r option will delete the entire directory as
well as all the subdirectories, very dangerous
command.
• Rmdir command.
rmdir command will remove directory or
directories if a directory is empty.
Options:
• rm -r directory_name will remove all files
even if directory is not empty.
• rmdir priya is how you use it to remove priya
directory.
• rmdir -p will remove directories and any
parent directories that are empty.
Comparison and Searching
diff,dircmp, cmp, grep, find.
• Diff command.
diff command will compare the two files and print out the differences
between.
filetwo contains
• This is first file
• this is second line
• this is third line
• this is different xxxxxxxas;lkdjf this is not different
• Grep Command
grep command is the most useful search command. You can use it to find
processes running on system, to find a pattern in a file, etc. It can be used
to search one or more files to match an expression.
• $ cat apple.txt
Core
worm seed
jewel
• $ grep jewel apple.txt
jewel
• $
Options:
• -b option will precede each line with its block number.
• -c option will only print the count of matched lines.
• -i ignores uppercase and lowercase distinctions.
• -l lists filenames but not matched lines.
• $ls -l | grep "Aug"
-rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02
-rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07
-rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
• Find command.
Find command is a extremely useful command. you
can search for any file anywhere using this command
provided that file and directory you are searching
•
Some Examples:
•
find $HOME -print will lists all files in your home
directory.
find /work -name chapter1 -print will list all files
named chapter1 in /work directory.
find / -type d -name 'man*' -print will list all
manpage directories.
find / -size 0 -ok rm {} \; will remove all empty files
on system.
• -atime +n |-n| n will find files that were last accessed more
than n or less than -n days or n days.
• -ctime +n or -n will find that were changed +n -n or n days
ago.
• -depth descend the directory structure, working on actual
files first and then directories. You can use it with cpio
command.
• -exec commad {} \; run the Unix command on each file
matched by find. Very useful condition.
• -print print or list to standard output (screen).
• -size n find files that contain n blocks.
• -type c Find file whole type is c. C could be b or block, c
Character special file, d directory, p fifo or named pipe, l
symbolic link, or f plain file.
•
• Chgrp command.
chgrp command is used to change the group of a file
or directory.
You must own the file or be a super user.
chgrp [options] newgroup files is syntax of chgrp.
Newgroup is either a group Id or a group name
located in /etc/group .
• Chown command.
chown command to change ownership of a file or
directory to one or more users.
Syntax is
chown options newowner files
• Date command.
Date displays today’s date, to use it type date at prompt.
• Who command
who command displays information about the current status
of system.
who options file
Who as default prints login names of users currently logged
in.
Options
• -a use all options.
• -b Report information about last reboot.
• -d report expired processes.
• -H print headings.
• -p report previously spawned processes.
• -u report terminal usage.
• Write command will initiate an interactive
conversation with user. Syntax is
write user tty
The sort Command:
The sort command arranges lines of text alphabetically or
numerically. The example below sorts the lines in the food
file:
$sort food
• Afghani Cuisine
• Bangkok Wok
• Big Apple
• Deli Isle of Java
• Mandalay Sushi and Sashimi
• Sweet Tooth
• Tio Pepe's Peppers
• initially when this file will be created the permissions for this file
depends upon umask set in your profile files. As you can see this
file has 666 or -rw-rw-rw attributes.
ls -la cal.txt
• -rw-rw-rw- 1 ssb dxidev 135 Dec 3 16:14 cal.txt
• In this line above I have -rw-rw-rw- meaning respectively that
owner can read and write file, member of the owner's group
can read and write this file and anyone else connected to this
system can read and write this file.,
• ssb is owner of this file dxidev is the group of this file, there are
135 bytes in this file, this file was created on
• Banner command.
banner prints characters in a sort of ascii art poster
• Cal command
cal command will print the calendar on current
month by default. If you want to print calendar of
august of 1965. That's eighth month of 1965.
cal 8 1965
• Clear command
clear command clears the screen and puts cursor at
beginning of first line.
Pipes and Filters
• Unix commands alone are powerful, but when you combine them together, you
can accomplish complex tasks with ease. The way you combine Unix commands is
through using pipes and filters.
• Using a Pipe
• The symbol | is the Unix pipe symbol that is used on the command line. What it
means is that the standard output of the command to the left of the pipe gets sent
as standard input of the command to the right of the pipe. Note that this functions
a lot like the > symbol used to redirect the standard output of a command to a file.
However, the pipe is different because it is used to pass the output of a command
to another command, not a file.
• $ cat apple.txt
core
worm seed
jewel
• $ cat apple.txt | wc
Output 3 4 21 $
• $ cat apple.txt | wc | mail -s "The count" nobody@december.com
Using a Filter
• A filter is a Unix command that does some manipulation of the text of a file. Two of
the most powerful and popular Unix filters are the sed and awk commands. Both
of these commands are extremely powerful and complex.
• sed
• Here is a simple way to use the sed command to manipulate the contents of the
apple.txt file:
• $ cat apple.txt
core
worm seed
Jewel
• $ cat apple.txt | sed -e "s/e/WWW/“
corWWW
worm sWWWed
jWWWwel
• $ cat apple.txt | sed -e "s/e/J/g"
corJ
worm sJJd
jJwJl $
• awk
• The Unix command awk is another powerful filter. You can use awk to
manipulate the contents of a file. Here is an example:
$ cat basket.txt
• Layer1 = cloth
• Layer2 = strawberries
• Layer3 = fish
• Layer4 = chocolate
• Layer5 = punch cards
$ cat basket.txt | awk -F= '{print $1}‘
• Layer1
• Layer2
• Layer3
• Layer4
• Layer5
$ cat basket.txt | awk -F= '{print "HAS: " $2}'
• HAS: cloth
• HAS: strawberries
• HAS: fish
• HAS: chocolate
• HAS: punch cards $
• The pg and more Commands:
A long output would normally zip by you on
the screen, but if you run text through more
or pg as a filter, the display stops after each
screenful of text.
• Let's assume that you have a long directory
listing. To make it easier to read the sorted
listing, pipe the output through more as
follows:
System vi Editor
– The default editor. Other editors include pico, emacs
– The UNIX vi editor is a full screen editor and has two modes of operation:
– Command mode commands which cause action to be taken on
the file, and
– Insert mode in which entered text is inserted into the file.
– In the command mode, every character typed is a
command that does something to the text file being
edited; a character typed in the command mode may
even cause the vi editor to enter the insert mode. In
the insert mode, every character typed is added to
the text in the file; pressing the <Esc> (Escape) key
turns off the Insert mode.
• To Get Into and Out Of vi
• To Start vi
– To use vi on a file, type in vi filename. If the file named
f1 exists, then the first page (or screen) of the file will
be displayed; if the file does not exist, then an empty
file and screen are created into which you may enter
text.
• vi f1 edit f1 starting at line
• vi -r f1 recover filename that was being edited
when system crashed
• To Exit vi
– Usually the new or modified file is saved when you leave
vi. However, it is also possible to quit vi without saving the
file.
– In the table below, the symbol ^ before a letter means that the <Ctrl> key
should be held down while the letter key is pressed.
• j or <Return>
[or down-arrow] move cursor down one line
• k [or up-arrow] move cursor up one line
• h or <Backspace> [or left-arrow] move cursor left one character
• l or <Space> [or right-arrow] move cursor right one character
• 0 (zero) move cursor to start of current line (the one with the cursor)
• $ move cursor to end of current line
• w move cursor to beginning of next word
• b move cursor back to beginning of preceding word
• :0<Return> or 1G move cursor to first line in file
• :n<Return> or nG move cursor to line n
• :$<Return> or G move cursor to last line in file
• Screen Manipulation
– The following commands allow the vi editor screen (or
window) to move up or down several lines and to be
refreshed.
• ^f move forward one screen
• ^b move backward one screen
• ^d move down (forward) one half screen
• ^u move up (back) one half screen
• ^l redraws the screen
• ^r redraws the screen, removing deleted lines
• Adding, Changing, and Deleting Text
– Unlike PC editors, you cannot replace or delete text by
highlighting it with the mouse. Instead use the commands
in the following tables.
– Being able to determine the line number of the current line or the total
number of lines in the file being edited is sometimes useful.