2.1系统目录结构 上
关于LS:
ls全称lis意为列出目录结构。
[root@xy-01 ~]# ls /
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
就是根目录下的所有目录。
---------------------------------------------------------------------------
/bin/ /sbin/ /usr/bin/ /usr/sbin
以上几个都是用来存放命令的目录。
其中sbin下的命令都是root用户所使用的命令。
而bin存放的都是是普通用户所使用的文件
----------------------------------------------------------------------------
/boot/下存放的都是启动相关的文件
---------------------------------------------------------------------------
/dev/下存放的是系统所使用的设备文件
---------------------------------------------------------------------------
/etc/下存放的是系统设置文件
例如之前所使用的/etc/sysconfig/network-scripts/ifcfg-ens33就是网卡配直文件
---------------------------------------------------------------------------
/lib/和l/ib64/下存放系统的库文件
---------------------------------------------------------------------------
其中/root/目录是超级用户root的家目录,用于存放root相关的文件。
比如/root/.ssh/authorized_keys就是控制root用户密钥的配置文件。
----------------------------------------------------------------------------
/home/是普通用户的家目录的根。所有普通用户的家目录都被存放在这里
2.2系统目录结构 下
/media/是媒介专用目录,比如插入U盘会自动识别到这个目录下。
----------------------------------------------------------------------------
/mnt/是一个临时的挂载目录和上边的目录一样默认为空。
----------------------------------------------------------------------------
/proc/存放系统进程
---------------------------------------------------------------------------
/run/存放进程所产生临时文件
---------------------------------------------------------------------------
/srv/存放服务所产生的文件
---------------------------------------------------------------------------
/sys/系统内核所相关的文件
---------------------------------------------------------------------------
/tmp/临时文件所存放的目录
---------------------------------------------------------------------------
/usr/用户所需要的文件
---------------------------------------------------------------------------
/var/下经常用的/var/log存放各种日志文件
/var/run/存放进程pid
---------------------------------------------------------------------------
2.3 ls命令
参数 -l
[root@xy-01 ~]# ls
anaconda-ks.cfg
[root@xy-01 ~]# ls -l
总用量 4
-rw-------. 1 root root 1257 10月 16 23:54 anaconda-ks.cfg
-l 参数列出了当前目录下anaconda-ks.cfg文件详细信息。
其中第一列-rw-------.为文件权限
其中第二列 1 是多少个文件使用了相同的inod号
其中第三列指出了该文件的所有者
其中第四列指出了该文件的所属组
其中第五列是文件大小单位为字节(Byte)
其中第六列和第七列分别表示文件创建的日期和时间
其中第八列是文件名
------------------------------------------------------------------------------
参数 -i
[root@xy-01 ~]# ls -i
33582978 anaconda-ks.cfg
列出了文件的inode编号。
inode是这个文件在磁盘的物理位置。
如果有两个文件使用了相同的inode编号,那么可以认为这两个文件是一个文件
------------------------------------------------------------------------------
参数 -a
[root@xy-01 ~]# ls -la
总用量 28
dr-xr-x---. 3 root root 147 4月 11 2018 .
dr-xr-xr-x. 17 root root 224 10月 17 21:22 ..
-rw-------. 1 root root 1257 10月 16 23:54 anaconda-ks.cfg
-rw-------. 1 root root 1008 10月 17 18:40 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 2 root root 48 10月 17 13:41 .ssh
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
首先,多个参数在不冲突的情况下可以联合使用。
-a是显示目录下所有(all)内容,包括隐藏
-------------------------------------------------------------------------------
关于目录,一般目录会有三种表现形式。
以/root/为例:
[root@xy-01 ~]# ls -i /root
33582978 anaconda-ks.cfg
[root@xy-01 ~]# ls -i .
33582978 anaconda-ks.cfg
[root@xy-01 ~]# ls -i /root/.ssh/..
33582978 anaconda-ks.cfg
我们看到这三个目录都使用了相同的inode编号33582978
其中 . 和 .. 分别表示当前目录和上级目录
使用参数-la查看.的相同inode使用数量可以判断当前目录有多少子目录(包含.和..)
---------------------------------------------------------------------------------
参数-t
[root@xy-01 ~]# ls -lat
总用量 28
dr-xr-xr-x. 17 root root 224 10月 17 21:22 ..
-rw-------. 1 root root 1008 10月 17 18:40 .bash_history
drwx------. 2 root root 48 10月 17 13:41 .ssh
-rw-------. 1 root root 1257 10月 16 23:54 anaconda-ks.cfg
dr-xr-x---. 3 root root 147 4月 11 2018 .
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
显示文件以时间排序
-------------------------------------------------------------------------------------
参数 -d
[root@xy-01 ~]# ls -d /root
/root
[root@xy-01 ~]# ls -l /root
总用量 4
-rw-------. 1 root root 1257 10月 16 23:54 anaconda-ks.cfg
仅显示目录本身
-------------------------------------------------------------------------------------
参数 -h
[root@xy-01 ~]# ls -lah /root
总用量 28K
dr-xr-x---. 3 root root 147 4月 11 2018 .
dr-xr-xr-x. 17 root root 224 10月 17 21:22 ..
-rw-------. 1 root root 1.3K 10月 16 23:54 anaconda-ks.cfg
-rw-------. 1 root root 1008 10月 17 18:40 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 2 root root 48 10月 17 13:41 .ssh
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
自动适配数量的单位,比如上边大于1024B的文件都以K单位结尾
------------------------------------------------------------------------------------
2.4 文件类型
[root@xy-01 ~]# ls -la
总用量 28
dr-xr-x---. 3 root root 147 4月 11 2018 .
dr-xr-xr-x. 17 root root 224 10月 17 21:22 ..
-rw-------. 1 root root 1257 10月 16 23:54 anaconda-ks.cfg
-rw-------. 1 root root 1008 10月 17 18:40 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 2 root root 48 10月 17 13:41 .ssh
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
权限的第一位表示这个文件的类型。
在上边显示的内容中
d表示目录
-表示普通文件
c表示字符串设备
l表示软链接
b表示块设备
s表示sockt文件
2.5 alias命令
[root@xy-01 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@xy-01 ~]# which ll
alias ll='ls -l --color=auto'
/usr/bin/ls
[root@xy-01 ~]# which man
/usr/bin/man
[root@xy-01 ~]# which yum
/usr/bin/yum
wich命令:查看命令的真实路径
--------------------------------------------------------------------------------
环境变量:PATH
如何查看环境变量
[root@xy-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PATH环境变量指定了以上路径下的命令可生效
----------------------------------------------------------------------------
查看系统内所有别名
[root@xy-01 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
自定义一个命令
[root@xy-01 ~]# alias xy='ls -lha' #自定义别名
[root@xy-01 ~]# xy
总用量 28K
dr-xr-x---. 3 root root 147 4月 11 2018 .
dr-xr-xr-x. 17 root root 224 10月 17 21:22 ..
-rw-------. 1 root root 1.3K 10月 16 23:54 anaconda-ks.cfg
-rw-------. 1 root root 1008 10月 17 18:40 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 2 root root 48 10月 17 13:41 .ssh
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
[root@xy-01 ~]# which xy #查看自定义别名
alias xy='ls -lha'
/usr/bin/ls
--------------------------------------------------------------------------------------------
[root@xy-01 ~]# unalias xy
[root@xy-01 ~]# which xy
/usr/bin/which: no xy in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@xy-01 ~]# xy
-bash: xy: 未找到命令
使用unalias命令取消别名
2.6 相对路径和绝对路径
路径就是指一个文件或目录所在系统从根目录开始的具体位置。
从根目录开始的路径即为绝对路径
以/root/.ssh/authorized_keys为例
[root@xy-01 ~]# cat /root/.ssh/authorized_keys
#putty
ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAt6w0FkGKBG/dyO71u7gPGYW2doVqUAEfi5xYQSM8lk0QvMh5ZL7/xK6PXfbOzFy+mVV4IHtEqbhIsjckmNGlzSvS4iL/bMWOdJBDpo4KNBqqhsOcVU6gM/9dMoKN63lNF2ruzItfc/I1GpteIPb+e4x7p8mUx6M5zphWnC0bjX4qVetFETY9jrbWzC42mB+x6Pd3y7TX4lZ0eM6NL2dgbfBoC1QwAzKB+sDMoKu+Ysq/ptoEQaIu7d2d1GvUt91S+SYzXmhC/icgekkF7fMYoXQ+oHxct/dxVR3DMbol6GYnYgKdxAd3mz/R1mE0KCHTL30YN6kQR5+38u8kDtGT7Q== rsa-key-20181016
这里就是从根目录出发查看authorized_keys。也就是绝对路径
----------------------------------------------------------------------------------------------
[root@xy-01 ~]# pwd #查看当前目录所在位置
/root
[root@xy-01 ~]# cat .ssh/authorized_keys
#putty
ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAt6w0FkGKBG/dyO71u7gPGYW2doVqUAEfi5xYQSM8lk0QvMh5ZL7/xK6PXfbOzFy+mVV4IHtEqbhIsjckmNGlzSvS4iL/bMWOdJBDpo4KNBqqhsOcVU6gM/9dMoKN63lNF2ruzItfc/I1GpteIPb+e4x7p8mUx6M5zphWnC0bjX4qVetFETY9jrbWzC42mB+x6Pd3y7TX4lZ0eM6NL2dgbfBoC1QwAzKB+sDMoKu+Ysq/ptoEQaIu7d2d1GvUt91S+SYzXmhC/icgekkF7fMYoXQ+oHxct/dxVR3DMbol6GYnYgKdxAd3mz/R1mE0KCHTL30YN6kQR5+38u8kDtGT7Q== rsa-key-20181016
在以上环境下,我们所处的位置是/root/目录下。
输入所要查看的文件时,并没有从根目录出发,而是从本级目录出发,所以文件开头没有/
所以查看文件的路径和本目录处于一个相对的关系,这就是相对路径。
2.7 CD命令
cd 命令就是更换当前目录
------------------------------------------------------------------------------------------------
参数 -
[root@xy-01 ~]# pwd
/root
[root@xy-01 ~]# cd /etc/sysconfig/
[root@xy-01 sysconfig]# pwd
/etc/sysconfig
[root@xy-01 sysconfig]# cd -
/root
[root@xy-01 ~]# cd -
/etc/sysconfig
[root@xy-01 sysconfig]# cd -
/root
这里我们可以看出cd -命令可以在当前所在目录与变更目录前的状态间切换。
-------------------------------------------------------------------------------------------------
[root@xy-01 sysconfig]# pwd
/etc/sysconfig
[root@xy-01 sysconfig]# cd
[root@xy-01 ~]# pwd
/root
当我们只输入cd时,当前目录会切换到用户的家目录下。
cd ~也可以实现相同效果
-------------------------------------------------------------------------------------------------
cd ..达到当前目录的上一级目录。
[root@xy-01 .ssh]# pwd
/root/.ssh
[root@xy-01 .ssh]# cd ..
[root@xy-01 ~]# pwd
/root
[root@xy-01 ~]# cd ..
[root@xy-01 /]# pwd
/
连续使用cd ..到达了根目录
-------------------------------------------------------------------------------------------------
2.8 创建和删除目录mkdir和rmdir
[root@xy-01 /]# mkdir /tmp/xylinux
[root@xy-01 /]# ls -ld /tmp/xylinux/
drwxr-xr-x. 2 root root 6 10月 18 16:12 /tmp/xylinux/
[root@xy-01 /]# date
2018年 10月 18日 星期四 16:12:53 CST
mkdir的基本用法。跟据时间我们可以看出这个目录是新创建的。
---------------------------------------------------------------------------------------------------
参数 -p
[root@xy-01 /]# mkdir -p /tmp/xylinux/1/2
[root@xy-01 /]# tree /tmp/
/tmp/
├── ks-script-srqpr5
├── systemd-private-d0b7ada2c7f94158b45312877a29a6d5-chronyd.service-j1EVy9
│ └── tmp
├── xylinux
│ └── 1
│ └── 2
└── yum.log
5 directories, 2 files
使用-p参数直接创建了级联目录。
----------------------------------------------------------------------------------------------------
参数 -v
[root@xy-01 /]# mkdir -pv /tmp/xylinux/2/3/4
mkdir: 已创建目录 "/tmp/xylinux/2"
mkdir: 已创建目录 "/tmp/xylinux/2/3"
mkdir: 已创建目录 "/tmp/xylinux/2/3/4"
可视化创建目录的过程
---------------------------------------------------------------------------------------------------
rmdir
[root@xy-01 /]# rmdir /tmp/xylinux/2
rmdir: 删除 "/tmp/xylinux/2" 失败: 目录非空
[root@xy-01 /]# rmdir /tmp/xylinux/2/3/4
[root@xy-01 /]# tree /tmp/xylinux/
/tmp/xylinux/
├── 1
│ └── 2
└── 2
└── 3
4 directories, 0 files
rmdir只能删除非空目录
----------------------------------------------------------------------------------------------------
2.9 rm命令
[root@xy-01 /]# tree /tmp/xylinux/
/tmp/xylinux/
├── 1
│ └── 2
└── 2
└── 3
└── 1.txt
4 directories, 1 file
[root@xy-01 /]# rm /tmp/xylinux/2/3/*
rm:是否删除普通空文件 "/tmp/xylinux/2/3/1.txt"?y #这里要输入y才能继续删除
[root@xy-01 /]# tree /tmp/xylinux/
/tmp/xylinux/
├── 1
│ └── 2
└── 2
└── 3
4 directories, 0 files
单纯使用rm命令删除了1.txt
-------------------------------------------------------------------------------------------------------
参数 -f
[root@xy-01 /]# touch /tmp/xylinux/2/3/1.txt
[root@xy-01 /]# rm -f /tmp/xylinux/2/3/1.txt
[root@xy-01 /]# tree /tmp/xylinux/
/tmp/xylinux/
├── 1
│ └── 2
└── 2
└── 3
4 directories, 0 files
这里加了 -f 不再有任何提示。即使有错误也不会提示。
--------------------------------------------------------------------------------------------------------
参数 -r
/tmp/xylinux/
├── 1
│ └── 2
└── 2
└── 3
4 directories, 0 files
[root@xy-01 /]# rm -r /tmp/xylinux/2
rm:是否进入目录"/tmp/xylinux/2"? y
rm:是否删除目录 "/tmp/xylinux/2/3"?y
rm:是否删除目录 "/tmp/xylinux/2"?y
[root@xy-01 /]# tree /tmp/xylinux/
/tmp/xylinux/
└── 1
└── 2
2 directories, 0 files
加了-r可以删除目录以及下边所有的子目录。
---------------------------------------------------------------------------------------------------------
2.10 环境变量
[root@xy-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
当命令出现在以上路径,那么将不需要输入绝对路径
这些路径是用:隔开的。
----------------------------------------------------------------------------------------------------------
[root@xy-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@xy-01 ~]# PATH=$PATH:/tmp/
[root@xy-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
临时将/tmp/目录加入到环境变量当中。重新在远程打开终端或者重启将恢复原状。
-----------------------------------------------------------------------------------------------------------
[root@xy-01 ~]# vi /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
.............................中略
unset i
unset -f pathmunge
PATH=$PATH:/tmp/ #文件的未尾
:wq
在/etc/profile文件的未尾加入 PATH=$PATH:/tmp/
即是在每次开机都都会加载这条语句实现永久改变PATH变量。
2.11 cp命令
cp就是复制:将源文件复制为目标文件
[root@xy-01 ~]# cp /etc/passwd /tmp/1.txt
[root@xy-01 ~]# tree /tmp
/tmp
├── 1.txt
├── ks-script-srqpr5
├── systemd-private-d0b7ada2c7f94158b45312877a29a6d5-chronyd.service-j1EVy9
│ └── tmp
└── yum.log
将/etc/passwd复制到/tmp/1.txt复制的过程中完成了改名。
---------------------------------------------------------------------------------------------------------
参数 -r
[root@xy-01 ~]# mkdir /tmp/1/
[root@xy-01 ~]# touch /tmp/1/xy.txt
[root@xy-01 ~]# tree /tmp
/tmp
├── 1
│ └── xy.txt
├── 1.txt
├── ks-script-srqpr5
├── systemd-private-d0b7ada2c7f94158b45312877a29a6d5-chronyd.service-j1EVy9
│ └── tmp
└── yum.log
3 directories, 4 files
[root@xy-01 ~]# cp -r /tmp/1/ /tmp/xy/
[root@xy-01 ~]# tree /tmp
/tmp
├── 1
│ └── xy.txt
├── 1.txt
├── ks-script-srqpr5
├── systemd-private-d0b7ada2c7f94158b45312877a29a6d5-chronyd.service-j1EVy9
│ └── tmp
├── xy
│ └── xy.txt
└── yum.log
4 directories, 5 files
参数-r可以复制目录及其中的文件。
规定:凡是目录都应该由/结尾
--------------------------------------------------------------------------------------------------------
复制目录重名的情况:
[root@xy-01 ~]# tree /tmp
/tmp
├── 1
│ └── xy.txt
├── 1.txt
├── ks-script-srqpr5
├── systemd-private-d0b7ada2c7f94158b45312877a29a6d5-chronyd.service-j1EVy9
│ └── tmp
├── xy
│ ├── 1
│ └── xy.txt
└── yum.log
5 directories, 5 files
[root@xy-01 ~]# cp -r /tmp/xy/1/ /tmp/1/
[root@xy-01 ~]# tree /tmp
/tmp
├── 1
│ ├── 1
│ └── xy.txt
├── 1.txt
├── ks-script-srqpr5
├── systemd-private-d0b7ada2c7f94158b45312877a29a6d5-chronyd.service-j1EVy9
│ └── tmp
├── xy
│ ├── 1
│ └── xy.txt
└── yum.log
6 directories, 5 files
当源目录与目标目录重名时,被复制的目录会自动复制到目标目录的下一级
-------------------------------------------------------------------------------------------------------------
2.12 mv命令
mv就是move移动命令。
[root@xy-01 ~]# ls
anaconda-ks.cfg
[root@xy-01 ~]# mv anaconda-ks.cfg anaconda-ks.cfg.1
[root@xy-01 ~]# ls
anaconda-ks.cfg.1
在原目录使用mv命令相当于给文件改名。
注意这里使用的是相对路径,都是在本级目录做的操作。
----------------------------------------------------------------------------------------------------------------
[root@xy-01 tmp]# tree
.
├── 1
│ ├── 1
│ └── xy.txt
├── 1.txt
├── ks-script-srqpr5
├── systemd-private-d0b7ada2c7f94158b45312877a29a6d5-chronyd.service-j1EVy9
│ └── tmp
├── xy
│ ├── 1
│ └── xy.txt
└── yum.log
6 directories, 5 files
[root@xy-01 tmp]# mv 1.txt xy/
[root@xy-01 tmp]# tree
.
├── 1
│ ├── 1
│ └── xy.txt
├── ks-script-srqpr5
├── systemd-private-d0b7ada2c7f94158b45312877a29a6d5-chronyd.service-j1EVy9
│ └── tmp
├── xy
│ ├── 1
│ ├── 1.txt
│ └── xy.txt
└── yum.log
6 directories, 5 files
不改名更换目录
--------------------------------------------------------------------------------------------------------------------------
复制目录的三种情况。
源目录与目标目录重名:移动至目标目录的下一级
源目录与目标目录不存在:将源目录改名
源目录与目录目录的下一级目录中的一个重名:提示是否覆盖
mv命令与cp命令用法很相似,但是复制结束后源文件或目录会消失。
-------------------------------------------------------------------------------------------------------------------------
2.13 文档查看cat more less head tail
cat 查看文件内容
[root@xy-01 xy]# cat xy.txt
1
2
3
4
5
6
7
8
9
[root@xy-01 xy]# tac xy.txt
9
8
7
6
5
4
3
2
1
cat正序查看文件内容,tac反序查看文件内容
这两个命令用法相同,只是内容顺序相反。
[root@xy-01 xy]# cat -A xy.txt
1$
2$
3$
4$
5$
6$
7$
8$
9$
参数-A显示所有内容,这里显示出了结束符$
[root@xy-01 xy]# cat -n xy.txt
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
参数-n为内容增加了行号
----------------------------------------------------------------------------------------------------------------
more分屏显示文件内容。
按空格显示下一页内容,至结尾自动退出
Control+B显示下一页内容。
----------------------------------------------------------------------------------------------------------------
less可以按箭头上下每次移动一行
Control+B显示下一页
Control+F显示上一页。
当看到文件结尾时并不会自动退出,需要按Q键。
/+字符会自动在文件内从开头向结尾查询该字符所在的位置。按n键会跳转到下一个。按SHIFT+n会跳转到上一个。
?+字符会自动在文件内从结尾向开头查询该字符所在的位置。按n键会跳转到下一个。按SHIFT+n会跳转到上一个。
按g定位到行首,按shift+g定位到行尾。
-----------------------------------------------------------------------------------------------------------------
head 和tail分别显示一个文件的前十行和末十行。
head -n 数字:显示一个文件的前(数字)行。
tail -n 数字:显示一个文件的末(数字)行。
tail -f 文件:动态显示一个文件的后十行。执行后不会退出程序 ,结束时需按ctrl+C。
2.14 文件或目录权限chmod
[root@xy-01 xy]# ll
总用量 8
drwxr-xr-x. 2 root root 6 10月 18 22:46 1
-rw-r--r--. 1 root root 846 10月 18 22:27 1.txt
-rw-r--r--. 1 root root 21 10月 18 23:08 xy.txt
使用ls -l或ll命令后会显示一个文件的权限(标红部分)
去掉头尾,一共显示了9位字符。
分别以每3个字符分为3段。
第1段指文件所有者权限
第2段指文件所属组权限
第3段指文件其他用户权限
每段中的3个字符分别表示:
第1位 r 是否可读
第2位 w 是否可写
第3位 x 是否可执行
权限的数字表示法:
r=4 w=2 x=1
例:权限rwx=4+2+1=7 权限r--=4
---------------------------------------------------------------------------------------------------------------------
chomd
[root@xy-01 xy]# ll
-rw-r--r--. 1 root root 21 10月 18 23:08 xy.txt
[root@xy-01 xy]# chmod 700 xy.txt
[root@xy-01 xy]# ll
-rwx------. 1 root root 21 10月 18 23:08 xy.txt
将xy.txt的权限由rw-r--r--(644)改为了rwx------(700)
chmod -R如果对一个目录使用会使其下的文件和子目录得到相等的权限。
---------------------------------------------------------------------------------------------------------------------
另一种权限授予方法。
a=全部
u=用户
g=所属组
o=其他用户。
例如:
[root@xy-01 xy]# ll xy.txt
-rwx------. 1 root root 21 10月 18 23:08 xy.txt
[root@xy-01 xy]# chmod a+x xy.txt
[root@xy-01 xy]# ll xy.txt
-rwx--x--x. 1 root root 21 10月 18 23:08 xy.txt
为所有用户授予了权限
2.15 更改所有者和所属组chown chgrp
[root@xy-01 xy]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
......................中略
user1:x:1000:1000::/home/user1:/bin/bash
userxy:x:1001:1001::/home/userxy:/bin/bash
查看系统中的所有用户。其中user1和userxy是新创建的用户。
[root@xy-01 xy]# ll xy.txt
-rwx--x--x. 1 root root 21 10月 18 23:08 xy.txt
[root@xy-01 xy]# chown userxy xy.txt
[root@xy-01 xy]# ll xy.txt
-rwx--x--x. 1 userxy root 21 10月 18 23:08 xy.txt
将xy.txt的所有者从root变更为了userxy
[root@xy-01 xy]# ll xy.txt
-rwx--x--x. 1 userxy root 21 10月 18 23:08 xy.txt
[root@xy-01 xy]# chgrp user1 xy.txt
[root@xy-01 xy]# ll xy.txt
-rwx--x--x. 1 userxy user1 21 10月 18 23:08 xy.txt
将xy.txt的所属组从root变更为了user1
[root@xy-01 xy]# ll xy.txt
-rwx--x--x. 1 userxy user1 21 10月 18 23:08 xy.txt
[root@xy-01 xy]# chown :root xy.txt
[root@xy-01 xy]# ll xy.txt
-rwx--x--x. 1 userxy root 21 10月 18 23:08 xy.txt
使用chown变更文件年所属组。冒号前如果写入所有者将连所有者也变更
---------------------------------------------------------------------------------------------------------------
参数 -R
对目录使用更改所有者或所属组时,
增加-R参数会使其下的子目录或文件都受到这条命令的影响。
2.16 umask
[root@xy-01 xy]# umask
0022
显示当前用户的umask值
[root@xy-01 xy]# umask
0022
[root@xy-01 xy]# touch 333.txt
[root@xy-01 xy]# ll 333.txt
-rw-r--r--. 1 root root 0 10月 19 00:26 333.txt #umask值为0022时创建文件的权限值为644
[root@xy-01 xy]# mkdir 456
[root@xy-01 xy]# ll -d 456
drwxr-xr-x. 2 root root 6 10月 19 00:27 456 #umask值为0022时创建目录的权限值为755
[root@xy-01 xy]# umask 002 #将umask值变更为0002(首位可无视)
[root@xy-01 xy]# umask
0002
[root@xy-01 xy]# touch 444.txt
[root@xy-01 xy]# ll 444.txt
-rw-rw-r--. 1 root root 0 10月 19 00:28 444.txt #umask值为0002时创建文件的权限值为664
[root@xy-01 xy]# mkdir 567
[root@xy-01 xy]# ll -d 567
drwxrwxr-x. 2 root root 6 10月 19 00:28 567 #umask值为0002时创建目录的权限值为775
文件和目录的默认权限区别在于:目录必须要有x权限才能打开目录。
因此文件和目录的默认最高权限分别为:
文件:rw-(所有者)rw-(所属组)rw-(其他)=666
目录:rwx(所有者)rwx(所属组)rwx(其他)=777
计算方式:
umask值的三位分别表示: 所有者 所属组 其他
当前用户的每一位权限位去减umask值的权限位。
即为当前用户创建时的权限。
注1:这里的减法是用权限位(rwx)去减,而不是权限值(数字)
注2:当-减任何值都为-
转载于:https://blog.51cto.com/14019809/2304901