Professional Documents
Culture Documents
• Prefix Description
– - Regular file, such as an ASCII text file, binary executable, or hard link.
– b Block special file. Block input/output device file such as a physical hard
drive.
– c Character special file. Raw input/output device file such as a physical hard
drive
– d Directory file that contains a listing of other files and directories.
– l Symbolic link file. Links on any regular file.
– p pipe file.
– s socket file
• Rest of the first column represents different access mode that is permission
associated with a file or directory.
• The permissions are broken into groups of threes, and each position in the group
denotes a specific permission, in this order:
– read (r), write (w), execute (x):
• The first three characters represent the permissions for the file's owner.
– For example -rwxr-xr-- represents that owner has read (r), write (w) and execute (x)
permission.
• The second group of three characters consists of the permissions for the group to
which the file belongs.
– For example -rwxr-xr-- represents that group has read (r) and execute (x) permission but no
write permission.
• The last group of three characters represents the permissions for everyone else.
– For example -rwxr-xr-- represents that other world has read (r) only permission.
File/Directory Access Modes
File Access Modes:
• The basic building blocks of Unix permissions are the read, write, and
execute permissions:
• 1. Read:
– Grants the capability to read i.e. view the contents of the file.
• 2. Write:
– Grants the capability to modify, or remove the content of the file.
• 3. Execute:
– User with execute permissions can run a file as a program.
Directory Access Modes:
• Directory access modes are listed and organized in the same manner as
any other file. There are a few differences that need to be mentioned:
1. Read:
– Read access to a directory means that the user can read the contents.
The user can look at the filenames inside the directory.
2. Write:
– Write access means that the user can add or delete files to the
contents of the directory.
3. Execute:
– Executing a directory doesn't really make a lot of sense so think of this
as a traverse permission.
– For example: A user must have execute access to the bin directory in
order to execute ls or cd command.
Changing Permission
Changing Permissions:
• To change file or directory permissions, you use the chmod (change mode)
command.
• There are two ways to use chmod:
– symbolic mode
– absolute mode.
Using chmod in Symbolic Mode:
• The easiest way for a beginner to modify file or directory permissions is to use the
symbolic mode. With symbolic permissions you can add, delete, or specify the
permission set you want by using the operator.
Changing Permission Options
• There is no entry for file name in the Inode, rather the file
name is kept as a separate entry parallel to Inode number.
• The reason for separating out file name from the other
information related to same file is for maintaining hard-links
to files.
• All the other information is separated out from the file name
then we can have various file names which point to same
Inode.
When are Inodes created?
• Space for Inodes is allocated when the operating system or a
new file system is installed and when it does its initial
structuring.
• So this way we can see that in a file system, maximum number
of Inodes and hence maximum number of files are set.
• We typically count on having 1 inode per 2 to 8 kilobytes of
storage.
• So the above concept brings up another interesting fact. A file
system can run out of space in two ways :
– No space for adding new data is left
– All the Inodes are consumed.
• The first one is pretty obvious as we can have files with large
sizes.
• The second one: a case arises where we have free storage
space but still we cannot add any new data in file system
because all the Inodes are consumed.
• This may happen in a case where file system contains very
large number of very small sized files.
When Inode is assigned to file
• When a file is created inside a directory then the file-name
and Inode number are assigned to file.
• These two entries are associated with every file in a directory.
Commands to access Inode numbers
• Following are some commands to access the Inode numbers for files :
1) ls -i Command
is used to print the Inode number for each file.
2) df -i Command
df -i command displays the inode information of the file system.
3) stat Command
Stat command is used to display file statistics that also displays inode
number of a file
Directory
• A directory is a file whose sole job is to store file
names and related information.
• All files, whether ordinary, special, or directory, are
contained in directories.
• UNIX uses a hierarchical structure for organizing
files and directories. This structure is often referred
to as a directory tree .
• The tree has a single root node, the slash character
( /), and all other directories are contained below it.
Directory
• A directory is a simple concept: it contains a list of filenames, each of
which maps to an inode number.
• Each name is called a directory entry, and each name to-inode mapping
is called a link.
• Directories can also contain other directories. A subdirectory is a
directory inside of another directory.
• Every directory contains two special directories, . and .. (called dot and
dot-dot).
• The dot directory is a reference to the directory itself (current directory).
• The dot-dot directory is a reference to the directory’s parent directory.
• For example, /home/kidd/gold/.. is the same directory as /home/kidd.
• The root directory’s dot and dot-dot directories point to itself—that
is, /, /., and /.. are all the same directory.
Directory Hierarchy
• A file system is a logical collection of files on a partition .
• Unix uses a hierarchical file system structure, much like an upside-down
tree, with root(/) at the base of the file system and all other directories
spreading from there.
• A Unix file system is a collection of files and directories that has the
following properties:
– It has a root directory (/) that contains other files and directories.
– Each file or directory is uniquely identified by its name, the directory in which it
resides, and a unique identifier, typically called an inode.
– By convention, the root directory has an inode number of 2 and the lost+found
directory has an inode number of 3. Inode numbers 0 and 1 are not used. File
inode numbers can be seen by specifying the -i option to ls command.
– It is self contained. There are no dependencies between one file system and any
other.
/
File1 dir1
Directory Discription
/ This is the root directory which should contain only the directories
needed at the top level of the file structure.
/bin This is where the executable files are located. They are available to all
user.
/lib Contains shared library files and sometimes other kernel-related files.
1. Finding directories
Find –type d
2. Finding socket files
find –type s
3. Finding regular files
find –type f
4. Finding hidden directories
find –type d –name “.*”
5. Finding hidden files
find –type f –name “.*”
How to find files based on the file size?
1. Finding files whose size is exactly 10M
find –size 10M
2. Finding files larger than 10M size
find . -size +10M
3. Finding files smaller than 10M size
find . -size -10M
4. How to find the files which are modified after the modification of a give file.
find -newer “abc.txt“
5. Display the files which are accessed after the modification of a give file.
find -anewer "sum.java“
6. How to find the files based on the file permissions?
find . -perm 777
7. Find the files which are modified within 30 minutes.
find . -mmin -30
8. Find the files which are accesed within 1 day.
find . -atime -1
Links
• Each name-to-inode mapping in a directory is called a link.
• That a link is essentially just a name in a list (a directory) that points at an
inode.
• In Linux operating system, file linking is a process by which a single file is
referred to by multiple names from various locations.
• That is, a single inode (and thus a single file) could be referenced from,
say, both /etc/customs and /var/run/ledger.
• Because links map to inodes, and inode numbers are specific to a
particular file system, /etc/customs and /var/run/ledger must both reside
on the same file system.
• Within a single file system, there can be a large number of links to any
given file.
Types of Links
• There are two different types of linking that you can create
– Symbolic link
– Hard link
• Soft Link also called as symbolic links or symlinks
– This method creates a special file which does not have any user
content but has information (ie. Path and name) about the file name it
links to, which in turn knows about the content.
– One way to create this kind of mapping is to treat it like a shortcut.
– The links has an inode for itself but links to the original file name
object to access the user content.
– So deleting/ moving the original file will break the link, it cannot
access the content. But the link object itself will exist as a (broken) file
as it has its own inode entry.
Pros and Cons of Soft Link
• Pros
– You can create soft link to almost all files system objects, including
files, directories, devices etc.
– Links can be created across file systems and even across disks or
mounts.
– Easier to find what the file actually links to or what and where the
original file was.
– Ability to have separate or different metadata such as file permission
for symbolic link than the original.
• Cons
– Deleting/ moving the files causes the symlink to break
– Some software do not work with soft link and require an actual file.
Hard Link
• Another way to do this mapping or linking is to create a brand
new file object, but associate it with the existing content and
metadata by providing it with the same inode number as the
original file name.
• In this method, the hard link itself does not know about or the
original file name and behaves as if none exist.
• It is the file system that is responsible for managing the file
names, file counts and other related information.
Pros and Cons of Hard Link
• Pros
– Deleting the original file does not cause the link to break.
– No separate inode is required as it is shared between the link and the
file.
– This is much more efficient than maintaining a different inode for the
link object.
• Cons
– Cannot link across file system. This is because each file system has its
own inode table, so sharing inodes using numbers will not work as
these ids might not be the same across inode table.
– Cannot links directories easily again as the inode table entries are to
be shared.
– It is much harder to keep track of the hard links unless you want to
search using the inode number.
Creating a soft and hard Link
• Both the hard and soft links are created using the ln
command.
• By default the ln command creates a hard link.
• In order to create a symlink you have to specify the –
symbolic(-s) command line option.
• For example
$ ln –s <filename> <symlink path>
Processing Environment
• On a Unix system everything is file, if something is not a file ,
it is a process.
• A program is an executable file, and a process is an instance
of a running program.
• Many processes can execute simultaneously on UNIX system
with no logical limit to their number, and many instances of a
program can exist simultaneously in the system.
• As soon as you typed command and press enter, the
program gets ready in the memory and executed by kernel.
• Some programs create single process, such as ‘ls command’
while some creates more than one, such as open office.
Process ID
• Every process executing in the UNIX system has an
execution environment that includes a current
directory.
• In Unix based operating system, every process has a
process ID called PID.
• At any one time, no two processes with the same PID
exist in the system because it is the PID that UNIX uses
to track each process.
• PID must be unique integer value , no negative
interger
Process
• When you start a process (run a command), there are two ways you can run it:
– Foreground Processes
– Background Processes
Foreground Processes
• By default, every process that you start runs in the foreground. It gets its input
from the keyboard and sends its output to the screen.
• For Example
– You can see this happen with the ls command. If I want to list all the files in my current
directory, I can use the following command:
$ls ch*.doc
– This would display all the files whose name start with ch and ends with .doc:
– The process runs in the foreground, the output is directed to my screen, and if the ls
command wants any input (which it does not), it waits for it from the keyboard.
• While a program is running in foreground and taking much time, we cannot run
any other commands (start any other processes) because prompt would not be
available until program finishes its processing and comes out.
Background Processes:
• A background process runs without being connected to
your keyboard. If the background process requires any
keyboard input, it waits.
• The advantage of running a process in the background is
that you can run other commands; you do not have to
wait until it completes to start another!
• The simplest way to start a background process is to add
an ampersand ( &) at the end of the command.
• For Example:
$ls ch*.doc &
– This would also display all the files whose name start with ch
and ends with .doc:
Ps Command
• The ps (process status) command produces a list of the currently running processes on your
computer.
$ ps
• ps
• The output will show rows of data containing the following information:
• PID, TTY, Time, Command
• The PID is the process ID which identifies the running process. The TTY is the terminal type.
TIME is the amount of CPU (central processing unit) time in minutes and seconds that the
process has been running. CMD is the name of the command that launched the process.
• One of the most commonly used flags for ps is the -f ( f for full) option.
$ ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts/6 0:00 -ksh
• Here is the description of all the fileds displayed by ps -f command:
Column Description
UID User ID that this process belongs to (the person
running it).
PID Process ID.
PPID Parent process ID (the ID of the process that started
it).
C CPU utilization of process.
STIME Process start time.
TTY Terminal type associated with the process
TIME CPU time taken by the process.
CMD The command that started this process.
Stopping Processes
• Ending a process can be done in several different ways. Often,
from a console-based command, sending a CTRL + C keystroke
(the default interrupt character) will exit the command. This
works when process is running in foreground mode.
• If a process is running in background mode then first you would
need to get its Job ID using ps command and after that you can
use kill command to kill the process as follows:
$ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts/6 0:00 –ksh
$ kill 6738
Here kill command would terminate first_one process