You are on page 1of 55

Linux File System

File : File is a collection of information, data items and programs


stored onto the disk. Whatever a user stores in computer (text,
music, picture, image, sound, data, etc.) it must e in the formof
files. Files are al!ays associated !ith devices like hard disk. File is
last o"ect in the hierarchy of file systemtree.
Directory : #irectory is a group of files. #irectories are used to
organi$e data, programs, files and su%directories. & related organi$e data, programs, files and su%directories. & related
group of information is stored in directories.
#irectory is divided into t!o types:%
Root Directory: 'his is the top most directory in the hierarchy
of directory system under !hich all the files and su%directories
are created. (t is denoted y / or root.
Sub Directory: #irectory under root directory ()) or any other
directory is called su directory.
*ath +ames:
&solute : *ath specified from the ,oot directory is
called &solute, i.e. the path starting from )
,elative : *ath specified from !ithin the current ,elative : *ath specified from !ithin the current
directory is called ,elative.
File System: & Linux file system is a collection of files and
directories stored in it. & !hole hard disk may e comprised as a
single file system or it may e partitioned to house several file
systems.
A File system is a group of files and relevant information
regarding them such as data structures.
A file system is the methods and data structures that an A file system is the methods and data structures that an
operating system uses to keep track of files on a disk or
partition; that is, the way the files are organized on the
disk.
-efore a partition or disk can e used as a file system, it needs to
e initiali$ed, and the ookkeeping data structures need to e
!ritten to the disk. 'his process is called making a file system.
File Type Meaning
- Ordinary File
d Directory File
c Character Special File
b Block Special File
File Types
b Block Special File
l Symbolic Link
p Named Pipe
m Shared Memory File
The disk space allotted to a Linux file system is made
up of blocks each of which typically of 512, 1024 or
2048 bytes.
Whenever the file is created one block is made
available for storing files data/contents. available for storing files data/contents.
All the blocks belonging to the file system are logically
divided into four parts.
The first block of the file system is called Boot block
which is followed by Super block, I-node block and
Data block.
!ery "ile system has "ollo#ing "o$r components%
&Boot Block
&S$per Block
&'-node Block
&Data Block
Boot Block: This represents the beginning of the file
system.
It contains a program called bootstrap loader and Linux
Kernel.
It is also called Master Boot Record (MBR) because it loaded
into the memory when the system is booted.
Although only one Boot block is needed to startup the system, Although only one Boot block is needed to startup the system,
all file systems contain one boot block.
Boot block is used to recover from the situation where the
BIOS code is incorrect or corrupted. During booting when PC
finds that the BIOS code is corrupt, the boot block will try to
recover the BIOS code.
Super Block: The Super block describes the state of the file
system How large it is, how many max files can it
accommodate, how many more files can be created, etc.
It contains global file information about disk usage and
availability of data blocks and i-nodes.
The Kernel maintains a copy of Super block in memory and
also periodically (in 30sec.) updates the disk copy from the
memory copy. memory copy.
Super block contains details regarding:-
1. The size of file system
2. The block size used by the file system
3. The number of free (unused) data blocks
available and allocated (used) data blocks
4. The number of free and usable i-nodes.
5. Last time of updating.
I-node Block: We know that all entries in the Linux are
treated as files even the deices also.
Each file in the system is described by a structure called i-
node.
An i-node is a data structure which stores basic information
(metadata) about a file (not the contents).
The information related to these files (data structure not the The information related to these files (data structure not the
contents) is stored in a table on the disk called Inode Table.
The information related to these files (i-nodes) are located in
the i-node table or i-list. The i-node table contains the i-node
and the block numbers that stores the contents of the file. Each
i-node occupies 64 bytes and contains all information about a
file except the file name.
I-node is a unique identification for a file created in Linux. For
each file there is an inode entry in the table.
The information contained in i-node are:
1. Owner of the file. (Owner)
2. Group to which the owner belongs. (Group)
3. Type of file. (File Type)
4. File access permissions. (Permissions)
5. Date and time of last access. (Access Time)
6. Date and time of last modification. 6. Date and time of last modification.
(Modification Time)
7. Number of links to the file. (Links)
8. Size of the file. (File Size)
9. Addresses of blocks where the file is
physically present.
Data Block: These are located on the disk and
contain all the data of the file. These blocks contain
the actual file contents.
An allocated block can belong to only one file in the
file system.
This block cannot be used for storing any other files This block cannot be used for storing any other files
contents unless the file to which it originally belonged
is deleted.
Inode Structure
The 'node
Direct/Indirect Blocks : Pointers to the blocks that
contain the data that this Inode is describing.
Timestamp: The time that the Inode was created
and the last time that it was modiied.
Si!e : The si!e o the ile in bytes. Si!e : The si!e o the ile in bytes.
"wner ino : This stores user and group identiiers
o the owners o this ile or directory
#ode : This holds two pieces o inormation$ what
this inode describes and the permissions that
users ha%e to it .
(t) File System Layo$t
B&"'( *P B&"'( *P . . . . . . . . . . B&"'( *P B&"'( *P
+ , -., +
S.*/, -L012
3,0.*
#/S1,(*'0,
-L012
-('4&*
(+0#/
-('4&*
(+0#/
'&-L/
#&'& -L012S
File System Layo$t
'he !hole area is divided into several lock groups, and
lock groups contains several locks.
& lock group is used to store file metadata and file
& disk is divided into small groups of sectors called 5locks5.
'hese locks are then grouped into larger units called lock
groups.
& lock group is used to store file metadata and file
content
Super
Block
Group
Desc
Table
Block
Bitmap
Inode
Bitmap
Inode
Table
File
Data
File
Data
Block
Bitmap
Inode
Bitmap
-lock 3roup 6 -lock 3roup 7
-lock 3roup #escriptor 'ale:
(t contains a group descriptor data structure for every
lock group.
'he group descriptor stores the address of lock itmap
and inode itmap for the lock group.
-itmaps: -itmaps:
'he lock itmap manages the allocation status of the
locks in the group.
'he inode itmap manages the allocation status of the
inodes in the group.
Super
-lock
-lock
#esc
'ale
-lock
-itmap
-lock
-itmap
3roup 6
3roup 7
(node
-itmap
(node
'ale
(node
-itmap
(node
'ale
-lock
-itmap
8
3roup n
(node
-itmap
(node
'ale
(node 'ales:
(node tale contains the inodes that descries the files in
the group
(nodes:
/ach inode corresponds to one file, and it stores file9s
primary metadata, such as file9s si$e, o!nership, and primary metadata, such as file9s si$e, o!nership, and
temporal information.
(node is typically :; ytes in si$e and is allocated to each
file and directory
Standard File SystemTypes
'raditional file systems
/xt< : Same as .nix file system use concepts of locks,
inodes and directories.
4inix : File System for 4inix 0S
4S%#0S)=F&' F&' 7:)><: used y Windo!s, uses clusters
to store data instead of locks
?*FS (?igh *erformance File System)
7@
?*FS (?igh *erformance File System)
Aournaling file systems
Aournaling filesystems offer several important advantages over static
filesystems, such as ext<. (n particular, if the systemis halted !ithout a proper
shutdo!n, they guarantee consistency of the data and eliminate the need for a
long and complex file systemcheck during reooting.
'he term "ournaling derives its name from the fact that a special file called a
/ournal is used to keep track of the data that has een !ritten to the hard
disk.
/xt> : Same as /x< FS enhanced !ith Aournaling capailities
i.e. metadata updates are recorded and FS can e recovered
after a crash.
,eiserFS : &dvanced Aournal
+'FS (+e! 'echnology File System) used in Windo!s
AFS : Aournal file System, developed y (-4
BFS : File System, developed y S3( (Silicon 3raphics)
=erita9s =xFS
0thers
iso@::6 Filesystemused on 1#%,04s
proc +ot a real filesystem, so uses none as the device.
.sed as a !ay for the kernel to report system information to user
processes
Sysfs exports information aout devices and drivers from the kernel device
model to user space
Lin$( File Systems
"ther ile systems:
sysv % S10)Benix
ufs % Sun0S)-S#
vfat % Win@x
msdos % 4S%#0S)Win
umsdos % Linux)#0S
ntfs % Win+' (r)o)
hpfs % 0S)<
cms % 14S (r)o) cms % 14S (r)o)
"ther File systems:
iso@::6 (1#%,04)
nfs C +FS
coda % +FS%like
ncp C +ovell
sm % L&+4anager
afs % &ndre! File System
Lin$( File System *ierarchical
Str$ct$re Str$ct$re
Lin$( File System Basics
Linux files are stored
in a single rooted,
hierarchical file
system
Directories
root
system
#ata files are stored in
directories (folders)
#irectories may e
nested as deep as
needed
User home
directories
Data files
Naming Files
Files are named y
naming each containing
directory
starting at the root starting at the root
'his is kno!n as the
pathname
/etc/passwd
/home/neale/b
The C$rrent Directory
0ne directory is
designated the current
working directory
if you omit the leading
/ then path name is / then path name is
relative to the current
!orking directory
.se pwd to find out
!here you are
Current working
directory doc/letter
./doc/letter
/home/neale/doc/letter
Some Special File Names
Some file names are special:
/ 'he root directory (not to e confused !ith the root user)
. 'he current directory
.. 'he parent (previous) directory
~ 4y home directory
/xamples:
./a same as a
../jane/x go up one level then look in directory jane for x
Special Files
/home % all users9 home directories are stored here
/bin, /usr/bin % system commands
/sbin, /usr/sbin % commands used y
sysadmins sysadmins
/etc % all sorts of configuration files
/var % logs, spool directories etc.
/dev % device files
/proc % special system files
/ this is the root folder, all other folders come under root.. think
of it as 1:D in a Windo!s context.
/bin this folder contains all executale files for most of the Linux
commands and all the user%essential inaries (programs) that
are needed to administer and run your Linux system8 delete this
folder and your systemis roken. folder and your systemis roken.
/boot as the name suggests, this folder contains configuration
files and other necessary files that are needed y the oot%loader
/dev this folder contains files that control various ()0 devices
called device files (rememer, these files represent physical
devices, so e careful !hen !orking !ith them)
/etc this folder contains all the configuration files used y the
system, you can also start and stop services (daemons) fromhere
/home this folder contains the home folders of all the normal
(non C root ) users on the system .. think of it as my documents
in !indo!s
/lib this folder contains all the lirary functions (soft!are
liraries) !hich can e later used y Linux users. liraries) !hich can e later used y Linux users.
/media this is a mount point for removale devices8 this is
!here you !ould usually mount your thumdrives 8 etc
/mnt this is a temporary mount point
/opt this folder contains add on soft!are (extra soft!are)
/tmp this folder contains temporary files that are erased upon
reoot
/usr this folder and its sufolders contains user installed
programs and utilities and liraries. 'here are several folders
under usr, each associated !ith particular user.
/var this folder contains files that change alot (E=ariale filesE) /var this folder contains files that change alot (E=ariale filesE)
/root this folder contians the root user9s files
/proc this is a psuedo folder, that contains information aout the
linux kernel and hard!are that is updated in realtime.
/sbin this folder contains inaries that can only e run as the
root user (EsuperuserE)
1. / Root
&!ery single "ile and directory starts "rom the root directory+
&Only root $ser has #rite pri!ilege $nder this directory+
&Please note that ,root is root $ser-s home directory. #hich is
not same as ,+
2. /bin User Binaries
&Contains binary e(ec$tables+ &Contains binary e(ec$tables+
&Common Lin$( commands yo$ need to $se in single-$ser
modes are located $nder this directory+
&Commands $sed by all the $sers o" the system are located
here+
&For e(ample% ps. ls. grep. cp. p#d+
3. /sbin SystemBinaries
&/$st like ,bin. ,sbin also contains binary e(ec$tables+
&B$t. the lin$( commands located $nder this directory are
$sed typically by system administrator. "or system
maintenance p$rpose+
&For e(ample% iptables. reboot. "disk. i"con"ig. s#apon
4. /etc Configuration Files
&Contains con"ig$ration "iles re0$ired by all programs+
&This also contains start$p and sh$tdo#n shell scripts $sed to
start,stop indi!id$al programs+
&For e(ample% ,etc,resol!+con". ,etc,logrotate+con"
1+ /de !eice Files
&Contains de!ice "iles+
&These incl$de terminal de!ices. $sb. or any de!ice attached
to the system+
&For e(ample% ,de!,tty2. ,de!,$sbmon3
4+ /"roc #rocess $nformation 4+ /"roc #rocess $nformation
&Contains in"ormation abo$t system process+
&This is a pse$do "ilesystem contains in"ormation abo$t
r$nning process+ For e(ample% ,proc,5pid6 directory contains
in"ormation abo$t the process #ith that partic$lar pid+
&This is a !irt$al "ilesystem #ith te(t in"ormation abo$t
systemreso$rces+ For e(ample% ,proc,$ptime
7+ /ar %ariable Files
&!ar stands "or !ariable "iles+
&Content o" the "iles that are e(pected to gro# can be "o$nd
$nder this directory+
&This incl$des 8 system log "iles 9,!ar,log:; packages and
database "iles 9,!ar,lib:; emails 9,!ar,mail:; print 0$e$es
9,!ar,spool:; lock "iles 9,!ar,lock:; temp "iles needed across
reboots 9,!ar,tmp:; reboots 9,!ar,tmp:;
<+ /tm" &em"orary Files
&Directory that contains temporary "iles created by system
and $sers+
&Files $nder this directory are deleted #hen system is
rebooted+
=+ /usr User #rograms
&Contains binaries. libraries. doc$mentation. and so$rce-code "or
second le!el programs+
&,$sr,bin contains binary "iles "or $ser programs+ '" yo$ can-t "ind a $ser
binary $nder ,bin. look $nder ,$sr,bin+ For e(ample% at. a#k. cc. less.
scp
&,$sr,sbin contains binary "iles "or system administrators+ '" yo$ can-t
"ind a systembinary $nder ,sbin. look $nder ,$sr,sbin+ For e(ample% atd.
cron. sshd. $seradd. $serdel cron. sshd. $seradd. $serdel
&,$sr,lib contains libraries "or ,$sr,bin and ,$sr,sbin
&,$sr,local contains $sers programs that yo$ install "rom so$rce+ For
e(ample. #hen yo$ install apache "rom so$rce. it goes $nder
,$sr,local,apache)
23+ /'ome (ome !irectories
&*ome directories "or all $sers to store their personal "iles+
&For e(ample% ,home,>ohn. ,home,nikita
22+ /boot Boot )oader Files
&Contains boot loader related "iles+
&?ernel initrd. !mlin$(. gr$b "iles are located $nder ,boot
&For e(ample% initrd+img-)+4+@)-)A-generic. !mlin$B-)+4+@)-
)A-generic
2)+ /lib System )ibraries 2)+ /lib System )ibraries
&Contains library "iles that s$pports the binaries located
$nder ,bin and ,sbin
&Library "ilenames are either ldC or libC+so+C
&For e(ample% ld-)+22+2+so. libnc$rses+so+1+7
&2@+ /o"t *"tional add+on ,""lications
&opt stands "or optional+
&Contains add-on applications "rom indi!id$al !endors+
&add-on applications sho$ld be installed $nder either ,opt, or
,opt, s$b-directory+
&2A+ /mnt -ount !irectory
&Temporary mo$nt directory #here sysadmins can mo$nt
"ilesystems+
21+ /media Remoable -edia !eices
&Temporary mo$nt directory "or remo!able de!ices+
&For e(amples. ,media,cdrom "or CD-DOM; ,media,"loppy "or
"loppy dri!es; ,media,cdrecorder "or CD #riter
24+ /sr Serice !ata 24+ /sr Serice !ata
&sr! stands "or ser!ice+
&Contains ser!er speci"ic ser!ices related data+
&For e(ample. ,sr!,c!s contains CES related data+
< Mounting >
As you know, you can store your data in different physical
storage devices, like floppies, CD-ROMs, and hard disk drives.
Your hard disk or disks are also very likely split up into different
partitions with different file-systems.
If you're migrating to Linux from Microsoft Windows, you're
probably used to accessing all your file systems very easily:
you just boot up your computer, go to My Computer, and find
all your Windows partitions there immediately.
For example, if you have a second hard drive (or a second
Windows partition), it automatically appears as D:\ and you can
immediately access it. The same goes for floppies, CD-ROMs,
digital cameras, and other storage devices - you just plug them
in, and you'll be able to immediately access them.
However, this isn't the case in Linux
You're probably a bit confused at first:
you put your floppy or CD into the drive and start wondering
why you're not able to access it! This is because your floppies,
CDs, hard disk partitions, and other storage devices must be
attached to some existing directory in the form of file on your
system before they can be accessed. Because all hardware will
be treated as files in Linux.
The process of attaching of devices into the some directory
in the form of file, to make them usable in the environment in the form of file, to make them usable in the environment
is called mounting, and the directory where the device is
attached is called a mount point.
After the device is mounted, you can access the files on that
device by accessing the directory where the device is attached.
When you're done and want to remove the floppy or CD or
other device, you need to detach, unmount, it before removing
it.
A File system is self contained entity with its own tree structure
and root directory.
There are several File systems contained in different devices.
These File systems unite to become a single File system at the
time of booting. The root File system then becomes the main
File system, and its root directory becomes the main directory
of the unified File system.
The process of making all different File systems united is The process of making all different File systems united is
known as Mounting, when all secondary File systems mount
(attach) themselves to the main File system at different points.
Point at which mounting takes place is called Mount Point.
After mounting users see a single File system instead of two
separate ones. User can then copy or move files between two
File systems.
Mounting is the attaching of an additional filesystem to
the currently accessible filesystemof a computer.
& filesystemis a hierarchy of directories (also referred to
as a directory tree) that is used to organi$e files on a
computer or storage media (e.g., a 1#,04 or floppy
disk). 0n computers running Linux or other .nix%
like operating systems, the directories start !ith like operating systems, the directories start !ith
the root directory, !hich is the directory that contains
all other directories and files on the systemand !hich is
designated y a for!ard slash ( ) ). 'he currently
accessile filesystem is the filesystem that can e
accessed on a computer at a given time.
(n order to gain access to files on a storage device, the user must
first inform the operating system !here in the directory tree to
mount the device. & device in a mounting context can e
a partition (i.e., a logically independent section) on a hard disk
drive (?##), a 1#,04, a floppy disk, a .S- (universal serial
us) key drive, a tape drive, or any other external media. For
example, to access the files on a 1#,04, the user must inform
the systemto make the filesystemon the 1#,04appear in some
directory, typically /mnt/cdrom (!hich exists for this very directory, typically /mnt/cdrom (!hich exists for this very
purpose).
'he mount point is the directory (usually an empty one) in the
currently accessile filesystem to !hich a additional filesystem is
mounted. (t ecomes the root directory of the added directory
tree, and that tree ecomes accessile fromthe directory to !hich
it is mounted (i.e., its mount point).
< How to mount >
Mounting is done with the mount command which requires two arguments
The device name or partition of the file system
Mount point
The mount point must be a directory that already exists on your system.
For example, to mount your floppy:
$ mount /dev/fd0 /mnt/floppy
In this example, /dev/fd0 is your floppy drive, and /mnt/floppy is the mount In this example, /dev/fd0 is your floppy drive, and /mnt/floppy is the mount
point. Now when you access /mnt/floppy, you'll actually access the files on
your floppy.
Usually /dev/fd0 is your floppy drive, although some distros are configured so
that /dev/floppy is the same thing as /dev/fd0.
Usually your CD-ROM is configured the same way: /dev/cdrom is your CD-
ROM device (or, more specifically, /dev/floppy is a symbolic link to your actual
floppy drive, and /dev/cdrom is a symbolic link to your CD-ROM drive).
< How to unmount >
Unmounting is done with the umount command. No, I didn't make a typo:
the command really is umount, not unmount.
When unmounting, you'll need to tell umount what mounted device to
unmount, either by telling what's the device or the mount point. For example,
if /dev/fd0 is mounted to /mnt/floppy, you'll unmount it with
$ $ $ $ umount umount umount umount / // /mnt mnt mnt mnt/floppy /floppy /floppy /floppy
or or or or or or or or
$ $ $ $ umount umount umount umount /dev/fd0 /dev/fd0 /dev/fd0 /dev/fd0
It's not wise to remove the floppy from the floppy drive without unmounting it
first! In the worst case the data you were writing to the floppy wasn't written
into it yet.
With CD-ROMs you can't do this: the tray won't even open if you haven't
unmounted the CD first.
Entering into Linux
Login Process: To enter into Linux system every user must
have his distinct identification (user account) which consist of
username and password.
To use the system the user has to login to the system. This
procedure of entering the system is called logging-in or
signing.
user.s interaction with the system !etween login and logout
is termed as session.
To login the user has to identify to the system that he is an
authori"ed user of the system !y giving following entries
Login:
Password:
Linux #ommand $asics
'o execute a command, type its name, options and
arguments at the command line
ls -l /etc
Command name
Options
(flags)
Arguments
Linux %ile and &irectory #ommands
cat
Creates a new file and allows to store
few lines in it until ctrl+d is pressed
Also used to display the contents of
the file.
Also concatenate the contents of two
files and store them in third file.
$ cat > filename
$ cat filename
$ cat file1 file2 > file3
cd
Change directory. $ cd dirname
chmod
Change the file permissions by
owner or superuser.
Read(r)-4, write(w)-2, execute(x)-1.
Three entities to which any
combination of these permissions
are assigned are Owner, Group &
Other
$ chmod n1n2n3 filename
cp
Copy a file from one location
to another. Copy the contents
of file1 into file2, if file2 does
not exist it will be created.
To copy the file from one
directory to another directory
the file path is required
$ cp file1 file2
$ cp path1/file1 path2/file2
ln
Creates symbolic link (alias/shortcut) to a file
A file can be accessible by different names
ls
It gives directory listing or list the contents of current dir in
alphabetical order.
Options
-a display all files including hidden files (starts using .) also
-l giving long listing first is no. of blocks occupied by the
file (total 22) col1-file type ( - ordinary file, d Dir, b block
special file, l link, etc) & permissions, col2-number of links to
the file, col3-user name followed by group name, col4-size of
$ ls
$ ls -a
$ ls l
the file, col3-user name followed by group name, col4-size of
the file in bytes, col5-date&time when last modified, col-6
filename
-ld long listing of directory
-r lists all files in present dir and file in sub directories
-s lists the files along with their size (in blocks not in
bytes)
-i lists the files along with their inode numbers
$ ls -ld
$ ls r
$ ls s
$ ls -i
lc Displays the files in
columnar faishon
lf Puts * after executables & /
after all sub directories.
mkdir Make (creates)a new
directory.
-p creates multiple
$ mkdir dirname
$ mkdir p
abcd/abc/ab/a -p creates multiple
generations of directories at
one go(creates all patent
directories)
-m creates dir with specific
permissions irrespective of
unmask value.
abcd/abc/ab/a
mv
Move or rename a file or directory.
Rename file1/dir1 to file2/dir2
Move the file by removing it from source,
file1 & file2 moved to newdir and removed
from source
$ mv file1 file2
$ mv dir1 dir2
$ mv file1 file2 newdir
pwd
Display the present working directory with
full path.
When user login he brought into default
$ pwd
When user login he brought into default
working (home) directory, the command is
used for finding this directory
rm
Delete files (Remove files). Prompts before
deletion
Recursively (without making directory
empty) removes directory and all its
contents
Removes file forcibly without write
permission
$ rm i filename
$ rm r dirname
$ rm f filename

You might also like