Linux Find And Report On File Fragmentation

How do I find out badly fragmented a particular file might be under Linux operating systems?

You need to use the filefrag command. It reports on how badly fragmented a particular file might be. It makesallowances for indirect blocks for ext2 and ext3 filesystems, but can be used on files for any filesystem. The syntax is:

filefrag file
filefrag files
filefrag file1 file2

In this example find how how badly fragmented rhel-workstation-6.3-x86_64-dvd.iso:
# filefrag /home/vivek/Downloads/rhel-workstation-6.3-x86_64-dvd.iso
Sample outputs:

/home/vivek/Downloads/rhel-workstation-6.3-x86_64-dvd.iso: 2093 extents found

You can verbose when checking for file fragmentation with the -v option:
# filefrag -v /etc/passwd
Sample outputs:

Filesystem type is: ef53
Filesystem cylinder groups is approximately 3547
File size of /etc/passwd is 1808 (1 block, blocksize 4096)
 ext logical physical expected length flags
   0       0 103071773               1 merged,eof
/etc/passwd: 1 extent found

Other options (taken from the man page):

       -B     Force  the  use  of  the  older FIBMAP ioctl instead of the FIEMAP ioctl for
              testing purposes.

       -b     Use 1024 byte blocksize for the output.

       -s     Sync the file before requesting the mapping.

       -x     Display mapping of extended attributes.

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

🐧 1 comment so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
1 comment… add one
  • Jalal Hajigholamali Jul 27, 2012 @ 5:35

    Hi,

    Thanks a lot, very useful article

    and useful command for admin

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