简体   繁体   中英

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:

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:

$ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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