Filefrag is a command-line tool that is used to determine the level of fragmentation of a particular file on a Linux or Unix-based system. The tool works by analyzing the layout of a file on disk and reporting on how badly fragmented it might be.
When a file is saved to a hard drive, it is typically split up into smaller pieces called blocks, which are stored in different locations on the disk. Over time, as files are added and deleted, the blocks that make up a file can become scattered across the disk, making it more difficult for the operating system to access the file quickly. This is known as fragmentation.
Filefrag analyzes the layout of a file on disk and reports on how many fragments the file is split into and where they are located on the disk. By default, it shows the fragment number, block number, and block offset of each fragment. Additionally, it can also report the extents of a file which are consecutive blocks of a file, in a more readable format.
The tool can be used to identify which files on a system are most heavily fragmented and may require defragmentation, which is a process of reorganizing the blocks of a file so that they are stored in contiguous locations on disk, to improve disk access times.
If you encounter the below error while running the command filefrag:
filefrag: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
Debian | apt-get install e2fsprogs |
Ubuntu | apt-get install e2fsprogs |
Alpine | apk add e2fsprogs |
Arch Linux | pacman -S e2fsprogs |
Kali Linux | apt-get install e2fsprogs |
CentOS | yum install e2fsprogs |
Fedora | dnf install e2fsprogs |
OS X | brew install e2fsprogs |
Raspbian | apt-get install e2fsprogs |
filefrag Command Examples
1. Display a report for a specific file:
# filefrag path/to/file
2. Display a report for space-separated list of files:
# filefrag path/to/file1 path/to/file2
3. Display a report using a 1024 byte blocksize:
# filefrag -b path/to/file
4. Sync the file before requesting the mapping:
# filefrag -s path/to/files
5. Display mapping of extended attributes:
# filefrag -x path/to/files
6. Display a report with verbose information:
# filefrag -v path/to/files