How to touch a file and mkdir if needed in one line

前端 未结 8 1222
抹茶落季
抹茶落季 2021-02-18 14:38

I need to touch a file with an absolute file name such as: /opt/test/test.txt, but I\'m not sure if there is /opt/test existed on the system. So the code should similar with thi

8条回答
  •  没有蜡笔的小新
    2021-02-18 14:52

    I defined a touchp in my ~/.bash_aliases:

    function touchp() {
      /bin/mkdir -p "$(dirname "$1")/" && /usr/bin/touch "$1"
    }
    

    It silently creates the structure above the file if not present, and is perfectly safe to use when passed a single filename without any directory in front of it.

提交回复
热议问题