Can I write to a read only file from terminal without using any text editor?

Viewed 32

I am writing a script file for fedora to setup the necessary settings. I want to add some lines to the file /etc/dnf/dnf.conf directly from the terminal using echo command. However, I do not have the permission to write to that directory itself. Hence I cannot write to that file even after giving the permission to write to the dnf.conf file.

While researching I found out that we can add lines to that file even if it does not have write permission using some text editors like nano, vi etc. But I could not find any solution on how to write to that file directly from the terminal.

I tried changing to super user using sudo su command from the script file. However, after switching to super user the remaining commands do not execute.

Can someone help me with this.

Thank you.

1 Answers

I used tee command to solve this problem.

echo 'line to insert' | sudo tee -a <filepath>

The tee command catches the output of the echo command puts it to the desired file given in the filepath.

Related