I also want to do mkdir-p with touch.

Asked 2 years ago, Updated 2 years ago, 50 views

Is it possible to create a file by digging the directory in touch?

When you want to create /var/www/html/index.html, it's troublesome to create a directory one by one if you only have a directory up to /var/www/.

Please let me know if there is an easy way to do it with just one command

linux

2022-09-30 11:23

4 Answers

install-D/dev/null/var/www/html/index.html

The --backup option can also be used to save, but if you expect the same effect as touch, you should combine mkdir and touch as in other answers.

What I really wanted

sh-c'mkdir-p"$(dirname"$0")"&touch"$0"'/var/wwww/html/index.html

Description

  • sh-c<command><args...>
    Create a nonlogin/non-interactive sh process for an existing session and run <command>. Since /bin/sh is a dash in Ubuntu and bash in CentOS, I think it would be better to write the command in Bourne-shell.Outside <args...> are substituted by $0, $1... in <command>, respectively.

  • dirname<arg>
    Browse to the directory as a file path from a string passed to &arg>.&arg> may contain spaces or globes (such as *), so double-quote them.

  • $(<command>)
    Run <command> and use its standard output as a string.The old Bourne-shell had to be backquoted, but nowadays, POSIX-compliant shells are basically widely used.It is not impossible to write in the backquote, but you must escape the inner ".

  • mkdir-p<arg>
    As you know, <arg> can contain spaces, so I'll double-quote it.

  • &
    Short-circuit evaluation to prevent touch from running when mkdir fails.If the directory named /var/www/html/index.html/ existed before the command was executed, the mkdir-p would fail.

  • touch "$0"
    touch with arguments passed by the sh command.

sh-c<command><args...>
Create a nonlogin/non-interactive sh process for an existing session and run <command>. Since /bin/sh is a dash in Ubuntu and bash in CentOS, I think it would be better to write the command in Bourne-shell.Outside <args...> are substituted by $0, $1... in <command> respectively.

dirname<arg>
Browse to the directory as a file path from a string passed to &arg>.&arg> may contain spaces or globes (such as *), so double-quote them.

$(<command>)
Run <command> and use its standard output as a string.The old Bourne-shell had to be backquoted, but nowadays, POSIX-compliant shells are basically widely used.It is not impossible to write in the backquote, but the inside " must be escaped.

mkdir-p<arg>
As you know, <arg> may contain spaces, so I'll double-quote it.

&
Short-circuit evaluation to prevent touch from running when mkdir fails.If the directory named /var/www/html/index.html/ existed before the command was executed, the mkdir-p would fail.

touch "$0"
touch with the arguments passed by the sh command.


2022-09-30 11:23

How about this? Put it in a .bashrc or something like that

dirtouch(){
    mkdir-p "$(dirname$1)"
    touch "$1"
}
alias touch=dirtouch

editing:space protection quotes


2022-09-30 11:23

Multiple paths are supported by:
It can also be used with dirtouch test/{a,b}.txt.

dirtouch(){
    for in "$@"
    do
        mkdir-p$(dirname "$i")
        touch "$i"
    done
}

(As the credit rating of the account is low, I will reply as a separate comment)


2022-09-30 11:23

This is the development of answer by kebabu_wrap.
I used arguments such as -h and --date, so I ignored them.

touch(){
    for argin "$@"; do
        case "$arg" in
            -d|-r|-t) next="skip"; continue;
                  -*) continue;;
                   *) test "$next" = "skip" & {unset next; continue;};
        esac
        mkdir --parent "$(dirname"$arg")"
    done
    command-p touch "$@"
}


2022-09-30 11:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.