Nee*_*pta 7 command-line bash files directory mkdir
我正在在线学习 Unix,我遇到了这个问题来创建一个层次结构。我已经使用mkdir命令创建了目录,但是在目录中创建文件时我被卡住了。
我创建目录的命令是
mkdir -p mydir/{colors/{basic,blended},shape,animals/{mammals,reptiles}}
Run Code Online (Sandbox Code Playgroud)
Ser*_*nyy 11
这里有两种方法可以做到。还有更多,但概念是相同的:要么扩展您拥有的内容,要么遍历列表并将每个列表项分解为多个部分。
没有什么特别需要用touch. 只需扩展与mkdir命令相同的参数即可包含文件。
bash-4.3$ mkdir -p mydir/{colors/{basic,blended},shape,animals/{mammals,reptiles}}
bash-4.3$ tree mydir
mydir
??? animals
? ??? mammals
? ??? reptiles
??? colors
? ??? basic
? ??? blended
??? shape
7 directories, 0 files
bash-4.3$ touch mydir/{colors/{basic/{red,blue,green},blended/{yellow,orange,pink}},shape/{circle,square,cube},animals/{mammals/{platipus,bat,dog},reptiles/{snakes,crocodile,lizard}}}
bash-4.3$ tree mydir
mydir
??? animals
? ??? mammals
? ? ??? bat
? ? ??? dog
? ? ??? platipus
? ??? reptiles
? ??? crocodile
? ??? lizard
? ??? snakes
??? colors
? ??? basic
? ? ??? blue
? ? ??? green
? ? ??? red
? ??? blended
? ??? orange
? ??? pink
? ??? yellow
??? shape
??? circle
??? cube
??? square
Run Code Online (Sandbox Code Playgroud)
如果您观察到,您的所有目录都有要创建的文件。因此,我们可以做的是创建项目列表(实际上是一个 bash 数组)并迭代它们,使用mkdir后缀删除,然后使用touch. 像这样:
bash-4.3$ arr=( mydir/{colors/{basic/{red,blue,green},blended/{yellow,orange,pink}},shape/{circle,square,cube},animals/{mammals/{platipus,bat,dog},reptiles/{snakes,crocodile,lizard}}} )
bash-4.3$ for i in "${arr[@]}"; do mkdir -p "${i%/*}" && touch "$i"; done
bash-4.3$ tree mydir
mydir
??? animals
? ??? mammals
? ? ??? bat
? ? ??? dog
? ? ??? platipus
? ??? reptiles
? ??? crocodile
? ??? lizard
? ??? snakes
??? colors
? ??? basic
? ? ??? blue
? ? ??? green
? ? ??? red
? ??? blended
? ??? orange
? ??? pink
? ??? yellow
??? shape
??? circle
??? cube
??? square
7 directories, 15 files
Run Code Online (Sandbox Code Playgroud)
旁注:如果任何文件或目录名称中有空格,请确保将这些项目用单引号或双引号括起来,例如:
arr=( mydir/{'with space',without_space/{file1,file2}} )
Run Code Online (Sandbox Code Playgroud)
另见。
| 归档时间: |
|
| 查看次数: |
27791 次 |
| 最近记录: |