Skip to content

Instantly share code, notes, and snippets.

@Tahutipai
Last active April 22, 2024 18:30
Show Gist options
  • Save Tahutipai/d86346eeb4c3376a86703cf0cfedf978 to your computer and use it in GitHub Desktop.
Save Tahutipai/d86346eeb4c3376a86703cf0cfedf978 to your computer and use it in GitHub Desktop.
Script to fix Plex permissions in Linux
#!/bin/bash
folders_to_fix=()
#-----------USER EDITABLE SETTINGS----------------------------
#Edit these lines, one for each of your plex root folders. If you only have one plex folder, then you only need one line.
folders_to_fix+=("/mnt/2tb/dropbox/videos/plex")
folders_to_fix+=("/mnt/4tb/plex")
folders_to_fix+=("/mnt/12tb/4kmovies")
#Edit this line if you need to change the Group that plex and your user account use to access these folders
DATAGROUP=plex
#-------------------------------------------------------------
#Users should not need to edit anything below this line.
func_testplexaccess(){
if [ $# -eq 0 ]; then
echo "Error: Please provide a folder as an argument."
exit 1
fi
local folder="$1"
if sudo -u plex [ -r $folder ] && sudo -u plex [ -w $folder ]; then
echo Plex already has some access to this folder
else
echo Plex does NOT currently have r/w access to this folder
fi
}
func_fixplexpermissions(){
if [ $# -eq 0 ]; then
echo "Error: Please provide a folder as an argument."
exit 1
fi
local folder="$1"
echo Fixing permissions for folder $folder
if [ ! -d "$folder" ]; then
echo "Error: Folder '$folder' does not exist."
exit 1
fi
func_testplexaccess "$folder"
echo "--Changing ownership..."
sudo chown -R $USER:$DATAGROUP $folder
echo "--Setting umask..."
umask 0007 $folder
echo "--Setting permissions..."
chmod u+rwx,g+srwx,o-rwx $folder
find $folder -type f -exec chmod -R u+rw-x,g+rw-x,o-rwx "{}" \;
find $folder -type d -exec chmod -fR u+rwx,g+srwx,o-rwx "{}" \;
#Testing access....
if sudo -u plex [ ! -r $folder ] || sudo -u plex [ ! -w $folder ]; then
echo Something went wrong. Plex still can not access folder $folder
echo May need to consider running chmod with --parent
echo consider chown $USER:plex on all parent folders, and chmod 710
exit 1
else
echo -e "Plex access tested, and confirmed successful\n"
fi
}
#-------------------------------------------------------
if ! command -v members &> /dev/null
then
echo "members is not installed. please install it"
exit 1
fi
if groups "$USER" | grep -q "\b$DATAGROUP\\b"; then
echo "User $USER is already in the $DATAGROUP group, this is good"
else
echo "ERROR You need to add $USER to the $DATAGROUP group"
echo "Use this command: sudo useradd –G $DATAGROUP $USERNAME"
exit 1
fi
for element in "${folders_to_fix[@]}"; do
func_fixplexpermissions "$element"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment