每天2个Linux命令 rmdir pwd


2017-07-15 每天2个Linux命令 rmdir命令

rmdir命令用来删除空目录。

利用rmdir命令可以从一个目录中删除一个或多个空的子目录。该命令从一个目录中删除一个或多个子目录,其中dirname表示目录名。如果dirname中没有指定路径,则删除当前目录下由dirname指定的目录;如dirname中包含路径,则删除指定位置的目录。删除目录时,必须具有对其父目录的写权限。

注意:子目录被删除之前应该是空目录。就是说,该目录中的所有文件必须用rm命令全部删除,另外,当前工作目录必须在被删除目录之上,不能是被删除目录本身,也不能是被删除目录的子目录。 虽然还可以用带有-r选项的rm命令递归删除一个目录中的所有文件和该目录本身,但是这样做存在很大的危险性。


(1)用法:

用法:rmdir [选项]... 目录...

(2)功能:

功能:删除指定的空目录

(3)选项参数:

  1) -p或--parents                                                     删除指定目录后,若该目录的上层目录已变成空目录,则将其一并删除

  2) --ignore-fail-on-non-empty                                 此选项使rmdir命令忽略由于删除非空目录时导致的错误信息  

  3) -v或-verboes                                                      显示命令的详细执行过程

  4) --help                                                                显示命令的帮助信息

  5) --version                                                            显示命令的版本信息。

(4)实例:

1)[root@localhost sunjimeng]# rmdir –ignore-fail-on-non-empty Documents
与[root@localhost sunjimeng]# rmdir –ignore有一样的功能, 目录不为空时,既不提醒也不删除

复制代码
[root@localhost sunjimeng]# ls -l /home/sunjimeng/Documents
总用量 0
drwxrwxr-x. 2 sunjimeng sunjimeng  6 5月   1 02:49 mainDir
drwxrwxr-x. 2 sunjimeng sunjimeng  6 5月   1 02:52 secondDir
drwxrwxr-x. 8 sunjimeng sunjimeng 78 5月   1 03:01 thirdDir
[root@localhost sunjimeng]# rmdir Documents
rmdir: 删除 "Documents" 失败: 目录非空
[root@localhost sunjimeng]# rmdir --ignore-fail-on-non-empty Documents
[root@localhost sunjimeng]# 

2)[root@localhost sunjimeng]# rmdir ./Documents/thirdDir/te* 用rmdir删除指定路径的目录

复制代码
[root@localhost sunjimeng]# ls -l ./Documents/thirdDir
总用量 0
drwxrwxr-x. 2 sunjimeng sunjimeng 6 5月   1 02:57 test1
drwxrwxr-x. 2 sunjimeng sunjimeng 6 5月   1 02:57 test2
drwxrwxr-x. 2 sunjimeng sunjimeng 6 5月   1 02:57 test3
drwxrwxr-x. 2 sunjimeng sunjimeng 6 5月   1 03:01 test4
drwxrwxr-x. 2 sunjimeng sunjimeng 6 5月   1 03:01 test5
drwxrwxr-x. 2 sunjimeng sunjimeng 6 5月   1 03:01 test6
[root@localhost sunjimeng]# rmdir ./Documents/thirdDir/te*   //用通配符将所有te开头的全部删除
[root@localhost sunjimeng]# ls -l ./Documents/thirdDir
总用量 0

3)[root@localhost sunjimeng]# rmdir -p ./Documents/{mainDir,secondDir,thirdDir} 删除全部的3个子目录,若删除后父目录也为空则一并删除

复制代码
[root@localhost sunjimeng]# rmdir -p ./Documents/{mainDir,secondDir,thirdDir}
rmdir: 删除目录 "./Documents" 失败: 目录非空                                //这里虽然出现提醒但依然被删除了
rmdir: 删除目录 "./Documents" 失败: 目录非空
rmdir: 删除目录 "." 失败: 无效的参数
[root@localhost sunjimeng]# ll
总用量 0
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Desktop
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Downloads
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Videos
[root@localhost sunjimeng]# cd Documents                                 //这里可以验证
bash: cd: Documents: 没有那个文件或目录

4)[root@localhost sunjimeng]# rmdir –verbose -p ./Documents/firstDir/test1 递归的删除目录,并输出过程信息(–verbose这里注意是两个-)

复制代码
[root@localhost sunjimeng]# ls -l ./Documents
总用量 0
drwxr-xr-x. 3 root root 18 5月   2 22:26 firstDir
[root@localhost sunjimeng]# ls -l ./Documents/firstDir
总用量 0
drwxr-xr-x. 2 root root 6 5月   2 22:26 test1
[root@localhost sunjimeng]# rmdir --verbose -p ./Documents/firstDir/test1
rmdir: 正在删除目录 "./Documents/firstDir/test1"
rmdir: 正在删除目录 "./Documents/firstDir"
rmdir: 正在删除目录 "./Documents"
rmdir: 正在删除目录 "."
rmdir: 删除目录 "." 失败: 无效的参数
[root@localhost sunjimeng]# ll
总用量 0
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Desktop
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Downloads
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng 6 5月   1 01:23 Videos
[root@localhost sunjimeng]# 

5)[root@localhost sunjimeng]# rmdir –help

复制代码
[root@localhost sunjimeng]# rmdir --help
用法:rmdir [选项]... 目录...
删除指定的空目录。

      --ignore-fail-on-non-empty
            忽略仅由目录非空产生的所有错误
  -p, --parents   remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is
                    similar to 'rmdir a/b/c a/b a'
  -v, --verbose   output a diagnostic for every directory processed
      --help        显示此帮助信息并退出
      --version        显示版本信息并退出

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告rmdir 的翻译错误
要获取完整文档,请运行:info coreutils 'rmdir invocation'

