If you are using the linux command line, there are several programs for you to use to edit files in SSH. This is a convenient way to modify files since you can quickly change them in terminal without using a separate text editor.
Create a File in SSH
- Open a terminal or connect to your server vis SSH.
- Use the cd command to change to the directory you want to create the file in. In this example, I’m creating a file in the public_html folder.
- Run the following command and replace example.html with the name of the file you want to create.
- The nano editor will then open up the file and you can begin entering text.
- Once you have finished editing click Ctrl+O then the Enter key to save and create the file.
Edit File in SSH
In this section, we’ll walk you through the process of editing a file via SSH using nano.
- When editing files with nano, the first thing you’ll need to do is connect to your server via SSH.
- We’re currently logged in, so let’s run “pwd” command to see what directory we’re in
- Now we’ll run the “ls” command to see what files are in this directory.
- As you can see, we’re in the testa folder and there are currently three files: file1.txt, file2.txt, and file3.txt. If we wanted to edit file2.txt using nano, enter the following command:
After running this command, the nano file editor will open and load the contents of file2.txt.
What you’re looking at above is me editing a file using nano. The top line shows the version of nano you’re using and the name of the file you’re editing.
The bottom part of the screen includes all of the commands that you can run.
You can then run the ls command to view the new file.
You can move the cursor around with your keyboard arrows, but the mouse will not work in the nano editor. Here I am deleting a line of text in the file.
So what do you do now? Start editing the file! You can edit the file as you would using any other editor, except you don’t have the option to use your mouse to point and click on items.
How to Save & Exit Nano
The commands at the bottom of the nano page are executed by holding down Ctrl on your keyboard followed by the letter listed. Here are the steps for saving a file and exiting the nano editor.
Save File in Nano
- To save the file (referred to as “WriteOut”), hold Ctrl + O on your keyboard.
Ctrl + O
- When you’re ready to exit nano, hold Ctrl + X and you will be exited from the application.
Ctrl + X
Congratulations, now you know how to edit a file in SSH using nano, as well as how to save and exit.