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
touchcommand and add switch-pto it? – M.A. Heshmat Khah Jul 20 '16 at 12:35touch-p(no space!) instead oftouch2, if you prefer the "p", but I would not attempt to replace the original command. – Byte Commander Jul 20 '16 at 15:11dirnameas well , i.e. you could domkdir -p "${1%/}" && touch "$1", same as https://askubuntu.com/a/928544/295286 – Sergiy Kolodyazhnyy Jun 23 '17 at 18:59touch2from another script asbash x.shor./x.sh, then bash saystouch2: command not found. What is the root cause? How to fix? UPD: I've already tried:#!/bin/bash -i,shopt -s expand_aliases,source ~/.bashrc. Nothing worked for me. So, I've had to puttouch2() { mkdir -p "$(dirname "$1")" && touch "$1" ; }directly into thex.sh. – pmor May 20 '23 at 10:15