Darlyn
Darlyn

Reputation: 4938

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.

Upvotes: 1

Views: 4089

Answers (2)

dlink
dlink

Reputation: 1605

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

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96306

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

Upvotes: 5

Related Questions