Ubuntu: Create a file and write to it [closed]


Trying to create a file auto.home under /etc and write this to the file:  

I am not doing it with commandline, but bash script.

Tried this but didnt work: cat > /etc/auto.home


You can use "EOL" to indicate when you want to end writing to your file using cat as follows:

#!/usr/bin/env bash  cat > temp_file << EOL  EOL  sudo mv temp_file /etc/<filename>  

This would keep writing to temp_file, until it gets "EOL" meaning End of Line.

Refered from: A similar question on SO


You can't use sudo for redirection, the command will be run with root privileges but the > (redirection) will not so you'll get a "Permission denied" error. Using here-docs as Jobin's answer does will not help.

Instead, you can use one of these approaches:

echo "" | sudo tee -a /etc/auto.home  

tee lets you append (-a) to a file, since you're running it with sudo it will work.

Alternatively, use a temp file and move it:

echo "" > home.auto && sudo mv home.auto /etc  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »