So I have kinda just resigned to using nano for this, but I though I would put it out on Unix.Linux to A) Challenge somebody and B) learn how/if It can be done.

I want to prepend a link to an rsa file (command="/sbin/shutdown -h now").

Most of the things I found when google "cat prepend to file" make it so it would end up like this .

command="/sbin/shutdown -h now"
ssh-rsa MyRSsAkEyasetcetc

What I need is :

command="/sbin/shutdown -h now" ssh-rsa MySRasKeytsadnasdnasd

Aka all one line, prepend to first line.

  • 2
    printf '%s ' 'command="/sbin/shutdown -h now"'; cat file – cuonglm Aug 18 '16 at 17:07
  • That seems easier than it is to cat a new line!, much cleaner. Will have to try it out... Here I was thinking it would be a challenge :P – FreeSoftwareServers Aug 18 '16 at 17:07
  • That seems to display what I want in the terminal, but not actually add it to the file – FreeSoftwareServers Aug 18 '16 at 17:09
  • Using the approach requires you to redirect the output; eg printf .. >newfile ; cat ... >> newfile or maybe easier to wrap it ; eg ( printf ... ; cat .. ) > newfile – Stephen Harris Aug 18 '16 at 17:13
  • I was trying to use pipe vs redirect, I understand now. Thankz – FreeSoftwareServers Aug 18 '16 at 17:14

This is a simple sed command:

sed 's!^!command="/sbin/shutdown -h now" !'

If the public key is in a file then you can use the -i flag to edit the file in place:

$ cat key.pub 
ssh-rsa MySRasKeytsadnasdnasd
$ sed -i 's!^!command="/sbin/shutdown -h now" !' key.pub
$ cat key.pub 
command="/sbin/shutdown -h now" ssh-rsa MySRasKeytsadnasdnasd

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.