Skip to content

Instantly share code, notes, and snippets.

@nilreml
Last active December 10, 2024 14:57
Show Gist options
  • Save nilreml/8c34df79fe203baa9951f606dd82c2e5 to your computer and use it in GitHub Desktop.
Save nilreml/8c34df79fe203baa9951f606dd82c2e5 to your computer and use it in GitHub Desktop.
check for bash version 4+ (POSIX-compliant) :
[ -z "${BASH_VERSINFO}" ] || [ -z "${BASH_VERSINFO[0]}" ] || [ ${BASH_VERSINFO[0]} -lt 4 ]
path to script even if symlinked:
SCRIPT_DIR="$( cd -P "$( dirname "$(readlink -f "${BASH_SOURCE[0]}")" )" >/dev/null 2>&1 && pwd )"
#!/usr/bin/env bash
#
# Test bed for a recursive chmod one-liner
#
# 1. Generate a set of test directories and files using improper permissions
# 2. Run a recursive chmod to heal
# 3. Show before, fixes and after for inspection
#
# Cleaning up by diligently running `sudo rm -rf ./data` is left as an exercise for the reader
#
set -eu
declare -a modes=("400" "422" "600" "644" "755" "777")
# `apt install lsd`, see https://github.com/lsd-rs/lsd
tree() { lsd -l -g --tree "$@"; }
mkdir data
cd data
for dir_mode in "${modes[@]}"; do {
dir="d${dir_mode}"
mkdir "${dir}"
cd "${dir}"
for file_mode in "${modes[@]}"; do {
file=f"${file_mode}"
echo "${file_mode}" > "${file}"
chmod "${file_mode}" "${file}"
} done
cd ..
chmod "${dir_mode}" "${dir}"
} done
cd ..
# Before - fails to look inside directories not having the executable bit set
tree -a || true
# chmod
chmod -vR a=r-wx,u=wr,a+X data/
# After
tree -a || true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment