1

If I want to add a line to fstab or another system file, I execute the following in the command line. Example:

echo 'line1' | sudo tee --append /etc/fstab
echo 'line1' | sudo tee --append /etc/apache2/apache2.conf

How do I add many lines within a file.txt to a system file (apache2.conf, fstab, etc)?

file.txt

line1
line2
line3

Thanks

PD: Only from user account with sudo privileges. Not from the root account

1

cat from one file to another.

cat file1.txt >> file2.txt
  • It is a system file. you can not with "sudo cat" – ajcg Jun 3 '18 at 23:01
  • And? – Ignacio Vazquez-Abrams Jun 3 '18 at 23:08
  • And... Permission denied. You must be root. The question is about executing commands from a user account with sudo privileges. Not from the root account – ajcg Jun 3 '18 at 23:11
  • 1
    Did you read the linked question? – Ignacio Vazquez-Abrams Jun 3 '18 at 23:12
  • Sorry. I did not see it. I got it. sudo sh -c "cat file.txt >> /etc/apache2/apache2.conf"... Thanks – ajcg Jun 3 '18 at 23:37
3

You need a root shell in order to open an output redirection when you don't have write access from your user account, eg:-

sudo bash -c "cat file.txt >>/etc/fstab"
  • Upvoted, despite the fact your answer says what Ignacio says. But one has to understand both his answers (one here and one linked in comments) and connect them to make things work. Your answer is lot more friendly, working out of the box and explaining "why". – Kamil Maciorowski Jun 3 '18 at 23:28
  • @KamilMaciorowski - Your answer and Ignacio's appeared while I was testing my command and I didn't notice the updates, which were off-screen when I posted my answer. I have returned the compliment, since your answer is closer to what the questioner quoted in his question. – AFH Jun 3 '18 at 23:34
2
sudo tee --append /etc/fstab <file.txt

because sudo passes its stdin to the command it runs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.