Need help displaying file/folder permissions in clear text
Solution 1:
This script will do the trick (I called the script filestat
- put it in your path):
#!/bin/bash
# Iterate over each argument
for file in "$@"; do
perm_type=('User' 'Group' 'Other')
(( j = 0 ))
# Check if file exists
if [[ -e "$file" ]]; then
# Print filename
echo -e "\nFilename: $file"
# Isolate permission octet
perm_octet=$( stat -c "%a %n" "$file" | cut -d ' ' -f 1 )
# Add each value of octet to array
perm_array=()
for (( i = 0; i < "${#perm_octet}"; i++ )); do
perm_array+=("${perm_octet:$i:1}")
done
# Iterate over array
for x in "${perm_array[@]}"; do
# Print permission type and increase counter
echo -n "${perm_type[$j]} permission: "
(( j++ ))
# Check if permission is zero (none), print and start next iteration
if (( "$x" == 0 )); then
echo "NONE "
continue
fi
# Check if permission has "read", print and subtract 4
if (( "$x" > 3 )); then
echo -n "read "
(( x = x - 4 ))
fi
# Check if permission has "write", print and subtract 2
if (( "$x" > 1 )); then
echo -n "write "
(( x = x - 2 ))
fi
# Check if permission has "execute", print and subtract 1
if (( "$x" > 0 )); then
echo -n "execute "
(( x = x - 1 ))
fi
echo ""
done
fi
done
EDIT: Takes any number of files as input, and checks if the file exists. Example output:
$ filestat ~/.bashrc ~/.config
Filename: /home/am/.bashrc
User permission: read write
Group permission: read write
Other permission: read
Filename: /home/am/.config
User permission: read write execute
Group permission: NONE
Other permission: NONE