[英]How to change all folders and files permessions base on folder name?
For exemple we have folders named with permission numbers 555 or 700 or 777... how can we take the file name and change all the subfolders and files with permissions based on the folder name?例如,我们有以权限编号 555 或 700 或 777 命名的文件夹......我们如何获取文件名并根据文件夹名称更改所有具有权限的子文件夹和文件? so if we apply it on the folder 555 the current folder and everything inside will have permission as 555 i've tried
所以如果我们将它应用到文件夹 555 上,当前文件夹和里面的所有内容都将获得 555 的权限,我试过了
find . -name "[0-9][0-9][0-9]" -type d | xargs chmod -Rv [0-9][0-9][0-9]
but it only takes the first folder name and apply it on everything it i want it to treat each folder name ( named withe permission) seperately thank you.但它只需要第一个文件夹名称并将其应用于我希望它单独处理每个文件夹名称(命名为withe许可)的所有内容谢谢。
i've tried find.
我试过找到。 -name "[0-9][0-9][0-9]" -type d |
-名称“[0-9][0-9][0-9]”-类型 d | xargs chmod -Rv [0-9][0-9][0-9] but it only takes the first folder name and apply it on everything it i want it to treat each folder name ( named withe permission) seperately
xargs chmod -Rv [0-9][0-9][0-9] 但它只采用第一个文件夹名称并将其应用于我希望它单独处理每个文件夹名称(命名为权限)的所有内容
Your attempt is wrong in several ways, among them:您的尝试在几个方面是错误的,其中包括:
The [0-9][0-9][0-9]
pattern matches names that include characters that are not octal digits. [0-9][0-9][0-9]
模式匹配包含非八进制数字字符的名称。
The [0-9][0-9][0-9]
pattern will be expanded by the shell when it parses the command. [0-9][0-9][0-9]
模式在解析命令时将被 shell 扩展。 If the expansion is successful, then the first matching name will give the mode to apply.如果扩展成功,则第一个匹配的名称将给出要应用的模式。 If the expansion is not successful then (by default) the pattern itself will be passed through to
chmod
, which will reject it as an invalid mode.如果扩展不成功,那么(默认情况下)模式本身将传递给
chmod
,它将拒绝它作为无效模式。
By default, the xargs
command will take as many inputs as it can to form arguments to one command.默认情况下,
xargs
命令将接受尽可能多的输入,以将 arguments 形成为一个命令。 So in your example command, if find
identifies folders 700
, 555
, 755
, and 400
then the xargs
will end up executing something like this:因此,在您的示例命令中,如果
find
标识文件夹700
、 555
、 755
和400
那么xargs
将最终执行如下操作:
chmod -Rv <... expansion of [0-9][0-9][0-9]...> 700 555 755 400
Even in the event that the pattern expanded to nothing (which could happen under slightly different circumstances) you have a result similar to before: the first directory name would provide the mode, and all the others would be updated to that mode.即使模式扩展为空(这可能在稍微不同的情况下发生),您也会得到与以前类似的结果:第一个目录名将提供模式,所有其他目录名将更新为该模式。
This variation should do what you want:这种变化应该做你想做的:
find . -name "[0-7][0-7][0-7]" -type d |
sed 's,^.*/\([^/]*\)$,\1\n\0,' |
xargs -r -l2 chmod -Rv
The sed
command takes each path emitted by find
, extracts the base name, and puts that on a separate, preceding line. sed
命令获取find
发出的每个路径,提取基本名称,并将其放在单独的前一行中。 For example, if one of the lines find
emits is例如,如果其中一行
find
发出的是
./subdir/755
then sed will change that to然后 sed 将其更改为
755
./subdir/755
. . That is, the mode to apply and the directory to which to apply it.
即,要应用的模式和应用它的目录。
Then the -l2
option to xargs
tells it to use (at most) two input lines in forming each chmod
command, so you will end up with a series of commands of the form然后
xargs
的-l2
选项告诉它在形成每个chmod
命令时使用(最多)两个输入行,所以你最终会得到一系列形式的命令
chmod -Rv 755 ./subdir/755
The -r
option to xargs
is a nicety that tells it to avoid executing any commands at all if no lines are read from the standard input. xargs
的-r
选项很巧妙,告诉它如果没有从标准输入中读取任何行,则完全避免执行任何命令。
sed
expression in more detailsed
表达式[Note: I have slightly simplified the sed
command given in the original version of this answer.] [注意:我稍微简化了此答案原始版本中给出的
sed
命令。]
The sed
command... sed
命令...
sed 's,^.*/\([^/]*\)$,\1\n\0,'
... processes each input line according to the given expression, a s
ubstitute command with comma ( ,
) delimters. ... 根据给定的表达式处理每个输入行,这是一个带有逗号 (
,
) 分隔符s
替换命令。
^\(.*/\)\([^/]*\)$
.^\(.*/\)\([^/]*\)$
。 This matches^
),^
),.*
,.*
,/
) character,/
) 字符,[^/]*
)[^/]*
) 以外的任意数量的任何字符$
).$
)。\1
), followed by\1
)的内容,然后是\n
), followed by\n
),然后是\0
), which will be the original line because the match is anchored to both the beginning and end of the line.\0
),这将是原始行,因为匹配被锚定到行的开头和结尾。 sed
will match each input line against the target pattern. sed
会将每个输入行与目标模式进行匹配。 Any line containing at least one slash will match, and every line emitted by the given find
command will satisfy that criterion.任何包含至少一个斜杠的行都将匹配,并且给定
find
命令发出的每一行都将满足该条件。 The whole match (which will be the whole line) will be replaced by the given replacement, consisting of two lines -- one containing the tail of the original line following the last slash, and the other containing the whole original line.整个匹配项(将是整行)将由给定的替换项替换,由两行组成——一行包含最后一个斜杠后面的原始行的尾部,另一行包含整个原始行。
Not sure understand the question.不确定是否理解问题。
Need sample input and sample output to understand.需要示例输入和示例output才能理解。
dir_name="555"; chmod -R "$dir_name" $(find . -path "*$dir_name*")
dir_name="555";
: Assign a variable dir_name
that holds the directory name 555
and the chmod
permission value 555
: 分配一个变量
dir_name
保存目录名555
和chmod
权限值555
$(find. -path "*$dir_name*")
: List all files under current directory having 555
somewhere in their path. $(find. -path "*$dir_name*")
:列出当前目录下的所有文件,其路径中某处有555
个文件。
chmod -R "$dir_name"
: Chmod command using the value of dir_name
555
chmod -R "$dir_name"
: Chmod 命令使用dir_name
555
的值
I'd use something like this.我会用这样的东西。
find . -type d -name '???' -printf '%f\n' | xargs -I'{}' chmod -R '{}' '{}'
-type d
-- find only directories. -type d
-- 只查找目录。-name???
-- this will cover directories named 555, 700, 777, etc printf '%f\n'
-- print only directory's name. printf '%f\n'
-- 只打印目录名。 To be really safe, I would add another option ( -maxdepth
):为了真正安全,我会添加另一个选项(
-maxdepth
):
find . -maxdepth 1 -type d -name '???' -printf '%f\n' | xargs -I'{}' chmod -R '{}' '{}'
maxdepth 1
-- Don't do any search on subfolders. maxdepth 1
-- 不要对子文件夹进行任何搜索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.