rmdir  删除空目录,非空的目录不能删除

1、命令格式

rmdir [options]      DIRECTORY


2、命令参数

    -p, 递归删除,删除目录之前是否删除父目录,如果父目录为空的话就删除

     -v, 删除目录时,显示删除信息

    --help   显示此帮助信息并退出

       --version  输出版本信息并退出

 3、常用范例

1:删除空目录doc

命令:rmdir doc

[root@server~]# tree doc/
doc/
 
0directories, 0 files
[root@server~]#
[root@server~]# ls
anaconda-ks.cfg  doc install.log  install.log.syslog
[root@server~]# rmdir doc/
[root@server~]# ls
anaconda-ks.cfg  install.log install.log.syslog
[root@server~]#


2:删除非空目录echo

命令:rmdir echo

[root@server~]# ls echo/
fstab
[root@server~]# rmdir echo/
rmdir:failed to remove `echo/': Directory not empty
[root@server~]#


提示:rmdir不能删除非空的目录,如果想删除非空的目录用rm -r命令 rm -r echo

[root@server~]# rm -r echo/
rm:descend into directory `echo'? y
rm:remove regular file `echo/fstab'? y
rm:remove directory `echo'? y
[root@server~]#


 

3:递归删除目录 tree1/tree2/tree3并显示删除过程

命令 rmdir -pv tree1/tree2/tree3

[root@server~]# tree tree1/
tree1/
└── tree2
    └── tree3
 
2directories, 0 files
[root@server~]# rmdir -pv tree1/tree2/tree3/
rmdir:removing directory, `tree1/tree2/tree3/'
rmdir:removing directory, `tree1/tree2'
rmdir:removing directory, `tree1'


 

扩展:

tree命令:以树状图列出文件目录结构。Centos6.5没有tree命令,需要手动安装,下面介绍tree命令的安装方法

1、命令格式:

tree [options] DIRECTORY

2、常用参数

-d:生成目录树的时候只显示目录

3、使用范例

1、显示/var/log下的文件,以树状形式展现

命令:tree /var/log

root@server~]# tree /var/log/
/var/log/
├── anaconda.ifcfg.log
├── anaconda.log
├── anaconda.program.log
├── anaconda.storage.log
├── anaconda.syslog
├── anaconda.xlog
├── anaconda.yum.log
├── audit
│   └── audit.log
├── boot.log
├── btmp
├── ConsoleKit
│   └── history
├── cron
├── cups
├── dmesg
├── dmesg.old
├── dracut.log
├── lastlog
├── maillog
├── mcelog
├── messages
├── ntpstats
├── prelink
│   └── prelink.log
├── sa
│   ├── sa07
│   └── sa08
├── samba
│   └── old
├── secure
├── spooler
├── sssd
├── tallylog
├── wtmp
└── yum.log
 
9directories, 27 files
[root@server~]#


2、显示/var/log目录下的子目录,以树状展现

命令:tree -d  /var/log

[root@server~]# tree -d /var/log/
/var/log/
├── audit
├── ConsoleKit
├── cups
├── ntpstats
├── prelink
├── sa
├── samba
│   └── old
└── sssd
 
9directories
[root@server~]#


tree安装方式,以CentOS6.5为例

 

[root@server~]# mv /etc/yum.repos.d/* /bak/     //移动yum源仓库的所有文件 到/bak目录下
[root@server~]# cp /bak/CentOS-Media.repo /etc/yum.repos.d/  //把/bak下的e本地yum源仓库(CentOS_Media.repo)复制一份到yum源仓库
[root@server~]# vim /etc/yum.repos.d/CentOS-Media.repo //用vim编辑器编辑yum源(CentOS-Media.repo)
#CentOS-Media.repo
#
#  This repo can be used with mounted DVD media,verify the mount point for
#  CentOS-6. You can use this repo and yum to install items directly off the
#  DVD ISO that we release.
#
# To usethis repo, put in your DVD and use it with the other repos too:
#  yum --enablerepo=c6-media [command]
#  
# or forONLY the media repo, do this:
#
#  yum --disablerepo=\* --enablerepo=c6-media[command]
 
[c6-media]
name=CentOS-$releasever- Media
baseurl=file:///media/CentOS/
        file:///media/cdrom/
        file:///media/cdrecorder/
gpgcheck=1
enabled=1    //在此将0修改为1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

提示:默认CentOS-Media.repo没有开启这个软件仓库固在郁闷源里面查找不到此源,需把enabled0改到1开启  0是关闭状态,1是开启

      链接cdromCentOS6.5的安装光盘插入然后挂载至上面三个目录中的某一个即可

以下我挂载到/media下的cdrom目录中

[root@server~]# mount /dev/cdrom /media/cdrom/  //挂载光盘至/media/cdrom目录下
mount:block device /dev/sr0 is write-protected, mounting read-only
[root@server~]# mount  //查看挂载设备信息
/dev/mapper/vg_server-LogVol00on / type ext4 (rw)
proc on/proc type proc (rw)
sysfs on/sys type sysfs (rw)
devpts on/dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on/dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1on /boot type ext4 (rw)
/dev/mapper/vg_server-LogVol02on /home type ext4 (rw)
none on/proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sr0 on /media/cdrom type iso9660 (ro)
[root@server~]# yum -y install tree   //安装tree命令
Loadedplugins: fastestmirror, security
Loadingmirror speeds from cached hostfile
 * c6-media: 
Settingup Install Process
ResolvingDependencies
--> Runningtransaction check
--->Package tree.x86_64 0:1.5.3-2.el6 will be installed
-->Finished Dependency Resolution
 
DependenciesResolved
 
==================================================================================================================================
 Package                    Arch                         Version                             Repository                      Size
==================================================================================================================================
Installing:
 tree                       x86_64                       1.5.3-2.el6                         c6-media                        36 k
 
TransactionSummary
==================================================================================================================================
Install       1 Package(s)
 
Totaldownload size: 36 k
Installedsize: 65 k
DownloadingPackages:
Runningrpm_check_debug
RunningTransaction Test
TransactionTest Succeeded
RunningTransaction
  Installing : tree-1.5.3-2.el6.x86_64                                                                                       1/1 
  Verifying : tree-1.5.3-2.el6.x86_64                                                                                       1/1 
 
Installed:
  tree.x86_64 0:1.5.3-2.el6                                                                                                      
 
Complete!
[root@server~]#


以上tree命令安装成功