Skip to content

Instantly share code, notes, and snippets.

@michaelmrose
Created July 24, 2025 19:19
Show Gist options
  • Save michaelmrose/e4cc0c33a9f4e3a64903ae8ca7e8888a to your computer and use it in GitHub Desktop.
Save michaelmrose/e4cc0c33a9f4e3a64903ae8ca7e8888a to your computer and use it in GitHub Desktop.
A bash script that mounts a fs to a path creating it if need be and infers its type and sets it to be owned by user and users group and writable by anyone
#!/bin/bash
set -e
# Usage message
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <device> <mount-point>"
exit 1
fi
DEVICE="$1"
MOUNT_POINT="$2"
# Validate device
if [ ! -b "$DEVICE" ]; then
echo "Error: $DEVICE is not a block device"
exit 1
fi
# Create mount point if it doesn't exist
mkdir -p "$MOUNT_POINT"
# Detect filesystem type
# FSTYPE=$(blkid -o value -s TYPE "$DEVICE")
FSTYPE=$(lsblk -no FSTYPE "$DEVICE")
if [ -z "$FSTYPE" ]; then
echo "Error: Could not detect filesystem type for $DEVICE"
exit 1
fi
# Get user and group info
FSUID=$(id -u)
FSGID=$(getent group users | cut -d: -f3)
if [ -z "$FSGID" ]; then
echo "Group 'users' not found, falling back to your primary group"
GID=$(id -g)
fi
# Assemble mount options
OPTS="uid=$FSUID,gid=$FSGID,umask=000"
echo "Mounting $DEVICE as $FSTYPE to $MOUNT_POINT with options: $OPTS"
# Perform the mount
sudo mount -t "$FSTYPE" -o "$OPTS" "$DEVICE" "$MOUNT_POINT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment