How do I find file fragmentation for specific file under Linux?

If you would like to find out file fragmentation information for specific file, use filefrag command that report on file fragmentation.

ADVERTISEMENTS

filefrag reports on how badly fragmented a particular file might be. It makes allowances for indirect blocks for ext2 and ext3 filesystems, but can be used on files for any filesystem.

For example see file fragmentation for /var/log/message file:

# filefrag -v /var/log/messages

Output:

Checking /var/log/messages
Filesystem type is: ef53
Filesystem cylinder groups is approximately 9448
Blocksize of file /var/log/messages is 4096
File size of /var/log/messages is 72 (1 blocks)
First block: 1841075
Last block: 1841075
/var/log/messages: 1 extent found
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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

ADVERTISEMENTS
3 comments… add one
  • Anonymous Mar 22, 2006 @ 19:45

    Nice tip. Is there also a command to defrag a file?

  • 🐧 nixcraft Mar 23, 2006 @ 18:16

    Use defrag (ext2, minix and xiafs filesystem defragmenter ) command. Read man page of defrag for more info.

    Ext3 file system donot need defrag as it is optimized and do not suffer from fragmentation problem in general.

    • wellwell May 18, 2016 @ 8:29

      Ext3 is more protected against fragmentation, but it may also occur.
      Rare, but possible. I’ve experienced once with big data base files.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.