简体   繁体   English

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

[英]Linux - Bash - apply chown within many user directories

I have about 400 users on /home/ directory. 我在/home/目录上大约有400个用户。

I have a git repository on /var/repos/my_repo.git 我在/var/repos/my_repo.git上有一个git存储库

I have cloned, using root account, this repository to all users on home folder through bash command. 我已经使用root帐户通过bash命令将此存储库克隆到主文件夹上的所有用户。

Now I want to change the owner of cloned directory for each user, according to each folder. 现在,我想根据每个文件夹更改每个用户的克隆目录的所有者。

Something like: 就像是:

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

It will not work because {} returns ./username , so I just need a way to clean ./ 因为{}返回./username ,所以它./username ,所以我只需要一种清理./

Somebody have a better solution? 有人有更好的解决方案吗?

If you are not properly recursing into subdirectories, find is just an unnecessary complication here. 如果您没有正确地递归到子目录,则find只是在这里不必要的麻烦。

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

A much superior approach might have been to clone as the target user via sudo in the first place. 首先,一种更好的方法可能是通过sudo克隆为目标用户。

For completeness, a slightly ugly fix if you really want to use find could be 为了完整起见,如果您真的想使用find可能要进行一个稍微难看的修复

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

Parsing the output of ls is usually a very bad practice but in this case you very likely don't have any directory with spaces in /home , so you can safely try the following 解析ls的输出通常是一个非常糟糕的做法,但是在这种情况下,您很可能在/home 没有任何带有空格的目录,因此可以安全地尝试以下操作

cd /home
ls | xargs -I% echo chown -R %:mygroup %/www/my_repo.git

Try it, it is in DRY mode (will only echo the chown command), if satisfied remove the echo . 尝试一下,它处于DRY模式(仅回显chown命令),如果满意,请删除echo

Of course, you can also use the find command like so: 当然,您也可以像这样使用find命令:

cd /home
find . -maxdepth 1 -type d -printf "%f\0" | xargs -I% echo chown -R "%:mygroup" "%/www/my_repo.git"

There's a way to indirectly achieve what you want, provided your chown version supports the --reference option. 只要您的chown版本支持--reference选项,就有一种间接实现您想要的--reference In this case, the command: 在这种情况下,命令:

chown --reference=RFILE file_to_chown

where RFILE and file_to_chown are two files (or directories), will change owner and group of file_to_chown to match that of RFILE . 其中RFILEfile_to_chown有两个文件(或目录),将改变的所有者和组file_to_chown相匹配的RFILE

Since you want a special group, we'll need to use an auxilliary file that will be owned by user, and have group MY_GROUP . 由于您需要一个特殊的组,因此我们需要使用一个用户拥有的辅助文件,并具有组MY_GROUP We'll use mktemp to create such a file. 我们将使用mktemp创建这样的文件。

#!/bin/bash

shopt -s nullglob

rfile=$(mktemp) || { echo "oops"; exit 1; }

for i in /home/*/; do
    [[ -d $i/www/my_repo.git ]] || continue
    chown --reference="$i" "$rfile"
    chown :MY_GROUP "$rfile" # or chgrp MY_GROUP "$rfile"
    chown -R --reference="$rfile" "$i/www/my_repo.git"
done

This method (using --reference ) has the following advantage: a user's home name may be distinct from user's name. 此方法(使用--reference )具有以下优点:用户的家名可能与用户名不同。

The exact same approach without the temporary file, using the my_repo.git in user's home as a reference file. 使用临时目录的完全相同的方法,使用用户主my_repo.git中的my_repo.git作为参考文件。

#!/bin/bash

shopt -s nullglob

for i in /home/*/; do
    [[ -d $i/www/my_repo.git ]] || continue
    chown --reference="$i" "$i/www/my_repo.git"
    chown :MY_GROUP "$i/www/my_repo.git" # or chgrp MY_GROUP "$i/www/my_repo.git"
    chown -R --reference="$i/www/my_repo.git" "$i/www/my_repo.git"
done

As a final note: if you want to check how the script behaves before running it, you can define (inside the script, before the for loop) a function chown : 最后一点:如果要在运行之前检查脚本的行为,可以定义一个函数chown (在脚本内部,在for循环之前):

chown() {
    echo "chown $@"
}

that will only show what's going to happen (minus the quotes), without executing anything. 那只会显示将要发生的事情(减去引号),而不执行任何操作。 When you're happy, remove it from the script. 当您感到满意时,请将其从脚本中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM