Linux命令之目录管理类命令:mkdir, rmdir, tree, dirname, basename
mkdir命令:创建目录/新建目录
语法:mkdir [OPTION] /PATH/TO/SOMEWHERE
常见参数选项:
-p:创建父目录//递归创建
-V:显示创建信息
示例:
如何/tmp/下创建目录:x_m, x_n, y_m, y_n
# mkdir /tmp/{x_,y_}{m,n}
rmdir命令:删除目录
语法:rmdir /PATH/TO/SOMEWHERE
常见的参数选项:
-p:连同上层的空目录一起删除(慎重使用)
-p: will also create all directories leading up to the given directory that do not exist already. If the given directory already exists, ignore the error.
-v: display each directory that mkdir creates. Most often used with -p.
-m: specify the octal permissions of directories created by mkdir.
-p is most often used when using mkdir to build up complex directory hierarchies, in case a necessary directory is missing or already there. -m is commonly used to lock down temporary directories used by shell scripts.
Examples[edit]
An example of -p in action is:
mkdir -p /tmp/a/b/c
If /tmp/a exists but /tmp/a/b does not, mkdir will create /tmp/a/b before creating /tmp/a/b/c.
And an even more powerful command, creating a full tree at once (this however is a Shell extension, nothing mkdir does itself):
mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags}
If one is using variables with mkdir in a bash script, POSIX `special‘ built-in command ‘eval‘ would serve its purpose.
DOMAIN_NAME=includes,docs
eval "mkdir -p tmpdir/{trunk/sources/{${DOMAIN_NAME}},branches,tags}"
This will create:
tmpdir
________|______
| | |
branches tags trunk
|
sources
____|_____
| |
includes docs
tree命令:以树状图列出目录的内容
语法:tree [OPTION]... [DIR]
-d:只层级目录类型的文件;
-L level: 只显示几个层级;
dirname命令:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的
路径(目录的部分)
语法:dirname FILENAME
示例:
# dirname /etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts
basename命令:从给定的包含绝对路径的文件名中去除左边目录部分或者同时去除某个后缀的内
容(目录的部分),然后返回剩下的部分(非目录的部分)
语法:basename FILENAME [SUFFIX]
示例:
#basename /etc/sysconfig/network-scripts/ifcfg-eth0
ifcfg-eth0