Linux Lite Forums

Development => Scripting and Bash => Topic started by: anon222 on May 09, 2015, 12:01:11 AM

Title: File permissions
Post by: anon222 on May 09, 2015, 12:01:11 AM
A script for the thunar custom action that displays the file permissions and can change them.
Technicaly, it will work with directories but will not change them recursively. The files and directories inside will remain unchanged.
Also it works only for a single file.
This is not something I would recommend to someone who doen't understand file permissions.
Be carefull about symbolic links! Never use this on them! chmod changes the permissions of the pointed-to file.
Thunar custom action
Name:
Code: [Select]
PermissionsDescription:
Code: [Select]
Displays and changes file permissionsCommand:
Code: [Select]
/path/to/the/script %nAppearance conditions:
Pattern:
Code: [Select]
*Everything

The script:
Code: [Select]
#!/bin/bash
file="$@"

PERM="$(stat --printf=%a%A "$file")"
USER_="${PERM:0:1}"

if [[ $USER_ = 7 ]]; then TF1="TRUE"; TF2="TRUE"; TF3="TRUE"
  fi
if [[ $USER_ = 6 ]]; then  TF1="TRUE"; TF2="TRUE"; TF3="FALSE"
  fi
if [[ $USER_ = 5 ]]; then TF1="TRUE"; TF2="FALSE"; TF3="TRUE"
  fi
if [[ $USER_ = 4 ]]; then TF1="TRUE"; TF2="FALSE"; TF3="FALSE"
  fi
if [[ $USER_ = 3 ]]; then TF1="FALSE"; TF2="TRUE"; TF3="TRUE"
  fi
if [[ $USER_ = 2 ]]; then TF1="FALSE"; TF2="TRUE"; TF3="FALSE"
  fi
if [[ $USER_ = 1 ]]; then TF1="FALSE"; TF2="FALSE"; TF3="TRUE"
  fi
if [[ $USER_ = 0 ]]; then TF1="FALSE"; TF2="FALSE"; TF3="FALSE"
  fi

GRP_="${PERM:1:1}"
if [[ $GRP_ = 7 ]]; then TF4="TRUE"; TF5="TRUE"; TF6="TRUE"
  fi
if [[ $GRP_ = 6 ]]; then  TF4="TRUE"; TF5="TRUE"; TF6="FALSE"
  fi
if [[ $GRP_ = 5 ]]; then TF4="TRUE"; TF5="FALSE"; TF6="TRUE"
  fi
if [[ $GRP_ = 4 ]]; then TF4="TRUE"; TF5="FALSE"; TF6="FALSE"
  fi
if [[ $GRP_ = 3 ]]; then TF4="FALSE"; TF5="TRUE"; TF6="TRUE"
  fi
if [[ $GRP_ = 2 ]]; then TF4="FALSE"; TF5="TRUE"; TF6="FALSE"
  fi
if [[ $GRP_ = 1 ]]; then TF4="FALSE"; TF5="FALSE"; TF6="TRUE"
  fi
if [[ $GRP_ = 0 ]]; then TF4="FALSE"; TF5="FALSE"; TF6="FALSE"
  fi

ALL_="${PERM:2:1}"
if [[ $ALL_ = 7 ]]; then TF7="TRUE"; TF8="TRUE"; TF9="TRUE"
  fi
if [[ $ALL_ = 6 ]]; then  TF7="TRUE"; TF8="TRUE"; TF9="FALSE"
  fi
if [[ $ALL_ = 5 ]]; then TF7="TRUE"; TF8="FALSE"; TF9="TRUE"
  fi
if [[ $ALL_ = 4 ]]; then TF7="TRUE"; TF8="FALSE"; TF9="FALSE"
  fi
if [[ $ALL_ = 3 ]]; then TF7="FALSE"; TF8="TRUE"; TF9="TRUE"
  fi
if [[ $ALL_ = 2 ]]; then TF7="FALSE"; TF8="TRUE"; TF9="FALSE"
  fi
if [[ $ALL_ = 1 ]]; then TF7="FALSE"; TF8="FALSE"; TF9="TRUE"
  fi
if [[ $ALL_ = 0 ]]; then TF7="FALSE"; TF8="FALSE"; TF9="FALSE"
  fi

STAT_="${PERM:3:13}"
ans=$(zenity  --height=450 --width=350 --list  --text "File:\n<b>$file</b>\nPermissions:\n<b>${STAT_}</b>\nchange files permissions" --checklist  --column "pick" --column "options" \
"$TF1" "user-read" "$TF2" "user-write" "$TF3" "user-exec" "$TF4" "group-read" "$TF5" "group-write" "$TF6" "group-exec" "$TF7" "all-read" "$TF8" "all-write" "$TF9" "all-exec" --separator=":")
if [ "$ans" != "" ]; then
    searchuserread="user-read"
    searchuserwrite="user-write"
    searchuserexec="user-exec"
    user1="0"
    user2="0"
    user3="0"
    searchgroupread="group-read"
    searchgroupwrite="group-write"
    searchgroupexec="group-exec"
    group1="0"
    group2="0"
    group3="0"
    searchallread="all-read"
    searchallwrite="all-write"
    searchallexec="all-exec"
    all1="0"
    all2="0"
    all3="0"

    case $ans in  *"$searchuserread"*)
        user1="4" ;;
    esac

    case $ans in  *"$searchuserwrite"*)
        user2="2" ;;
    esac

    case $ans in  *"$searchuserexec"*)
        user3="1" ;;
    esac

    case $ans in  *"$searchgroupread"*)
        group1="4" ;;
    esac

    case $ans in  *"$searchgroupwrite"*)
        group2="2" ;;
    esac

    case $ans in  *"$searchgroupexec"*)
        group3="1" ;;
    esac

    case $ans in  *"$searchallread"*)
        all1="4" ;;
    esac

    case $ans in  *"$searchallwrite"*)
        all2="2" ;;
    esac

    case $ans in  *"$searchallexec"*)
        all3="1" ;;
    esac

    u=$(($user1 + $user2 + $user3))
    g=$(($group1 + $group2 + $group3))
    a=$(($all1 + $all2 + $all3))
    result="$u$g$a"
    chmod $result "$file" || { zenity --error --text="An error occurred!\ncheck if you are allowed\nto change permissions\nof the selected files"; }
fi

Bonus:
If you want to display the full path in the file name use
Code: [Select]
/path/to/the/script %ffor the Thunar custom action.
Cheers :)
Title: Re: File permissions
Post by: Jerry on May 10, 2015, 09:32:37 PM
Thank you for the share misko :)
Title: Re: File permissions
Post by: anon222 on May 13, 2015, 07:41:59 AM
Thank you for the share misko :)
(http://imallvirtual.com/wp-content/uploads/2015/02/sharing-is-caring.jpg)
 :D
It's good for learning about file permissions. Maybe a more user friendly approach would be to use the common ones like 777 755 700 644 ...

Coulumn | Column    |             Column

777
 
(rwxrwxrwx) No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.

755
 
(rwxr-xr-x) The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.

700
 
(rwx------) The file's owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.

666
 
(rw-rw-rw-) All users may read and write the file.

644
 
(rw-r--r--) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.

600
 
(rw-------) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.