#!/bin/bash # Script to remove a Samba share # Usage: ./remove_samba_share.sh if [ "$#" -ne 1 ]; then echo "Usage: $0 " exit 1 fi SHARE_NAME="$1" SHARE_CONFIG_DIR="/etc/samba/shares.d" SMBCONF="/etc/samba/smb.conf" SHARE_CONFIG_FILE="$SHARE_CONFIG_DIR/$SHARE_NAME.conf" # 1. Remove share config file if [ -f "$SHARE_CONFIG_FILE" ]; then echo "Removing share config: $SHARE_CONFIG_FILE" rm -f "$SHARE_CONFIG_FILE" else echo "Warning: Share config file does not exist: $SHARE_CONFIG_FILE" fi # 2. Remove include line from smb.conf INCLUDE_LINE="include = $SHARE_CONFIG_FILE" if grep -Fxq "$INCLUDE_LINE" "$SMBCONF"; then echo "Removing include line from $SMBCONF" # Remove both the comment and the include line if present sed -i "/# Include share definition for $SHARE_NAME/d" "$SMBCONF" sed -i "\|$INCLUDE_LINE|d" "$SMBCONF" else echo "Include line not found in $SMBCONF" fi # 3. Restart Samba to apply changes echo "Restarting Samba service..." systemctl restart smbd && echo "Samba restarted successfully." || echo "Failed to restart Samba. Check logs."