You are on page 1of 52

Lecture-2

File Management in Unix Based


Operating System
What is file?
• All data in UNIX is organized into files.
• All files are organized into directories.
• These directories are organized into a tree-like structure called
the file system.
• In UNIX there are three basic types of files:
1. Ordinary Files: An ordinary file is a file on the system that
contains data, text, or program instructions.
2. Special Files (This category is having 5 sub types in it.) :
Some special files provide access to hardware such as hard
drives, CD-ROM drives, modems, and Ethernet adapters.
3. Directories: Directories store both special and ordinary files.
Listing Files:
• To list the files and directories stored in the current directory, we use ls command
• The command ls supports the -l option which would help you to get more information
about the listed files:
• For Example:
$ls -l
total 10
drwxrwxr-x 2 amrood amrood 4096 Dec 25 09:59 uml
-rw-rw-r-- 1 amrood amrood 5341 Dec 25 08:38 uml.jpg
drwxr-xr-x 2 amrood amrood 4096 Feb 15 2006 univ
drwxr-xr-x 2 root root 4096 Dec 9 2007 urlspedia
$
• Here is the information about all the listed columns:
– First Column: represents file type and permission given on
the file.
– Second Column: represents the number of links the file or
directory. have
– Third Column: represents owner of the file. This is the
Unix user who created this file.
– Fourth Column: represents group of the owner. Every Unix
user would have an associated group.
– Fifth Column : represents file size in bytes.
– Sixth Column: represents date and time when this file was
created or modified last time.
– Seventh Column: represents file or directory name.
• In the ls -l listing example, every first column began with a d, -, or l.
This describes the type of file.

• 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

Chmod Operator Description


+ Adds the designated permission(s) to a file or
directory.
- Removes the designated permission(s) from a
file or directory.
= Sets the designated permission(s)

Here permission can be set to these three group:


u= user
g= group
o= other
•Here's an example using testfile.
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
$
•Now lets change some permission and see the result...
• $chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$

•$chmod u-x testfile


$ls -l testfile
-rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$
• $chmod g=r-x testfile
$ ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
$
• you could also combine these commands on a single line:
• $chmod o+wx,u-x,g=r-x testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
$
Using chmod with Absolute Permissions:
• The second way to modify permissions with the chmod command is to use
a number to specify each set of permissions for the file.

Number Octal Permission Representation Ref


0 No permission ---
1 Execute Permission --x
2 Write permission -w-
3 Execute and write permission: 1(execute) -wx
+2(write)=3
4 Read Permission r--
5 Read and execute permission: 4(read) r-x
+1(execute)=5
6 Read and write permission:4(read) rw-
+2(write)=6
7 All Permission :4(read)+2(write) rwx
+1(execute)=7
An example using testfile.
• Running ls -l on testfile shows that the file's permissions are as follows:
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
$
• Now lets change some permission and see the result followed by ls -l
so you can see the permission changes:

$ chmod 755 testfile


$ls -l testfile
-rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile
$
$chmod 743 testfile
$ls -l testfile
-rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile
$
$chmod 043 testfile
$ls -l testfile
----r---wx 1 amrood users 1024 Nov 2 00:10 testfile
$
Changing Owners and Groups:
• While creating an account on Unix, it assigns a owner ID and a group ID to each
user.
• All the permissions mentioned above are also assigned based on Owner and
Groups.
• Two commands are available to change the owner and the group of files:
– chown: The chown command stands for "change owner" and is used to
change the owner of a file.
– chgrp: The chgrp command stands for "change group" and is used to change
the group of a file.
Changing Ownership:
• The chown command changes the ownership of a file.
• The basic syntax is as follows:
$ chown user filelist
• The value of user is uid of a user on the system and filelist are the file for which
you want to change owner.
• Following example:
$ chown amrood testfile
$
– Changes the owner of the given file to the user amrood.
• NOTE: The super user, root, has the unrestricted capability to change the
ownership of a any file but normal users can change only the owner of files they
own.
Changing Group Ownership:
• The chrgp command changes the group ownership of a file.
• The basic syntax is as follows:
$ chgrp group filelist
• The value of group can be the name of a group on the system or the group ID (GID)
of a group on the system.
• Following example:
$ chgrp special testfile
$
Changes the group of the given file to special group.
Inodes
Unix File System Nodes (inodes)
• Each file is referenced by an inode, which is addressed by a file system-unique
numerical value known as an inode number.
• An Inode number points to an Inode.
• An Inode is a data structure that stores the following information about a file:
– Size of file
– File Type
– User ID of the file
– Group ID of the file
– The file mode information and access privileges for owner, group and others
– The timestamps for file creation, modification, inode modification.
– link counter to determine the number of hard links
– Pointers to the blocks storing file’s contents
• Unix permits all files to have many name-to-number
mappings.
• So, a file may appear to have several different "names" that is,
several names that all map to the same node number. Or, the
file may have the same "name"; but, that name may appear in
different directories.
• Directories are not allowed to have many name-to-number
mappings.
• Each directory name-to-number map is allowed to appear in
exactly one parent directory and no more.
• This restriction means that every directory has only one
"name". It prevents loops and cycles in the file system tree.
Why no file-name in Inode information?

• 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.
/

bin lib tmp dev usr boot var etc

User1 Log_msg passwd

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.

/tmp Holds temporary files used between system boots


/dev It contains block and character-special files that are usually associated
with the hardware drivers.
/usr Used for miscellaneous purposes, or can be used by many users.
Includes administrative commands, shared files, library files, and
others
/boot Contains files for booting the system.
/var Typically contains variable-length files such as log and print files and
any other type of file that may contain a variable amount of data
/etc This directory is where many basic administrative commands and
directories are kept. Supervisor directory commands, configuration
files, disk configuration files, valid user lists, groups.
Home Directory
• The directory in which you find yourself when you first login is
called your home directory.
• You will be doing much of your work in your home directory
and subdirectories that you'll be creating to organize your files.
• You can go in your home directory anytime using the following
command −
$cd ~
$
• Here ~ indicates home directory. If you want to go in any other
user's home directory then use the following command −
$cd ~username
$
• To go in your last directory you can use following command −
$cd -
$
Absolute/Relative Pathnames
• Directories are arranged in a hierarchy with root (/) at the top. The position of any
file within the hierarchy is described by its pathname.
• Elements of a pathname are separated by a /.
• A pathname is absolute if it is described in relation to root, so absolute pathnames
always begin with a /.
• These are some example of absolute filenames.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
• A pathname can also be relative to your current working directory. Relative
pathnames never begin with /. Relative to user amrood' home directory, some
pathnames might look like this −
chem/notes
personal/res
• To determine where you are within the filesystem hierarchy at any time, enter the
command pwd to print the current working directory −
$pwd /user0/home/amrood
$
Listing Directories
• To list the files in a directory you can use the
following syntax −
$ls dirname
• Following is the example to list all the files
contained in /usr/local directory −
• $ls /usr/local
X11 bin gimp Jikes sbin
ace doc include lib share
atalk etc info man ami
Creating Directories
• Directories are created by the following command −
$mkdir dirname
Here, directory is the absolute or relative pathname of the directory you want to
create. For example, the command −
$mkdir mydir
$
• Creates the directory mydir in the current directory. Here is another example −
$mkdir /tmp/test_dir
$
• This command creates the directory test_dir in the /tmp directory. The mkdir
command produces no output if it successfully creates the requested directory.
• If you give more than one directory on the command line, mkdir creates each of
the directories. For example −
$mkdir docs pub
$
• Creates the directories docs and pub under the current directory.
Creating Parent Directories
• Sometimes when you want to create a directory, its parent
directory or directories might not exist. In this case, mkdir issues
an error message as follows −
$mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
$
• In such cases, you can specify the -p option to the mkdir
command. It creates all the necessary directories for you. For
example −
$mkdir -p /tmp/amrood/test
$
• Above command creates all the required parent directories.
Removing Directories
• Directories can be deleted using the rmdir command as follows −
$rmdir dirname
$
• Note − To remove a directory make sure it is empty which means
there should not be any file or sub-directory inside this directory.
• You can remove multiple directories at a time as follows −
$rmdir dirname1 dirname2 dirname3
$
• Above command removes the directories dirname1, dirname2,
and dirname2 if they are empty.
• The rmdir command produces no output if it is successful.
Renaming Directories
• The mv (move) command can also be used to
rename a directory. The syntax is as follows −
$mv olddir newdir
$
• You can rename a directory mydir to yourdir
as follows −
$mv mydir yourdir
$
The directories . (dot) and .. (dot dot)
• The filename . (dot) represents the current working directory; and
the filename .. (dot dot) represent the directory one level above
the current working directory, often referred to as the parent
directory.
• If we enter the command to show a listing of the current working
directories files and use the -a option to list all the files and the -l
option provides the long listing, this is the result.
$ls -la
drwxrwxr-x 4 teacher class 2048 Jul 16 17.56 .
drwxr-xr-x 60 root 1536 Jul 13 14:18 ..
$
$ cd ..
will take you one level up directory.
Unix Commands - find
• Find is one of the powerful utility of unix or linux used for searching the files in a
directory hierarchy.
• Syntax:
$ find [pathnames] [condition]
• Examples:
1. How to find for a file using name?
Find –name “abc.txt”
This will find all the files with name abc.txt in the current and sub- directories.
2. How to find for a file in the current directory only?
find –maxdepth 1 –name “abc.txt”
3. How to find for a file in a specific directory?
find /etc –name “abc.txt”
4. How to find for files using name and ignoring case?
find -iname “abc.txt“
5. How to find the files whose name are not "sum.java"?
find -not -name “abc.txt"
How to find files based on the file type?

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

You might also like