关于目录
一、创建目录
## 创建一个目录
mkdir dir1
## 创建多个目录
mkdir dir1 dir2
## 创建多级目录
mkdir -p /a/b/c/d/e/f/g ## -p 创建多级目录
# 查看
[root@c702 ~]# ls -R /a
/a:
b
/a/b:
c
/a/b/c:
d
/a/b/c/d:
e
/a/b/c/d/e:
f
/a/b/c/d/e/f:
g
/a/b/c/d/e/f/g:
二、删除目录
rmdir /dir ## 只能删除目录
rm -r /a ## 需要逐一确认
rm:是否进入目录"/a"? y
rm:是否进入目录"/a/b"? y
rm:是否进入目录"/a/b/c"? y
rm:是否进入目录"/a/b/c/d"? y
rm:是否进入目录"/a/b/c/d/e"? y
rm:是否进入目录"/a/b/c/d/e/f"? y
rm:是否删除目录 "/a/b/c/d/e/f/g"?y
rm:是否删除目录 "/a/b/c/d/e/f"?y
rm:是否删除目录 "/a/b/c/d/e"?y
rm:是否删除目录 "/a/b/c/d"?y
rm:是否删除目录 "/a/b/c"?y
rm:是否删除目录 "/a/b"?y
rm:是否删除目录 "/a"?y
rm -rf / a ## 如果/ a 之前有空格,表示 删除根目录和a目录,这样系统就瘫痪了
rm -rf /a ## 删除非空目录
三、复制目录
## 复制目录
cp 源文件地址 目标文件地址
cp /root/a /tmp ## 复制 /root目录下的a目录,到/tmp目录下,tmp目录是系统的临时目录
cp: 略过目录"/root/a" ## 出现这个原因是 cp只能复制文件,不能复制目录
## 想要复制目录需要添加参数 -r
[root@c702 /]# cp -r /root/a /tmp
[root@c702 /]# ll /tmp
总用量 0
drwxr-xr-x. 3 root root 15 10月 29 10:42 a # 可以看到复制成功了
drwx------. 3 root root 17 10月 26 09:43 systemd-private-d17b75f7b9b64400987bafc36f0c473f-chronyd.service-JjiAYz
drwx------. 2 root root 6 10月 26 09:43 vmware-root_6210-692686742
## 复制文件
# 首先创建一个文件
touch /filea
cp /filea /tmp
[root@c702 /]# ll /tmp/
总用量 0
drwxr-xr-x. 3 root root 15 10月 29 10:42 a
-rw-r--r--. 1 root root 0 10月 29 10:45 filea ## 复制成功
drwx------. 3 root root 17 10月 26 09:43 systemd-private-d17b75f7b9b64400987bafc36f0c473f-chronyd.service-JjiAYz
drwx------. 2 root root 6 10月 26 09:43 vmware-root_6210-692686742
## 如果想要显示复制的进度
[root@c702 /]# cp -v /filea /tmp
cp:是否覆盖"/tmp/filea"? y ## y表示yes, n 表示no
"/filea" -> "/tmp/filea"
## 复制时保留文件创建的时间
cp -p /filea /tmp
四、移动目录
移动文件
mv 原文件 目标文件
## 在linux的底层架构,移动文件就是重命名文件
[root@c702 /]# mv /filea /fileb
[root@c702 /]# ls ## ls查看只有fileb
bin boot cyy cyy1 cyy2 cyy3 dev etc fileb home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
## 将fileb移动到 /tmp目录下
[root@c702 /]# mv /fileb /tmp
[root@c702 /]# ll /tmp/fileb
-rw-r--r--. 1 root root 0 10月 29 10:45 /tmp/fileb
## 将/tmp/fileb 移动到/目录下 ,并重命名为filec
[root@c702 /]# mv /tmp/fileb /filec
[root@c702 /]# ll /filec
-rw-r--r--. 1 root root 0 10月 29 10:45 /filec
移动目录
mv 原目录 目标目录
mkdir /dirc
mv /dirc /tmp
[root@c702 /]# ll /tmp/
总用量 0
drwxr-xr-x. 3 root root 15 10月 29 10:42 a
drwxr-xr-x. 2 root root 6 10月 29 11:04 dirc
-rw-r--r--. 1 root root 0 10月 29 10:45 filea
drwx------. 3 root root 17 10月 26 09:43 systemd-private-d17b75f7b9b64400987bafc36f0c473f-chronyd.service-JjiAYz
drwx------. 2 root root 6 10月 26 09:43 vmware-root_6210-692686742
五、通配符
用途 : 操作多个相似(简单有规律)的文件
常用通配符
- *:匹配任何字符串
- ?:匹配1个字符串
- [xyz]: 匹配xyz任意一个字符
- [a-z] : 匹配一个范围
- [!xyz] : 不匹配 xyz
六、查看文件权限
[root@c702 ~]# ll
总用量 20
drwxr-xr-x. 3 root root 15 10月 29 10:40 a
-rw-------. 1 root root 1257 4月 24 2020 anaconda-ks.cfg
###
第一组字段:第一个字母表示文件的类型 后面的9位字母表示的这个文件的所有者 所属组 其他用户对这个文件拥有的权限
r:表示可读 数字表示方法为 r=4
w:表示可写 数字表示方法为 w=2
x:表示可执行 数字表示方法为 x=1
权限的部分如果是- 表示没有这个权限
第二字段: 表示这个目录下有多少个文件
第三字段: 表示所属用户和组
第四个字段: 表示这个文件的大小
第五字段: 表示这个文件最后的修改时间是什么时候
第六个字段: 表示这个文件的名字
###
文件类型
- -:普通文件
- d:目录文件
- b:块特殊文件
- c:字符特殊文件
- l:符号链接
- f:命名管道
- s:套接字文件
目录权限的表示方法
x:进入目录
rx:显示目录内的文件名
wx:修改目录内的文件名
七、修改目录文件权限
-
chmod 修改文件 目录权限
- chmod u+x /tmp/dir1
- chmod 755 /tmp/dir2
## 字符修改 文件的权限 u: 修改属主的权限 g: 修改属组的权限 o: 修改其他用户的权限 a: 同时修改属主,属组,其他用户的权限 ## 首先在 /tmp/dir1 目录下创建测试文件 # cd /tmp/dir1 # touch file1 file2 [root@c702 /tmp/dir1]# ll 总用量 0 -rw-r--r--. 1 root root 0 11月 5 15:56 file1 -rw-r--r--. 1 root root 0 11月 5 15:56 file2 # # chmod u=rwx file1 # ll 总用量 0 -rwxr--r--. 1 root root 0 11月 5 15:56 file1 # chmod g+w file1 # ll 总用量 0 -rwxrw-r--. 1 root root 0 11月 5 15:56 file1 # chmod o-r file1 # ll 总用量 0 -rwxrw----. 1 root root 0 11月 5 15:56 file1 # chmod a=rwx file1 [root@c702 /tmp/dir1]# ll 总用量 0 -rwxrwxrwx. 1 root root 0 11月 5 15:56 file1 +:表示增加权限 -:表示减去权限 =:表示拥有什么权限 ## 数字修改文件的权限,用我们刚刚创建的测试文件file2 [root@c702 /tmp/dir1]# chmod 700 file2 [root@c702 /tmp/dir1]# ll 总用量 0 -rwx------. 1 root root 0 11月 5 15:56 file2 [root@c702 /tmp/dir1]# chmod 741 file2 [root@c702 /tmp/dir1]# ll 总用量 0 -rwxr----x. 1 root root 0 11月 5 15:56 file2 [root@c702 /tmp/dir1]# chmod 333 file2 [root@c702 /tmp/dir1]# ll 总用量 0 --wx-wx-wx. 1 root root 0 11月 5 15:56 file2 [root@c702 /tmp/dir1]# chmod 755 file2 [root@c702 /tmp/dir1]# ll 总用量 0 -rwxr-xr-x. 1 root root 0 11月 5 15:56 file2 [root@c702 /tmp/dir1]# chmod 764 file2 [root@c702 /tmp/dir1]# ll 总用量 0 -rwxrw-r--. 1 764 root 0 11月 5 15:56 file2 1:表示只有执行权限 2:表示只有写的权限 4:表示只有读的权限 0:表示没有任何权限 3:表示只有执行和写的权限 5:表示只有执行和读的权限 6:表示只有读和写的权限 7:表示拥有读写和执行的权限
-
chown 修改属主 属组
## 创建目录时,这个目录默认的属主是当前用户,属组是当前用户的属组 # mkdir /tmp/dir1 # mkdir /tmp/dir2 # ll /tmp/ 总用量 47256 drwxr-xr-x. 2 root root 6 11月 5 15:27 dir1 drwxr-xr-x. 2 root root 6 11月 5 15:27 dir2 drwxr-xr-x 将这个分成四部分。 d rwx 这个文件的属主拥有的权限 r-x 这个文件的属组拥有的权限 r-x 这个文件的其他用户拥有的权限 ## ,这是一个普通用户 # id user1 uid=2008(user1) gid=2008(group1) 组=2008(group1) ## 将dir1 的属主修改为user1 # chown user1 /tmp/dir1 # ll /tmp/ 总用量 47256 drwxr-xr-x. 2 user1 root 6 11月 5 15:27 dir1 ## 将dir1 的属组修改为user1 的属主 # chown :group1 /tmp/dir1 # ll /tmp/ 总用量 47256 drwxr-xr-x. 2 user1 group1 6 11月 5 15:27 dir1
-
chgrp 可以单独更改属组,不常用
八、特殊权限
-
SUID:属于二进制可执行文件,执行命令时去的文件属主权限,比如/usr/bin/passwd 文件
[root@c702 ~]# ls -l /usr/bin/passwd -rwsr-xr-x. 1 root root 27832 6月 10 2014 /usr/bin/passwd ## 想要给其他文件添加suid的权限,用chmod 4+原有权限 [root@c702 ~]# ll /tmp/dir1/file2 -rwxrw-r--. 1 764 root 0 11月 5 15:56 /tmp/dir1/file2 [root@c702 ~]# chmod 4764 /tmp/dir1/file2 [root@c702 ~]# ll /tmp/dir1/file2 -rwsrw-r--. 1 764 root 0 11月 5 15:56 /tmp/dir1/file2
-
SGID:用于目录,在该目录下创建新的文件和目录,权限自动更改为目录的属组
-
SBIT:用于目录,该目录下新建的文件和目录,仅root和自己可以删除 如/tmp
[root@c702 ~]# ls -ld /tmp
drwxrwxrwt. 11 root root 4096 11月 5 15:27 /tmp
## 如果想要给其他目录添加这个权限 chmod 1+原有权限
[root@c702 /tmp]# ll -d /tmp/dir1
drwxr-xr-x. 2 user1 group1 19 11月 5 16:03 /tmp/dir1
[root@c702 /tmp]# chmod 1755 /tmp/dir1
[root@c702 /tmp]# ll -d /tmp/dir1
drwxr-xr-t. 2 user1 group1 19 11月 5 16:03 /tmp/dir1