bhylu.com

Advertisements

Bash Permissions, chmod and Ownership

Understand Unix mode bits before changing access.

How it works

Unix permissions distinguish owner, group and other access, with different meanings for files and directories. A directory’s execute bit permits traversal; it is not the same as making a text file executable. chmod changes mode bits, while chown and chgrp change ownership subject to privilege rules. Windows ACLs are different, so Git Bash cannot establish full POSIX permission semantics for an NTFS file.

Run the code

Reviewed with Git for Windows Bash 5.2.37. Open Git Bash, save the download with LF line endings in a separate learning folder, check it with bash -n lesson-name.sh, then run bash lesson-name.sh using the actual filename. Bash 4 or newer is required for the full course. Linux-only notes identify unavailable tools or POSIX behaviour. There is no Bash browser runner; Apache and MySQL are not required.

This Linux walkthrough was syntax-checked only: the required tool or POSIX behaviour was not available in the Windows review. Its expected section describes the intended result, not captured execution.

The complete source creates a fresh private temporary workspace and checks its physical path before cleanup. All file fixtures belong to that workspace; do not remove the guards or point it at existing documents.

Download complete source

Example

permissions.sh

#!/usr/bin/env bash
set -u
set -o pipefail
export LC_ALL=C

# Own a new directory; verify physical paths before cleanup.
parent=$(cd -- "${TMPDIR:-/tmp}" && pwd -P) || exit 1
work=$(mktemp -d "$parent/bhylu-bash.XXXXXX") || exit 1
work=$(cd -- "$work" && pwd -P) || exit 1
case "$work" in "$parent"/bhylu-bash.??????) ;; *) exit 1 ;; esac
cleanup() {
    cd -- "$parent" || return 1
    [[ -d $work && ! -L $work ]] || return 1
    [[ $(cd -- "$work" && pwd -P) == "$work" ]] || return 1
    case "$work" in "$parent"/bhylu-bash.??????) rm -r -- "$work" ;; *) return 1 ;; esac
}
trap cleanup EXIT
cd -- "$work" || exit 1

file="private-note.txt"
printf "synthetic note\n" > "$file"
chmod u=rw,go= -- "$file"
ls -l -- "$file"
printf "Review owner/group with id and ls before any chown or chgrp change.\n"

Focus on the lesson commands

This excerpt omits temporary-workspace setup and cleanup. Use the complete source above to run the example.

Command excerpt

file="private-note.txt"
printf "synthetic note\n" > "$file"
chmod u=rw,go= -- "$file"
ls -l -- "$file"
printf "Review owner/group with id and ls before any chown or chgrp change.\n"

Expected result

On a POSIX learning filesystem, the note is intended to be owner-readable/writable with no group/other permissions. Inspect the actual mode and identity. Syntax checked here; Linux permission enforcement and ownership changes were not executed.

The listing changes only its newly created note, and requires a POSIX learning environment for the intended access model. It intentionally performs no administrator ownership change. If another identity must access the file, define the group and directory traversal policy as well as the file mode. A numeric mode alone does not describe every ACL or mount setting.

Primary technical reference: Bash documentation.

Advertisements