Skip to content

Instantly share code, notes, and snippets.

@FaiChou
Last active June 3, 2023 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FaiChou/5f84c452328ac12a0015f5859d07fecd to your computer and use it in GitHub Desktop.
Save FaiChou/5f84c452328ac12a0015f5859d07fecd to your computer and use it in GitHub Desktop.
A macOS bash script monitors a specific directory, changes the permissions of any newly created files to '775', and logs these operations.
#!/bin/bash
# 指定共享文件夹路径
DIRECTORY=/Users/faichou/Public
# 使用 fswatch 监视 DIRECTORY 目录,检测新文件的创建
fswatch -0 $DIRECTORY | while read -d "" NEWFILE
do
# 当有新文件创建时,检查其权限是否为775,如果不是则使用 chmod 命令更改
if [ -f "$NEWFILE" ]; then
PERM=$(stat -f "%A" "$NEWFILE")
if [ "$PERM" != "775" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S'): Chmod 775 $NEWFILE"
sudo chmod 775 "$NEWFILE" || true
else
echo "$(date '+%Y-%m-%d %H:%M:%S'): The file $NEWFILE already has 775 permissions"
fi
fi
done
# run with: nohup ./watch_and_chmod_public_folder_items.sh > logs/watch_and_chmod.log 2>&1 &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment