Score:1

Need help displaying file/folder permissions in clear text

cn flag

I need help displaying permissions in clear and understandable text.

I.e. rwx-wx--- as:

User permission: read, write, execute
Group permission: write, execute 
Other permission: No permission

I need this script to make it easier to understand for non-technical users.

Score:4
vn flag

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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.