UNIT2 Student
UNIT2 Student
UNIT2 Student
UNIT 2
✓ Looping
Outline
• Overview
• Logging in & out
• I node and File Structure
• File System Structure and Features
• Booting Sequence & init process
• File Access Permissions
Files and processes
🞂 Everything in UNIX is either a file or a process.
🞂 A process is an executing program identified by a unique PID (process identifier).
Examples of files:-
🞂 a document (report, essay etc.)
🞂 the text of a program written in some high-level programming language
🞂 Instructions comprehensible directly to the machine and incomprehensible to a casual user,
for example, a collection of binary digits (an executable or binary file);
🞂 a directory, containing information about its contents, which may be a mixture of other
directories (subdirectories) and ordinary files.
File Naming Rules
Some Rules:-
➢ Filename can consist of upto 255 characters.
➢ Files may or may not have extensions.
➢ Practically, it can consist of any ASCII characters except the slash(/).
➢ Unix is case-sensitive.
🞂 Some suggestions:-
➢ Never use hyphen at the beginning of a filenames.
➢ Do not use special characters like $,-,?, *, & etc.
➢ You should use only the following characters when framing filename:
1. Alphabets and numerals.
2. The period(.) the hyphen(-) and underscore(_).
File Types
🞂 Ordinary files (files):-
Files are containers for data. This data can be anything, including the text of a report,
an image (picture) of a house, an executable program like a word processor, or any arbitrary data.
All data files, files containing programs, compiled and executable program files are considered as
ordinary files. these files are control by user.
🞂 Directory files (directories):-
Each directory contains a number of files. A directory can contain other directories,
be contained in another directory or both.
🞂 Special files:-
A special file is much like an ordinary file, and shares the same basic interface.
However, special files are not stored in the file system because they represent input/output
devices.
File System Structure
File System Structure
�The Unix file system is a hierarchical structure that allows users to store information by name.
At the top of the hierarchy is the root directory, which always has the name /.
�The location of a file in the file system is called its path
➢ All paths start from the root directory, so the path begins with /
➢ We need to go to the home subdirectory, and the path becomes /home
➢ In the home directory, we go into the bca subdirectory. The path is now
➢ /home/bca
➢ In the bca directory, we go into the Ty subdirectory. The path is now
➢ /home/bca/Ty
➢ The file quiz1.txt is in theTy directory. Appending the filename to the path,
➢ the path becomes /home/bca/Ty/quiz1.txt
File System Structure
🞂 These special paths and their shorthand names are listed below:
🞂 Special files:-
🞂 / → A single slash / specifies the root directory.
~ → tilde ~ specifies the current user's home directory.
~user → tilde ~ followed by a user-id specifies the home directory of the
given user. For example, at UBC, ~a1a1 is the home directory of the user
who has the user-id a1a1.
. → This single dot . specifies the current working directory, that is, the
directory that the user is currently in.
.. → A pair of dots .. refers to the parent of the current
working directory.
Directory Structure
➢ The directory structure is hierarchical with the root directory (/)
➢ /: The slash / character alone denotes the root of the file system tree.
➢ /bin and usr/bin:These are the directories where all the commonly used unix commands
(binaries) are found. PATH variable always shows these directories in its list.
➢ /dev: Stands for “devices”. Contains file representations of peripheral devices and
pseudo-devices.
➢ /etc: Contains system configuration files and system databases. Your login name and
password are stored in files /etc/passwd and /etc/shadow.
➢ /home: Contains the home directories for the users.
➢ /var : The variable part of the file system. Contain all your print jobs and your outgoing as
well as incoming mail.
➢ /lib: Contains system libraries, and some critical files such as kernel modules or device
drivers.
➢ /root: The home directory for the superuser “root” – that is, the system administrator.
Directory Structure
➢ /tmp: A place for temporary files. Many systems clear this directory upon start
up; it might have tmpfs mounted atop it, in which case its contents do not survive
a reboot, or it might be explicitly cleared by a start up script at boot time.
➢ /usr: Originally the directory holding user home directories, its use has changed.
It now holds executables, libraries, and shared resources that are not system
critical, like the X Window System, KDE, Perl, etc. However, on some Unix
systems, some user accounts may still have a home directory that is a direct
subdirectory of /usr by a person).
🞂 Relative path: Relative path is defined as the path related to the present
working directly(pwd). It starts at your current directory and never starts with
a /. (bca/test1.txt)
Process
🞂 A process is an executing command or program. The process is what actually
performs the work of the command or program.
🞂 Foreground Process
E.g: If you want to list all the files in your current directory, you can use the following
command ,
$ ls a*doc
It would display all the files, which start with a and end with .doc
a1.doc aa1.doc abc.doc a3.doc
🞂 The process runs in the foreground, the output is directed to the screen. While a
program is running in the foreground, no other commands can be run because the
prompt would not be available until the program finishes processing and comes
out.
Process
Background Process:-
🞂 The simplest way to start a background process is to add an ampersand (&) at the
end of the command
$ls ch*.doc &
In background mode the parent process continues to run using the keyboard and monitor as
before, while the child process runs asynchronously.
Job:-
A job consists of one or more processes working to perform a specific task. Each
time you run a command or program on the system, you are starting a job.
Process
Process
🞂 Process:-
🞂 A process is simply an instance of a running program.
🞂 A process is said to be born when the program starts execution and remains
a live till the process is active.
🞂 After the execution the process is said to be died.
🞂 A process is also has a name (name of the program (cmd) is a process name)
🞂 The kernel is responsible for the management of a process.
🞂 Some attributes of every process are maintained by the kernel in memory in a
separate structure called the process table.
Process
🞂 Two important attribute of a process are,
1) The process id (PID)- each process is uniquely identified by a unique integer called the
PID . That is allotted by the kernel when the process is born. PID is useful for kill the
process.
2) The parent process id(PPID)- the pid of the parent is also available as a process
attribute. when several processes have the same PPID.
(1023)
Process
Child Process
🞂 A child process is a process created by a parent process in operating system using
a fork() system call. A child process may also be called a subprocess or a subtask.
🞂 A child process is created as its parent process’s copy and inherits most of its
attributes. If a child process has no parent process, it was created directly by the
kernel.
Process Commands
🞂 ps : to know the process attributes
$ps
PID TTY(Terminal) TIME CMP(Process Name)
Where
UID- User ID for every process
PID- Process ID
PPID- Parent Process ID
C- CPU utilization of process
STIME- time of process started
Process
Process Commands
🞂 ps -u: displaying a process of user only. To know activity of any user.
$ps –u bca
(PID TTY TIME CMD )
ps –a: list the process of all users but doesn’t display the system process. (-a all)
$ps –a
(PID TTY TIME CMD )
ps –f -h: detailed listing which also show the parent of every process and child process
$ps –f -h
(PID TTY TIME CMD )
or $ps aux |more
ps -ef | grep httpd
Process
Process Commands
🞂 ps –e or ps –A: displaying all processes including system and user process both , apart
form the user processes , a number of system processes keep running all the time.
$ps –e
Operator Description
+ Adds the specified modes to the specified classes
- Removes the specified modes from the specified classes
Permission Description
r Permission to read the file
w Permission to write (or delete) the file
Permission to execute the file, or, in the case of a directory,
x
search it
chmod(change mode) Command
• Each write, read, and execute permissions have following number value:
r (read) 4 u Owner/user
w (write) 2 g group
x (execute) 1 o others
no permissions 0 a all
chmod Command Example
chmod Command Example
chown(change owner) Command
• The chown command changes ownership of files and directories in a Linux filesystem.
🞂 Syntax :
chown [OPTIONS] USER[:GROUP] FILE(s)
cd
Sudo chown root t1.txt
chown Command Example
How the shell is created??
Which process is created by getty process?
86
Process states and Zombies.
519
$_
Shell return PID of the invoked command and the prompt is return so the
shell is ready to accept the another command. but the previous command
has not been terminated yet.
&: no logging out
• When user logs out , all job stop their working because their shell is killed.
• When the parent is killed, its children are also normally killed.
• But nohup command , prefix with any command , it permits execution of the
process even after the user has logged out. But you must use & sign with it.
So you can safely logout of the system without aborting the command
nice (job execution with low priority)
• In unix OS all processes are usually executed with equal priority if there is a high-priority
process or low priority process.
• But nice command is used with & operator to reduce the priority of jobs. More important jobs
can have greater access to the system resource.
• Nice command reduce priority of any command.
• To run a job with a low priority, the command name should be prefix with nice,
If you don’t remember PID then use ps command then use kill command .
$ps
PID TTY TIME CMD
If all these process have the same parent, you may simply kill the parent
then all its children gets kill.
Killing the last background job ($!)
• The system variable $! Store the PID of the last background job.
• So you can kill the last background job without using ps to find out its PID.
• To know the PID of current shell $$
• To show last backgroung job use & at the end of any command.
e.g $ sort –o emp.lst &
345
$ kill $! Kill the sort command
File systems and Inodes
• In unix files are organized in a hierarchical way.
• Unix file hierarchy as a “file system” i.e all the files and directory are held together in a
big superstructure i.e root.
• Every file system has a directory structure headed by root. Every file is associated with a
table that contain all the information about a file – except its name and content.
• That table is called inode(index node) and it is access by inode number.
• It’s a directory that stores the inode number along with the file name.
• When you use command with a file name as an argument , the kernel first locates the
inode number of the file from directory then read the inode to fetch data relevant to the file.
• For showing all inode information use ls –i(inode)
inode contains the following attributes of a file:
• Inode is a data structure that contains meta data abouts a file and they change when file is
accessed and modified.
• File type(regular , directory, device)
• File permission (rwx, rwx ,rwx for UGO)
• Number of links
• UID of owner
• GID of group owner
• File size in bytes
• Date and Time of last modification
• Date and time of last access
• Date and time of last change of the inode
• An array of pointer that keep track of all disk blocks used by the file.
For e.g
$ ls –il emp.lst
9059 -rw-r- - r- - 1 bca rofel 51813 sep 11 10:15 emp
INODE
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. Links allow more than one file name to
refer to the same file.
• A directory entry can have an Inode pointing to another file. There are two types of links:
1. Hard Links
2. Soft Link or Symbolic links
These links behave differently when the source of the link (what is being linked to) is moved or
removed.
HARD LINK
• Hard links always refer to the source, even if moved or removed. When a hard link is made,
then the i-numbers of two different directory file entries point to the same inode.
• Each hard linked file is assigned the same Inode value as the original, therefore they
reference the same physical file location. Hard links more flexible and remain linked even if
the original or linked files are moved throughout the file system.
• ls -l command shows all the links in the link column .
• Links have actual file contents
• Removing any link, just reduces the link count, but doesn’t affect other links.
• We cannot create a hard link for a directory to avoid recursive loops.
• If original file is removed then the link will still show the content of the file.
Symbolic link
• Symbolic or a soft link is a special type of file containing links or references to another file or
directory in the form of a path. The path may be relative or absolute. Symbolic link are not
updated.
• A soft link is similar to the file shortcut feature which is used in Windows Operating systems.
Each soft linked file contains a separate Inode value that points to the original file. As similar
to hard links, any changes to the data in either file is reflected in the other. Soft links can be
linked across different file systems, although if the original file is deleted or moved, the soft
linked file will not work correctly (called hanging link).
• Soft Link contains the path for original file and not the contents.
• Removing soft link doesn’t affect anything but removing original file, the link becomes
“dangling” link which points to nonexistent file.
• A soft link can link to a directory.
• Link can be confirm by using ls –li (inode --- same inode number for link file)
ln (creating a link(hard and soft))
umask is a number which defines the default permissions which are not to be given on a file. A
umask of 022 means not to give the write permission to the group(022) and others(022) by
default. "user file-creation mode mask”
Following are default permission for all files and directories.
• rw- rw- rw- (octal 666) for regular file
• rwx rwx rwx (octal 777) for directories
• Defaut umask value is 022
The umask specifies the permissions you do not want given by default to newly created files and
directories.
umask works by doing a bitwise AND with the bitwise complement of the umask. Bits that are set
in the umask correspond to permissions that are not automatically assigned to newly created
files.
e.g. we create a file say "file1". The permissions given for this file will be the result
coming from the substraction of the umask from the default value ,
Default : 666
umask : 022
---------------
Result : 644
644 is the permission to be given on the file "file1".
644 means read and write for the User(644), read only for the group(644) and
other(644)
umask
umask –S // retrive permission
umask u+rw,g+rx,o-r // change permission
umask //change umask value
exit //logout
Example
Unix three time stamps
ls –l --- Time of last file modification
ls –lu --- Time of last access
ls –lc --- Time of last inode modification
ls –lt --- listing in order of their modification time
ls –lut --- listing in order of their access time
touch : (changing the time stamp)
• It is used to create, change and modify timestamps of a file. Basically, there are two different
commands to create a file ,
• cat : It is used to create the file with content.
• touch: It is used to create a file without any content.
The file created using touch command is empty. This command can be used when the
use doesn’t have data to store at the time of file creation.
touch command can be used to create the multiple numbers of files at the same time but
these files would be empty while creation.
touch command creates new file if it does’t exist , otherwise it just change the time stamp
of the file.