When creating a new empty file, how do I then add a newline to it?
Running these commands:
$ touch test.txt
$ sed -i '$a\' test.txt
$ sed -i '1s/^/\n/' test.txt
$ wc -c test.txt
0 test.txt
it is clear sed
had no effect.
My intention is to write to a new file which at all times has the correct permissions. Following this answer an empty file is created, but I'm having trouble editing the file non-interactively.
Using echo
is not desirable, because it does not preserve permissions, as seen in this example:
$ sudo echo "text" > test.txt
$ stat -c "%U:%G" test.txt
me:mygroup
sudo ... > file
, but that won't create/write the new file with sudo permissions. – JigglyNaga Feb 05 '20 at 10:31echo > test.txt
does not preserve permissions?touch test.txt; chmod 200 test.txt; echo >test.txt; ls -l test.txt
=>--w-------
– Feb 05 '20 at 10:32