You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何借助fswatch与xargs自动修改/var/www/html的权限及属主?

Auto-Fix Permissions for /var/www/html on File Changes

Hey there! Let's get this auto-permission setup working for your /var/www/html directory. Here's how to do it properly, with some optimizations to avoid common pitfalls:

Basic Implementation

If you want a quick-and-dirty solution that refreshes permissions across the entire directory whenever any change happens, use this command:

fswatch -o /var/www/html | xargs -I {} sh -c 'chown -R www-data:www-data /var/www/html && chmod -R 755 /var/www/html'

Breakdown:

  • -o tells fswatch to output a single newline per batch of events (instead of every individual event), which prevents spamming the chown/chmod commands for a single file creation.
  • xargs -I {} replaces the placeholder with the trigger from fswatch, then runs the shell command to fix ownership and permissions.
  • chown -R recursively sets the owner/group to www-data (adjust this to match your web server's user/group, e.g., apache for Apache).
  • chmod -R 755 sets read/execute permissions for everyone and full access for the owner (good for directories, but files can be more restrictive).

Optimized Version (Target Only Changed Files)

Recursively modifying the entire directory every time a single file changes is inefficient, and can trigger infinite loops (since chown/chmod themselves modify file attributes, which fswatch detects). Instead, target only the changed files/directories:

fswatch -x /var/www/html | grep -E "(Created|Updated)" | awk '{print $NF}' | xargs -I {} sh -c '
    chown www-data:www-data {}
    if [ -d {} ]; then
        chmod 755 {}
    else
        chmod 644 {}
    fi
'

Breakdown:

  • -x makes fswatch output the event type (e.g., Created, Updated) alongside the file path.
  • grep -E "(Created|Updated)" filters out irrelevant events like permission changes or deletions (we don't need to fix permissions for deleted files!).
  • awk '{print $NF}' extracts just the file/directory path from the output.
  • The shell command checks if the path is a directory or file, then applies appropriate permissions: 755 for directories (so users can traverse them) and 644 for files (read-only for everyone except the owner).

Run in the Background (Systemd Service)

To make this watcher run automatically on boot and stay in the background, create a systemd service file:

  1. Create the service file:
sudo nano /etc/systemd/system/www-perms-watcher.service
  1. Paste this content (adjust user/group and paths if needed):
[Unit]
Description=Watch /var/www/html for changes and auto-fix permissions
After=network.target

[Service]
User=root
ExecStart=/usr/bin/fswatch -x /var/www/html | /usr/bin/grep -E "(Created|Updated)" | /usr/bin/awk '{print $NF}' | /usr/bin/xargs -I {} /bin/sh -c 'chown www-data:www-data {} && if [ -d {} ]; then chmod 755 {}; else chmod 644 {}; fi'
Restart=always

[Install]
WantedBy=multi-user.target
  1. Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable --now www-perms-watcher.service

Key Notes

  • Adjust User/Group: Replace www-data with your web server's actual user (e.g., apache for Apache, nginx for some Nginx setups).
  • Permission Values: Tweak 755/644 if your use case requires more permissive settings (e.g., 775 for directories that need group write access), but prioritize security where possible.
  • Test First: Run the fswatch command in your terminal first to verify it works before setting up the systemd service. This helps catch any path or permission issues upfront.

内容的提问来源于stack exchange,提问作者Tejas Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:19:34