Вы находитесь на странице: 1из 39

UNIX Operating Systems

and
Commands
Introduction
• Operating System:
 It acts as an interface between the user and
system.
 a system that manages the resources of a
computer.

• Resources:
 CPUs, Memory, I/O devices, Network etc..
Unix Architecture
 Hardware sh who
 Kernel cpp date
kernel
 Shell as ed
hardware
wc
ld
grep
nroff
Other apps
Kernel
• Three major tasks of kernel:
 Process Management
 Device Management
 File Management
Shell
 The shell acts as an interface between
the user and the kernel
 A shell is an environment in which we
can run our commands, it is also called
as a command-line interpreter
Basic Linux Commands
• File Handling
• Text Processing
• System Administration
• Process Management
• Archival
• Network
• File Systems
• Advanced Commands
Files
 Everything in unix is considered as a file,
including the physical devices like flash
device, network cards etc..
 Logical collection of files is called as file
system or Unix file system(UFS).
 A UFS(unix file system) contains both inode
and contents of the file.
 Every file as a unique number to identify, this
number is called as an inode number.
Files…..
 We can classify files into 3

1. Ordinary Files
• Text File
• Binary File

2. Device Files

3. Directory Files / Special Files


Unix File Attributes
 File Permissions
1. Read: ‘r’ If you have read permission of a file, you
can see the contents of the file.
2. Write: ‘w’ If you have write permission of a file,
you can change the file. This means you can add to
a file, or overwrite a file. You can empty a file.
3. Execute: ‘x’ If the file has execute permission,
then you can ask the operating system to run the
file as if it were a program. If it's a binary
file/program, you can execute it like any other
program.
File Attribute…
1. File type/File Permissions
2. Link
3. Owner
4. Group
5. Size
6. Last Modified Time
7. File Name
NOTE: ‘ls –l’ command lists the files in
the directory along with their attributes.
1. File type/ File permissions
 The first field of ls –l command gives the details of file type
and file permission
 This field as 10 characters.
_|_ _ _|_ _ _|_ _ _

FILE PERMISSIONS
FILE TYPE

1. FILE TYPE
the first character of the first field defines the type of
the file.

‘d’ – specifies that the file is a ‘directory file’ or a ‘special file’


‘-’ – spefies that a file is not a directory file.
 File Permissions:
unix has three categories of file permission.

_ _ _|_ _ _|_ _ _

USER (U)

GROUP (G)

OTHER (O)

rwx rwx rwx

U G O
To change file Permission
 ‘chmod’ command is used to change the permissions of
the file.
USAGE: chmod [options] mode[,mode] file1 [file2 ...]
 Unix allows the user to specify modes in two ways.
1. Absolute
2. Relative

1. Absolute:
in this we use a series of 3 octal numbers to
specify the permission of a file.
Ex: chmod 501 demo.txt, chmod 777 demo.txt
 | rwx | 111 | 7 | Read, write and execute |
 | rw- | 110 | 6 | Read, write |
 | r-x | 101 | 5 | Read, and execute |
 | r-- | 100 | 4 | Read, |
 | -wx | 011 | 3 | Write and execute |
 | -w- | 010 | 2 | Write |
 | --x | 001 | 1 | Execute |
 | --- | 000 | 0 | no permissions |
+----------------------------------------------------+
 RELATIVE
In this mode ‘=‘ , ‘+’, ‘-’ operators are used
to assign, give and remove permissions.
On the LHS specify the category u,g,o,a.
On the RHS specify the permission r,w,x.
Ex: chmod u+r demo.txt
chmod u+rw demo.txt
chmod ug+rwx demo.txt
chmod a+rwx demo.txt
chmod u+rw,g+x demo.txt
INODE
 Is a data structure and it contains following details of the
file:
1. Mode/permission (protection)
2. Owner ID
3. Group ID
4. Size of file
5. Number of hard links to the file
6. Time last accessed
7. Time last modified
8. Time inode last modified
 ‘-i’ option along with ls command is used to see the
inode number of a file.
2. Link

 A link in UNIX is a pointer to a file. Like pointers in


any programming languages, links in UNIX are
pointers pointing to a file or a directory . Creating
links is a kind of shortcuts to access a file.
 It is similar to creating multiple names of the file to
access from different directories.
 The two different types of links in UNIX are:

1. Soft Links or Symbolic Links


2. Hard links
ln Command

 This command is used to create link for a file.


USAGE: ln [option] target link_name
ex: ln file1 file2

 ‘-s’ option is used to provide a soft link.


USAGE: ln –s target_file Link_name
ex: ln –s file1 file3
Hard link
 A hard link is an additional name for an existing file
on Unix-like operating systems. Any number of hard
links can be created for a file, and thus any number of
names, can be created for any file
 The inode of the hard linked file remains same as the
original file.
 On deleting the original file, hard linked file can still be
accessed.
 By giving the hard link the link count of the file will
increase.
 Hard links do not need any extra data memory to save
since it uses links
 Can be created only on files, not on directories.
