use tee command to redirect output to a file in a non-existent dir

前端 未结 4 2514
攒了一身酷
攒了一身酷 2021-02-20 02:22

I am trying to use the tee command to redirect output to a file, and I want the file to be created in a dir which is yet to be created.

date | tee new_dir/new_fi         


        
4条回答
  •  长发绾君心
    2021-02-20 03:04

    Replace tee with a function that creates the directory for you:

    tee() { mkdir -p ${1%/*} && command tee "$@"; }
    

    If you want the function to work when invoked with a simple file name:

    tee() { if test "$1" != "${1%/*}"; then mkdir -p ${1%/*}; fi &&
       command tee "$1"; }
    

提交回复
热议问题