What is Linux / Unix?
Linux / Unix is an operating system
A computer can be divided into 3 main functional layers:
o Applications: the highest functional layer. This layer includes software
programs that run for a specific purpose.
Example: this power point presentation, an email tool, a web
browser
o Operating System: the middle functional layer. The OS controls the
hardware and supports the applications running above it.
Example of popular OS: Linux, Unix, Windows, Mac OS
o Hardware: physical devices that make up the computer
Example hardware include: keyboard, monitor, hard disk
The main tasks of an operating system:
o Interacts with the user
o Manages how applications run
o Handles data storage
o Controls hardware peripherals
Main components of Linux
The kernel, the file system and utilities, and the shell are the main components of
the Linux OS
Kernel: the core of the OS
o only one kernel can run per system
o gets loaded into in the main memory during power up
o manages resources so that multitasking can take place
File system: for data storage
o data are organized into files
o files are organized into directory structures
Shell: how the user interfaces with the OS
o the shell interprets what the user types on the keyboard so it can be run
by the system
o there can be more than one shell running at a time
o the shell also coordinates multiple commands in a file and acts as a
programming language
Utilities: tools to help a user do work on the system
o also called commands
o there are many utilities, each utility is supposed to do one task and do it
well
o they are programs that are run by the user to do specific tasks, such as
copy a file, send email, communicate with another user, check system
resources
Files in Linux
The Linux philosophy is “everything is a file”
The term “file” not only means a file of data (the typical meaning), it can also
refer to an input device (such as a scanner), an output device (such as the
monitor), a hardware component (such as the hard drive), or a process (such as
the shell that you work with)
Linux divides files into 7 different types:
1. regular file: a file of data. Data can be text (human readable) or binary (machine
readable)
2. directory: a file that can “contain” other files (equivalent to folders in Windows)
3. character special file: IO device that processes one character at a time, such as a
printer
4. block special file: IO device that processes a block of characters at a time, such as
a hard disk
5. symbolic link: a file that holds the address of another file
Directories
Directories help you organize files by providing ways to group similar files
together
In Linux, files are grouped into directories (which themselves are files), and
directories are grouped under other directories, in “tree” form called a directory
tree or directory hierarchy
Each file in the hierarchy is called a node
The top node is called root
Except for root, each node can have
o 1 node above it, called its parent node
o 0 or more nodes below it, called its child nodes
A parent directory can have directory child nodes, called subdirectories
Directory Tree Diagram
In this sample directory tree:
o There are 9 files
o Each file is a node
o One root node
o DirectoryC is the child node of root, and is the parent node of a regular
file
o DirectoryD is the subdirectory of DirectoryB
Root Directory
There is only one root node for every directory hierarchy
The actual name you type in for root is /
Some common subdirectories under root are
o bin: (for binary) contains general Linux utilities, in binary format
o sbin: (for system binary) contains utilities for system administration
o etc: contains configuration files for the system
o usr: (for user) contains applications for the users
o lib: (for library) shared libraries
o var: (for variable) contains data files that change when the system is
running
o tmp: (for temporary) contains temporary files
o dev: (for device) contains files for hardware devices
o mnt: (for mount) contains mount points for storage devices
o home: contains home directories of users
Home Directory
Every user is assigned a unique directory to store his / her files. This directory is
called the home directory
It has the same name as your log in name
You cannot change your home directory name or its location in the system
When you first log in the system, you are at the home directory
Often you will not be at the home directory. Instead you will move to a different
directory, depending on the task you are doing. The directory where you are
working is called the current directory or the working directory
Directory Path
Since the Linux directory tree is a huge hierarchy of files, it is important to know
the path to a file in order to access it
The path of a file is the location of the file in the directory tree
A path is formed by
o listing node names that are connected to each other in the tree structure
o separate node names with / (not the same as root)
2 types of paths: absolute path and relative path
Absolute path
o Shows the location of a file, starting from root /
o The absolute path of a file will not change unless the file is moved to
another place in the hierarchy
Relative path
o Shows the location of a file, starting from your current directory
o Since the relative path is relative to your current directory, the relative path
of a file will change if you change your current directory
Assume your home directory is ImaStudent:
o The absolute path of your home directory is /home/student/ImaStudent
o From the root directory, you can use a relative path to the distribution
directory by traversing down to home, then down to distribution. The
relative path from root is: home/distribution
All commands that accept a filename as an argument will accept a filename
with a path
When a filename with no path is the argument, the file needs to be in the current
directory or the shell will not be able to find it
When a filename with a path is the argument, the file needs wherever the path
indicates
Examples:
o ls fileA will list fileA in the current directory
o ls /labs/fileA will list fileA that is in the labs directory which is under root
o cp fileA fileB will make a copy of fileA in the current directory and store
it as fileB in the current directory
o cp fileA /files/fileB will make a copy of fileA in the current directory and
store it as fileB in the files directory, which is under root directory
pwd and cd
pwd: (for print working directory) shows the absolute path of where you are in
the directory tree
cd: (for change directory) moves you to another directory
Common format: cd path
o where path can be:
Nothing: moves you to your home directory
Absolute path: make sure it starts with / (root)
Relative path: make sure the first node in the path is connected to
your current directory
To go to a subdirectory: type the name of the subdirectory
To go to a parent directory: type ..
To go to the current directory: type .
Path with special symbols:
~ your home directory
~userID home directory of user userID
ls and Directories
Recall:
o ls will list filenames in the current directory
o ls filename will echo filename if file exists and is a regular file
If filename is the name of a directory, then ls will list filenames under that
directory
To see the file type of a file:
o Use the long listing: ls –l
The first character in the mode column will tell you the file type:
d for directory, l for link, - for regular file
o Use: ls –F
The filenames will be listed with an additional symbol at the end:
/ for directory, @ for link, * for executable regular file, nothing
for text file (which is also a regular file)
mkdir and rmdir
mkdir: (for make directory) creates a new directory in the current directory, or
under a different directory if a path is given
o Common format: mkdir directory_name
o When first created, the directory is an empty directory (no files in it, except
2 hidden files . and ..)
rmdir: (for remove directory) deletes an empty directory
o If the directory is not empty, you must delete all files in it first
o Common format: rmdir directory_name
o Alternatively, to remove a non-empty directory, use rm –r
directory_name where -r is for recursive
o Removes the directory and recursively go down all its subdirectories and
remove all the files under them
o Caution: this can remove a large number of files, make sure you don’t run
this command ‘by accident’
cp and Directories
Recall that cp will copy the source file to the destination file. Now we discuss all
the combinations of cp with regular files and with directories
o File below means regular file, Dir below means directory
Copying a source regular file
o cp existingFile nonExistingFile new nonExistingFile is created
o cp existingFile1 existingFile2 existingFile2 is overwritten
o cp existingFile nonExistingDir new regular file created with the
name of nonExistingDir
o cp existingFile existingDir new file called existingFile created
under existingDir
o Copying a source directory (need to use –r option, for recursively copy, all
files and subdirectories will also get copied)
o cp –r existingDir nonExistingDir new nonExistingDir is created
o cp –r existingDir1 existingDir2 existingDir1 is copied and put
under existingDir2
o cp –r existingDir nonExistingFile new directory called nonExistingFile
is created
o cp –r existingDir existingFile not possible
mv and Directories
Recall that mv will move the source file to the destination file, and the source file
will no longer exist. Now we discuss all the combinations of mv with regular files
and with directories
o File below means regular file, Dir below means directory
Move a source regular file
o mv existingFile nonExistingFile new nonExistingFile is created
o mv existingFile1 existingFile2 existingFile2 is overwritten
o mv existingFile nonExistingDir new regular file created with the
name of nonExistingDir
o mv existingFile existingDir new file called existingFile created
under existingDirectory
o Moving a source directory (don’t need option, all files and subdirectories
will move)
o mv existingDir nonExistingDir new nonExistingDir is created
o mv existingDir1 existingDir2 existingDir1 moves under existingDir2
o mv existingDir nonExistingFile new directory called nonExistingFile
is created
o mv existingDir existingFile not possible
File Permissions
Linux makes it easy for users to share data, but only if the owner of the file allows
his/her file to be shared.
The file owner is the user who created the file. The owner can set permissions for
the file to allow or deny access to the file
The types of permissions that can be set: read, write, execute
The file owner always has permission to his / her own files by default
If the file owner changes a file’s permission so that there is no access to the file,
s/he can always change that file’s permission so that there is access again
Note: even if the owner of a file gives no access to the file, system administrators
with root or superuser login ID still has full access to the file (read, write, and
execute permission)
Types of Permissions
3 types of permission: r for read, w for write, and x for execute.
For regular files:
o r : the file can be opened for reading, copying, and linked to
o w : the file can be modified
o x : the file can be executed or run
For directories:
o r : the directory can be “read,” which means the filenames in the directory
can be listed
o w : the directory can be modified, which means files can be added to or
deleted from the directory
o x : the files in the directory can be accessed
For links:
o all 3 permissions are always set, and the owner cannot change this
permission
o This is not a problem since no user can access a link, only the system
accesses a link
Levels of Permissions
3 levels of permission:
o u : (for user) permission for the owner of the file
o g: (for group) permission for the group in which the file owner belongs.
Each user belongs to at least 1 group, as set by system admin. Your
group choice is most likely dependent on your job in your organization.
o o: (for other) permission for all users who are not the owner or who don’t
belong in the same group as the owner
Each level of permission contains all 3 types of access (r,w,x)
This means that for each file you own, you can set r,w,x access for yourself (as
owner), r,w,x access for users in your group, and r,w,x access for all other users
The 3 types of permission at each of the 3 levels make up the 9 characters of the
mode of the file
Mode
The mode of a file shows its access permission
The mode is made of 9 characters, representing the 3 types (r,w,x) access for
each of the 3 levels of access (u,g,o)
The mode of a file can be found in the first column of the long listing of the file
For example: -rwxr-xr-x
o The first character is the file type:
o d (directory), l (link), - (regular file)
o The next 9 characters are the permissions: the first 3 for user, the middle
3 for group, the last 3 for other. The 3 characters always go in order of
read, then write, then execute.
o If a permission character shows up as r, w, or x, then the corresponding
permission type is set. If the permission character shows up as – then the
corresponding permission is not set.
o In the example above: the owner has all 3 r,w,x permission, the group and
others can only read or execute the file
To See the File Permission
Use ls to see the permission of a file
Regular file: ls –l regFileName
o The mode starts with a – (for regular file), the next 9 characters show the
read, write, execute permissions for user, then group, then others
Directory: ls –ld directoryName
o The d option tells ls to list at the directory level, rather than list the files
that are under the directory
o The mode starts with a d (for directory), the next 9 characters show the
read, write, execute permissions for user, then group, then others
Link: ls –l linkName
o All permissions for links are always on
File Access Rights
Whether you can access a file, and what type of access you have, depend on the
permission of the file and the permission of all parent directories to which the file
belongs.
Example1: fileA has rwxrwxrwx permission, but it belongs in directory dirA,
which has rwxr--r-- permission.
o If you are not the owner of fileA, you will not be able to access fileA at all
because dirA does not give you access to any file under it (no x
permission)
Example 2: fileB has rw-rw---- permission and belongs in directory dirB, which
has rwx--x--x permission.
o If you are in the same group as the owner of fileB:
you cannot do a listing of dirB and see fileB (no r at dirB)
you can modify fileB (x at dirB and rw at fileB)
you cannot delete fileB (no w at dirB)
o If you are not in the same group as the owner of fileB:
you have no access to fileB (no permission at fileB, and no r at dirB
in order to see a listing of fileB)
chmod - Symbolic Format
A file permission can be changed only by the owner of the file or by system
admin with superuser (or root) privilege
chmod: (for change mode) changes the permission of a file
2 ways to use chmod: symbolic and absolute
Symbolic format for chmod: chmod who operator permission filename
o where
who: u (user), g (group), o (other), a (all)
a means u and g and o
operator: + (add), - (remove), = (set)
For add and remove, the existing permission is modified by
the specified add or remove
For set, the existing permission is overwritten by the
specified permission
permission: r (read), w (write), x (execute)
filename: can contain a path and/or can be a file list
o The who, operator, and permission arguments have no space in between
them on the command line
Example 1: chmod go+rx filename
o Add read and execute permission for group and others
Example 2: chmod u=rw filename
o Owner changes to read and write permission, group and other
permissions remain the same
oTo change multiple types of permission and multiple levels of permission,
you can group the different types together for one level, or you can group
different levels together for one type
o With multiple groupings, separate them by comma, but there is no space
in between all the groupings
Example 3: chmod ug+x,og-r filename
o Add execute permission for user and group, remove read permission for
others and group
Example 4: chmod u+x,o-r,g+rx filename
o Add execute permission for user, remove read permission for others, and
add read and execute permission for group
Special cases
To apply a permission to all levels (owner, group, others), use a for the who field
o Example: chmod a-x filename
Remove execute permission for all levels
To remove all permissions for one level, set the permission to nothing
o Example : chmod o= filename
Remove all permission for others
Since regular files do not have execute permission by default, add execute
permission for all levels: chmod +x filename
chmod – Absolute Format
Absolute format for chmod: chmod octal_number filename
o filename: can contain a path and/or be a file list
o octal_number: a 3 digit number, one for each level of permission
where: 1st digit represents the user (owner) level
2nd digit represents the group level
3rd digit represents the other level
To calculate each digit of the octal number, which sets the permission of each
level:
o r permission has a value of 4 (or 22)
o w permission has a value of 2 (or 21)
o x permission has a value of 1 (or 20)
o If a permission is set, multiply the permission value with 1
o If a permission is not set, multiply the permission value with 0
o Add all 3 permission products together to get a number (or digit) between
0 and 7
Example: To get a mode of rwxr-xr--
o Owner level: rwx which is calculated as:
1*4 + 1*2 + 1*1 = 4+2+1 = 7
o Group level: r-x which is calculated as:
1*4 + 0*2 + 1*1 = 4+0+1 = 5
o Other level: r-- which is calculated as:
1*4 + 0*2 + 0*1 = 4+0+0 = 4
Therefore: chmod 754 filename