[root@aminglinux-01 ~]# pwd 查看当前路径
/root
[root@aminglinux-01 ~]# ls .ssh/authorized_keys 这就是相对路径,相对于root
.ssh/authorized_keys
[root@aminglinux-01 ~]# cd /etc/sysconfig
[root@aminglinux-01 sysconfig]# ls network-scripts/ifcfg-eno16777736 这也是一个相对路径,相对于/etc/sysconfig
network-scripts/ifcfg-eno16777736
[root@aminglinux-01 sysconfig]# pwd
/etc/sysconfig
cd 等于change directory
cd命令
- 参数,"cd -"进入上一次所在目录
只输入"cd",进入用户当前home目录下,也就是登陆系统时用户的家目录
~ 参数,"cd ~"表示切换到用户home目录,等于cd命令
.. 参数,"cd .."表示切换到上层目录,逐级返回
cd -命令,就表示你上一次所在的目录在哪里
[root@hf-01 ~]# cd /etc/sysconfig
[root@hf-01 sysconfig]# cd -
/root
[root@hf-01 ~]# cd -
/etc/sysconfig
[root@hf-01 sysconfig]# pwd
/etc/sysconfig
[root@hf-01 sysconfig]# cd -
/root
[root@hf-01 ~]# pwd
/root
[root@hf-01 ~]# cd -
/etc/sysconfig
[root@hf-01 sysconfig]# pwd
/etc/sysconfig
cd和cd ~命令,都是返回用户的家目录
[root@hf-01 sysconfig]# cd
[root@hf-01 ~]# cd /tmp/
[root@hf-01 tmp]# cd
[root@hf-01 ~]# pwd
/root
[root@hf-01 ~]# cd ~ 这个~则表示/root
cd .. 命令,表示返回到上层目录
[root@hf-01 ~]# cd .. 返回的是根目录,到达根目录则无法返回了
[root@hf-01 /]# cd ..
[root@hf-01 /]# cd /etc/sysconfig/network-scripts/
[root@hf-01 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@hf-01 network-scripts]# cd ..
[root@hf-01 sysconfig]# pwd
/etc/sysconfig
[root@hf-01 sysconfig]# cd .. 这里会看见逐级返回上层目录
[root@hf-01 etc]# pwd
/etc
[root@hf-01 etc]# cd ..
[root@hf-01 /]# pwd
/
当想查看cd的其他用法,可以man cd 查看所有cd的用法
[root@hf-01 ~]# mkdir /tmp/aminglinux
[root@hf-01 ~]# ls -ld /tmp/aminglinux/
drwxr-xr-x. 2 root root 6 10月 24 05:58 /tmp/aminglinux/
[root@hf-01 ~]# date 显示出当前时间
2017年 10月 24日 星期二 05:59:00 CST
[root@hf-01 ~]# mkdir /tmp/aminglinux/1/2/
mkdir: 无法创建目录"/tmp/aminglinux/1/2/": 没有那个文件或目录
就是说/tmp目录下存在/aminglinux目录,而不存在/1和/2目录
[root@hf-01 ~]# ls -l /tmp/aminglinux/
总用量 0
mkdir -p 可以级联创建目录
[root@hf-01 ~]# mkdir -p /tmp/aminglinux/1/2/
[root@hf-01 ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 3 root root 14 10月 24 06:07 1
[root@hf-01 ~]# ls -l /tmp/aminglinux/1
总用量 0
drwxr-xr-x. 2 root root 6 10月 24 06:07 2
mkdir -pv 可以看见创建目录的可视化过程
[root@hf-01 ~]# mkdir -pv /tmp/aminglinux/2/3/4
mkdir: 已创建目录 "/tmp/aminglinux/2"
mkdir: 已创建目录 "/tmp/aminglinux/2/3"
mkdir: 已创建目录 "/tmp/aminglinux/2/3/4"
[root@hf-01 ~]# rmdir /tmp/aminglinux/2
rmdir: 删除 "/tmp/aminglinux/2" 失败: 目录非空
**这就表示rmdir可以删除目录,但前提是这个目录是空的**
[root@hf-01 ~]# rmdir /tmp/aminglinux/2/3/4 这时4目录就删除了
[root@hf-01 ~]# touch /tmp/aminglinux/2/3/1.txt 这时在目录下创建一个文件
[root@hf-01 ~]# ls /tmp/aminglinux/2/3
1.txt
[root@hf-01 ~]# tree /tmp/aminglinux/ 若没有tree命令,则yum install -y tree
/tmp/aminglinux/
├── 1
│ └── 2
└── 2
└── 3
└── 1.txt
4 directories, 1 file
[root@hf-01 ~]# rmdir /tmp/aminglinux/2/3
rmdir: 删除 "/tmp/aminglinux/2/3" 失败: 目录非空
这时发现也是不能删除,因为目录下有个文件
[root@hf-01 ~]# rmdir /tmp/aminglinux/2/3/1.txt
rmdir: 删除 "/tmp/aminglinux/2/3/1.txt" 失败: 不是目录
**若非要删除,则可以使用rm命令**
rmdir -p (parents)可以级联的删除空目录
[root@hf-01 ~]# rmdir -p /tmp/aminglinux/1/2
rmdir: 删除目录 "/tmp/aminglinux" 失败: 目录非空
[root@hf-01 ~]# tree /tmp/aminglinux/ 这里会发现/1/2这两个目录已经被删除了
/tmp/aminglinux/
└── 2
└── 3
└── 1.txt
2 directories, 1 file
rmdir有一定的局限性,只能删除目录,不能删除文件
rm命令可以删除文件,还可以删除非空的目录 (单独使用rm命令会提示是否进行删除)
-f (force)强制执行,只能删除文件,不能删除目录
-r 等同于 -recursive 级联的把这些目录删掉 删除过程,就是有文件,先删除文件,之后在删除目录
-v 使运行结果可视化
使用时,最好带上 v 能可视化结果
rm -rfv 在加入-f选项后,若没有该文件或目录,不会产生任何提示
history 命令可以查看使用过的命令 使用 !后跟命令会去history中执行最近一次执行的这个命令
当使用man命令的时候去查看某一相关字符,去搜索的时候,直接/加搜索的字符,空格后然后回车,要退出的时候,按q退出!
[root@hf-01 ~]# rm /tmp/aminglinux/2/3/1.txt
rm:是否删除普通空文件 "/tmp/aminglinux/2/3/1.txt"?y 这里会提示是否删除文件
[root@hf-01 ~]# tree /tmp/aminglinux/
/tmp/aminglinux/
└── 2
└── 3
2 directories, 0 files
[root@hf-01 ~]# touch /tmp/aminglinux/2/3/1.txt
[root@hf-01 ~]# touch /tmp/aminglinux/2/3/2.txt
[root@hf-01 ~]# rm /tmp/aminglinux/2/3/
1.txt 2.txt
按两次tab键,会列出目录下的文件
[root@hf-01 ~]# rm /tmp/aminglinux/2/3/*.txt
或者直接表示成rm /tmp/aminglinux/2/3/*
rm:是否删除普通空文件 "/tmp/aminglinux/2/3/1.txt"?n
rm:是否删除普通空文件 "/tmp/aminglinux/2/3/2.txt"?n
[root@hf-01 ~]# rm -f /tmp/aminglinux/2/3/*.txt
若是嫌每次确认删除麻烦,则可以加入-f(force)强制删除
[root@hf-01 ~]# !tree
tree /tmp/aminglinux/
/tmp/aminglinux/
└── 2
└── 3
2 directories, 0 files
(!tree表示历史命令中,上一次最近执行的tree命令)使用tree命令查看,发现目录下的文件已经被删除
[root@hf-01 ~]# rm -rf /tmp/aminglinux/2
这里不会有提示,会直接删除文件和目录
[root@hf-01 ~]# !tree
tree /tmp/aminglinux/
/tmp/aminglinux/
0 directories, 0 files
[root@hf-01 ~]# mkdir -pv /tmp/aminglinux/2/3/
这时再来可视化级联新建目录
mkdir: 已创建目录 "/tmp/aminglinux/2"
mkdir: 已创建目录 "/tmp/aminglinux/2/3/"
[root@hf-01 ~]# touch /tmp/aminglinux/2/3/1.txt 新建文件
[root@hf-01 ~]# touch /tmp/aminglinux/2/2.txt
[root@hf-01 ~]# !tree
tree /tmp/aminglinux/
/tmp/aminglinux/
└── 2
├── 2.txt
└── 3
└── 1.txt
2 directories, 2 files
[root@hf-01 ~]# rm -rfv /tmp/aminglinux/2/3/
可视化强制删除
已删除"/tmp/aminglinux/2/3/1.txt"
已删除目录:"/tmp/aminglinux/2/3/"
我们会发现删除目录下的文件,然后在删除目录
[root@hf-01 ~]# !tree
tree /tmp/aminglinux/
/tmp/aminglinux/
└── 2
└── 2.txt
1 directory, 1 file
[root@hf-01 ~]# rm -rfv /tmp/aminglinux/2/3/ 若删除一个不存在的目录,会发现什么提示都没有
这就是因为加入了-f选项,代表着强制删除,不管它是否存在
[root@hf-01 ~]# rm -rv /tmp/aminglinux/2/3/ 去除-f的时候,会发现提示目录不存在
rm: 无法删除"/tmp/aminglinux/2/3/": 没有那个文件或目录