I have moved my blog to Wordpress at theunixgeek.wordpress.com. I will still be checking back periodically on this one as well, though. 19 April 2009


Merging Mkdir and Cd | 280 Slides Interview | I Switched to KDE 4

clickable portals

Wednesday, November 26, 2008

Understanding the Linux Filesystem

Ah, the old UNIX adage: everything is either a file or process.

Let's take a closer look at the Linux filesystem and forget about processes for now.

Files can either be regular data-holding files, directories, references to hardware, links, and inter-process communicators. Believe it or not, directories are simply lists of files, links are files that tell where other files are, and those file in /dev contain information about the connected peripherals.

Unlike other operating systems, Linux distributions tend to have many partitions on a disk instead of a single one in order to have more data security. In case something happens to a partition, not all data is lost. There are various worst-case scenarios, such as a partition being completely filled up, bad blocks, etc.

Example: print command in parted
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type filesystem Flags
1 32.3kB 20.5GB 20.5GB primary ext3 boot
2 20.5GB 21.5GB 938MB extended
5 20.5GB 21.5GB 938MB logical linux-swap

As can be seen, there are usually at least two partitions in a Linux system: a primary data partition (at least one root partition, indicated by /, that holds system files) and a swap partition, which serves as extra physical memory (this allows the system to continuously work without running out of memory). In some cases, usually based on your sysadmin's choice, separate kernel, home, or program partitions are created upon installation. This is, again, to make sure that data is always kept intact if something bad happens.

Also, Linux supports various types of filesystems, such as its natives ext3 and ext2, along with those found natively in other operating systems, such as FAT or NTFS.

Partition mounting occurs when the mount command is used to attach a partition to a directory in the root filesystem. This way, the partition can be manipulated as a separate directory without messing with the system partition. The /etc/fstab file defines which partitions should be mounted on startup.

The df ("disk full") command, under GNU/Linux distributions, has a . (dot) command that tells you what non-swap partition the working directory (remember pwd?) is on. If you're familiar with Linux, you should be aware that this dot returns the pwd. However, it prints some information that is not always useful, so the -h option that allows for a more readable listing of information.

$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 19G 2.3G 16G 13% /
tmpfs 252M 0 252M 0% /lib/init/rw
varrun 252M 104K 252M 1% /var/run
varlock 252M 0 252M 0% /var/lock
udev 252M 2.7M 249M 2% /dev
tmpfs 252M 160K 251M 1% /dev/shm

By listing the root directory, one would see the following directories:

bin: programs shared by all of the system's users
boot: kernel and other essential boot files, such as a bootloader
dev: references to all peripheral hardware
etc: system configuration files
home: users' personal files
initrd: info on booting, not on all distros, not always dir
lib: the system library (files necessary for programs to run)
lost+found:files not saved correctly
mnt: mount point for external peripherals and filesystems
net: mount point for networked filesystems
opt: typically for third-party software
proc: a virtual filesystem for system resources (man proc)
root: home for the sysadmin
sbin: sysadmin's version of bin
tmp: temporary directory, emptied on boot
usr: programs, libraries, documentation, images, etc*
var: stores variable files for temporary use by applications

*be careful not to confuse the terms "usr" and "user," as "usr" stands for "Unix System Resources," a naming legacy from System V.

Remember, though, that filesystems are not trees, although they may seem like so. Directories are simply files that list other files. In filesystems, files are represented by an inode, a "serial number" that contains the information about the data in a file, such as:

  • its owner

  • its type

  • permissions

  • timestamps (that is, when it was created, last modified)

  • number of links (remember, links are just files) to it

  • its size (in pure kilobytes! try running df without -h and you'll see what I mean)

  • its location (again, don't imagine UNIX filesystems as trees! This portion of the inode simply says which directory file references this file!)

Be careful, though: not all filesystems have alike. Not all have inodes, many have limits on how many directories can be inside another directory, etc.

I hope you enjoyed this article. You can find out more here, here, and here.


Jadu Kumar Saikia said...

Nice explanation.

//Jadu, unstableme.blogspot.com

pbsl said...

you have a nice site. thanks for sharing this enormous resources. keep it up. anyway, various kinds of ebooks are available here


Sumant said...

The first thing that most new users shifting from Windows will find
confusing is navigating the Linux filesystem. The Linux filesystem
does things a lot more differently than the Windows filesystem.

Thanks for your explanation.

Recently I just came across a good article on "Linux"
Here is its link.

Anonymous said...

The Java Enterprise platform leverages the robustness of the Java programming language that allows developers to write the code only once and execute the application on any platform. Presently more than two-thirds of web development managers use the Java Enterprise platform to develop and deploy their applications. The Java Enterprise platform provides a framework for developing and deploying web services on the Java platform. The Java API for XML enables Java developers to develop interoperable and portable web services. java software company | software development company | java web development | blackberry application development | iphone application development | android application development | java outsourcing | it outsourcing services | http://www.tenaxtechnologies.com