How to Create, Edit and Delete Files & Folders Using SSH

In this quick article, I am going to show you how to create new files and folders using SSH command. Here, I will also explain how to edit the content of the existing files and delete the files and folders using the SSH command.

SSH Command to Create Files & Folders:

Run the below SSH command in your CLI to create a new directory,

mkdir yourDirectoryName

To create a new file, use the below SSH command,

touch newfile.php

SSH Command to Edit Files:

After creating the file using touch command, use the below SSH command to add/edit the content of the files,

nano newfile.php

It will open up the file on the editor interface. You can change the content of the file directly on the editor window.

After completing the modification on the file, press Ctrl + X, and the below message will appear.

Save modified buffer (ANSWERING “No” WILL DESTROY CHANGES)?

Then, press Y on your keyboard to save the modified content in the file.

Here is another SSH command to edit the file,

vi newfile.php

It will be also open the file on the editor interface. Then, on your keyboard, press the Insert key and start writing. Once you modified the file content use the below steps to save the file,

  • Press ESC button on your keyboard and you will exit from the editor.
  • Then press SHIFT + : and it will direct you to type a command line at the bottom left.
  • Type any one of the following,
    • q! – Exit without saving the modified content.
    • x! – Save the modified content and Exit.

SSH Command to Delete Files & Folders:

Here is the SSH command to remove the folder,

rm -r yourDirectoryName

The above command is used to delete the folder. Here, -r is used to remove a folder and its subfolders/files.

Use the below SSH command to delete the file,

rm newfile.php

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *