简体   繁体   English

Linux Shell脚本更改文件模式

[英]Linux Shell Script to change file mode

I am entirely new to shell scripts. 我对Shell脚本完全陌生。 I know how to change the mode for a single file. 我知道如何更改单个文件的模式。

But, I need to meet the following requirement and my operating system is red hat enterprise linux 但是,我需要满足以下要求,并且我的操作系统是Red Hat Enterprise Linux。

  1. To find all files in a directory which are having 640 mode and then change it to 644. 要查找目录中具有640模式的所有文件,然后将其更改为644。

Like wise im having 10 directories where i need to recursively find all the files in all directories and change the mode to 644. 就像明智的即时通讯有10个目录,我需要递归地找到所有目录中的所有文件,并将模式更改为644。

  1. Later sending email with the file names whose status have been changed. 稍后发送电子邮件,其文件名的状态已更改。

Expecting your kind assistance to complete this requirement. 期待您的协助以完成此要求。

Some research points to get you going. 一些研究要点可以帮助您前进。

  • The find command can be used to find files of varying properties under a given point. find命令可用于在给定点下查找具有不同属性的文件。
  • You can use it with -type to select specific file types (regular files, directories, etc). 您可以将其与-type一起使用以选择特定的文件类型(常规文件,目录等)。
  • You can use it with -maxdepth to restrict how deep it will go down the tree. 可以将它与-maxdepth一起使用,以限制它在树中的下降深度。
  • You can use it with -perm to select only files with specific permissions. 您可以将它与-perm一起使用,以仅选择具有特定权限的文件。
  • You can use it with -print to output the filenames, including capturing them to a text file for later mailing (with a tool like mailx ). 您可以将它与-print一起使用以输出文件名,包括将它们捕获到文本文件中以供以后发送邮件(使用mailx类的工具)。
  • You can use it with -exec to carry out arbitrary commands on each file matching the conditions. 可以将它与-exec一起使用,以对每个符合条件的文件执行任意命令。
  • chmod is the command to change permissions. chmod是更改权限的命令。

For example, the following commands will find all regular files of the form *.dat , in the current directory (no subdirectories) and with permission 640 , then change all those permissions to 644 : 例如,以下命令将在当前目录(无子目录)中以*.dat格式找到所有常规文件,并且具有权限640 ,然后将所有这些权限更改为644

find . -type f -perm 640 -name '*.dat' -maxdepth 1 -exec chmod 644 {} ';'

All these options, and more, can be found in the manpage for find with the command: 所有这些选项以及更多选项都可以在手册页中find使用以下命令进行find

man find

or by looking for some good explanations, such as the GNU find documentation . 或通过寻找一些很好的解释,例如GNU find documentation

However, find is not a tool for the faint of heart, it will bite you at every opportunity. 但是, find并不是使您晕倒的工具,它会抓住每一个机会。 Expect to ask at least another ten questions here before you get what you need :-) 希望在这里提出,至少另一个十个问题你之前,你需要什么:-)

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

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