6)[root@localhost sunjimeng]# rmdir –version

复制代码
[root@localhost sunjimeng]# rmdir --version
rmdir (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。

由David MacKenzie 编写。

2017-07-15 每天2个Linux命令 pwd命令

pwd命令以绝对路径的方式显示用户当前工作目录。命令将当前目录的全路径名称(从根目录)写入标准输出。全部目录使用/分隔。第一个/表示根目录,最后一个目录是当前目录。


(1)用法介绍:

pwd[选项] 

(2)选项参数:

  一般情况下不带任何参数

  1) -L, --logical                                                显示当前目录

  2) -P, --physical                                             显示当前目录的实际物理地址
  3) --help                                                        帮助

  4) --version                                                    版本

(3)功能:

执行pwd命令可立刻得知您目前所在的工作目录的绝对路径名称。

(4)运行实例:

1)[root@localhost Documents]# pwd 查看默认工作目录的完整路径,查看是否成功到达指定文件夹

[root@localhost sunjimeng]# mkdir Documents
[root@localhost sunjimeng]# cd ../../
[root@localhost /]# cd /home/sunjimeng/Documents
[root@localhost Documents]# pwd
/home/sunjimeng/Documents
[root@localhost Documents]# 

2)[sunjimeng@localhost init.d]$ pwd -P 目录连接链接时,pwd -P 显示出实际物理路径,pwd显示连接路径

复制代码
[sunjimeng@localhost mail]$ cd /
[sunjimeng@localhost /]$ cd etc
[sunjimeng@localhost etc]$ cd init.d
[sunjimeng@localhost init.d]$ pwd
/etc/init.d
[sunjimeng@localhost init.d]$ pwd -P
/etc/rc.d/init.d
[sunjimeng@localhost init.d]$ 

3)[sunjimeng@localhost init.d]$ pwd -L 与pwd命令具有一样的功能 显示当前目录的连接路径

[sunjimeng@localhost /]$ cd etc/init.d
[sunjimeng@localhost init.d]$ pwd
/etc/init.d
[sunjimeng@localhost init.d]$ pwd -L
/etc/init.d
[sunjimeng@localhost init.d]$

4)[sunjimeng@localhost /]$ man pwd

复制代码
PWD(1)                           User Commands                          PWD(1)

NAME
       pwd - print name of current/working directory

SYNOPSIS
       pwd [OPTION]...

DESCRIPTION
       Print the full filename of the current working directory.

       -L, --logical
              use PWD from environment, even if it contains symlinks

       -P, --physical
              avoid all symlinks

       --help display this help and exit

       --version
              output version information and exit
复制代码

5)/bin/pwd同pwd的用法一样:

  /bin/pwd [选项]

 选项:

 -L 目录连接链接时,输出连接路径

 -P 输出物理路径

复制代码
[root@localhost init.d]# cd /
[root@localhost /]# cd etc/init.d
[root@localhost init.d]# pwd
/etc/init.d
[root@localhost init.d]# pwd -P
/etc/rc.d/init.d
[root@localhost init.d]# pwd -L
/etc/init.d
[root@localhost init.d]# /bin/pwd
/etc/rc.d/init.d
[root@localhost init.d]# /bin/pwd -L
/etc/init.d
[root@localhost init.d]# /bin/pwd -P
/etc/rc.d/init.d


如果cd命令是逐级进入的化,区分连接路径和实际路径就没有意义了:

复制代码
[root@localhost init.d]# cd /                      //无论什么命令,输出的工作路径都是/etc/rc.d/unit.d
[root@localhost /]# cd etc/rc.d/init.d
[root@localhost init.d]# pwd
/etc/rc.d/init.d
[root@localhost init.d]# /bin/pwd
/etc/rc.d/init.d
[root@localhost init.d]# pwd -L
/etc/rc.d/init.d
[root@localhost init.d]# pwd -P
/etc/rc.d/init.d
[root@localhost init.d]# /bin/pwd -L
/etc/rc.d/init.d
[root@localhost init.d]# /bin/pwd -P
/etc/rc.d/init.d

6)/bin/pwd与pwd命令的区别:(当前目录被删除了,而pwd命令仍然显示那个目录,而/bin/pwd则不会)

复制代码
[root@localhost init.d]# cd /
[root@localhost /]# cd /home/sunjimeng/Documents
[root@localhost Documents]# mkdir removed
[root@localhost Documents]# ls -l
总用量 0
drwxr-xr-x. 2 root root 6 5月   4 07:29 removed
[root@localhost Documents]# cd removed
[root@localhost removed]# rmdir ../removed -rf 
 //这里犯了一个错误,rmdir没有rf选项参数,rm ../removed -rf等价于 rmdir ../removed
rmdir:无效选项 -- r
Try 'rmdir --help' for more information.
[root@localhost removed]# rmdir ../removed
[root@localhost removed]# pwd
/home/sunjimeng/Documents/removed
[root@localhost removed]# /bin/pwd
/bin/pwd: 在匹配的inode ".." 上找不到目录入口      //这里的结果表明了他们的区别
[root@localhost removed]# cd ../
[root@localhost Documents]# ll
总用量 0

(5)其它

软链接与硬链接的区别(讲解):

Linux 软连接与硬连接

对于一个文件来说,有唯一的索引接点与之对应,而对于一个索引接点号,却可以有多个文件名与之对应。因此,在磁盘上的同一个文件可以通过不同的路径去访问该文件。注意在Linux下是一切皆文件的啊,文件夹、新加的硬盘 ...都可以看着文件来处理的啊。

连接有软连接和硬连接(hard link)之分的,软连接(symbolic link)又叫符号连接。符号连接相当于Windows下的快捷方式

文章作者: 邓滔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 邓滔 !
评论
  目录