Linux - 输出到尚存在的文件夹(在一个命令中)

Lon*_*est 1 linux redirection command-line pipe

是否可以在不需要脚本的情况下通过一个命令实现此目的:

echo "test" > /folder/that/does/not/exist/newFile.txt
Run Code Online (Sandbox Code Playgroud)

小智 5

这是我最接近单命令解决方案的方法:

install -Dm644 <(echo test) /folder/that/does/not/exist/newFile.txt
Run Code Online (Sandbox Code Playgroud)

它仅适用于支持<( )-style 命令替换的shell 。这使用与上述解决方案相同数量的命令:

mkdir -p /folder/that/does/not/exist; echo test > /folder/that/does/not/exist/newFile.txt
Run Code Online (Sandbox Code Playgroud)

但至少前一个解决方案看起来有点像一个单一的命令。