Mastering the Findmnt Command - A Comprehensive Guide

January 24, 2024 (9 months ago)
TuxDigest
4 min read

The findmnt command is a powerful tool in Linux for discovering and analyzing mounted filesystems.

Findmnt provides a wealth of information about your system's mount points, making it an essential utility for system administrators and power users alike. In this guide, we'll explore the various features and use cases of findmnt, demonstrating how it can simplify filesystem management tasks.

Basic Usage

At its simplest, running findmnt without any arguments will display all mounted filesystems in a tree-like structure[1]:

findmnt

This command provides a clear overview of your system's mount points, including the following information:

  • TARGET: The mount point location
  • SOURCE: The source device or filesystem
  • FSTYPE: The filesystem type
  • OPTIONS: Mount options used

Customizing Output Format

List Format

To display the output in a list format instead of the default tree structure, use the -l option[2]:

findmnt -l

DF-Style Output

For those familiar with the df command, findmnt can mimic its output style using the -D or --df option[2]:

findmnt -D

This format includes information about disk space usage, similar to the df command.

Filtering Results

By Filesystem Type

To filter results by filesystem type, use the -t option followed by the desired filesystem type[2]:

findmnt -t ext4

This command will display only ext4 filesystems. You can specify multiple types by separating them with commas:

findmnt -t ext4,nfs

By Mount Point

To search for a specific mount point, use the -T option[3]:

findmnt -T /boot

This will display information about the filesystem mounted at /boot.

By Source Device

To find information about a specific device, use the -S option[4]:

findmnt -S /dev/sda1

Advanced Features

Reading from /etc/fstab

To display filesystem information from /etc/fstab, use the -s or --fstab option[2]:

findmnt -s

This is particularly useful for viewing configured mounts that may not be currently mounted.

Polling for Changes

One of findmnt's most powerful features is its ability to monitor for filesystem changes in real-time. This is achieved using the --poll option[1].

To monitor for any mount, unmount, or remount actions:

findmnt --poll

To monitor for specific actions, you can specify them:

findmnt --poll=mount,umount

This feature is incredibly useful for scripting and automation tasks related to removable media.

Custom Output Columns

You can customize the output columns using the -o option. For example, to display only the source and target:

findmnt -o SOURCE,TARGET

Practical Use Cases

  1. Checking if a Specific Filesystem is Mounted:
findmnt /mnt/external_drive

If the filesystem is mounted, findmnt will display its details. If not, it will return nothing.

  1. Monitoring USB Drive Insertion:
findmnt --poll=mount --first-only

This command will wait for the next mount event, which is useful for detecting when a USB drive is plugged in.

  1. Listing All Read-Only Filesystems:
findmnt -O ro

This command filters filesystems by the read-only mount option.

  1. Checking Available Space on Root Partition:
findmnt -T / --df -o AVAIL -n

This command displays the available space on the root partition without headers[1].

Conclusion

The findmnt command is a versatile and powerful tool for managing and monitoring filesystems in Linux. Its ability to provide detailed information about mount points, filter results, and monitor for changes makes it an invaluable utility for system administration tasks. By mastering findmnt, you can streamline your workflow and gain deeper insights into your system's filesystem structure.

Whether you're troubleshooting mount issues, scripting automated responses to filesystem changes, or simply exploring your system's configuration, findmnt offers a comprehensive solution that goes beyond traditional tools like mount and df. Incorporate it into your Linux toolkit, and you'll find yourself with a more efficient and informed approach to filesystem management.

Citations: [1] https://linuxhandbook.com/findmnt-command-guide/ [2] https://www.binarytides.com/linux-findmnt-command/ [3] https://man7.org/linux/man-pages/man8/findmnt.8.html [4] https://www.geeksforgeeks.org/findmnt-find-all-mounted-filesystems-on-linux/ [5] https://www.howtogeek.com/774913/how-to-use-the-findmnt-command-on-linux/