BTRFS-FILESYSTEM(8)

๐Ÿ’– btrfs-filesystem(8) ๐Ÿ’–

okay bestie, welcome to the btrfs-filesystem page (manual name BTRFS), part of the btrfs fam. this one's a whole command group, and it's giving main character energy because it handles the big-picture stuff. buckle up ๐ŸŽ€


โœจ NAME โœจ

btrfs-filesystem โ€” a command group that primarily does work on the whole filesystems, no cap


๐ŸŒธ SYNOPSIS ๐ŸŒธ

btrfs filesystem <subcommand> <args>

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so btrfs filesystem is your girl for all the whole-filesystem-level tasks. like, we're talking every regular filesystem operation you could want: resizing, space stats, label setting/getting, and defragmentation. the whole menu ๐Ÿ’…

there are other whole-filesystem tasks โ€” like scrub or balance โ€” but those got grouped into their own separate commands, so go check btrfs-scrub(8) and btrfs-balance(8) for those. they're not in this house ๐Ÿ 


๐Ÿ‘‘ SUBCOMMAND ๐Ÿ‘‘

๐Ÿ’ซ df [options] <path> ๐Ÿ’ซ

shows you a terse summary of the allocation of block group types for a given mount point. lowkey important lore: the original purpose of this command was as a debugging helper. so likeโ€ฆ the output needs to be further interpreted and is not suitable for a quick overview. it's not a vibe check you can skim, bestie, it's homework ๐Ÿ˜ญ

an example with description:

  • device size: 1.9TiB, one device, no RAID
  • filesystem size: 1.9TiB
  • created with: mkfs.btrfs -d single -m single
$ btrfs filesystem df /path
Data, single: total=1.15TiB, used=1.13TiB
System, single: total=32.00MiB, used=144.00KiB
Metadata, single: total=12.00GiB, used=6.45GiB
GlobalReserve, single: total=512.00MiB, used=0.00B
  • Data, System and Metadata are separate block group types. GlobalReserve is an artificial and internal emergency space, see below ๐Ÿ‘‡
  • single โ€” the allocation profile, defined at mkfs time
  • total โ€” sum of space reserved for all allocation profiles of the given type, i.e. all Data/single. Note that it's not the total size of the filesystem, okay? don't get it twisted ๐Ÿ™ˆ
  • used โ€” sum of used space of the above, i.e. file extents, metadata blocks

GlobalReserve is an artificial and internal emergency space. it gets used e.g. when the filesystem is full. its total size is dynamic, based on the filesystem size, and it's usually not larger than 512MiB; used may fluctuate ๐Ÿซง

the GlobalReserve is a portion of Metadata. in case the filesystem metadata is exhausted:

GlobalReserve/total + Metadata/used = Metadata/total

otherwise, there appears to be some unused space of Metadata. math queen behavior โœจ

Options ๐Ÿ’…

  • -b, --raw โ€” raw numbers in bytes, without the B suffix
  • -h, --human-readable โ€” print human friendly numbers, base 1024, this is the default
  • -H โ€” print human friendly numbers, base 1000
  • --iec โ€” select the 1024 base for the following options, according to the IEC standard
  • --si โ€” select the 1000 base for the following options, according to the SI standard
  • -k, --kbytes โ€” show sizes in KiB, or kB with --si
  • -m, --mbytes โ€” show sizes in MiB, or MB with --si
  • -g, --gbytes โ€” show sizes in GiB, or GB with --si
  • -t, --tbytes โ€” show sizes in TiB, or TB with --si
if conflicting options are passed, the last one takes precedence. periodt.

๐Ÿงš defragment [options] <file>|<dir> [<file>|<dir>...] ๐Ÿงš

defragments file data on a mounted filesystem. requires kernel 2.6.33 and newer, so if you're on some ancient fossil, sorry bestie ๐Ÿ’€

if -r is passed, files in dir will be defragmented recursively (not descending to subvolumes, mount points and directory symlinks). the start position and the number of bytes to defragment can be specified by start and length using the -s and -l options below. extents bigger than the value given by -t will be skipped, otherwise this value is used as a target extent size โ€” but it's only advisory and may not be reached if the free space is too fragmented. use 0 to take the kernel default, which is 256KiB but may change in the future (so don't get attached ๐Ÿฅฒ). you can also turn on compression in defragment operations. iconic.

Warning โš ๏ธ

Defragmenting with Linux kernel versions < 3.9 or โ‰ฅ 3.14-rc2 as well as with Linux stable kernel versions โ‰ฅ 3.10.31, โ‰ฅ 3.12.12 or โ‰ฅ 3.13.4 will break up the reflinks of COW data (for example files copied with cp --reflink, snapshots or de-duplicated data). This may cause considerable increase of space usage depending on the broken up reflinks. not the vibe. read this twice, i'm serious ๐Ÿ˜ญ

Note ๐Ÿ“Œ

Directory arguments without -r do not defragment files recursively but will defragment certain internal trees (the extent tree and the subvolume tree). This has been confusing and could be removed in the future. so like, it's a weirdo, don't rely on it.

for start, len, size it is possible to append a units designator: K, M, G, T, P, or E, which represent KiB, MiB, GiB, TiB, PiB, or EiB, respectively (case does not matter, so go off with your lowercase era ๐Ÿ’…).

Options ๐Ÿ’–

  • -c[<algo>] โ€” compress file contents while defragmenting. the optional argument selects the compression algorithm: zlib (default), lzo or zstd. currently it's not possible to select no compression. see also the EXAMPLES section.
  • -L, --level <level> โ€” since kernel 6.15 the compression can also take the level parameter, which will be used only for the defragmentation and overrides the eventual mount option compression level. valid levels depend on the compression algorithms: zlib 1..9, lzo does not have any levels, zstd the standard levels 1..15 and also the realtime -1..-15.
  • --nocomp โ€” do not compress while defragmenting, uncompress extents if needed.
  • -r โ€” defragment files recursively in given directories, does not descend to subvolumes or mount points
  • -f โ€” flush data for each file before going to the next file.

this will limit the amount of dirty data to the current file, otherwise the amount accumulates from several files and will increase system load. this can also lead to ENOSPC if there's too much dirty data to write and it's not possible to make the reservations for the new data (i.e. how the COW design works). scary but real ๐Ÿซ 

  • -s <start>[kKmMgGtTpPeE] โ€” defragmentation will start from the given offset, default is the beginning of a file
  • -l <len>[kKmMgGtTpPeE] โ€” defragment only up to len bytes, default is the file size
  • -t <size>[kKmMgGtTpPeE] โ€” target extent size, do not touch extents bigger than size, default: 32MiB

the value is only advisory and the final size of the extents may differ, depending on the state of the free space and fragmentation or other internal logic. reasonable values are from tens to hundreds of megabytes (so like, 10M, 100M, that zone โœจ).

  • --step SIZE โ€” perform defragmentation in the range in SIZE steps and flush (-f) after each one. the range is default (the whole file) or given by -s and -l, split into the steps, or done in one go if the step is larger. minimum range size is 256KiB. with verbosity options the progress of defragmentation will also be printed ๐ŸŽ€
  • -v โ€” (deprecated) alias for the global -v option

๐ŸŒˆ du [options] <path> [<path>..] ๐ŸŒˆ

calculates disk usage of the target files using FIEMAP. for individual files, it will report a count of total bytes, and exclusive (not shared) bytes. we also calculate a 'set shared' value, which is described below ๐Ÿ‘‡

each argument to btrfs filesystem du will have a set shared value calculated for it. we define each set as those files found by a recursive search of an argument (recursion descends to subvolumes but not mount points). the set shared value then is a sum of all shared space referenced by the set.

set shared takes into account overlapping shared extents, hence it isn't as simple as adding up shared extents. it's giving complicated math girlie ๐Ÿงฎ

Options โญ

  • -s, --summarize โ€” display only a total for each argument
  • --raw โ€” raw numbers in bytes, without the B suffix.
  • --human-readable โ€” print human friendly numbers, base 1024, this is the default
  • --iec โ€” select the 1024 base for the following options, according to the IEC standard.
  • --si โ€” select the 1000 base for the following options, according to the SI standard.
  • --kbytes โ€” show sizes in KiB, or kB with --si.
  • --mbytes โ€” show sizes in MiB, or MB with --si.
  • --gbytes โ€” show sizes in GiB, or GB with --si.
  • --tbytes โ€” show sizes in TiB, or TB with --si.

๐ŸŽ€ label [<device>|<mountpoint>] [<newlabel>] ๐ŸŽ€

show or update the label of a filesystem. this works on a mounted filesystem or a filesystem image, so flexible queen ๐Ÿ’…

the newlabel argument is optional. the current label is printed if the argument is omitted.

Note ๐Ÿ™ˆ

The maximum allowable length shall be less than 256 chars and must not contain a newline. The trailing newline is stripped automatically. so no sneaky line breaks, bestie.

๐Ÿ“ mkswapfile [-s size] [-p pagesize] file ๐Ÿ“

creates a new file that's suitable and formatted as a swapfile. default size is 2GiB (minimum size is 40KiB), default page size is from the running kernel.

a swapfile must be created in a specific way: NOCOW and preallocated. a subvolume containing a swapfile cannot be snapshotted and blocks of an activated swapfile cannot be balanced. rules are rules ๐Ÿซถ

