Skip to content

Instantly share code, notes, and snippets.

@paranlee
Created May 16, 2024 14:46
Show Gist options
  • Save paranlee/46f54a692bd08d8187a5f03a7eb1e3b9 to your computer and use it in GitHub Desktop.
Save paranlee/46f54a692bd08d8187a5f03a7eb1e3b9 to your computer and use it in GitHub Desktop.
The provided bash script is designed to check write permission for regular files only. It filters out directories and symbolic links by using the `-type f` option with find. This option ensures that only files with the regular file type are processed.
#!/bin/bash
# Target path to check (passed as argument)
target_path="$1"
# Check if target path is empty
if [ -z "$target_path" ]; then
echo "Usage: $0 <target path>"
exit 1
fi
# Check if target path exists and is a directory
if [ ! -d "$target_path" ]; then
echo "Error: Path '$target_path' does not exist or is not a directory."
exit 1
fi
# Check if user has sufficient permissions to read files
if [ ! -r "$target_path" ]; then
echo "Error: Insufficient permissions to read files in '$target_path'."
exit 1
fi
# Function to check write permission for other users
check_write_permission() {
local file="$1"
file_permission=$(stat -c "%a" "$file")
other_write_permission=$(( file_permission & 7 ))
# Check if other users have write permission (2 in octal)
if [ $other_write_permission -eq 2 ]; then
ls -ahl "$file"
fi
}
# Find all files in the target path and check write permission
find "$target_path" -type f -print 2>/dev/null | while read -r file; do
check_write_permission "$file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment