One can use install
command with -D
flag.
bash-4.3$ install -D /dev/null mydir/one/two
bash-4.3$ tree mydir
mydir
└── one
└── two
1 directory, 1 file
bash-4.3$
If we have multiple files, we might want to consider using a list of items(note, remember to quote items with spaces), and iterating over them:
bash-4.3$ for i in mydir/{'subdir one'/{file1,file2},'subdir 2'/{file3,file4}} ; do
> install -D /dev/null "$i"
> done
bash-4.3$ tree mydir
mydir
├── one
│ └── two
├── subdir 2
│ ├── file3
│ └── file4
└── subdir one
├── file1
└── file2
Or alternatively with array:
bash-4.3$ arr=( mydir/{'subdir one'/{file1,file2},'subdir 2'/{file3,file4}} )
bash-4.3$ for i in "${arr[@]}"; do install -D /dev/null "$i"; done
bash-4.3$ tree mydir
mydir
├── one
│ └── two
├── subdir 2
│ ├── file3
│ └── file4
└── subdir one
├── file1
└── file2
mkdir -p parent/child && touch $_/file.txt
by @francis – magnump0 Nov 07 '22 at 09:12