One command to create a directory and file inside it linux command

前端 未结 9 1266
無奈伤痛
無奈伤痛 2020-12-24 12:39

Suppose my current directory is A. I want to create a directory B and a file \"myfile.txt\" inside B.

How to do th

9条回答
  •  抹茶落季
    2020-12-24 12:59

    devnull's answer provides a function:

    mkfile() { mkdir -p -- "$1" && touch -- "$1"/"$2" }
    

    This function did not work for me as is (I'm running bash 4.3.48 on WSL Ubuntu), but did work once I removed the double dashes. So, this worked for me:

    echo 'mkfile() { mkdir -p "$1" && touch "$1"/"$2" }' >> ~/.bash_profile
    source ~/.bash_profile
    mkfile sample/dir test.file
    

提交回复
热议问题