findmnt 命令详解与参数实测(Ubuntu 22.04)

findmnt 命令逐参数实测详解:默认树状输出、精确查询与退出码、--target 反查路径所属挂载、-t 类型过滤、-o 自定义列、-D df 风格人类可读输出,附 mount/findmnt/df/lsblk 工具对比,全部命令与回显在 Ubuntu 22.04(util-linux 2.37.2)实测通过。

Linux命令Linux系统UbuntuUbuntu 22.04命令行文件管理系统信息2026/10/15

findmnt 命令详解与参数实测(Ubuntu 22.04)

目标:全面掌握 findmnt 命令——默认树状视图、精确查询与退出码、--target 反查路径所属挂载、-t 类型过滤、-o 自定义列、-D df 风格人类可读输出,全部命令与回显已在 Ubuntu 22.04(util-linux 2.37.2)实测通过。

一、环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(x86_64)
  • 内核版本:6.8.0-138-generic(uname -r 实测)
  • findmnt 版本:util-linux 2.37.2(findmnt --version 实测)
  • 测试介质:/root/demo/img/ext4.img(dd 创建的 64 MiB 映像)→ losetup 关联 /dev/loop17 → mkfs.ext4 格式化 → 挂载 /mnt/ext4-demo

二、命令概览

2.1 语法与实测环境准备

findmnt 的语法核心是三种:findmnt [options](列全部)、findmnt [options] <device> | <mountpoint>(精确查询)、findmnt [options] --target <path>(反查路径所属挂载)。准备一块 ext4 测试盘并挂载:

💡 技术拓展:"查看当前有哪些挂载"有多个来源,信息颗粒度横向对比——

工具原理/职责适用场景
findmnt读取挂载表并以树形/表格呈现,支持按列过滤需要结构化、可过滤的挂载信息
mount(无参)直接打印挂载表,纯文本行快速肉眼浏览
cat /proc/mounts内核视角的原始挂载表排查内核实际记录(最权威)
df -h挂载点 + 容量使用率视角关心空间而非挂载属性

本文主角是 findmnt:util-linux 专用挂载查询工具,树形展示 + 精确过滤是它对纯文本来源的最大优势。

$ mkdir -p /root/demo/img

$ dd if=/dev/zero of=/root/demo/img/ext4.img bs=1M count=64
64+0 records in
64+0 records out
67108864 bytes (67 MB, 64 MiB) copied, 0.276135 s, 243 MB/s

$ losetup -f --show /root/demo/img/ext4.img
/dev/loop17

$ mkfs.ext4 -F /dev/loop17
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks:     0/16384           done
Creating filesystem with 16384 4k blocks and 16384 inodes

Allocating group tables: 0/1   done
Writing inode tables: 0/1   done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: 0/1   done


$ mkdir -p /mnt/ext4-demo

$ mount /dev/loop17 /mnt/ext4-demo

$ echo demo > /mnt/ext4-demo/hello.txt

2.2 --help 全量回显

findmnt --help 输出全部参数与可用输出列(实测原样保留,节选关键部分):

Usage:
 findmnt [options]
 findmnt [options] <device> | <mountpoint>
 findmnt [options] <device> <mountpoint>
 findmnt [options] [--source <device>] [--target <path> | --mountpoint <dir>]

Find a (mounted) filesystem.

Options:
 -s, --fstab            search in static table of filesystems
 -m, --mtab             search in table of mounted filesystems
                          (includes user space mount options)
 -k, --kernel           search in kernel table of mounted
                          filesystems (default)

 -p, --poll[=<list>]    monitor changes in table of mounted filesystems
 -w, --timeout <num>    upper limit in milliseconds that --poll will block

 -A, --all              disable all built-in filters, print all filesystems
 -a, --ascii            use ASCII chars for tree formatting
 -b, --bytes            print sizes in bytes rather than in human readable format
 -C, --nocanonicalize   don't canonicalize when comparing paths
 -c, --canonicalize     canonicalize printed paths
 -D, --df               imitate the output of df(1)
 -d, --direction <word> direction of search, 'forward' or 'backward'
 -e, --evaluate         convert tags (LABEL,UUID,PARTUUID,PARTLABEL) 
                          to device names
 -F, --tab-file <path>  alternative file for -s, -m or -k options
 -f, --first-only       print the first found filesystem only
 -i, --invert           invert the sense of matching
 -J, --json             use JSON output format
 -l, --list             use list format output
 -N, --task <tid>       use alternative namespace (/proc/<tid>/mountinfo file)
 -n, --noheadings       don't print column headings
 -O, --options <list>   limit the set of filesystems by mount options
 -o, --output <list>    the output columns to be shown
     --output-all       output all available columns
 -P, --pairs            use key="value" output format
     --pseudo           print only pseudo-filesystems
     --shadowed         print only filesystems over-mounted by another filesystem
 -R, --submounts        print all submounts for the matching filesystems
 -r, --raw              use raw output format
     --real             print only real filesystems
 -S, --source <string>  the device to mount (by name, maj:min, 
                          LABEL=, UUID=, PARTUUID=, PARTLABEL=)
 -T, --target <path>    the path to the filesystem to use
     --tree             enable tree format output if possible
 -M, --mountpoint <dir> the mountpoint directory
 -t, --types <list>     limit the set of filesystems by FS types
 -U, --uniq             ignore filesystems with duplicate target
 -u, --notruncate       don't truncate text in columns
 -v, --nofsroot         don't print [/dir] for bind or btrfs mounts

 -x, --verify           verify mount table content (default is fstab)
     --verbose          print more details
     --vfs-all          print all VFS options

 -h, --help             display this help
 -V, --version          display version

--help 后半段是 Available output columns 全表(ACTION/AVAIL/FREQ/FSTYPE/FS-OPTIONS/ID/LABEL/MAJ:MIN/OPTIONS/PARTUUID/PROPAGATION/SIZE/SOURCE/TARGET/UUID 等 30 余列),配合 -o 使用;--output-all 可一次输出全部列。

💡 说明:findmnt 默认读内核挂载表(-k),-s 查 fstab 静态配置,-m 查含用户态选项的挂载表——三张"表"用一套参数查询是它的设计核心。

2.3 版本确认

$ findmnt --version
findmnt from util-linux 2.37.2

三、参数逐项实测

3.1 默认树状输出

不带参数,findmnt 按挂载层级画树(输出节选(实测):根文件系统行与 3 个 bind 挂载行(/etc/hostname、/etc/hosts、/etc/resolv.conf)与本文主题无关已省略,剩余行内容与实测一致、为便于阅读重新对齐):

$ findmnt
TARGET             SOURCE      FSTYPE  OPTIONS
|-/proc            proc        proc    rw,nosuid,nodev,noexec,relatime
|-/dev             tmpfs       tmpfs   rw,nosuid,size=65536k,mode=755,inode64
| |-/dev/pts       devpts      devpts  rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666
| |-/dev/mqueue    mqueue      mqueue  rw,nosuid,nodev,noexec,relatime
| `-/dev/shm       shm         tmpfs   rw,nosuid,nodev,noexec,relatime,size=65536k,inode64
|-/sys             sysfs       sysfs   rw,nosuid,nodev,noexec,relatime
| `-/sys/fs/cgroup cgroup      cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot
`-/mnt/ext4-demo   /dev/loop17 ext4    rw,relatime

树状缩进直接表达挂载父子关系:/dev/shm 挂在 tmpfs /dev 之上,/mnt/ext4-demo 是顶层挂载。比起 mount 的平铺文本,层级一眼可见。

3.2 精确查询挂载点与退出码

参数给挂载点路径即精确查询;查不存在的结果为空、退出码 1:

$ findmnt /mnt/ext4-demo
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/ext4-demo /dev/loop17 ext4   rw,relatime

$ findmnt /mnt/ghost; echo exit=$?
exit=1

💡 说明:退出码 0=查到、1=没查到,这让 findmnt 成为脚本里判断"目录是否已挂载"的标准工具(见实战一)。

3.3 --target:反查任意路径所属挂载

--target 不要求参数是挂载点本身——给它挂载点下的任意文件/子目录,它反查出所在文件系统:

$ findmnt --target /mnt/ext4-demo
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/ext4-demo /dev/loop17 ext4   rw,relatime

$ findmnt --target /mnt/ext4-demo/hello.txt
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/ext4-demo /dev/loop17 ext4   rw,relatime

直接路径参数(3.2)与 --target 的分工:前者只匹配挂载点本身,后者从任意路径向上找最近挂载。"这个文件落在哪个文件系统上"就用 --target。

3.4 -t:按文件系统类型过滤

$ findmnt -t ext4
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/ext4-demo /dev/loop17 ext4   rw,relatime

$ findmnt -t tmpfs
TARGET     SOURCE FSTYPE OPTIONS
/dev       tmpfs  tmpfs  rw,nosuid,size=65536k,mode=755,inode64
`-/dev/shm shm    tmpfs  rw,nosuid,nodev,noexec,relatime,size=65536k,inode64

