Welcome to kriss.run!


Back

setowner

#!/usr/bin/env bash
# This script is used to set the owner of a file / directory


__setowner() {

    if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
        echo "Usage: setowner [user = $(whoami)] [path = .]"
        echo "Set the permissions of the files in the specified directory"
        echo "If no path is specified, the permissions of the current directory will be changed"
        return 1
    fi

    if [ $# -eq 0 ]; then
        read -p "No arguments provided. Do you want to set the owner to the current user ($(whoami)) and path to the current directory ($PWD)? (y/n): " confirm
        if [[ "$confirm" != [yY] ]]; then
            echo "Operation cancelled."
            return 1
        fi
    fi

    selected_user="${1}"
    if [ -z "$selected_user" ]; then
        selected_user="$(whoami)"
    fi

    selected_path="${2}"
    if [ -z "$selected_path" ]; then
        selected_path="${PWD}"
    fi

    if [ ! -e "$selected_path" ]; then
        echo "The specified path '$selected_path' does not exist!"
        return 1
    fi

    sudo chown -R "$selected_user" "$selected_path"

    echo "Permissions set successfully for user '$selected_user' in path '$selected_path'"
}

__setowner "$@"