How to use the command 'filefrag' (with examples)
- Linux
- December 25, 2023
The filefrag
command is used to report on how badly fragmented a particular file might be. It provides information about the fragmentation of a file, such as the number of extents, the fragmentation percentage, and the location of fragments within the file.
Use case 1: Display a report for a specific file
Code:
filefrag path/to/file
Motivation: You might want to check the level of fragmentation of a specific file to determine if defragmentation is needed. This use case allows you to quickly obtain a report on the fragmentation status of a single file.
Explanation:
path/to/file
: The path to the file you want to analyze.
Example output:
Filesystem type is: ext4
File size of path/to/file is 1000000 (244 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 2047: 4096.. 6143: 2048:
1: 2048.. 4095: 6144.. 8191: 2048:
This output shows that the file has 2 extents and provides information about the logical offsets, physical offsets, length, and flags for each extent.
Use case 2: Display a report for a space-separated list of files
Code:
filefrag path/to/file1 path/to/file2
Motivation: If you want to obtain fragmentation reports for multiple files, you can provide a space-separated list of file paths as arguments. This allows you to analyze the fragmentation status of multiple files at once.
Explanation:
path/to/file1 path/to/file2
: The paths to the files you want to analyze, separated by spaces.
Example output:
Filesystem type is: ext4
File size of path/to/file1 is 10000 (44 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 63: 4096.. 4159: 64:
...
Filesystem type is: ext4
File size of path/to/file2 is 20000 (89 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 1023: 8192.. 9215: 1024:
...
This output provides fragmentation reports for each specified file. Each report includes information about the extents, such as logical offsets, physical offsets, length, and flags.
Use case 3: Display a report using a 1024 byte blocksize
Code:
filefrag -b path/to/file
Motivation: By default, filefrag
uses a blocksize of 4096 bytes to display the fragmentation information. However, you might want to use a different blocksize for more detailed or specific analysis. This use case allows you to specify a 1024 byte blocksize.
Explanation:
-b
: Specifies the blocksize to use for reporting fragmentation. In this case, it is set to 1024 bytes.path/to/file
: The path to the file you want to analyze.
Example output:
Filesystem type is: ext4
File size of path/to/file is 1000000 (977 blocks of 1024 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 1023: 4096.. 5119: 1024:
1: 1024.. 2047: 5120.. 6143: 1024:
...
This output shows the fragmentation information using a 1024 byte blocksize. Each extent is defined with logical offsets, physical offsets, length, and flags.
Use case 4: Sync the file before requesting the mapping
Code:
filefrag -s path/to/files
Motivation: Syncing the file before requesting the mapping ensures that you are analyzing the most up-to-date version of the file. This is useful when you suspect that the file might have changed since the last analysis.
Explanation:
-s
: Syncs the file before requesting the mapping.path/to/files
: The path to the file(s) you want to analyze.
Example output:
Filesystem type is: ext4
File size of path/to/files is 5000000 (1221 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 2047: 4096.. 6143: 2048:
1: 2048.. 4095: 6144.. 8191: 2048:
...
This output provides the requested mapping after syncing the file. The report includes information about the extents, such as logical offsets, physical offsets, length, and flags.
Use case 5: Display mapping of extended attributes
Code:
filefrag -x path/to/files
Motivation: Extended attributes are additional metadata associated with a file. Analyzing the mapping of extended attributes can provide insights into the structure and organization of the file’s metadata.
Explanation:
-x
: Displays the mapping of extended attributes.path/to/files
: The path to the file(s) you want to analyze.
Example output:
Filesystem type is: ext4
File size of path/to/files is 5000000 (1226 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 2047: 4096.. 6143: 2048:
1: 2048.. 4095: 6144.. 8191: 2048:
...
Extended attributes for path/to/files:
0: 81120.. 81279 (160 blocks of 1024 bytes)
...
In addition to the regular fragmentation report, this output also includes the mapping of extended attributes. The extended attribute information shows the logical offsets, physical offsets, length, and flags for each extent.
Use case 6: Display a report with verbose information
Code:
filefrag -v path/to/files
Motivation: Sometimes, you need more detailed information about the fragmentation of a file. This use case allows you to generate a report that includes verbose information, providing a more comprehensive analysis of the file’s fragmentation status.
Explanation:
-v
: Displays a report with verbose information.path/to/files
: The path to the file(s) you want to analyze.
Example output:
Filesystem type is: ext4
File size of path/to/files is 5000000 (1228 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 2047: 4096.. 6143: 2048: last,eof
1: 2048.. 3202: 6144.. 7299: 1155: last,eof
...