简体   繁体   English

在多个目录中创建文件

[英]creating files in multiple directories

Is it possible to create files in multiple directirories at once / in one command? 是否可以一次在一个命令中同时在多个目录中创建文件? Im doing something like this 我正在做这样的事情

Add 

    -add1
         - file 1
         - file 2
    -add2
         - file 1
         - file 2

I'm creating directories with 我正在创建目录

mkdir -p Add/add{1,2}

but can't figure out how to create files in each subdirectory without manualy swaping to directory and creating a file there. 但是无法找出如何在每个子目录中创建文件的过程,而无需手动切换到目录并在那里创建文件。

You can use the same technique: 您可以使用相同的技术:

touch Add/add{1,2}/file{1,2}

With set -x you'll see that it gets expanded to: 使用set -x您将看到它被扩展为:

touch Add/add1/file1 Add/add1/file2 Add/add2/file1 Add/add2/file2

You can use a for loops if your Unix shell is Bash: 如果您的Unix shell是Bash,则可以使用for循环:

$ for dir in add1 add2; do mkdir -vp $dir; for file in file1 file2; do echo creating $dir/$file; touch $dir/$file; done; done

This produces this output: 这将产生以下输出:

mkdir: created directory ‘add1’
creating add1/file1
creating add1/file2
mkdir: created directory ‘add2’
creating add2/file1
creating add2/file2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM