Manage Your SiteGround Website with SSH
SSH (Secure Shell) allows us to remotely access a server to configure and install scripts, manage files and folders, plus much more.
This article will show you the commands necessary to manage your website on SiteGround via SSH.
You may want to read my article on how to connect to a SiteGround website using SSH.
List Files & Directories via SSH
To list all files and directories on a server while connected via an SSH client use the following command: ls
The output will contain all visible files and folders without additional formatting or information.
To view more file information (such as permissions, ownership, last modified date, etc) and at the same time to list the files and directories, you would need to supply additional arguments to the command above.
- -a, lists all files and folders including hidden (starting with a dot) files and directories.
- -R, lists recursively (follows subfolders as well) all files and folders under the current directory.
- -l, lists all files and folders, each on a separate line, and provides additional information (permissions, ownership and modified date).
The arguments, as is with most Unix based commands, can be combined. For example: ls -alR will list all files (even hidden files starting with a dot), provide more information about them and will continue recursively into each subfolder.
Create & Edit Files & Folders via SSH
There are various ways you can create a new file using the SSH command line. The easiest and most common way is to use the command assigned for this task – touch. For example, if you would like to create a new file called .htaccess you would need to type the following command: touch .htaccess
The command also accepts several arguments, which for example can instruct the OS to edit the modified date of already existing file. For more information you can check the manual page for the command by typing: man touch
To edit the newly created file, you should use a file editor. There are several editors available on all SiteGround servers. The most commonly used are vim and pico (aka nano).
To request the file to be opened in nano, type: nano .htaccess
You can type text directly into the window. To save and exit the interface, you would need to press CTRL + X.
To create a new directory, you can use (replace the myDirectory string with the name of your folder): mkdir myDirectory
Move & Copy Files & Folders via SSH
Often you will need to move one or more files/folders or copy them to a different location. You can do so using an SSH connection. The commands which you would need to use are mv (short from move) and cp (short from copy). The mv command syntax looks like this: mv original_file new_name
By executing the above command you will move (rename) the file original_file to new_name. You can also use mv to move a whole directory and its content: mv includes/* ./
This will move all files (and folders) from the includes/ directory to the current working directory. In some cases, however, you will need to only update and move only files that were changed, which you can do by passing -u as an argument to the command: mv -u includes/* admin/includes
The copy (cp) command works the same way as mv, but instead of moving the files/folders it copies them. For example: cp original_file new_file
The command will copy the original_file file to new_file and will preserve the original one (the file will NOT be removed after it is copied).
cp also accepts various arguments: cp -R includes/ includes_backup/
-R instructs cp to copy files recursively (for example, a whole directory). To overwrite already existing files you should use the -f argument: cp -Rf includes/ admin/includes/
A more convenient way to copy files/folders is to use a 3rd party application, such as Midnight Commander. All our servers have mc installed and it is available by executing the mc command using the SSH Shell. Once inside the application you will see two sections – left and right. You can easily copy/move files from the left side directory to the right side using a semi-visual approach. You can even use your mouse to select files and function keys to execute commands.
Delete Files & Folders via SSH
Sometimes you would need to remove a file or a folder from the system. To do so using SSH, you would need to execute the appropriate command – rm
The command in its simplest form looks like: rm myFile.txt myFile1.txt myFile2.txt
However, listing all files/folders that need to be deleted can be quite time-consuming. Fortunately, rm accepts several arguments which can ease your task.
In the above example, you could type: rm myFile*.txt
This will match all files starting with ‘myFile’ and ending in ‘.txt’ and delete them.
To delete a whole folder and its content recursively, you can use: rm -rf foldername/
To delete all files/folders in the current directory, without deleting the directory itself, you would need to use: rm -rf *
Extract & Create Archives via SSH
Sometimes you would need to extract or create an archive file, i.e to install a script, you would usually download an archive and extract it to continue the installation. The very first step in the process would be to identify the exact archive type by looking at the file extension. The most common archive types are zip (ending with .zip), tar (.tar), Tar+Gunzip (.tar.gz), Bzip (.bz2) and Rar (.rar).
Each archive type has its own command for compressing/extracting as listed below.
To extract a ZIP file, you can use: unzip archive.zip
To extract a Tar file, you can use: tar -xvf archive.tar
To extract a Tar.Gz file, you can use: tar -zxvf archive.tar.gz
To extract a Rar file, you can use: rar x archive.rar
Each archive type has its own mechanism to create a new archive file. The most commonly used however is the tar.gz format.
An example of creating such a file is: tar -zcf archive-name.tar.gz foldername/
The above will archive the entire folder foldername in an archive named archive-name.tar.gz in the current working directory.
Search for Files & Folders via SSH
In some cases, you would need to find the location of a given file or to search for a certain text in all files under a directory. SSH provides two different commands, which can be used to accomplish this. In order to search for a file location, you can use the find command. Find is a very powerful tool and accepts various arguments allowing you to specify the exact search term (i.e search by name, by type or even by modified time).
For example, to search for a file called myFile.txt under the current folder (and all subfolders), you would need to use the following command: find . -name myFile.txt
If you are uncertain about the file name or would like to match a part of the name, you can use a wildcard pattern: find . -name “myFile*”
If you would like to list only directories and leave all files out of the result: find . -type d
Or if you want to filter only files modified in the last 2 days, you would need to use: find . -mtime -2
You can also search for a given text in the content of the files as well. The command you should be using, in this case, is grep. Grep is a very powerful tool and accepts various command line arguments. For a full list it is recommended to check the manual pages by typing man grep.
An example of using grep to find a certain text can be found below: grep “database” configuration.php
The above command instructs grep to look for the string “database” in the configuration.php file and display the containing line.
If you don’t know which file contains the text, you can use: grep -r -H “database” *
This will make grep look recursively (-r option) and provide the result in a human-readable format (-H option) for the string “database” in all (*) files under the current working directory.
To only list the file names containing the string you are searching but omit the line containing it, you can use the -l argument: grep -l “database” *
This will display the filenames containing the word “database”, but will not actually list the line containing it.
Grep can also be used to filter the results from other commands. For example, the line below will only output configuration.php result: ls -la | grep configuration.php
In some rare cases, find and grep may prove not useful.
For example, to find a certain file in the whole server, it would be best to use an alternative command – whereis or which: whereis perl or which perl
The execution of the above commands will locate the perl binary and display the full path(s) to it.
Manage Permissions & Ownership via SSH
The Unix files access is controlled. There are three types of access (permissions): read, write, execute
Each file belongs to a specific user and group (ownership). Access to the files is controlled by user, group, and what is called other/everyone permission bits and is usually set using a numerical value.
For example, 644 as permission bit will result in:
Owner/User Group Other/Everyone 644
Each number represents the access level and it can be from 0 to 7.
Different access levels depending on the numbers:
0 – no access to the file whatsoever
1 – execute permissions only
2 – write permissions only
3 – write and execute permissions
4 – read permissions only
5 – read and execute permissions
6 – read and write permissions
7 – read, write and execute permissions (full permissions)
Thus the above 644 permissions example will look like this:
Owner/User – Read and Write
Group – Read only
Other/Everyone – Read only
To allow a script to be executed and read by everyone but the only one who can write in it is your user, you would need to set 755 as permissions:
Owner/User – 7 – Full permissions
Group – 5 – read and execute
Other/Everyone – 5 – read and execute
Changing the permissions to 700 will make the file visible only for your username and no one else and setting it to 444 will allow only the file creator to modify it.
The command you need to execute to actually change the permissions is called ‘chmod’ and its syntax looks like this: chmod 755 file_name
The above example changes the permissions of the file_name file and sets them to 755.
You can recursively change the permissions of all folders and files using the recursive argument: chmod -R 755
This will modify the permissions of all files in the current folder and set them to 755.
You might wonder what the above user/group values are. These two settings are the actual ownership flags for a file or a folder. Each file has a primary user that owns it and a group assigned to it. To change those values, a special command exists — ‘chown’. Its syntax is: chown user:group file
For example: chown user:siteground file_name
The above line will set the owner of the file to ‘user’ and the group to ‘siteground’.
Changing ownership recursively is also permitted and the flag is -R: chown -R user:siteground *
Conclusion
I welcome your thoughts, questions or suggestions on my article on how to manage your SiteGround website via SSH.
You may support my work by sending me a tip using your Brave browser or by sending me a one time donation using your credit card.
Let me know if you found any errors within my article or if I may further assist you by answering any additional questions you may have.