相对和绝对路径、cd、rm命令

绝对路径和相对路径

  • 绝对路径:由根目录/开头的路径;例如:/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: 不是目录
  • cd .. 进入当前目录的上一级目录;例如:
[root@linux-128 local]# pwd
/usr/local
[root@linux-128 local]# cd ..
[root@linux-128 usr]# pwd
/usr
  • cd .进入当前目录;例如:
[root@linux-128 usr]# pwd
/usr
[root@linux-128 usr]# cd .
[root@linux-128 usr]# pwd
/usr
  • cd ~ 进入用户家目录;例如:
[root@linux-128 usr]# pwd
/usr
 [root@linux-128 usr]# cd ~
[root@linux-128 ~]# pwd
/root
  • cd – 进入上一次所在目录来回交替;例如:
[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
  • rmdir只能删除目录,不能删除文件;例如:
[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命令

  • 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
  • -r 用来删除目录
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": 是一个目录
  • -v 可视化,可以看见删除的步骤;例如:
[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 命令后面千万不要加“/”,否则它会把系统文件全部删除,是非常危险滴!

转载于:https://my.oschina.net/u/3706916/blog/1555165

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值