When i use >> nonexistent/file.log. Bash returns to me "There is no such file or directory."
>> nonexistent/file.log
How can I create one liner that redirects STDOUT / STDERR to a log file, even if it does not exist? If it does not exist, it must create the necessary folders and files.
Usage install:
install
command | install -D /dev/stdin nonexistent/file.log
or use
mkdir nonexistent
the first.
You can use dirnameto get the base path to the file and then use it with mkdir -p. After that, you can redirect:
dirname
mkdir -p
sh mkdir -p `dirname nonexistent/file.log` echo blah >> nonexistent/file.log
, , , , ( , )
if [ ! -d ~/nonexistent ] then mkdir ~/nonexistent fi
Then use the other examples provided to simply copy the resulting file created with ls, back to the host field in the newly created directory.
ls
To automatically generate all directories for a file path:
FILEPATH="/folder1/folder2/myfile.txt" if [ ! -f "$FILEPATH" ]; then mkdir -p "$FILEPATH" rm -r "$FILEPATH" fi #/folder1/folder2 has now been created.