Shell脚本,用于向目录和该目录中的文件添加完全读写和执行权限

时间:2015-06-02 11:34:39

标签: shell chmod

我对shell脚本很新,但至少我尝试了一些东西,但这不起作用。我想为我的网站的httpdocs文件夹添加读写和执行权限httpdocs/mysite文件夹和所有用户和组的php文件httpdocs/mysite/_conn.php

我有以下内容,但这不起作用,change-permission.sh

#!/bin/bash
/bin/date >> /tmp/event_handler.log 
chmod g=drwx "/var/www/vhosts/domain.com/httpdocs"
chmod o=drwx "/var/www/vhosts/domain.com/httpdocs"
chmod u=drwx "/var/www/vhosts/domain.com/httpdocs"
chmod g=drwx "/var/www/vhosts/domain.com/httpdocs/mysite"
chmod o=drwx "/var/www/vhosts/domain.com/httpdocs/mysite"
chmod u=drwx "/var/www/vhosts/domain.com/httpdocs/mysite"
chmod g=drwx "/var/www/vhosts/domain.com/httpdocs/mysite/_conn.php"
chmod o=drwx "/var/www/vhosts/domain.com/httpdocs/mysite/_conn.php"
chmod u=drwx "/var/www/vhosts/domain.com/httpdocs/mysite/_conn.php"

上述脚本不起作用。我需要在此change-permission.sh文件中进行哪些更改?

2 个答案:

答案 0 :(得分:2)

chmod o=drwx不起作用,您需要使用chmod o+rwx

完整脚本:

#!/bin/bash
/bin/date >> /tmp/event_handler.log 
chmod ugo+rwx "/var/www/vhosts/domain.com/httpdocs"
chmod ugo+rwx "/var/www/vhosts/domain.com/httpdocs/mysite"
chmod ugo+rwx "/var/www/vhosts/domain.com/httpdocs/mysite/_conn.php"

您甚至可以将3 chmod个命令合并为一个,如上所示。

答案 1 :(得分:0)

我已经创建了几个函数来实现相同的想法,具有不同的可访问性级别。

每个都运行vm_area_struct并指定文件或目录(find d或f),然后设置相应的权限,因此包括-type以使它们可读。

x

它不必记住所有细节,可用于您发送的任何路径。

任何这些都可以在shell脚本中定义,然后在需要时调用,例如:

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ chmod RWX ~~~~~~~
echo "Defining function 'chmod_group_rw' which requires single pathname passed"
function chmod_group_rw() {
  DEST=${1?'which path? (Can use *name* if all matches desired)'}
  find ${DEST} -type d -exec chmod 770 {} \;
  find ${DEST} -type f -exec chmod 660 {} \;
}
echo "Defining function 'chmod_all_rw' which requires single pathname passed"
function chmod_all_rw() {
  DEST=${1?'which path? (Can use *name* if all matches desired)'}
  find ${DEST} -type d -exec chmod 777 {} \;
  find ${DEST} -type f -exec chmod 666 {} \;
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ chmod RO ~~~~~~~
echo "Defining function 'chmod_archive_readonly' which requires single pathname passed"
function chmod_archive_readonly() {
  DEST=${1?'which path? (Can use *name* if all matches desired)'}
  find ${DEST} -type d -exec chmod 555 {} \;
  find ${DEST} -type f -exec chmod 444 {} \;
}