This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Own.sh

31 lines
690 B
Bash
Raw Normal View History

2023-12-07 04:02:47 +03:00
# Change file ownership to specified user id and restrict access to him.
# Root user by default. This directory recursively by default.
# Usage: own [USER] [FILES]
2023-12-07 01:44:42 +03:00
function own() {
2023-12-10 04:18:37 +03:00
local IFS=$'\n'
local files=("${@:2}")
2023-12-05 21:50:45 +03:00
local user="${1}"
2024-01-29 04:12:31 +03:00
local group="${1}"
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
# default to current dir.
2023-12-10 04:18:37 +03:00
if [ "${files[*]}" = "" ]; then
files=(".")
2023-12-05 21:50:45 +03:00
fi
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
# default to current user.
if [ "${user}" = "" ]; then
user="${UID}"
fi
2023-08-08 16:24:15 +03:00
2024-01-29 04:12:31 +03:00
# If not root, default to users group.
2024-02-02 14:03:43 +03:00
[[ "${user}" = 0 ]] && group="0" || group="100"
2024-01-29 04:12:31 +03:00
2023-12-10 04:18:37 +03:00
for file in "${files[@]}"; do
# set ownership.
2024-01-29 04:12:31 +03:00
chown "${user}":"${group}" -R "${file}" &> /dev/null
2023-08-08 16:24:15 +03:00
2023-12-10 04:18:37 +03:00
# remove access from group and others.
chmod -077 -R "${file}"
done
2023-08-08 16:24:15 +03:00
}