mkdir

格式:mkdir  (选项) (参数)

作用:创建文件夹

选项:

r -m                设置文件夹权限

r -p                 如果父目录不存在,则连同父目录一起创建

r -v                 显示详细

r -Z                 设置创建的目录SELinux为默认类型

r --context       

r --help            显示帮助并退出

r --version        现实版本信息并退出

参数:


目录:指定要创建的目录列表,多个目录之间用空格隔开


示例:

在/home下创建test目录,设置权限为644

[root@localhost ~]# ls /home

hfx

[root@localhost ~]# mkdir -m 644 /home/test

[root@localhost ~]# ll  /home/

total 0

drwx------. 3 hfx  hfx  74

Oct 21 18:04 hfx

drw-r--r-- 2 root root  6 Nov  7 15:45 test

在/mnt目录下创建连续目录/mnt/test1/test2/test3

[root@localhost ~]# ls /mnt/

[root@localhost ~]# mkdir /mnt/test1/test2/test3

mkdir: cannot create directory ‘/mnt/test1/test2/test3’:

No such file or directory

[root@localhost ~]# mkdir  -p /mnt/test1/test2/test3

[root@localhost ~]# tree /mnt/

/mnt/

└── test1

    └── test2


└──

test3

3 directories, 0 files

显示mkdir的版本信息

[root@localhost ~]# mkdir --version

mkdir (GNU coreutils) 8.22

Copyright (C) 2013 Free Software

Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later

<http://gnu.org/licenses/gpl.html>.

This is free software: you are free to

change and redistribute it.

There is NO WARRANTY, to the extent

permitted by law.

Written by David MacKenzie.

rmdir

格式:rmdir (选项) (参数)

作用:用来删除空目录

选项:

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

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

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

r--help               显示命令的帮助信息

r--version           显示命令的版本信息

参数:要删除的空目录列表。当删除多个空目录时,目录名之间使用空格隔开

示例:

删除空目录/mnt/test1/test2/test3

[root@localhost ~]# rmdir -p /mnt/test1/test2/test3/

rmdir: failed to remove directory ‘/’:

Device or resource busy

[root@localhost ~]# ls /mnt

ls: cannot access /mnt: No such file or

directory

◆可以看到上面把我们的/mnt目录也一起删掉了,这就是-p选项的威力。

删除/mnt/test/非空目录

[root@localhost ~]# mkdir /mnt/test/

[root@localhost ~]# echo "test" > /mnt/test/a.txt

[root@localhost ~]# ll /mnt/test/

total 4

-rw-r--r-- 1 root root 5 Nov  7 16:09 a.txt

[root@localhost ~]# rmdir /mnt/test/

rmdir: failed to remove ‘/mnt/test/’:

Directory not empty

[root@localhost ~]# rmdir --ignore-fail-on-non-empty /mnt/test/

[root@localhost ~]# ll /mnt/test/

total 4

-rw-r--r-- 1 root root 5 Nov  7 16:09 a.txt

◆虽然不能删除但是也不会报错了


使用-v选项删除/mnt下./test1/test2/test3目录

[root@localhost ~]# mkdir  -p /mnt/test1/test2/test3

[root@localhost ~]# rmdir -p -v /mnt/test1/test2/test3/

rmdir: removing directory,

‘/mnt/test1/test2/test3/’

rmdir: removing directory,

‘/mnt/test1/test2’

rmdir: removing directory, ‘/mnt/test1’

rmdir: removing directory, ‘/mnt’

rmdir: failed to remove directory ‘/mnt’:

Directory not empty

[root@localhost ~]# ll /mnt/

total 0

drwxr-xr-x 2 root root 18 Nov  7 16:09 test

cd

格式:cd  (选项) (参数)

作用:切换工作目录

选项:

r-p 如果要切换到的目标目录是一个符号连接,直接切换到符号连接指向的目标目录

r-L 如果要切换的目标目录是一个符号的连接,直接切换到字符连接名代表的目录,而非符号连接所指向的目标目录。

r-  当仅实用"-"一个选项时,当前工作目录将被切换到环境变量"OLDPWD"所表示的目录。

参数:


需要切换到的工作目录,可以是绝对路径和可以是相对路径

r~    当前用户的家目录

r..    当前目录的上级目录

r.     当前目录

示例:

切换到/etc/sys config/network-scripts/目录下

[root@localhost ~]# pwd

/root

[root@localhost ~]# cd /etc/sysconfig/network-scripts/

[root@localhost network-scripts]# pwd

/etc/sysconfig/network-scripts

返回上级目录

[root@localhost network-scripts]# pwd

/etc/sysconfig/network-scripts

[root@localhost network-scripts]# cd ..

[root@localhost sysconfig]# pwd

/etc/sysconfig

返回上两级目录

[root@localhost sysconfig]# pwd

/etc/sysconfig

[root@localhost sysconfig]# cd ../..

[root@localhost /]# pwd

/

进入当前用户主目录

[root@localhost /]# pwd

/

[root@localhost /]# cd ~

[root@localhost ~]# pwd

/root

或者用另一种写法直接cd

[root@localhost /]# pwd

/

[root@localhost /]# cd

[root@localhost ~]# pwd

/root

返回进入此目录之前所在的目录


[root@localhost /]# pwd

/

[root@localhost /]# cd ~

[root@localhost ~]# pwd

/root

[root@localhost ~]#

[root@localhost ~]# cd -

/