Linux - Bash - 在许多用户目录中应用 chown

标签 linux bash shell xargs chown

我在 /home/ 目录上有大约 400 个用户。

我在 /var/repos/my_repo.git 上有一个 git 存储库

我已使用 root 帐户通过 bash 命令将此存储库克隆到主文件夹上的所有用户。

现在我想根据每个文件夹更改每个用户克隆目录的所有者。

类似于:

find . -maxdepth 1 -type d ! -name . -prune -exec chown {}:MY_GROUP {}/www/my_repo.git -R

它不会工作,因为 {} 返回 ./username,所以我只需要一种方法来清理 ./

有人有更好的解决方案吗?

最佳答案

如果您没有正确递归到子目录,find 在这里只是一个不必要的复杂化。

for d in */www/my_repo.git; do
   chown -R "${d%%/*}:MY_GROUP "$d"
done

一种更好的方法可能是首先通过 sudo 克隆为目标用户。

为了完整起见,如果您确实想使用 find ,可能需要一个稍微丑陋的修复

find . -maxdepth 1 -type d ! -name . -prune -exec sh -c 'chown "${1#./}:MY_GROUP "$1"/www/my_repo.git -R' _ {} \;

关于Linux - Bash - 在许多用户目录中应用 chown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26368757/

相关文章:

linux - 用变量中包含空格的sed替换文本

bash - 使用 shell 中的内容创建多个文件

python - 为什么两个相同的字符串但以不同的方式连接在使用 python 调用的 shell 上下文中会产生不同的结果?

linux - 使用路径作为参数通过 bash 调用 shell 脚本

shell - mysql dump selection of databases(不确定有多少)

php - 创建一个任何人都可以执行的 shell.sh,它在内部以 root 身份运行

python - epoll 中如何通知 connect() 失败?

c++ - 子进程和父进程之间的交互式双向通信(使用管道)

bash - 使用 grep 查找文件并在编辑器中打开

bash - 从另一台机器复制 1 小时前的文件