Linux Optimize Directories ( File Access Time ) in ext3 Filesystem

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 19 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
19 comments… add one
  • momo Jan 5, 2008 @ 18:08

    I get: ‘umount: /: device is busy’ (logged on through ssh).
    What can I do?

  • 🐧 nixCraft Jan 5, 2008 @ 19:08


    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.

  • Nilesh Jan 6, 2008 @ 2:04

    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

  • Donald Jan 6, 2008 @ 5:59

    Is there a reason that you umount and fsck sdb1 and mount sdb2 or is that a typo?

  • 🐧 nixCraft Jan 6, 2008 @ 8:27

    Running e2fsck on a mounted filesystem corrupts file system for sure. So you need to unmount it.

  • Donald Jan 6, 2008 @ 15:54

    No, my point was that you umount/fsck a different filesystem than you mount at the end (sdb1, sdb2).

  • 🐧 nixCraft Jan 9, 2008 @ 10:16

    Now I feel stupid :(

    Thanks for the heads up. The faq has been updated.

  • dennisraj Jan 9, 2008 @ 17:26

    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?

  • 🐧 nixCraft Jan 9, 2008 @ 18:32

    Redhat 9?, No one can fix your problem. Get latest version or

  • Nilesh Jan 12, 2008 @ 17:46

    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.


  • Whyworry Feb 1, 2008 @ 14:24

    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.

  • Mauriat Feb 1, 2008 @ 14:55

    “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?

  • Alex Chekholko Feb 1, 2008 @ 16:05

    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.

  • Raj Feb 1, 2008 @ 16:23

    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

  • Raj Feb 1, 2008 @ 16:32

    Just a small addition,

    Run dd using for loop and read output to get real stats out of this.

    Hope this helps someone :)

  • gus3 Feb 1, 2008 @ 18:13

    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.

  • Bryan Feb 4, 2008 @ 22:31

    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?

  • AMITKUMAR May 15, 2008 @ 6:24

    how to design ext3 linux file system form the scratch

  • Alan Aversa Sep 9, 2012 @ 5:33

    Do console login and run “init 1” before doing any defragging.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum