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

前端 未结 4 2430
攒了一身酷
攒了一身酷 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:12

    Hmm... After some experiments, I've found some interesting things.

    First of all, let's try to touch some file:

    touch ~/.lein/profiles.clj
    

    It works fine. But let's use the same thing with quotes:

    touch "~/.lein/profiles.clj" # => touch: cannot touch ‘~/.lein/profiles.clj’: No such file or directory
    

    So, for my bash function:

    append_to_file() {
      echo $2 | tee -a $1
    }
    

    after that I changed call from it:

    append_to_file '~/.lein/projects.clj' '{:user {:plugins [[lein-exec "0.3.1"]]}}'
    

    to it (first argument without quotes):

    append_to_file ~/.lein/projects.clj '{:users {:plugins [[lein-exec "0.3.1"]]}}'
    

    And all is well.

    UPDATE

    This case considers .lein as existing directory.

提交回复
热议问题