简体   繁体   中英

Linux Shell Script to change file mode

I am entirely new to shell scripts. 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

  1. To find all files in a directory which are having 640 mode and then change it to 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.

  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.
  • You can use it with -type to select specific file types (regular files, directories, etc).
  • You can use it with -maxdepth to restrict how deep it will go down the tree.
  • You can use it with -perm to select only files with specific permissions.
  • 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 ).
  • You can use it with -exec to carry out arbitrary commands on each file matching the conditions.
  • chmod is the command to change permissions.

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 :

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:

man find

or by looking for some good explanations, such as the GNU find documentation .

However, find is not a tool for the faint of heart, it will bite you at every opportunity. Expect to ask at least another ten questions here before you get what you need :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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