Soft/Symbolic Link
 In computing, a symbolic link (soft link) is the nickname for
any file that contains a reference to another file or directory in
the form of an absolute or relative path and that affects
pathname resolution.
 Soft link can be created for non exiting file.
 oft link has a different inode number than the original file
 On deleting the original file, soft link cannot be accessed.
 Soft link needs extra memory to store the original file name as
its data.
 Access to the file is slower due to the overhead to access file.
3. Owner

 Gives the name of the owner of the file.

 We can change the owner of the file using the


command ‘chown’.
usage: chown owner file
4.Group

 Gives the name of the group a file belong to.

 We can change the group of the file using the


command ‘chgrp’.

Usage: chgrp group file


Sources to learn commands??
(man)

Primary
1
– man(manual) pages.
man <command> shows all information about the
command ex: man ls
2

<command> --help shows the available options


for that command ex: ls --help
File Handling
commands
• mkdir – is used to create directories
Usage: mkdir [OPTION]
DIRECTORY...
ex: mkdir demo

• ls – is used to list all the files and subdirectories


of the current directory.
Usage: ls [OPTION]... [FILE]...
eg. ls, ls l, ls -l demo
File
Handling(contd...)
• pwd - print name of current working directory
Usage: pwd

• cd - change directories
Usage: cd [DIRECTORY]
eg. cd demo
Note: the Directory can be a relative
or absolute path of Directory
cp – copy files and directories
Usage: cp [OPTION]... SOURCE DEST
Examples:
1. cp file1 file2
cp a.txt b.txt
2. cp file 1 file2…. filen directory
cp file1 file2 /home/user/demo
mv – this command is used to move a file
from one directory to another
It is also used to rename a file.

Usage: mv [OPTION]... SOURCE DEST


eg. mv source.txt target_dir
mv old.txt new.txt

rm remove files or directories


Usage: rm [OPTION]... FILE... eg. rm
file1.txt , rm rf some_dir
• find – search for files in a directory
hierarchy
Usage: find [OPTION] [path] [action]
eg. 1. find file1.txt,
2. find -name file1.txt

• history – prints recently used


commands
Usage: history
TO create an USER

 ‘addusr’ command is used to create a user.

 To create a new user the user should be logged


in as a root-user.

usage: addusr user_name


TO switch User

 ‘su’ command is used to switch from one user to another

Usage: su User_name

 It asks for the password to login.

 ‘exit’ command is used to come out of the logged in user.


How to login as root ?

 ‘sudo su’ command is used to login as a root-user.

usage: sudo su
 It will ask for the password.

 The user should have permission to login as the root-user.

 All the users having root permissions are stored in a file


‘visudo’.
To remove User

 ‘delusr’ command is used to delete a user.

usage: delusr user-name

 You should be log-in as a root-user to delete an


user.
Basic Regular Expression

 The BRE a{1,2} matches a{1,2} literally,


while a\{1,2\} matches a or aa.
 As {,},+,?,(,),.. Are treated as a normal symbols
and we have to use a ‘\’ to give special
meaning to them.
 As ?,+,.. Are not supported by POSIX (Portable
Operating system Interface for Unix) BRE.
 We use grep command for BRE.
Extended Regular Expression

 The quantifiers ?, +, {n}, {n,m} and {n,} repea


t the preceding token zero or once, once or
more, n times, between n and m times, and n or
more times, respectively.
 These above quantifiers are supported by
POSIX ERE and we use egrep or grep –E
command.
Metacharacters
 ^ (Caret)=match expression at the start of a line, as in
^A.
 $ (Dollar)=match expression at the end of a line, as in
A$.
 \ (Back Slash)=turn off the special meaning of the next
character, as in \^.
 [ ] (Brackets)=match any one of the enclosed characters,
as in [aeiou]. Use Hyphen "-" for a range, as in [0-9].
 [^ ]=match any one character except those enclosed in
[ ], as in [^0-9].
 . (Period)=match a single character of any
value, except end of line.
 * (Asterisk)=match zero or more of the
preceding character or expression.
 \{x,y\}=match x to y occurrences of the
preceding.
 \{x\}=match exactly x occurrences of the
preceding.
 \{x,\}=match x or more occurrences of
the preceding.
Searching for a pattern in UINIX
 Unix has a special family of commands for handling
search requirements.
 The main member of this family is the grep
command.

GREP: (Global Regular Expression Parser)


 It scans its input for a pattern and displays lines
containing the pattern.
Examples
 grep '^From: ' demo.txt
 grep '[a-zA-Z]'{any line with at least one
letter}
 grep '[^a-zA-Z0-9]{anything not a letter or
number}
 grep '[0-9]\{3\}-[0-9]\{4\}'{999-9999, like
phone numbers}
 grep '^.$'{lines with exactly one character}
 grep '"smug"'{'smug' within double
quotes}
 grep '"*smug"*'{'smug', with or without
quotes}
 grep '^\.'{any line that starts with a
Period "."}
 grep '^\.[a-z][a-z]'{line start with "." and
2 letters}

Вам также может понравиться