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

前端 未结 9 1206
無奈伤痛
無奈伤痛 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 13:05

    For this purpose, you can create your own function. For example:

    $ echo 'mkfile() { mkdir -p "$(dirname "$1")" && touch "$1" ;  }' >> ~/.bashrc
    $ source ~/.bashrc
    $ mkfile ./fldr1/fldr2/file.txt
    

    Explanation:

    • Insert the function to the end of ~/.bashrc file using the echo command
    • The -p flag is for creating the nested folders, such as fldr2
    • Update the ~/.bashrc file with the source command
    • Use the mkfile function to create the file

提交回复
热议问题