Skip to content

Instantly share code, notes, and snippets.

@stigok
Last active February 23, 2020 13:43
Show Gist options
  • Save stigok/351629fb4f570da187e156c20342766d to your computer and use it in GitHub Desktop.
Save stigok/351629fb4f570da187e156c20342766d to your computer and use it in GitHub Desktop.
Automatically set permissions on uploaded files and folders to use in, for example, multi-user FTP environments where different clients set different permissions, or otherwise ignore umask.
#!/bin/bash
# stig@stigok.com Oct 2019
#
# Reference:
# http://positon.org/a-solution-to-the-umask-problem-inotify-to-force-permissions
set -x
# Take the directory name as argument
inotifywait -mrq -e CREATE --format %w%f "$1" | while read FILE
do
if [ -d "$FILE" ]
then
# $FILE is a directory
chmod 755 "$FILE"
else
# Treat $FILE as a file
chmod 644 "$FILE"
fi
done
[Unit]
Description=Sets permissions of all uploaded files (644) and folders (755)
After=sshd.service
[Service]
Type=simple
WorkingDirectory=/srv/sftp
ExecStart=/usr/local/bin/inotify-set-permissions.sh .
Restart=on-failure
RestartSec=5
[Install]
WantedBy=default.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment