Skip to content

Instantly share code, notes, and snippets.

@psujit775
Created October 18, 2022 16:54
Show Gist options
  • Save psujit775/5887e3604c58dd8c7e11a07a83c72614 to your computer and use it in GitHub Desktop.
Save psujit775/5887e3604c58dd8c7e11a07a83c72614 to your computer and use it in GitHub Desktop.
check user permissions on file or directory
#!/bin/bash
echo -n "Enter File or DIR name to check permissions : "
read user_input # Read from user input
#check if input is directory or file
if [[ -d $user_input ]]; then #if input is dir then recursively list all sub-dir and files.
declare -a list_all_files=( $(find $user_input -d) ) #storing result in a array
for x in ${list_all_files[@]}
do
[ -w $x ] && W="Write = True" || W="Write = False" #check write permission
[ -x $x ] && X="Execute = True" || X="Execute = False" #check execute permission
[ -r $x ] && R="Read = True" || R="Read = False" #check read permission
echo "permissions for: $(pwd)/$x" #printing the file name with absolute path and permissions in True/False.
echo "$W"
echo "$R"
echo "$X"
echo ""
done
elif [[ -f $user_input ]]; then #if input is file check permissions only
echo "permissions for: $(pwd)/$x"
[ -w $x ] && W="Write = True" || W="Write = False"
[ -x $x ] && X="Execute = True" || X="Execute = False"
[ -r $x ] && R="Read = True" || R="Read = False"
echo "$W"
echo "$R"
echo "$X"
else
echo "ERROR: $user_input is not a valid file or directory." #if input is not file or directory show error.
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment