When working with Linux systems, you'll often need to identify which physical partition contains a particular directory. This is crucial for:
- Disk space management
- Filesystem maintenance
- Partition resizing operations
- Understanding storage architecture
The most straightforward solution is using the df
command with the directory path:
df -h /path/to/directory
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/nvme0n1p2 100G 45G 55G 45% /home
Using stat for Filesystem Information
For more detailed filesystem information:
stat -f -c %T /path/to/directory
Checking Mount Options
To see mount options for a partition:
findmnt -n -o OPTIONS --target /path/to/directory
Here's a bash function to display all relevant partition information:
#!/bin/bash
function show_partition_info() {
local dir=$1
echo "===== Partition Information for $dir ====="
df -h $dir | tail -n +2
echo -e "\nFilesystem type:"
stat -f -c %T $dir
echo -e "\nMount options:"
findmnt -n -o OPTIONS --target $dir
}
show_partition_info "/var/log"
Special scenarios to consider:
- Bind mounts (
mount --bind
) - Overlay filesystems
- Network filesystems (NFS, CIFS)
- Virtual filesystems (proc, sysfs)
For these cases, you might need:
findmnt -T /path -o SOURCE,TARGET,FSTYPE,OPTIONS
When querying frequently (e.g., in monitoring scripts):
- Cache results where possible
- Use
df -P
for predictable parsing - Consider
/proc/mounts
for low-level access
When managing Linux systems, administrators often need to identify the underlying storage partition for specific directories. This is crucial for:
- Disk space monitoring and management
- Filesystem maintenance operations
- Performance troubleshooting
- Storage capacity planning
The most straightforward solution is using the df
command with the directory path:
df -h /path/to/directory
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 50G 15G 33G 32% /home
For more detailed information, consider these approaches:
# 1. Using stat command
stat -c %m /path/to/directory
# 2. Combining findmnt with df
findmnt -n -o SOURCE --target /path/to/directory
# 3. For LVM volumes
lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,FSTYPE,MOUNTPOINT /path/to/directory
Here's how to implement this in scripts:
#!/bin/bash
TARGET_DIR="/var/log"
PARTITION=$(df --output=source "$TARGET_DIR" | tail -n 1)
echo "Directory $TARGET_DIR is on partition $PARTITION"
- For bind mounts: Use
findmnt -o TARGET,SOURCE
- For network filesystems: Check
mount
output
- For chroot environments: Prepend
chroot /path/to/chroot
When dealing with large directory structures:
# Faster alternative for recursive checks
time df --output=source /deep/nested/path >/dev/null