There's the install
command from GNU coreutils
with the -D
option which can copy a file and create the directories leading to them in one go (and also let you specify the ownership and permissions). By default, it creates executable files and doesn't honour the umask as it's typically used as a dev tool in make install
stages.
install -m u=rw,go=r -D /dev/null some/new/file
(the permissions of the directory components it creates are always u=rwx,go=rx
).
Or you could always implement it as a create
Zsh function such as:
create() {
local file ret=0
for file do
mkdir -p -- "$file:h" && true >> "$file" || ret=$?
done
return "$ret"
}
Though creating an empty regular file seems a bit pointless to me.
Generally, you'd do:
mkdir -p some/dir
your-editor some/dir/some-file
To create some-file (the file would be created as soon you save it (with actual content) in your editor).
Or any other command that creates some content like:
some-command > some/dir/some-file
wget -o some/dir/some-file https://example.com/whatever
cp source some/dir/some-file
...etc.
Also, you can write such an utility or a bash script which does this as well. Something akin to
– Artem S. Tashkinov Jul 24 '20 at 10:38mkdir -p "$1" && touch "$1/$2"
mkdir
can be made builtin inzsh
.(mkdir -p foo && touch foo/bar)
is a compound command and a command in the POSIX shell grammar definition of those for that matters. – Stéphane Chazelas Jul 24 '20 at 10:43