Understanding UNIX / Linux File System: What Is A File?
Understanding UNIX / Linux File System: What Is A File?
Understanding UNIX / Linux File System: What Is A File?
A conceptual understanding of file system, especially data structure and related terms will help
you become a successful system administrator. I have seen many new Linux system
administrator without any clue about file system. The conceptual knowledge can be applied to
restore file system in an emergency situation.
What is a File?
File are collection of data items stored on disk. Or, it's device which can store the information,
data, music (mp3 files), picture, movie, sound, book etc. In fact what ever you store in
computer it must be inform of file. Files are always associated with devices like hard
disk ,floppy disk etc. File is the last object in your file system tree. See Linux/UNIX - rules for
naming file and directory names.
What is a directory?
Directory is group of files. Directory is divided into two types:
Root directory - Strictly speaking, there is only one root directory in your system, which
is denoted by / (forward slash). It is root of your entire file system and can not be
renamed or deleted.
Sub directory - Directory under root (/) directory is subdirectory which can be created,
renamed by the user.
Directories are used to organize your data files, programs more efficiently.
Ext2: This is like UNIX file system. It has the concepts of blocks, inodes and directories.
Ext3: It is ext2 filesystem enhanced with journalling capabilities. Journalling allows fast
file system recovery. Supports POSIX ACL (Access Control Lists).
Isofs (iso9660): Used by CDROM file system.
Sysfs: It is a ram-based filesystem initially based on ramfs. It is use to exporting kernel
objects so that end user can use it easily.
Procfs: The proc file system acts as an interface to internal data structures in the kernel. It
can be used to obtain information about the system and to change certain kernel
parameters at runtime using sysctl command. For example you can find out cpuinfo with
following command:
# cat /proc/cpuinfo
Or you can enable or disable routing/forwarding of IP packets between interfaces with
following command:
# cat /proc/sys/net/ipv4/ip_forward
# echo "1" > /proc/sys/net/ipv4/ip_forward
# echo "0" > /proc/sys/net/ipv4/ip_forward
NFS: Network file system allows many users or systems to share the same files by using
a client/server methodology. NFS allows sharing all of the above file system.
Linux also supports Microsoft NTFS, vfat, and many other file systems. See Linux kernel
source tree Documentation/filesystem directory for list of all supported filesystem.
You can find out what type of file systems currently mounted with mount command:
$ mount
OR
$ cat /proc/mounts
Superblock
Each file system is different and they have type like ext2, ext3 etc. Further each file system
has size like 5 GB, 10 GB and status such as mount status. In short each file system has a
superblock, which contains information about file system such as:
File system type
Size
Status
Information about other metadata structures
If this information lost, you are in trouble (data loss) so Linux maintains multiple
redundant copies of the superblock in every file system. This is very important in many
emergency situation, for example you can use backup copies to restore damaged primary
super block. Following command displays primary and backup superblock location on /
dev/sda3:
# dumpe2fs /dev/hda3 | grep -i superblock
Output:
Primary superblock at 0, Group descriptors at 1-1
Backup superblock at 32768, Group descriptors at 32769-32769
Backup superblock at 98304, Group descriptors at 98305-98305
Backup superblock at 163840, Group descriptors at
163841-163841
Backup superblock at 229376, Group descriptors at
229377-229377
Backup superblock at 294912, Group descriptors at
294913-294913
Please note that If the superblock is not found, e2fsck will terminate with a fatal error.
However Linux maintains multiple redundant copies of the superblock in every file
system, so you can use -b {alternative-superblock} option to get rid of this problem. The
location of the backup superblock is dependent on the filesystem's blocksize:
For filesystems with 1k blocksizes, a backup superblock can be found at block 8193
For filesystems with 2k blocksizes, at block 16384
For 4k blocksizes, at block 32768.
Tip you can also try any one of the following command(s) to determine alternativesuperblock locations:
# mke2fs -n /dev/sda3
OR
# dumpe2fs /dev/sda3|grep -i superblock
To repair file system by alternative-superblock use command as follows:
# e2fsck -f -b 8193 /dev/sda3
However it is highly recommended that you make backup before you run fsck command on
system, use dd command to create a backup (provided that you have spare space under /
disk2)
# dd if=/dev/sda2 of=/disk2/backup-sda2.img
If you are using Sun Solaris UNIX, see howto: Restoring a Bad Superblock.
Please note that things started to get complicated if hard disk participates in software RAID
array. Take a look at Software-RAID HOWTO - Error Recovery. This article/tip is part of
Understanding UNIX/Linux file system series, Continue reading rest of the
Understanding Linux file system series (this is part III):
inode definition
An inode is a data structure on a traditional Unix-style file system such as UFS or ext3. An
inode stores basic information about a regular file, directory, or other file system object.
0/
Inode application
Many commands used by system administrators in UNIX / Linux operating systems often give
inode numbers to designate a file. Let us see he practical application of inode number. Type the
following commands:
$ cd /tmp
$ touch \"la*
$ ls -l
Now try to remove file "la*
You can't, to remove files having created with control characters or characters which are unable
to be input on a keyboard or special character such as ?, * ^ etc. You have to use inode number
to remove file. This is fourth part of "Understanding UNIX/Linux file system, continue reading
rest of the Understanding Linux file system series (this is part IV)
directories . (current directory) and .. (pointer to previous directory i.e. the directory
immediately above the one I am in now). The .. appears in every directory except for the root
directory.
Directory
A directory contained inside another directory is called a subdirectory. At the end the directories
form a tree structure. Use tree command to see directory tree structure:
$ tree /etc | less
Again a directory has an inode just like a file. It is a specially formatted file containing records
which associate each name with an inode number. Please note the following limitation of
directories under ext2/3 file system:
There is an upper limit of 32768 subdirectories in a single directory.
There is a "soft" upper limit of about 10-15k files in a single directory
However according to official documentation of ext2/3 file system points that Using a
hashed directory index (which is under development) allows 100k-1M+ files in a single
directory without performance problems'. Here are my two favorite alias commands
related to directory :
$ alias ..='cd ..'
alias d='ls -l | grep -E "^d"'
Well I'm sure all of you know the basic commands related to directories and files
managment. Click above (or here) to see summery of all basic commands related to
directories and files managment. See interesting discussion about soft links and
directories. This is 6th part of "Understanding UNIX/Linux file system, continue reading
rest of the Understanding Linux file system series (this is part IV):
20:19 ..
1048601 -rwxr-xr-x
1 vivek vivek
resolv.conf
Now create soft link to resolv.conf, enter:
$ ln -s resolv.conf alink.conf
$ ls -ali
Sample output:
total 152
1048600 drwxr-xr-x
2 vivek
1015809 drwxrwxrwt 220 root
20:19 ..
1048602 lrwxrwxrwx
1 vivek
alink.conf -> resolv.conf
1048601 -rwxr-xr-x
1 vivek
resolv.conf
vivek
4096 2008-12-09 20:24 .
root 143360 2008-12-09
vivek
11 2008-12-09 20:24
vivek
The reference count of the directory has not changed (total 152). Our symbolic (soft) link
is stored in a different inode than the text file (1048602). The information stored in
resolv.conf is accessible through the alink.conf file. If we delete the text file resolv.conf,
alink.conf becomes a broken link and our data is lost:
$ rm resolv.conf
$ ls -ali
If alink.conf was a hard link, our data would still be accessible through alink.conf. Also,
if you delete the soft link itself, the data would still be there. Read man page of ln for
more information.