Linux命令之目錄管理類命令:mkdir, rmdir, tree, dirname, basenameshell
mkdir命令:建立目錄/新建目錄bash
語法:mkdir [OPTION] /PATH/TO/SOMEWHEREide
常見參數選項:ui
-p:建立父目錄//遞歸建立 this
-V:顯示建立信息 遞歸
示例:ip
如何/tmp/下建立目錄:x_m, x_n, y_m, y_nci
# mkdir /tmp/{x_,y_}{m,n}it
rmdir命令:刪除目錄io
語法: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