绝对路径和相对路径
- 绝对路径:由根目录/开头的路径;例如:/etc/sysconfig/network-scripts/ifcfg-ens33
[root@linux-128 ~]# ls /etc/sysconfig/network-scripts/ifcfg-ens33
/etc/sysconfig/network-scripts/ifcfg-ens33
- 相对路径:相对于当前位置的路径;不是以根目录开头的路径。例如:sysconfig/network-scripts/ifcfg-ens33
[root@linux-128 ~]# cd /etc/
[root@linux-128 etc]# ls sysconfig/network-scripts/ifcfg-ens33
sysconfig/network-scripts/ifcfg-ens33
sysconfig/network-scripts/ifcfg-ens33相对于/etc目录来讲就是相对路径。
cd命令
- 命令cd(change directory缩写)使用来改变用户所在的目录;例如:
[root@linux-128 ~]# pwd
/root
[root@linux-128 ~]# cd /etc/
[root@linux-128 etc]# pwd
/etc
若是后面什么都不跟,就会进入当前用户的家目录下面;例如:
[root@linux-128 etc]# pwd
/etc
[root@linux-128 etc]# cd
[root@linux-128 ~]# pwd
/root
[root@linux-128 ~]# su wuzhou //切换用户
[wuzhou@linux-128 root]$ cd /etc/
[wuzhou@linux-128 etc]$ cd
[wuzhou@linux-128 ~]$ pwd
/home/wuzhou //用户wuzhou的家目录
- cd命令后面只能跟目录名,若是跟文件名,则会报错;例如:
[root@linux-128 ~]# cd /tmp/yum.log
-bash: cd: /tmp/yum.log: 不是目录
[root@linux-128 local]# pwd
/usr/local
[root@linux-128 local]# cd ..
[root@linux-128 usr]# pwd
/usr
[root@linux-128 usr]# pwd
/usr
[root@linux-128 usr]# cd .
[root@linux-128 usr]# pwd
/usr
[root@linux-128 usr]# pwd
/usr
[root@linux-128 usr]# cd ~
[root@linux-128 ~]# pwd
/root
[root@linux-128 usr]# pwd
/usr
[root@linux-128 usr]# cd /tmp
[root@linux-128 tmp]# cd -
/usr
[root@linux-128 usr]# cd -
/tmp
mkdir命令
- 命令mkdir(make directory简写)用于建立目录,格式以下: mkdir [选项] [目录名称]
[root@linux-128 ~]# mkdir /tmp/test/
[root@linux-128 ~]# ls /tmp/
ks-script-23u7xi test yum.log
[root@linux-128 ~]#
- -p能建立一大串级联目录;若是不加-p就会报错;例如: mkdir –p [目录名称]
[root@linux-128 ~]# mkdir /tmp/test/1/2/3
mkdir: 没法建立目录"/tmp/test/1/2/3": 没有那个文件或目录
[root@linux-128 ~]# mkdir -p /tmp/test/1/2/3
[root@linux-128 ~]# tree /tmp
/tmp
├── ks-script-23u7xi
├── systemd-private-5733ad3db50b4bfd85ef62fb0d460b4e-vmtoolsd.service-XXd56I
│ └── tmp
│ └── vmware-root
├── test
│ └── 1
│ └── 2
│ └── 3
└── yum.log
- 若是建立一个已经存在的目录会报错,加上-p后就不会报错;例如:
[root@linux-128 ~]# mkdir /tmp/test
mkdir: 没法建立目录"/tmp/test": 文件已存在
[root@linux-128 ~]# mkdir -p /tmp/test
rmdir命令
- 命令rmdir(remove directory简写)用于删除空目录,后面能够是一个目录,也能够是多个目录;例如:
[root@linux-128 ~]# mkdir /tmp/111
[root@linux-128 ~]# mkdir /tmp/222
[root@linux-128 ~]# ls /tmp
111 222 ks-script-23u7xi test yum.log
[root@linux-128 ~]# rmdir /tmp/111/ /tmp/222/
[root@linux-128 ~]# ls /tmp
ks-script-23u7xi test yum.log
[root@linux-128 ~]# rmdir /tmp/yum.log
rmdir: 删除 "/tmp/yum.log" 失败: 不是目录
- rmdir和mkdir有共同选项-p,能删除一大窜目录,可是在联级的目录中,若是某一个目录里面还有目录或者文件,这个命令就很差用,会报错;例如:
[root@linux-128 ~]# rmdir /tmp/test
rmdir: 删除 "/tmp/test/" 失败: 目录非空
[root@linux-128 tmp]# tree
.
├── 111
├── ks-script-23u7xi
│ └── tmp
│ └── vmware-root
├── test
│ └── 1
│ └── 2
│ └── 3
└── yum.log
8 directories, 2 files
[root@linux-128 tmp]# rmdir -p test/1/2/3/
[root@linux-128 tmp]# tree
.
├── 111
├── ks-script-23u7xi
│ └── tmp
│ └── vmware-root
└── yum.log
rmdir使用起来有必定的局限性,因此用的不多,能够使用rm来删除目录或者文件。
rm命令
[root@linux-128 tmp]# touch test.txt
[root@linux-128 tmp]# ls
111 ks-script-23u7xi test.txt yum.log
[root@linux-128 tmp]# rm test.txt
rm:是否删除普通空文件 "test.txt"?y
[root@linux-128 tmp]# ls
111 ks-script-23u7xi yum.log
rm –r [目录名] 删除目录;例如:
[root@linux-128 tmp]# rm -r 111
rm:是否删除目录 "111"?y
[root@linux-128 tmp]# ls
ks-script-23u7xi yum.log
- -f (forces)强制删除,它不会在提示是否删除,而是直接删除。若是后面跟一个不存在的文件或者目录,它也不会报错;例如:
[root@linux-128 tmp]# touch 1.txt 2.txt
[root@linux-128 tmp]# ls
1.txt 2.txt ks-script-23u7xi yum.log
[root@linux-128 tmp]# rm -f 1.txt
[root@linux-128 tmp]# ls
2.txt ks-script-23u7xi yum.log
[root@linux-128 tmp]# ls
2.txt ks-script-23u7xi yum.log //目录根本没有33.txt文件,它也没报错
[root@linux-128 tmp]# rm -f 33.txt
- 若是要删除目录,必需要加上-r,否则就算加上-f选项也会报错;例如:
[root@linux-128 tmp]# mkdir 111
[root@linux-128 tmp]# rm -f 111
rm: 没法删除"111": 是一个目录
[root@linux-128 tmp]# mkdir -p test/1/2/3/1.txt
[root@linux-128 tmp]# tree
.
├── ks-script-23u7xi
│ └── tmp
│ └── vmware-root
├── test
│ └── 1
│ └── 2
│ └── 3
│ └── 1.txt
└── yum.log
8 directories, 2 files
[root@linux-128 tmp]# rm -rvf test
已删除目录:"test/1/2/3/1.txt"
已删除目录:"test/1/2/3"
已删除目录:"test/1/2"
已删除目录:"test/1"
已删除目录:"test"
注意:rm -rf虽然好用,可是要千万注意,rm –rf 命令后面千万不要加“/”,不然它会把系统文件所有删除,是很是危险滴!