linux 下命令chmod 及案例介绍
chmod 是一个在 Linux 和 Unix 系统中用于更改文件和目录访问权限的命令。其名称是 "change mode" 的缩写。文件和目录的权限控制谁可以读取、写入或执行它们。
基本语法bashchmod [选项] 模式 文件名选项:可选的标志,用于修改 chmod 命令的行为。模式:表示要设置的权限,可以是八进制数字或符号表示法。文件名:要更改权限的文件或目录的名称。权限表示法权限通常分为三类:
u:文件的所有者(user)g:文件的组(group)o:其他用户(others)每种权限可以是:
r:读取权限(read)w:写入权限(write)x:执行权限(execute)常用选项-R:递归地更改目录及其子目录中的所有文件的权限。-v:在处理每个文件时输出诊断信息。-c:仅在实际更改时报告文件的权限更改。命令:
root@meng:~# which chmod
/usr/bin/chmod
root@meng:~# chmod
chmod: missing operand
Try 'chmod --help' for more information.
root@meng:~# chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
or: chmod [OPTION]... OCTAL-MODE FILE...
or: chmod [OPTION]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.
-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's mode instead of MODE values
-R, --recursive change files and directories recursively
--help display this help and exit
--version output version information and exit
Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/chmod>
or available locally via: info '(coreutils) chmod invocation'
命令案例:
root@meng:~# chmod +x -v meng.txt
mode of 'meng.txt' retained as 0755 (rwxr-xr-x)
root@meng:~# ls -al meng.txt
-rwxr-xr-x. 1 root meng 45 Aug 22 09:37 meng.txt
root@meng:~# chmod u=rwx,g=rx,o=r meng.txt
root@meng:~# ls -al meng.txt
-rwxr-xr--. 1 root meng 45 Aug 22 09:37 meng.txt
root@meng:~#