Pergunta

I am aware that it is possible to create a file in ssh through the "touch" command then editing it using text editors such as vi or pico to edit them and add content.

I was wondering if it was possible to create a file and add it's contents in one command line? Something like:

[create file command] [filename.txt] ["this is the contents of filename.txt"]

The reason I'm asking if this was possible is because I have an ssh client in Go where a session only accepts one call to run and I plan to use this for an app that is automated with no user inputs so I want to avoid using text editors.

Foi útil?

Solução

Here's one solution:

ssh [user@]hostname 'echo "this is the contents of filename.txt" > <path/filename>'

The echo with ">" overwrites an existing file, or creates a new file if it doesn't exist.

Replace ">" with ">>" to append the text to an existing file.

Cheers

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top