Handling Files & Folders with SSH Terminal Commands on Cloud Servers

by Mustaasam Saleem  June 29, 2017

An SSH Terminal is the Command Line Interface and the core handler of a server. If you have access to it, there is nothing you can’t do with SSH terminal commands. Several server hosting providers, especially Cloud service providers do not provide an online file manager directly owing to security flaws and bad performance. To upload, download, delete, edit or even rename a file, users have to log in to their server by using an FTP client.

Editing Files Using SSH Terminal on Cloud Server

Power of Command Line Interface:

Let’s look at an example. If you have a text box accompanied by a button to save the text from the textbox, you are limited to saving the text alone. But if you want to delete the text, you won’t be able to do so because there is no button to delete the text.

When it comes to differentiating UI based programs and command line interfaces, CLI always wins in the terms of power. Yes, we know it’s hard to handle things in CLI.

To overcome this hurdle. You just have to have basic knowledge of command line interface. It’s the same as in our desktop PCs as shown the image below.

SSH on Cloud

What Can We do Using SSH Terminal Commands?

One can say that the SSH terminal is the control room for your cloud server. While you can execute a countless number of commands from the terminal, today we’ll only cover folder and file management related tasks mentioned below.

SSH Terminal Commands for Files and Directories

  • Where Am I?
  • List all files and folders
  • Change Directory
  • Create a Folder
  • Create a File
  • Rename File
  • Rename Folder
  • Copy File
  • Remove File
  • Move File
  • Move Multiple Files at Once
  • Edit a File
  • Run a File
  • Remove Folder
  • File Permissions
  • Folder Permissions

Here, you are going to learn a few basic commands to perform on your files and folders by using the SSH Terminal so that you can control your files without connecting to FTP client again and again.

Let’s dive in without any further ado…

Login to Your Hosting

We’ll be using Cloudways – A Managed Cloud Hosting Platform for our tutorial.

Login to your Cloudways account and go to the Servers tab.

Servers Tab

Launching SSH Terminal

Once you get into the particular server, you’ll see the server credentials there. Click on “Launch SSH Terminal”.

P.S: To make the process faster, you can access SSH terminal using Putty and SSH keys too.

Launch SSH Terminal

Accessing the SSH Terminal

Go back to the server details, copy “Username” and paste on the terminal by right clicking and selecting “Paste from browser”.

Select Paste from Browser

Go back to the server details, copy “Password” and paste it on the terminal.

After successfully logging in, you’ll see a screen like in the image below.

SSH Terminal

We are finally logged in to our server via SSH Terminal and we now have a simple PHP “Application”.

Getting Inside an Application

We know we’re in the root directory. But just for your information, you should know the directory we’re currently in.

Where Am I?

Type: pwd

pwd: This will display the folder you’re currently in.

Folder

This is the root directory of the Cloudways platform.

List all Files and Folders

Type: ls

Ls – Used to list all files and folders

Files and Folders

You can see all files and folders that are present in the root directory.

Change Directory

Type: cd applications

cd – Used to change the current directory

Directory

We’re in the applications folder now.

Below are the steps to get into an application.

PHP Application

cd applications – Get into Applications folder

ls – List all files in the applications folder

cd efaryzckmf – Get into efaryzckmf (our application) folder

ls – List all files in efaryzckmf folder

cd public_html – Get into the public_html folder

ls – List all files and folders in public_html folder

Create a Folder

Now we’re going to create a folder so that we can play around with it.

Type: mkdir NewFolder-1

mkdir – Creates a new folder within public_html folder.

New Folder

You can see that the NewFolder-1 has been created successfully!

Create a File

Type: touch newfile.php

touch – Used to create a new file

Create a new file

newfile.php is now successfully created in the public_html folder.

Rename File

Type: mv newfile.php renamedfile.php

mv – Used to rename a file. It will rename newfile.php to renamedfile.php

Rename File

newfile.php has now successfully been renamed to renamedfile.php. Its syntax is mv OLDNAME NEWNAME.

Rename Folder

Type: mv BatchFolder Bat

mv – Used to rename a folder too. It will rename BatchFolder to Bat.

2

BatchFolder has now successfully been renamed to Bat.

Copy File

Type: cp renamedfile.php NewFolder-1

cp – Used to copy files.

Copy files

ls – List all files and folders in publich_html

cd NewFolder-1 – Get into NewFolder-1

ls – List all files and folders in NewFolder-1

You can see, the file is copied successfully. Its syntax is cp SOURCE DESTINATION.

Remove File

Type: rm renamedfile.php

rm – Used to remove a file.

Remove Files

The file is now removed successfully.

Move File

Type: mv renamedfile.php NewFolder-1

mv – Used to move files.

Move Files

You can see that the file has moved successfully. Its syntax is mv SOURCE DESTINATION

cd .. – Used to go one step back

ls – List all files and folders in the publich_html. Currently, you can see the file renamedfile.php there.

mv renamedfile.php NewFolder-1 – Moving file renamedfile.php to NewFolder-1

ls – List of all files and folders in the Public_html. You can see, file is not there anymore, as it is moved.

cd NewFolder-1 – Get into NewFolder-1

ls – List all files in NewFolder-1. You can see the new file is there.

Move Multiple Files at Once

Type: mv ht.html renamedfile.php ./BatchFolder

1

You can see, we’ve successfully moved two files from public_html to BatchFolder.

Edit a File

Type: vi renamedfile.php

Edit File

It will open up the file to write some text.

On your keyboard, press the “INSERT” key and start writing.

Insert Key

Once you’re done, press “ESC” and you will exit from the editor. Then press “ SHIFT + : ” and it will direct you to type a command line at bottom left.

Type anyone of the following.

q! – Exit without saving

x! – Save changes and Exit

Run a File

Type: php renamedfile.php

php – It is used to run a PHP script.

Run a File

The file is not running successfully.

Remove Folder

Type: rm -r NewFolder-1

rm – It is used to remove files and folders. To remove a folder and its sub folders/files the -r is used. It represents the removal of all files and sub folders within a folder.

SSH on Cloud (10)

File Permissions

Type: CHMOD 755 renamedfile.php

CHMOD – Used to control file permissions

P.S: If you are on Cloudways server, you might need to reset permissions so that the server admin can change the file permissions. To reset, navigate to your application, from the left pane, go to Application Settings and click on Reset Permission. Before making any changes, read the File Permissions guideline first.

File Permission

Numerical Permissions

Folder Permissions

Navigate to the directory, you want to set permissions.

Type: chmod -R [PERMISSIONS]

3

For more details about File Manager related commands. You can refer to “Linux Commands” page.

Conclusion

We’ve already mentioned that most cloud providers do not integrate online file manager with their servers due to security, privacy, and performance related issues. This is why we wrote the above guidelines, keeping in mind your needs and convenience. You can now handle your servers and applications even if you do not have an online file editor.

You have probably observed the power of SSH terminal commands from the article above. However, it’s not possible to cover every feature in a short article, hence we kept it limited. If you have any query though, feel free to ask by using the comments section below. You can also mention other features related to the file manager.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Mustaasam Saleem

Mustaasam is the WordPress Community Manager at Cloudways – A Managed WordPress Hosting Platform, where he actively works and loves sharing his knowledge with the WordPress Community. When he is not working, you can find him playing squash with his friends, or defending in Football, and listening to music. You can email him at mustaasam.saleem@cloudways.com

Stay Connected:

You Might Also Like...