-t ext4 只列 ext4 挂载(实测另过滤出 3 条 ext4 bind 挂载行,已省略),-t tmpfs 树状列出两处 tmpfs。-t 支持逗号分隔多类型与 no 前缀反选(如 -t nosquashfs)。

3.5 -o:自定义输出列

-o 逗号组合 2.2 中的任意列名,默认输出其实就等价于 -o TARGET,SOURCE,FSTYPE,OPTIONS:

$ findmnt -o TARGET,SOURCE,FSTYPE,OPTIONS
TARGET             SOURCE      FSTYPE  OPTIONS
|-/proc            proc        proc    rw,nosuid,nodev,noexec,relatime
|-/dev             tmpfs       tmpfs   rw,nosuid,size=65536k,mode=755,inode64
| |-/dev/pts       devpts      devpts  rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666
| |-/dev/mqueue    mqueue      mqueue  rw,nosuid,nodev,noexec,relatime
| `-/dev/shm       shm         tmpfs   rw,nosuid,nodev,noexec,relatime,size=65536k,inode64
|-/sys             sysfs       sysfs   rw,nosuid,nodev,noexec,relatime
| `-/sys/fs/cgroup cgroup      cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot
`-/mnt/ext4-demo   /dev/loop17 ext4    rw,relatime

💡 说明:与 3.1 的差别只在形式——这里显式声明了列组合。要加 UUID、MAJ:MIN、PROPAGATION 等列,直接往 -o 里追加列名即可(实测环境中根文件系统行与 3 条 bind 行已按 3.1 同样规则省略)。

3.6 -D:df 风格人类可读

-D 模仿 df(1) 的输出——列变成 SIZE/USED/AVAIL/USE%,容量自动换算人类可读单位:

$ findmnt -D
SOURCE      FSTYPE   SIZE   USED  AVAIL USE% TARGET
tmpfs       tmpfs     64M      0    64M   0% /dev
shm         tmpfs     64M      0    64M   0% /dev/shm
/dev/loop17 ext4      56M    28K  51.5M   0% /mnt/ext4-demo

/ext4-demo 这块 64 MiB 的盘格式化后可用 51.5M,已用 28K(就是 hello.txt 与文件系统元数据)。实测输出中另有 3 条 /dev/sda3 bind 挂载行已省略,且根文件系统条目未出现在 -D 列表中(实测如此)。

四、实战示例

实战一:脚本判断目录是否已挂载

退出码是 findmnt 的脚本价值所在:

if findmnt -n /mnt/ext4-demo >/dev/null; then
  echo "already mounted"
else
  mount /dev/loop17 /mnt/ext4-demo
fi

-n(--noheadings)去掉表头,只看退出码与 TARGET 列。实测查询不存在时 exit=1(3.2)、存在时 exit=0(3.3)。

实战二:查一个文件属于哪个文件系统

拿到任意路径,反查它落在哪个设备、什么类型:

findmnt --target /mnt/ext4-demo/hello.txt   # 回显见 3.3

排查磁盘空间问题时,先 df -h 看哪个文件系统满,再用 --target 确认目录与文件系统的对应关系。

五、注意事项与常见问题

Q1:findmnt 与不带参数的 mount 有什么区别? 两者读的是同一份内核挂载表,差别在呈现与可用性:mount 输出 源 on 挂载点 type 类型 (选项) 的原始文本行;findmnt 输出对齐表格与树状层级,且支持 -t/-o/-D/-P/-J 过滤与格式化,退出码可直接用于脚本判断。查询场景一律优先 findmnt。

Q2:直接给路径参数与 --target 有什么区别? 路径参数只做精确匹配(该路径必须就是挂载点,否则 exit=1);--target 做归属查找——给定挂载点下的任意文件或子目录,向上找最近一次挂载。前者答"这是不是挂载点",后者答"这文件在哪个文件系统上"。

Q3:输出里的 -s、-m、-k 三张表怎么选? 默认 -k 读内核挂载表;-s 读 /etc/fstab 静态配置(排启动挂载问题);-m 读含用户态挂载选项的表。改完 fstab 想验证语法正确性,用 findmnt --verify 或 -s 查询。

与相邻工具的分工对比

工具定位输出形式典型用途
mount(无参数)原始挂载表源 on 挂载点 type 类型 (选项) 文本快速人工浏览
findmnt挂载表查询与过滤对齐表格/树/JSON,退出码语义化脚本判断、精确查询、反查归属
df空间视角按文件系统列容量与使用率看剩余空间
lsblk设备拓扑视角块设备树(盘→分区→挂载点)看硬件与分区结构

同一份挂载事实,四个工具四种视角:findmnt 是其中唯一自带"查询语义"(过滤 + 退出码 + 反查)的。

六、参考链接