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

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


No. You'll have to create the directory before running tee.


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"; }


mkdir ./new_dir && date | tee ./new_dir/new_file

Since it is tee command, it simultaneously writes both to the new_file and to stdout