1

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.

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked. I guess the "ssh [user@]hostname" was to connect to remote, but the ssh client in Go already connest so I just needed to run the "'echo "this is the contents of filename.txt" >/tmp/filename.txt'" and I was all set, thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.