Last active
June 3, 2023 04:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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