When I use >> nonexistent/file.log
. Bash gives me back "No such file or directory".
How can I make a one liner that redirects the STDOUT/STDERR to a log file, even if it doesn't exist? If it doesn't exist, it must create the necessary folders and files.
The append >> operator adds the output to the existing content instead of overwriting it. This allows you to redirect the output from multiple commands to a single file. For example, I could redirect the output of date by using the > operator and then redirect hostname and uname -r to the specifications.
Using install
:
command | install -D /dev/stdin nonexistent/file.log
or use
mkdir nonexistent
first.
You can use dirname
to get the base path of the file, and later use it with mkdir -p
. After that you can do the redirection:
sh
mkdir -p `dirname nonexistent/file.log`
echo blah >> nonexistent/file.log
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With