swapfile creation can be achieved by standalone commands too. activation needs to be done by command swapon(8) (https://man7.org/linux/man-pages/man8/swapon.8.html). see also command btrfs inspect-internal map-swapfile and the Swapfile feature description.

Note ๐Ÿ’œ

The command is a simplified version of mkswap. If you want to set label or other parameters please use mkswap proper. don't ask this one to do more than it does, it's doing its best ๐Ÿ˜ญ

Options ๐ŸŒธ

  • -p, --pagesize SIZE โ€” specify page size (k/m/g/e/p suffix), default is from the running kernel, must be a power of two, in the range 4K..64K
  • -s, --size SIZE โ€” create swapfile of a given size SIZE (accepting k/m/g/e/p suffix).
  • -U, --uuid UUID โ€” tell it which UUID to use, or hand it one of the magic words: clear (all zeros), random, or time (time-based random). ๐Ÿ’ซ

resize โ€” [options] [<devid>:][+/-]<size>[kKmMgGtTpPeE]|[<devid>:]max <path>

okay so this one resizes a mounted filesystem, and you point at it with path. Wanna resize just one particular device instead of the whole vibe? Then you say which devid. ๐Ÿ‘‘

๐Ÿšจ Note: if you're trying to resize a file that contains a BTRFS image, please use the --offline flag, bestie. don't skip that.

devid is findable in the output of btrfs filesystem show, and it defaults to 1 if you don't specify anything. The size parameter is the new size of the filesystem. If there's a + or - prefix, the size gets increased or decreased by the quantity size instead. And if you give no units at all? Bytes are assumed, fr fr. Optionally you can suffix size with one of these unit designators: K, M, G, T, P, or E, which mean KiB, MiB, GiB, TiB, PiB, or EiB respectively (case does not matter, so don't stress about caps ๐ŸŒธ).

If you pass max, the filesystem will occupy all available space on the device respecting devid (and remember, devid 1 is the default ๐Ÿ˜Œ).

Now the part everyone forgets: the resize command does not manipulate the size of the underlying partition. So if you wish to enlarge/reduce a filesystem, you must make sure you expand the partition before enlarging the filesystem, and shrink the partition after reducing the size of the filesystem. You do that with fdisk(8) or parted(8) โ€” delete the existing partition and recreate it with the new desired size. When recreating the partition make sure to use the same starting partition offset as before. โš ๏ธ literally the whole thing can go sideways if you don't.

The size of the portion that the filesystem uses of an underlying device can be determined via the btrfs filesystem show --raw command on the filesystem's mount point (where it's given for each devid after the string size), or via the btrfs inspect-internal dump-super command on the specific device (where it's given as the value of dev_item.total_bytes, which is not to be confused with total_bytes). That value is also the address of the first byte not used by the filesystem. ๐Ÿง 

Growing is usually instant since it only updates the size. However, shrinking could take a long time if there are data in the device area that's beyond the new end. Relocation of the data takes time, and it will make you wait, no cap. ๐Ÿ˜ญ

Note that there's a lower limit on the new size (either specified as an absolute size or a difference) that is checked by the kernel and rejected eventually as invalid. Lower values will print a warning but still pass the request to the kernel. The currently known value is 256MiB. ๐Ÿ‘‘

See also the EXAMPLES section. ๐Ÿ’…

Options

  • --enqueue โ€” wait if there's another exclusive operation running, otherwise continue.
  • --offline โ€” resize an unmounted (offline) filesystem.
๐Ÿซ  Warning: the offline resize functionality currently supports only increasing the size of single-device filesystems. IOW, shrinking and multi-device filesystems are not supported with this option.

For filesystems stored in regular files, the file will be truncated to the new size as part of the resize operation. This flag cannot be used together with --enqueue since offline resizing is synchronous. ๐Ÿ”’


show โ€” [options] [<path>|<uuid>|<device>|<label>]

Show the btrfs filesystem with some additional info about devices and space allocation. it's giving receipts. ๐Ÿงพโœจ

If you pass no option and none of path/uuid/device/label, information about all the BTRFS filesystems is shown, both mounted and unmounted. all of them. every single one. ๐Ÿ‘€

Options

  • -m, --mounted โ€” probe the kernel for mounted BTRFS filesystems.
  • -d, --all-devices โ€” scan all devices under /dev, otherwise the devices list is extracted from the /proc/partitions file. This is a fallback option if there's no device node manager (like udev) available in the system.
  • --raw โ€” raw numbers in bytes, without the B suffix.
  • --human-readable โ€” print human friendly numbers, base 1024, this is the default. ๐Ÿฅฐ
  • --iec โ€” select the 1024 base for the following options, according to the IEC standard.
  • --si โ€” select the 1000 base for the following options, according to the SI standard.
  • --kbytes โ€” show sizes in KiB, or kB with --si.
  • --mbytes โ€” show sizes in MiB, or MB with --si.
  • --gbytes โ€” show sizes in GiB, or GB with --si.
  • --tbytes โ€” show sizes in TiB, or TB with --si.

commit-stats

Print the number of commits and time stats since mount. This is also available in /sys/fs/btrfs/FSID/commit_stats. The stats are not persistent and are collected since mount of the filesystem, and one of them can be reset to zero (Max commit duration). ๐Ÿ•ฐ๏ธ

UUID: bd18ebe1-7e1d-414f-a3d5-2644388b51cc
Commit stats since mount:
  Total commits:                  1133
  Last commit duration:              0ms
  Max commit duration:              10ms
  Total time spent in commit:     4543ms

Options

  • -z, --reset โ€” print stats and reset max_commit_ms (needs root). ๐Ÿ”

sync <path>

Force a sync of the filesystem at path, similar to the sync(1) command. On top of that, it starts cleaning of deleted subvolumes. ๐Ÿงนโœจ To wait for the subvolume deletion to complete, use the btrfs subvolume sync command.


usage โ€” [options] <path> [<path>...]

Show detailed information about internal filesystem usage. This is supposed to replace the btrfs filesystem df command in the long run. the glow-up of df, if you will. ๐Ÿ’…

The level of detail can differ if the command is run under a regular user or the root user (due to use of restricted ioctl). For both there's a summary section with information about space usage:

$ btrfs filesystem usage /path
WARNING: cannot read detailed chunk info, RAID5/6 numbers will be incorrect, run as root
Overall:
    Device size:                   1.82TiB
    Device allocated:              1.17TiB
    Device unallocated:          669.99GiB
    Device missing:                  0.00B
    Device slack:                  1.00GiB
    Used:                          1.14TiB
    Free (estimated):            692.57GiB      (min: 692.57GiB)
    Free (statfs, df)            692.57GiB
    Data ratio:                       1.00
    Metadata ratio:                   1.00
    Global reserve:              512.00MiB      (used: 0.00B)
    Multiple profiles:                  no

And here's what every single one of those lil numbers actually means, so you're not just nodding along: ๐Ÿซถ

  • Device size โ€” sum of raw device capacity available to the filesystem. Note that this may not be the same as the total device size (the difference is accounted as slack).
  • Device allocated โ€” sum of total space allocated for data/metadata/system profiles. This also accounts for space reserved but not yet used for extents.
  • Device unallocated โ€” the remaining unallocated space for future allocations (difference of the above two numbers).
  • Device missing โ€” sum of capacity of all missing devices.
  • Device slack โ€” sum of slack space on all devices (difference between entire device size and the space occupied by filesystem).
  • Used โ€” sum of the used space of data/metadata/system profiles, not including the reserved space.
  • Free (estimated) โ€” approximate size of the remaining free space usable for data, including currently allocated space and estimating the usage of the unallocated space based on the block group profiles. The min is the lower bound of the estimate in case multiple profiles are present.
  • Free (statfs, df) โ€” the amount of space available for data as reported by the statfs/statvfs syscall, also returned as Avail in the output of df. The value is calculated in a different way and may not match the estimate in some cases (e.g. multiple profiles).
  • Data ratio โ€” ratio of total space for data including redundancy or parity to the effectively usable data space, e.g. single is 1.0, RAID1 is 2.0 and for RAID5/6 it depends on the number of devices.
  • Metadata ratio โ€” ditto, but for metadata.
  • Global reserve โ€” portion of metadata currently used for global block reserve, used for emergency purposes (like deletion on a full filesystem ๐Ÿ†˜).
  • Multiple profiles โ€” what block group types (data, metadata) have more than one profile (single, raid1, ...), see btrfs(5) section FILESYSTEMS WITH MULTIPLE PROFILES.

And on a zoned filesystem there are two more lines in the Device section:

Device zone unusable:          5.13GiB
Device zone size:            256.00MiB
  • Device zone unusable โ€” sum of space that's been used in the past but now is not due to COW and not referenced anymore. The chunks have to be reclaimed and zones reset to make it usable again.
  • Device zone size โ€” the reported zone size of the host-managed device, same for all devices.

The root user will also see stats broken down by block group types: ๐Ÿ‘‘

Data,single: Size:1.15TiB, Used:1.13TiB (98.26%)
   /dev/sdb        1.15TiB

Metadata,single: Size:12.00GiB, Used:6.45GiB (53.75%)
   /dev/sdb       12.00GiB

System,single: Size:32.00MiB, Used:144.00KiB (0.44%)
   /dev/sdb       32.00MiB

Unallocated:
   /dev/sdb      669.99GiB

Okay so quick vibe check on the columns you're gonna see: Data is the block group type, single is the block group profile, Size is the total size occupied by this type, Used is the actually used space, the percent is the ratio Used/Size, and Unallocated is whatever space is left over. Math but make it cute. ๐Ÿงฎโœจ

โœจ Options โœจ

  • -b, --raw โ€” raw numbers, in bytes, no B suffix, we're keeping it unfiltered ๐Ÿ’…
  • -h, --human-readable โ€” print human friendly numbers, base 1024, and this one is the default bestie ๐Ÿซถ
  • -H โ€” human friendly numbers but base 1000, switching it up โœจ
  • --iec โ€” select the 1024 base for the following options, according to the IEC standard
  • --si โ€” select the 1000 base for the following options, according to the SI standard
  • -k, --kbytes โ€” show sizes in KiB, or kB with --si
  • -m, --mbytes โ€” show sizes in MiB, or MB with --si
  • -g, --gbytes โ€” show sizes in GiB, or GB with --si
  • -t, --tbytes โ€” show sizes in TiB, or TB with --si
  • -T โ€” show data in tabular format, iconic honestly ๐Ÿ“Š
If conflicting options are passed, the last one takes precedence. Last one wins, no debates, that's just the rules ๐Ÿ”ฅ

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

$ btrfs filesystem defrag -v -r dir/

Recursively defragment files under dir/, and print files as they are processed. The file names get printed in batches, and likewise the amount of data triggered by defragmentation will be proportional to the last N printed files. Heads up tho: the system dirty memory throttling is gonna slow down the defragmentation, but there can still be a lot of IO load and the system may stall for a moment. It's giving dramatic main character energy but for your disk ๐Ÿ’€

$ btrfs filesystem defrag -v -r -f dir/

Recursively defragment files under dir/, be verbose, and wait until all blocks are flushed before moving on to the next file. Bestie you'll literally notice the progress in the output moving slower, and a lower IO load (proportional to the currently defragmented file). Chill pace, less chaos ๐Ÿง˜โ€โ™€๏ธ

$ btrfs filesystem defrag -v -r -f -clzo dir/

Recursively defragment files under dir/, be verbose, wait until all blocks are flushed, and force file compression. Compress and slay โœจ

$ btrfs filesystem defrag -v -r -t 64M dir/

Recursively defragment files under dir/, be verbose, and try to merge extents to be about 64MiB. But like, as stated above, the success rate depends on actual free space fragmentation and the final result is not guaranteed to meet the target even if you run it repeatedly. So don't come for me if it doesn't hit the number ๐Ÿ™ˆ

$ btrfs filesystem resize -1G /path
$ btrfs filesystem resize 1:-1G /path

Shrink the size of the filesystem's device id 1 by 1GiB. The first syntax expects a device with id 1 to exist, otherwise it just fails. The second is equivalent and more explicit. For a single-device filesystem it's typically not necessary to specify the devid though. Lowkey the short version is fine ๐Ÿซถ

$ btrfs filesystem resize max /path
$ btrfs filesystem resize 1:max /path

Okay let's assume devid 1 exists and the filesystem does not occupy the whole block device โ€” like, say it has been enlarged and we wanna grow the filesystem. By simply using max as the size we will achieve that. Go big or go home, periodt ๐Ÿ‘‘

Note: There are two ways to minimize the filesystem on a given device. The btrfs inspect-internal min-dev-size command, or iteratively shrink in steps. Pick your fighter ๐Ÿ’œ

๐Ÿ’– EXIT STATUS ๐Ÿ’–

btrfs filesystem returns a zero exit status if it succeeds. Non zero is returned in case of failure. Zero equals happy, non-zero equals we're not okay ๐Ÿ˜ญ


๐Ÿฆ‹ AVAILABILITY ๐Ÿฆ‹

btrfs is part of btrfs-progs. Please refer to the documentation at <https://btrfs.readthedocs.io>. Go read the docs, they ate ๐Ÿซถ


๐ŸŒธ SEE ALSO ๐ŸŒธ


And that's a wrap bestie!! This whole thing is from btrfs-progs version 7.3, dated Oct 24, 2026, and it's page BTRFS-FILESYSTEM(8) โ€” go forth and filesystem, you got this ๐Ÿ’…๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from