0

Creating and editing a file via SSH

 2 years ago
source link: https://help.dreamhost.com/hc/en-us/articles/115006413028-Creating-and-editing-a-file-via-SSH
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Creating and editing a file via SSH

Overview

There are a few different ways to create and edit a file when logged into a DreamHost server via SSH. This article explains how to use two text editors:

  • nano — Best choice for beginners
  • vim — More features than nano, but also more difficult to use. vim is a good choice for advanced users

If you're not comfortable editing files in a terminal, view the following article:

dh-kb-important-icon.svg

This article only gives you an example of how to create and edit a file using SSH. When following these instructions, make sure to choose your actual file and edit it with whatever contents you need.

Using 'nano' to create and edit a file

Before you begin

The following section is also available as an instructional video:

nano is easier to use than vim so it's a good choice for newer users. Follow the steps below to create and edit a file using nano.

Creating or editing a file using 'nano'

  1. Log into your server via SSH.
  2. Navigate to the directory location you want to create the file, or edit an existing file.
  3. Type in nano followed by the name of the file. For example, if you wish to create (or edit) a new file name index.html, run the following:
    [server]$ nano index.html
    A new file opens named index.html: vim-nano-setup-01.png
  4. Start typing your data into the file. vim-nano-setup-02.png
  5. When you're ready to save the file, hold down the Ctrl key and press the letter O: (Ctrl + O). The bottom of the prompt asks you to confirm the name of the file, which is already set as index.html.
  6. Press the Enter key on your keyboard to save.
  7. When finished, press Ctrl + X to close nano and return to your shell.

Using 'vim' to create and edit a file

Before you begin

The following section is also available as an instructional video:

As stated above, vim includes more features to edit files but is also more difficult to learn. vim is only recommended for advanced users with prior experience working in the Linux shell.

Using 'vim' to create and edit a file

  1. Log into your server via SSH.
  2. Navigate to the directory location you wish to create the file in or edit an existing file.
  3. Type in vim followed by the name of the file. For example, if you wish to create (or edit) a new file named index.html, run the following:
    [server]$ vim index.html
    The vim editor may appear confusing at first because you cannot type into the file yet: vim-nano-setup-03.png
  4. Press the letter i on your keyboard to enter INSERT mode in vim. It now shows -- INSERT -- on the bottom left: vim-nano-setup-04.png
  5. Start typing into the file.
  6. When finished editing the file, press the ESC key. This takes you out of INSERT mode and -- INSERT -- disappears from the bottom left of your terminal.
  7. To save the file, type in a colon (:) followed by wq. For example:
    :wq
    The characters :wq appear on the bottom left: vim-nano-setup-07.png
  8. Press the Enter key on your keyboard to proceed.

Changing the name of a file

To change the name of a file, use the mv command. For example, this changes a file named file1.txt to file2.txt.

[server]$ mv file1.txt file2.txt

See also

Did this article answer your questions?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK