linux命令----- mkdir与rmdir

本文介绍了在Linux环境下如何使用mkdir创建目录,包括基本用法和mkdir-p用于递归创建多层目录。同时,详细讲解了rmdir命令的用途,强调它只能删除空目录,并展示了尝试删除非空目录时会遇到的错误。最后提到了rmdir-p命令用于删除包含子目录的路径。
摘要由CSDN通过智能技术生成

一 mkdir

mkdir是make directories的缩写,主要用于linux中创建目录

  • 创建的目录不能和同级目录中已经存在的目录重名
  • 可以mkdir p 递归的同时创建多个目录

在这里插入图片描述

1.mkdir 目录名

[root@localhost /]# mkdir test
[root@localhost /]# ll
总用量 20
lrwxrwxrwx.   1 root root    7 415 20:34 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 415 20:39 boot
drwxr-xr-x.  20 root root 3200 422 18:58 dev
drwxr-xr-x.  74 root root 8192 422 18:58 etc
drwxr-xr-x.   2 root root    6 411 2018 home
lrwxrwxrwx.   1 root root    7 415 20:34 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 415 20:34 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 411 2018 media
drwxr-xr-x.   2 root root    6 411 2018 mnt
drwxr-xr-x.   2 root root 4096 420 18:57 opt
dr-xr-xr-x. 104 root root    0 422 18:58 proc
dr-xr-x---.   3 root root  184 421 00:58 root
drwxr-xr-x.  24 root root  660 422 18:58 run
lrwxrwxrwx.   1 root root    8 415 20:34 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 411 2018 srv
dr-xr-xr-x.  13 root root    0 422 18:58 sys
drwxr-xr-x.   2 root root    6 422 19:54 test
drwxrwxrwt.   9 root root  232 422 19:39 tmp
drwxr-xr-x.  18 root root  233 420 05:54 usr
drwxr-xr-x.  20 root root  278 420 03:03 var

2.mkdir -p 目录一/目录二

出现了test/test1/test2的嵌套

[root@localhost test]# mkdir -p test1/test2
[root@localhost test]# ll
总用量 0
drwxr-xr-x. 3 root root 19 422 19:58 test1
[root@localhost test]# cd /test2
-bash: cd: /test2: 没有那个文件或目录
[root@localhost test]# cd test1
[root@localhost test1]# cd test2
[root@localhost test2]# 

二 rmdir

rmdir是remove directories的缩写,主要用于linux中删除目录
这个命令只能删除空目录。不能删除非空目录

1.rmdir 目录名

[root@localhost test2]# mkdir test4
[root@localhost test2]# ll
总用量 0
drwxr-xr-x. 2 root root 6 422 20:03 test
drwxr-xr-x. 2 root root 6 422 20:02 test2
drwxr-xr-x. 2 root root 6 422 20:03 test3
drwxr-xr-x. 2 root root 6 422 20:05 test4
drwxr-xr-x. 2 root root 6 422 20:02 –v
[root@localhost test2]# rmdir test4
[root@localhost test2]# ll
总用量 0
drwxr-xr-x. 2 root root 6 422 20:03 test
drwxr-xr-x. 2 root root 6 422 20:02 test2
drwxr-xr-x. 2 root root 6 422 20:03 test3
drwxr-xr-x. 2 root root 6 422 20:02 –v

2.删除非空目录时失败

[root@localhost test]# ll
总用量 0
drwxr-xr-x. 2 root root  6 422 20:12 edu
drwxr-xr-x. 3 root root 19 422 19:58 test1
[root@localhost test]# tree
.
├── edu
└── test1
    └── test2
        ├── test
        ├── test2
        ├── test3
        └── \342\200\223v

7 directories, 0 files
[root@localhost test]# rmdir test1
rmdir: 删除 "test1" 失败: 目录非空

3. rmkdir -p 目录1/目录2

示范:

rmdir -p edu/teacher/
[root@localhost test]# cd edu
[root@localhost edu]# mkdir teacher
[root@localhost edu]# tree
.
└── teacher

1 directory, 0 files
[root@localhost edu]# cd ../
[root@localhost test]# tree
.
├── edu
│   └── teacher
└── test1
    └── test2
        ├── test
        ├── test2
        ├── test3
        └── \342\200\223v

8 directories, 0 files
[root@localhost test]# rmdir -p /edu/teacher
rmdir: 删除目录 "/" 失败: 设备或资源忙
[root@localhost test]# rmdir -p edu/teacher/
[root@localhost test]# ll
总用量 0
drwxr-xr-x. 3 root root 19 422 19:58 test1
[root@localhost test]# tree
.
└── test1
    └── test2
        ├── test
        ├── test2
        ├── test3
        └── \342\200\223v

6 directories, 0 files
[root@localhost test]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值