Unix - create path of folders and file

后端 未结 11 2025
面向向阳花
面向向阳花 2020-12-22 18:20

I know you can do mkdir to create a directory and touch to create a file, but is there no way to do both operations in one go?

i.e. if I wa

11条回答
  •  执念已碎
    2020-12-22 18:48

    This is what I would do:

    mkdir -p /my/other/path/here && touch $_/cpredthing.txt

    Here, the $_ is a variable that represents the last argument to the previous command that we executed in line.

    As always if you want to see what the output might be, you can test it by using the echo command, like so:

    echo mkdir -p /code/temp/other/path/here && echo touch $_/cpredthing.txt

    Which outputs as:

    mkdir -p /code/temp/other/path/here
    touch /code/temp/other/path/here/cpredthing.txt
    

    As a bonus, you could write multiple files at once using brace expansion, for example:

    mkdir -p /code/temp/other/path/here &&
    touch $_/{cpredthing.txt,anotherfile,somescript.sh}
    

    Again, totally testable with echo:

    mkdir -p /code/temp/other/path/here
    touch /code/temp/other/path/here/cpredthing.txt /code/temp/other/path/here/anotherfile /code/temp/other/path/here/somescript.sh
    

提交回复
热议问题