I have a log file that has a bunch of stuff in it that I don't need anymore. I want to clear the contents.
I know how to print the contents to the screen:
cat file.log
I know how to edit the file, line-by-line:
nano file.log
But I don't want to delete each line one at a time. Is there a way to do it in one command without destroying the file to do it?
bashas your shell and not e. g.cmd.exeor PowerShell, neither of which are POSIXy enough to be expected to handle such things. – DopeGhoti Feb 05 '15 at 20:39true > filename– Pablo A Mar 23 '20 at 19:15sudo. Of course you can do it withsudo bash -c '> filename', but spawning a shell is (relatively) expensive. Second it can easily trip up interactive shells that will actually hang waiting for input. This is especially relevant if you want to copy bits of a script into a terminal and get the same results. For these reasons I suggest thetruncatebased answer is actually more robust. – Caleb Apr 19 '21 at 10:49