filefrag Command in Linux



The Linux filefrag command reports on file fragmentation. It gives detailed information on how data blocks are spread across the disk.

A fragmented file is stored in pieces on a disk instead of all together. It can be created by the filesystem to fit a large file on the disk. Accessing a fragmented file can be slower compared to reading a non-fragmented file.

The filefrag command first attempts to get file extent information using FIEMAP ioctl. It is an efficient and faster file extent querying method and provides detailed information. If it is not supported by the kernel or the filesystem, then the command uses an older file extent querying method called FIBMAP ioctl.

Table of Contents

Here is a comprehensive guide to the options available with the filefrag command −

Syntax of filefrag Command

The syntax of the Linux filefrag command is as follows −

filefrag [options] [file]

The [options] field is used to specify the options to modify the command’s output. The [file] field is used to specify the file name whose fragmentation is being queried.

Common Options of filefrag Command

The options used with the filefrag command are listed below −

Options Description
-B It is used to force the command to use FIBMAP instead of FIEMAP for testing
-b It is used to set block size, by default it is 1024
-s It is used to sync the file before performing a fragmentation check to ensure in-memory changes are updated
-v It is used to provide more detailed output
-x It is used to display the mapping of extended attributes for a file

Using filefrag Command in Linux

This section demonstrates the usage of the filefrag command in Linux with examples −

Checking File Fragmentation

To check the file fragmentation, use the filefrag command with the file name.

filefrag file.txt
Checking File Fragmentation 1

To get detailed information, use the -v option −

filefrag -v file.txt
Checking File Fragmentation 2

Checking File Fragmentation using FIBMAP

To force the filefrag command to use the older file extent querying method FIBMAP, use the -B option. Note that to perform the FIBMAP check, sudo permissions are needed −

filefrag -Bv file.txt
Checking File Fragmentation using FIBMAP

The -v option is used to get the verbose output.

Checking File Fragmentation with a Specific Blocksize

To check file fragmentation with a specific blocksize, use the -b option. For example, to set the blocksize to 1024, use the filefrag command in the following way −

filefrag -b1024 file.txt
Checking File Fragmentation with a Specific Blocksize

Syncing before File Fragmentation Check

To get an accurate output, the file must be synced with in-memory changes. To perform file syncing before the fragmentation check, use the -s option −

filefrag -s file.txt

Displaying Mapping of Extended Attributes

The extended attributes are additional metadata that is associated with the file. To display the mapping of extended attributes of a file, use the -x option −

filefrag -x file.txt
Displaying Mapping of Extended Attributes 1

To get detailed information on extended attributes, use the -v option −

filefrag -vx file.txt
Displaying Mapping of Extended Attributes 2

Conclusion

The filefrag command in Linux is used to display the file fragmentation report. It is a handy tool that can be used to get detailed information on a fragmented file.

A fragmented file is distributed across a disk instead of being one complete file. The filefrag performs FIEMAP or FIBMAP checks to query file fragmentation.

In this tutorial, we explained the filefrag command, its syntax, options, and usage in Linux through various examples.

Advertisements