Linux Optimize Directories ( File Access Time ) in ext3 Filesystem

last updated in Categories , , , , , , ,

Q. I’m using CentOS Linux 5 server with ext3 on /disk2 (/dev/sdb1). How do I improve my file server performance. I’ve many large files in directories and how do I speed up access time?

A. Use e2fsck command. The -D option causes e2fsck to try to optimize all directories, either by reindexing them if the filesystem supports directory indexing, or by sorting and compressing directories for smaller directories, or for filesystems using traditional linear directories. It improves performance by using hashed binary trees.


ext3 File Tuning / Directory Optimization

Login as the root user

Take down system to single user mode and unmount file system:
# umount /dev/sdb1

Type the following command:

WARNING! Running e2fsck on a mounted filesystem may cause SEVERE filesystem damage. Always unmount file system.

Now run e2fsck on unmounted file system:
# e2fsck -D -f /dev/sdb1

e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 3A: Optimizing directories
Pass 4: Checking reference counts
Pass 5: Checking group summary information

/disk2: ***** FILE SYSTEM WAS MODIFIED *****
/disk2: 39/26104 files (17.9% non-contiguous), 20045/104388 blocks

Mount file system:
# mount /dev/sdb1

Now your should see improvements while accessing files. For more information please read e2fsck man page:
man e2fsck

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


19 comment

  1. momo,

    You need to take system to a single user mode and this cannot be done using ssh. You need either physical access to computer or use KVM remote access.

  2. for this, either we need more than 1 partition or the rescue cd of the OS!

    because, if we have one partition, we can’t umount it for e2fsck. using rescue cd is the only option available to single partition systems (like me).

    sometimes even the rescue cd method fails. :D

  3. I am running redhat linux 8. i upgraded glibc 2.2 to glibc 2.3. i upgarded all the dependencies also.i took the glibc 2.3 from redhat linux 9. Now the problem is that i cant able to install or uninstall any rpm. It is showing segmentation fault. Please tell me what i have to do next.?? Waiting for your reply?

  4. dennisraj,
    solution is to run fsck -a on the filesystem.

    it will ask you to unmount. you could do it if you have two devices.

    you could run it on a mounted filesystem also but then don’t blame me if something goes wrong.

    nothing should happen on running it on a mounted filesystem as i have done that many times.


  5. Get a life dennisraj. Stop asking questions period!!

    If you want to stay on RH8, then sort your own problem out and don’t trouble other people. Also I can’t see your claim of RH8 to be credible, I’m quite sure your system is unique to yourself.

    Additionally stop hijacking articles by putting unrelated comments/questions.

    Sort it out and move on.

  6. “Now your should see improvements while accessing files.”

    I am really curious. What improvements should be seen? Is there some measure you can use?

    And since this is “reindexing”, would the process need to be run on some regular basis?

  7. While running fsck on your system is not a bad idea, it is not clear to me why it would improve performance. The author merely states that it does, without backing that statement up with any facts. Searching google for ‘”e2fsck -D” performance’ brings up a couple of other interesting articles, but no hard numbers.

    More importantly, directory indexing is turned on by default already on modern distros, just take a look at /etc/mke2fs.conf (on my CentOS5 system), so the -D option really doesn’t do anything.

  8. Mauriat,
    And since this is “reindexing”, would the process need to be run on some regular basis?
    Do it only once and it will turn it on, edit /etc/mke2fs.conf to set it on.

    Now your should see improvements while accessing files.
    Run before:
    dd if=/dev/zero of=/path/2/file bs=16384 count=131072
    time cat /path/2/file > /dev/null

    Reboot (so cache get away); turn on indexing and rerun above commands to measure performance.

    Directory Indexing increases the speed of reads and writes as they are pre-indexed – so the read speed should increase. This is based upon own experience. Yes, some distro does turn it on by default so no need to execute e2fsck.
    Kernel ext3 doc has more information

  9. It’s possible to fsck the root filesystem, if it is mounted read-only. After “telinit S”, and with all other filesystems unmounted, type:

    # mount -o remount,ro -t ext3 /dev/[device] /

    This will leave it accessible, but no files may be changed, and no new files may be created.

    Then, run the fsck -D command, and reboot immediately upon completion.

  10. Interesting tip. Since fsck runs periodically after a certain number of intervals (specified by tune2fs -i) I was wondering if there was a way to run the -D flag at that time?

Leave a Comment