简体   繁体   English

如何根据文件夹名称更改所有文件夹和文件权限?

[英]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:您的尝试在几个方面是错误的,其中包括:

  1. The [0-9][0-9][0-9] pattern matches names that include characters that are not octal digits. [0-9][0-9][0-9]模式匹配包含非八进制数字字符的名称。

  2. 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 ,它将拒绝它作为无效模式。

  3. 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标识文件夹700555755400那么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选项很巧妙,告诉它如果没有从标准输入中读取任何行,则完全避免执行任何命令。

Addendum: the sed expression in more detail附录:更详细的sed表达式

[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替换命令。

  • The target pattern is ^\(.*/\)\([^/]*\)$ .目标模式是^\(.*/\)\([^/]*\)$ This matches这匹配
    • a sequence starting at the beginning of the line ( ^ ),从行首开始的序列 ( ^ ),
    • matching any number of any character .* ,匹配任意数量的任意字符.* ,
    • followed by a slash ( / ) character,后跟一个斜杠 ( / ) 字符,
    • followed by any number of any character other than slash ( [^/]* )后跟除斜杠 ( [^/]* ) 以外的任意数量的任何字符
    • running to the end of the line ( $ ).运行到行尾 ( $ )。
  • The part after the last slash is captured as a group.最后一个斜线之后的部分被捕获为一个组。
  • the replacement text is替换文本是
    • the contents of the first (only) capture group ( \1 ), followed by第一个(仅)捕获组( \1 )的内容,然后是
    • a newline ( \n ), followed by换行符 ( \n ),然后是
    • the whole match ( \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才能理解。

Suggesting following solution:建议以下解决方案:

 dir_name="555"; chmod -R "$dir_name" $(find . -path "*$dir_name*")

Explanation解释

dir_name="555"; : Assign a variable dir_name that holds the directory name 555 and the chmod permission value 555 : 分配一个变量dir_name保存目录名555chmod权限值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 -- 这将覆盖名为 555、700、777 等的目录
  • 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.

相关问题 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 如何使用名称中的计数将不同文件夹中的所有文件重命名为另一个文件夹? - How to rename all the files across different folders into another folder using count in the name? 如何将所有文件和文件夹移动到同一文件夹的子文件夹中 - How to move all files and folders into a subfolder of the same folder 如何将所有隐藏的文件夹/文件更改为在多个子目录中可见 - How to change all hidden folders/files to visible in a multiple sub directories 除了某些文件夹中的文件外,我如何将其所有文件包含在文件夹或目录中? - How do I tar a folder or directory with all of it's files except leave out files from certain folders? Linux - 授予所有用户访问文件夹(以及下面的所有文件夹和文件)的权限 - Linux - Giving all users access to a folder (and all folders and files below) 如何通过比较文件名和文件夹名称将文件复制到具有相同名称的文件夹? - How to copy files to folders with same name by comparing file names with folder names? 如何输出两个文件夹中文件的差异,并将相同名称的输出保存在不同的文件夹中 - How to output difference of files from two folders and save the output with the same name on different folder 在 SFTP 中:如何放置作为文件而不是文件夹的文件夹内容 - In SFTP: How To Put Folder Contents That Are Files, Not Folders 如何重命名(通过添加前缀)从当前文件夹开始的所有文件和文件夹? - How to rename(by adding a prefix) all the files and folders starting from the current folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM