chmod 命令修改文件权限

在自定义命令实现过程中,写了一个文件内容为

1
2
3
#!/usr/bin/env node

console.log('hello')

#!/usr/bin/env node告诉操作系统在运行这个文件文件的时候,需要用node的解析器来解析
如果执行这个文件:

1
2
iMac:img_tools $ ./tools/index.js
bash: ./tools/index.js: Permission denied

这时就需要给文件添加一个执行权限,然后执行:

1
2
3
iMac:img_tools $ chmod +x tools/index.js 
iMac:img_tools $ ./tools/index.js
hello

chmod 参数

1
chmod 用户+操作+权限 文件

用户部分:使用字母 u 表示文件拥有者(user),g 表示拥有者所在群组(group),o 表示其他用户(other),a 表示全部用户(all,包含前面三种用户范围);

操作部分:“+” 符号表示增加权限,“-” 符号表示取消权限,“=” 符号表示赋值权限;

权限部分:“r” 符号表示可读(read),“w” 表示可写(write),“x” 表示可执行权限(execute);

文件部分:如不指定文件名,表示操作对象为当前目录下的所有文件

chmod +x tools/index.js这个命令就是添加可执行权限