0% found this document useful (0 votes)
32 views

Basic Unix

The document discusses the structure and organization of files and directories in a Unix operating system. It describes the default directories like bin, sbin, usr, var, etc and their purposes. It also explains concepts like absolute and relative pathnames, file permissions and basic file operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Basic Unix

The document discusses the structure and organization of files and directories in a Unix operating system. It describes the default directories like bin, sbin, usr, var, etc and their purposes. It also explains concepts like absolute and relative pathnames, file permissions and basic file operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 52

Unix Operating System Structure

 OS mediates between the user and the computer

User

Application Programs

Shell
Kernel

Hardware
Overview in file system

Topics:-

Definition of file system

Default directories in file system

Structure of file system

Use of each directory


Default directories in file system
/bin

/sbin

/usr

/var

/etc

/dev

/opt

/tmp

/lib

/home
Example Directory Structure

root (/)

bin dev lib home sbin tmp usr var


etc

jack jill

Bats marsupials
continents oceans

bandicoot opossum kangaroo wombat


wombat
Typical Directories

 bin: Software for the shell and most common Unix commands. Ex:-cat,who

 dev: Short for devices, holds the software necessary to operate peripherals such
as printers and terminals. Ex:-lp,hd

 etc: Contains various administrative files such as lists of user names and
passwords.
Ex:-/etc/passwd,/etc/shadow

 home: Contains the home directories of users.

 tmp: A directory used to hold temporary files.

 var: Files that contain information that varies frequently; most commonly, mail
directories.
Ex:-/var/adm/spool
/usr:-this is where the manual pages are stored. There are
separate sub-directories here (like man1,man2,etc)
that contain the pages
For ex the man page of ls can be found in
/usr/share/man/man1

/sbin: -this is where the administrator commands are stored


Ex:-useradd,format

/lib:- contains all library files in library form. You need to link
your c programs with files in these directories
Working with files and
directories
File and Directory Names

 Valid names can be made up of:


 Uppercase letters (A to Z)
 Lower case letters (a to z)
 Numbers (0 to 9)
 period (.), underscore (_), commas (,).

 Should not contain spaces or the following:


 &*\|[]{}$<>()#?‘“/;^!~%
 You should also avoid naming files or directories with Unix
commands.
Pathnames

Types of path names

•Absolute Path

•Relative path
Absolute Pathnames

 To use a file in Unix, all you need is its name.


 To access a file in another directory, you need to know its pathname.
 A pathname is an address that shows the file’s position in the file system.
 Absolute path names give a files location in relation to the top of the file
system structure.
 All absolute path names begin with a /.
 Also called full path names.
 Use the ~ as a shortcut for the full path to a home directory.
Relative path names

 Usually you are interested in a files location relative


to where you currently are in the system.
 A relative pathnames start at the working directory
rather than the root.
 Use the .. to refer to the parent of the current
directory.
Creating a directory
$mkdir <dir name>

$mkdir dir1 dir2 dir3 => creating multiple directories

$mkdir –p a1/a2/a3/a4 => creating multi level directories

Deleting directories
$rmdir <dir name> => If the dir is empty this command is
applicable

$rm <options> <dir name> =>If the dir is not empty this
command is applicable.
-i => Interactively
-r => Recursively
-f => forcefully
Creating files:-

files can be created by three methods

Using cat command


Using touch command
Using vi command

Creating a file using cat command:-


$cat > F.N

Opening a file using cat command:-


$cat < F.N
or
$cat F.N
Creating file using touch command:-
$touch F.N

Creating multiple files using touch command:-


$touch m1 m2 m3 m4 m5

Note:If you create a file with touch an empty file will be created.

Removing files:-
$rm <F.N>

Removing multiple files:-


$rm m1 m2 m3 m4 m5
Renaming files and directories

$mv <source file> <target file>

Ex:- $mv text text1


From the above example the file name text
changes to text1,same scenario is applicable to dir also

Copying files and directories

$cp <source file> <target file>

Ex:- $cp f1 d1
From the above example the file f1 is transferred i.e
copied to dir d1
I/O Redirection

 Most commands output to the screen


ls

 Output can be redirected to a file with‘>‘:


ls > dir.txt
cal 1997 > year1997

 Output can be appended to a file with ‘>>‘


cal 1997 > years
cal 1998 >> years
 Concatenate two files:
cat f1 f2 > fs

 Input redirection (less common) uses ‘<‘


wc < years

 Combine input and output redirection:


wc < years > year-counts
Unix file
permissions
Unix file security

 Each file has owner and group


 Permissions set by owner
 Read, write, execute
 Owner, group, other
 Represented by vector of four octal values
 Only owner, root can change permissions
 This privilege cannot be delegated or shared
File system security (access rights)

-rwxrwxrwx a file that everyone can read, write and execute


(and delete).

-rw------- a file that only the owner can read and write - no-
one else can read or write and no-one has
execution rights (e.g. your mailbox file).
Unix File Permissions

 File type, owner, group, others


drwx------ 2 jjoshi isfac 512 Aug 20 2003 risk management
lrwxrwxrwx 1 jjoshi isfac 15 Apr 7 09:11 risk_m->risk
management
-rw-r--r-- 1 jjoshi isfac 1754 Mar 8 18:11 words05.ps
-r-sr-xr-x 1 root bin 9176 Apr 6 2002 /usr/bin/rs
-r-sr-sr-x 1 root sys 2196 Apr 6 2002 /usr/bin/passwd

 File type: regular -, directory d, symlink l, device b/c, socket s,


fifo f/p
 Permission: r, w, x, s or S (set.id), t (sticky)
FAP

 Commands used to assign Permissions:-

chmod :- After creating files/dir we can modify permissions.

umask :- Before creating files/dir we can decide the permissions

mkdir -m :- At the time of creating dir we can assign


permissions.
FAP

 In FAP we can use two types:-


NUMERICAL MODE
SYMBOLIC MODE
. In Numerical mode we will use:-
read-r-4
write-w-2
execute-x-1
Syntax:- $chmod <perm> <file/dir>
Example:- $chmod 750 prince
FAP

 Max permissions for a dir 777


 Max permissions for a file 666
SYMBOLIC MODE:-
In this we don’t use numerical values
owner – u
group – g
others – o
+ for adding permissions
- for removing permissions
Basic general unix commands
Basic Commands(1)

ls list files and directories


o ls –a list all files and directories
o mkdir make a directory
o cd directory change to named directory
o cd change to home-directory
o cd ~ change to home-directory
o cd .. change to parent
directory
o pwd display current dir path
Basic Commands(2)

 cp file1 file2 copy file1 and call it file2


 mv file1 file2 move or rename file1 to file2
 rm file remove a file
 rmdir directory remove a directory
 cat file display a file
 more file display a file a page at a time
 who list users currently logged in

 * match any number of characters


 ? match one character
 man command read the online manual page for a command
Basic Commands(3)

 command > file redirect standard output to a file


 command >> file append standard output to a file
 command < file redirect standard input from a file
 grep 'keyword' file search a file for keywords
% grep science science.txt
 wc file count number of lines/words/characters in file
% wc -w science.txt
 sort sort data (numerically or alphabetically)
Ex:
to sort the list of object, type
% sort < biglist
and the sorted list will be output to the screen.
Process

Topics:-

Definition of process

Types of process

Commands related to process


Definition of process

Process is an instance of a running program

A process is said to be born when the program


starts execution and remains alive as long as
the program is active
Types of process

Foreground:-Commands that is running on the terminal

and the process that is assigned for them,those are known

as Foreground process
Background process:-
Some processes take a long time to run and hold up the terminal.

Backgrounding a long process has the effect that the UNIX prompt is

returned immediately, and other tasks can be carried out while the

Original process continues executing.


Commands related to process
$ ps –x

$ ps –ef => Shows all the process running in the system

How to send a process to back ground?

Ex:- sort –nr f* &

Just specify & before after the command it goes to back ground

How to kill a process?

$kill -9 <PID>
Vi Editor

 Brief History

 Vi was originally written by Bill Joy in 1976.


 The source code of 'ed' and 'em' was merged and modified by
Bill to create Vi
Vi Editor

 What is Vi ??

 Vi is a screen based editor.


 The screen of your terminal will act as a window into the file
you are editing.
 Most commands to Vi move the cursor around the file.
Vi Editor

 Starting the Vi editor

 Vi lets you create new files or edit existing files.

 Command to start Vi : vi

 Opening an existing file


vi filename

 Creating a new file


vi filename
Vi Editor

 Exiting the Vi editor

 :q :- Quit the editor


 :q! :- Quit the editor without saving the
changes to the file.
 ZZ :- The editing buffer is written to the file only if
any changes were made.
 :wq :- Same effect as ZZ
Vi Editor

 Modes of Operation

 Command Mode
Allows the entry of commands to manipulate text
Commands are usually one or two characters long

 Insert Mode
Puts anything you type on the keyboard into the current file
Vi Editor

 Vi starts in command mode by default

 Most commonly used commands to get into insert mode are a


and i

 Once in insert mode, you get out of it by hitting the Esc key
Vi Editor

 Some simple Vi Commands


 a
Enter insert mode; the characters typed will be inserted after the
current cursor position. If a count is specified, then the inserted text will
be repeated that many times
 i
Enter insert mode, the characters typed will be inserted before the
current cursor position. If a count is specified, the inserted text will be
repeated that many times
 x
Delete character under the cursor. Count specifies how many characters
to delete
Vi Editor

 Some simple Vi Commands


 r
replace one character under the cursor. Specify count to replace
that many characters.

 R
Starting from the current cursor position, replace the characters
with the one typed on the keyboard

 u
undo the last change to the file
Vi Editor

 Some simple Vi Commands


 h
Move the cursor to the left one character position

 l
Move the cursor to the right one character position

 j
Move the cursor down one line

 k
Move the cursor up one line
Vi Editor

 Cutting text
 d^
Deletes from current cursor position to the
beginning of the line
 d$
Deletes from current cursor position to the
end of the line
 dw
Deletes from current cursor position to the
end of the word
 dd
Deletes one line from current cursor position. Specify
count to delete many lines.
Vi Editor

 Yanking and Pasting


 yl
yank a single character. Specify count to yank more
characters
 yw
yank a single word. Specify count to yank more words
 yy
yank a single line. Specify count to yank more
lines
 p
paste the text that was either deleted or yanked
previously
Vi Editor

 To go to a specific line in the file


:linenumber

 String Search
 /[pattern] : search forward for the pattern
 ?[pattern] : search backward for the pattern
FILTERS

 Filter is a command or programme which takes input from the


standard input file and gives output to standard files.
Filters are Classified into two categories:-
1. SIMPLE:-
more less pg sort
cut tr uniq head tail
2. ADVANCED:-
grep sed awk
Topics :-
 Definition of shell
 Features of shell
 Activities of shell
 Major types of shell
Definition of shell

shell is a command interpreter and it is actually

interface between user and Kernel


Features of shell

> Redirect output

< Redirect input

>> Append to file

& Run process in back ground

echo Displays output

fg Foreground execution
Activities of shell

The following activities performed by shell

The shell issues the prompt and waits for you to enter a
command.

After a command is entered,the shell the command line to


recreate a simplified command line.

It the passes on the command line to the kernel for execution.

The shell waits for the command to complete and normally can’t
do any work while the command is running.

After command execution is complete ,the prompt reappears and


the shell returns to its waiting role to start the next cycle.You are
now free to enter the command

You might also like