2.6 相对路径和绝对路径
绝对路径:一个文件从根开始写起的完整路径,无论当前在什么位置,都可以通过该路径找到要找的文件
/etc/hostname
相对路径:不以根开头,相对当前位置来说的路径,只有在当前位置才能使用该相对路径找到该文件
[root@hyc-01-01 /]# ls etc/hostname
etc/hostname
pwd命令
[root@hyc-01-01 ~]# pwd 查看当前所在的路径
/root
cd命令
[root@hyc-01-01 ~]# cd / 移动到指定目录下
[root@hyc-01-01 /]#
2.7 cd命令
- 切换到上一次所在的目录
[root@hyc-01-01 /]# cd /root
[root@hyc-01-01 ~]# cd /home/hyc
[root@hyc-01-01 hyc]# cd -
/root
[root@hyc-01-01 ~]# cd -
/home/hyc
[root@hyc-01-01 hyc]#
cd
[root@hyc-01-01 hyc]# cd 只输入cd会回到当前用户家目录下
[root@hyc-01-01 ~]# pwd
/root
~
[root@hyc-01-01 etc]# cd ~ 进入当前用户家目录
[root@hyc-01-01 ~]#
..
[root@hyc-01-01 ~]# cd .. 进入当前目录的上一级目录
[root@hyc-01-01 /]#
/是最上面的目录,在/下敲cd ..会继续留在当前位置
2.8 创建和删除目录(mkdir/rmdir)
mkdir 创建目录
[root@hyc-01-01 /]# mkdir /tmp/hyc
[root@hyc-01-01 /]# ls -ld /tmp/hyc
drwxr-xr-x. 2 root root 6 6月 3 16:39 /tmp/hyc
[root@hyc-01-01 /]# date 显示当前日期、时间
2018年 06月 03日 星期日 16:41:29 CST
-p 批量创建目录
[root@hyc-01-01 /]# mkdir /tmp/hyc/1/2/
mkdir: 无法创建目录"/tmp/hyc/1/2/": 没有那个文件或目录
[root@hyc-01-01 /]# mkdir -p /tmp/hyc/1/2/
[root@hyc-01-01 /]# mkdir -p /tmp/hyc/1/2/
[root@hyc-01-01 /]# tree /tmp/hyc
/tmp/hyc
└── 1
└── 2
2 directories, 0 files
-v
[root@hyc-01-01 /]# mkdir -pv /tmp/hyc/5/6/ -v使创建过程可视化
mkdir: 已创建目录 "/tmp/hyc/5"
mkdir: 已创建目录 "/tmp/hyc/5/6/"
rmdir 删除空目录
[root@hyc-01-01 /]# rmdir /tmp/hyc/1/
rmdir: 删除 "/tmp/hyc/1/" 失败: 目录非空
[root@hyc-01-01 /]# rmdir /tmp/hyc/1/2
[root@hyc-01-01 /]#
rmdir在被删除目录下有子目录或文件时均无法删除该目录
[root@hyc-01-01 /]# touch /tmp/hyc/1/2 在目录1下创建空白文件2
[root@hyc-01-01 /]# rmdir /tmp/hyc/1
rmdir: 删除 "/tmp/hyc/1" 失败: 目录非空
[root@hyc-01-01 /]# tree /tmp/hyc
/tmp/hyc
├── 1
│ └── 2
├── 3
│ └── 4
└── 5
└── 6
5 directories, 1 file
-p 级联删除空目录
[root@hyc-01-01 /]# rmdir -p /tmp/hyc/3/4
rmdir: 删除目录 "/tmp/hyc" 失败: 目录非空
[root@hyc-01-01 /]# tree /tmp/hyc 3/4目录已被删除
/tmp/hyc
├── 1
│ └── 2
└── 5
└── 6
3 directories, 1 file
2.9 rm命令
rm可以删除文件和(非)空目录
[root@hyc-01-01 /]# rm /tmp/hyc/1/2
rm:是否删除普通空文件 "/tmp/hyc/1/2"?y 删除文件前会询问是否删除
[root@hyc-01-01 /]# tree /tmp/hyc
/tmp/hyc
├── 1
└── 5
└── 6
3 directories, 0 files
-f 强制删除
ls /tmp/hyc/1
2.txt 3.txt
[root@hyc-01-01 /]# rm -f /tmp/hyc/1/* *代表通配,指1目录下的所有内容,由于1目录下内容都有.txt后缀,所以也可以写成*.txt
[root@hyc-01-01 /]# ls /tmp/hyc/1
[root@hyc-01-01 /]#
!用法:
[root@hyc-01-01 /]# history
…
282 ls /tmp/hyc/1
283 rm -f /tmp/hyc/*
284 rm -f /tmp/hyc/1
285 rm -f /tmp/hyc/1/*
286 ls /tmp/hyc/1
287 history
[root@hyc-01-01 /]# !ls 这种写法表示在命令历史中(即history中)找到最近执行的ls命令并将其重复执行一遍
ls /tmp/hyc/1
-r rm删除目录
[root@hyc-01-01 /]# rm -r /tmp/hyc/5
rm:是否进入目录"/tmp/hyc/5"? y
rm:是否删除目录 "/tmp/hyc/5/6"?y
rm:是否删除目录 "/tmp/hyc/5"?y
[root@hyc-01-01 /]# rm -rf /tmp/hyc/1 rf配合使用,删除目录和文件并且不会提示
[root@hyc-01-01 /]#
[root@hyc-01-01 /]# mkdir -p /tmp/hyc/1/1.txt
[root@hyc-01-01 /]# mkdir -p /tmp/hyc/1/2/2.txt
[root@hyc-01-01 /]# tree /tmp/hyc
/tmp/hyc
└── 1
├── 1.txt
└── 2
└── 2.txt
4 directories, 0 files
[root@hyc-01-01 /]# rm -rfv /tmp/hyc/1/2 删除目录时需要先删光该目录下的文件或子目录,然后再删除该目录
已删除目录:"/tmp/hyc/1/2/2.txt"
已删除目录:"/tmp/hyc/1/2"
注意:
[root@hyc-01-01 /]# !tree 1目录下并没有2目录
tree /tmp/hyc
/tmp/hyc
└── 1
└── 1.txt
2 directories, 0 files
[root@hyc-01-01 /]# rm -rfv /tmp/hyc/1/2 若执行rm时带f选项则无论如何都执行该条命令并不会报错
[root@hyc-01-01 /]# rm -rv /tmp/hyc/1/2
rm: 无法删除"/tmp/hyc/1/2": 没有那个文件或目录