How would you go about using a single command to empty multiple text files in terminal?
My thought was that you could use something along these lines…:
find /var/log/apache2/*log -exec `echo > '{}'` \;
I know it’s simple enough to create a shell script that would easily do that:
echo "#!/bin/sh\n\necho \"\" > \"$1\"" > /usr/local/bin/empty && chmod +x /usr/local/bin/empty
…
find /var/log/apache2/*.log -exec empty {} \;
But is it possible to do this without creating your own script in a similar manner?
Or use
sed:replacing file contents in place by emptiness.
update with explanation
sedcan do all kinds of replacements, e.g. using regular expressions:with the pattern saying “substitute anything until : with USER”, giving:
Adding
-iin the mix,sedcan edit files in-place, so you probably NEVER want to do this:(Note that on e.g. Mac OS X, you need to put in an extra argument after
-ito provide the “backup suffix”, which is used to make backups beforeseddoes its magic on your files)Now the quest is for the shortest
sedscript to lose all input, which is eitherdorQ.dwould delete all input (and then output nothing),Qwould quit immediately (and output nothing).Qis presumably fastest.Then, the
sarnoldexample would look like: