Authentic.
  • Hosting Plans
  • Client Login
ChemiCloud Blog
  • Hosting Plans
  • Client Login

19 Basic SSH Commands in Linux With Examples

    Updated on
  • January 28, 2021
  • 12 minute read
  • By Michael Thomas
Total
1
Shares
1
0
0

Basic SSH Commands

Learning to use SSH can be one of the best ways you can interact with your website and server, and it will help you master the Linux Command-Line interface.

The benefit of SSH is you can connect to a server from anywhere, really, and control the majority of its functions without having to be in front of the machine. You can also use advanced functions, like SFTP and SCP to copy or transfer files from the server to your computer or vice versa. 

In this post, you’ll learn about some of the most useful SSH commands you can use with your Linux server. 

Let’s dive in!

What is SSH?

SSH, or Secure Shell, is an encrypted protocol designed to connect one computer to another computer via a secured link which is authenticated using public-key cryptography, using either automatically generated public-private key pairs, or a password and pre-defined keysets.

Simply put, SSH is a protocol purpose-built for secure remote login and communicating or transmitting other secure network services over an insecure network. Think of it as a private phone call between your computer and the server. 

How Do I Use SSH?

Using SSH requires a couple of things. First, you’ll need to have your SSH login credentials, usually, your web hosting provider would give you this. You’ll also need an SSH client. 

  • If you’re on Windows 10, you can use the integrated Windows Powershell application.
  • If you’re on Linux or Mac, you can use the native Terminal app to connect. 

However, we strongly encourage the use of a dedicated SSH app, like PuTTy or xShell. These apps are purpose-built for SSH and have advanced features such as key management to ensure the connection you’re initiating is (and remains) secure. 

If you’re a ChemiCloud customer, you can reference this Knowledge Base article on How to Login to your account via SSH as it covers the setup of PuTTy on Linux, Mac, and Windows, as well as how you can use the integrated Terminal in cPanel to connect. 

If you aren’t hosting with ChemiCloud, contact your host for details on how to connect to their platform with SSH or consult their knowledgebase. 

Basic SSH Commands in Linux That You Should Know

This section contains a variety of commonly used SSH commands + variations on how you can use them. 

Directory and File Listing Commands

pwd

The pwd command shows the full path where you are in your server right now. Let’s say you need the full path to your public_html folder so you can enter it into an app like Dreamweaver or RapidWeaver to make use of it’s integrated FTP features. Using the pwd command, you can obtain that full path: 

ls

The ls (lowercase L, not a capital i 😏) command lists the contents of the current directory you’re in. Super useful if you’re looking for a file or folder in a directory. There’s also a number of ‘switches’ you can add to the command for some differentiation in the results it provides. 

  • ls -a will list all files, even those which are hidden.
  • ls -l lists file details for the contents of the directory, including permissions, ownership, dates created and modified, for example.
  • ls -s lists the size of the files, in blocks.
  • ls -s -h lists the size of the files individually, but in human-readable text.

cd

The cd, or change directory command, allows you to move into another directory. You can use this to move into subdirectories of the current directory you’re in, or out of the directory into another above your present location.

Looking at the sequence of commands above, we can see I’m already in the public_html folder and I have issued the ls command to list the folders (directories) in the public_html folder. There are 3 folders, cgi-bin, cookiefree, and jbdatabasevideo. Using cd cookiefree and pressing return afterward, you can see I am now in the cookiefree folder. 

As with almost all Linux CLI commands, there are switches you can use to alter the command’s output. In this case, you can use these switches to move up the directory tree or go back from whence you came.

  • cd .. will go to the directory immediately above the one in which you are presently located.
  • cd ../foldername will move you sideways into another folder adjacent to the one above you. 
  • cd – will take you to the previous directory.
  • cd / will take you to the root directory

mkdir

The mkdir command allows you to create a new directory (or folder, if you prefer a UI term) inside of the directory which you are presently located. When using this command, you would give a name for the directory after mkdir. See example: 

In the above screenshot, we can see that I was located inside the public_html folder and used the ls command to list the folders in that directory. 

The only folders present are cgi-bin, cookiefree, and jbdatabaseservice. Using mkdir images, I have instructed the server to create an empty directory called images. Then, using ls I can see from the updated directory listing, the folder was created successfully. 

rmdir

The rmdir command can be used to remove or delete a directory on your server. Be very careful using this as you can’t undo the action and you won’t be asked if you’re sure you want to proceed. 

As we can see from the image above, I’ve used the rmdir command to remove the folder I previously created called images. Using ls, I can see the folder is no longer there. 

There are some variations of the rmdir command, as well: 

  • rm * nameoffolder – this will delete all of the files in the specified directory.
  • rm -r nameoffolder – this command will not only delete the specified folder, but all of the folders and files inside of it. Use this one with extreme caution!

cp

The cp, or copy command, is how you can copy both files and folders from one location to another. You need to take care when using the command to specify where you want the file (or folder) to be copied to when using the command. 

For example, to copy a file named file.txt to file_backup.txt you wound run the following command:

cp file file_backup

To copy a file to another directory, specify the absolute or the relative path to the destination directory.

When only the directory name is specified as a destination, the copied file has the same name as the original file. In the following example we are copying the file file.txt to the /backup directory:

cp file.txt /backup

To copy a directory, including all its files and subdirectories, use the -R or -r option.

In the following example we are copying the directory Images to Images_backup:

cp -R Images Images_backup

The command above creates the destination directory, and recursively copy all files and subdirectories from the source to the destination directory.

If the destination directory already exists, the source directory itself and all its content is copied inside the destination directory.

mv

The mv, or move command, allows you to move a file or folder from one directory to another. Similar to the cp command, except you aren’t duplicating the content, just moving it. 

For example, to move the file file.txt from the current working directory to the /backup directory you would run:

mv file.txt /backup

To rename a file you need to specify the destination file name:

mv file1.txt file2.txt

The mv command also allows you to use pattern matching. For example, to move all JPG files from the current directory to the ~/Pictures directory, you would use:

mv *.jpg ~/Pictures

zip

You’re probably familiar with the concept of zipping and unzipping files in your OS using a GUI, but you can do the same functions via the command line. 

For example, in my public_html folder, there is a directory called wordpress. I want to create a zip of this directory. To do that, I would use the command as shown below:

Let’s break down the structure of this command: 

zip invokes the zip command
-r means recursively, it’s informing the zip command that I want to zip everything in the folder
wordpress.zip is the name of the zip file I want to create
wordpress is the directory to be used in the creation of the zip file

The zip command also has a few switches you can use: 

  • zip -m filename.zip nameoffolder compresses a folder, then deletes the original folder, leaving just the zip file. Super convenient! 
  • zip -d filename.zip nameoffolderorfile deletes the name of the file or folder you specified within the zip file (if you use a file, be sure to include it’s extension, like image.jpg)

unzip

The unzip command, just as the name implies, will unzip zip files to their original structure. 

As you can see, this command will unzip the wordpress.zip into a folder called wordpress, within the public_html folder.

There are also a few other switches you can use with the unzip command:

  • unzip filename.zip -x excludedfile.extension will exclude the specified file from being unzipped, while unzipping the rest of the file
  • unzip zipfile1.zip zipfile2.zip zipfile3.zip will unzip multiple files. You can unzip as many zip files as you want, they will be done one after the other. 

tar

The tar command is very similar to the zip command, except it works with the .tar.gz file format, which is an alternative to zip files. 

As we can see, I’ve issued the ls command to list the directories in my public_html folder and there’s a folder called wordpress.

The command above will create a tar.gz called wordpress.tar.gz from the folder wordpress for me. 

As you can see from the image above, the tar.gz file was created successfully and the folder ‘wordpress’ still remains. 

To decompress a tar.gz file, you would use this command: 

  • tar xvzf tarfilename.tar.gz

The 4 letters after the command tar represent specific instructions. You can use them to vary your desired command: 

  • c instructs tar to compress.
  • x instructs tar to extract.
  • v stands for verbose and instructs tar to display the names of the files affected by the command.
  • z instructs tar to uncompress the archive.
  • f tells tar you are providing a file name for the archive. 

touch

This command creates a new empty file on the server. You would provide a filename and extension after the command, like so: 

  • touch index.html

Note, you can’t create files in other folders using the touch command. Only the folder in which you’re presently located. 

cat

cat shows the contents of a file. For example, if you were to cat your wp-config.php file, it would look similar to this: 

cat is a great way to quickly display the contents of a file without having to open it.

grep

grep is really handy if you know the file you need to examine is long and you know what you’re looking for. For example, if I wanted to search the readme.html file from WordPress for the word wordpress, the command would look like this: 

The end result would highlight the word wordpress in the file. 

The grep command also has a variety of switches you can use: 

  • grep -i “string” filename will search for a string that isn’t case sensitive.
  • grep -c “string” filename will count the number of instances of the string in a file.
  • grep -l “string” * will list the files that contain the string.
  • grep -n “string” filename will display the line numbers along with the result. 

head filename.extension

This is useful if you just want to open the first 10 lines of a file. You can use this command to do just that. 

tail filename.extension

This works in the same way as head, but in reverse. This command will show you the last 10 lines of a file. 

find

This command allows you to search through files and folders and return only the files/folders with matches to the search criteria you’ve specified. You can use this command in a variety of ways, considering it has a number of switches. Let’s review them: 

  • find operator criterion string

The operators you can use for find are: 

  • / (slash) which will search the whole system.
  • . (dot) will search the working directory.
  • ~ (tilde) will search the home directory.

The criterion you can use for find are:

  • -name = filename
  • -user = files belonging to a specified user
  • -size = files of a specific size
  • -type -d = the directory type, only search directories specified
  • -type -f = the file type, only find files of this type

The search term can be anything you like. For example: 

  • find / -name “index” will search the entire system for files named index.

Find command comes in handy if you want to find and remove large error_log files generated by your scripts for instance.

List the Error Log files with its disk space usage details:

find . -type f -iname error_log -exec du -sh {} \;
  • type : Specify the type to find.
  • iname: Specify the name to find.
  • exec: Execute the “du -sch” and lists the output with file size.

Find and Delete error_log Files in Linux

find . -type f -iname error_log -delete
  • delete: This switch removes the outputs from the find command.

Find and list files bigger than 100MB in Linux

find . -type f -size +100M -exec ls -lh {} \;

To find files that are bigger than 1GB, use the following command:

find . -type f -size +1G -exec ls -lh {} \;

You might wonder how to find and list files of a certain size. For instance, you can find files between 100MB and 500MB using the following command:

find . -type f -size +100M -size -500M -exec ls -l {} +

du

This command is handy if you want to find the size of one or more files on your account. 

For example:

  • du -h functions.php – this command will display the file size of the functions.php file in a human-readable format. I.e. it will display it in megabytes or kilobytes, not bytes. 

Like other Linux commands, there’s a number of switches for this one too: 

  • df -h will display the result in a human-readable format.
  • df -m will display the result in MB.
  • df -k will display the result in KB.
  • df -T will show the file system type in an extra column.
  • df -ht /directoryname lets you view information about a specific directory.
  • df help lists other options that you can use, with their descriptions.

chmod

The chmod command lets you change a file’s permissions. To determine which permissions you need is best to use a Unix Permissions Calculator, or contact our Happiness Engineers as this can be dangerous to use for the inexperienced. 

wget

This is a handy command that lets you download and save files from the internet to your computer. They will be downloaded into the directory from which you invoked the command. So, if you’re in your public_html folder and run wget, the file will be saved to the public_html directory. 

Here’s an example of the wget command in action: 

This command will download the jpg image specified and save it to my folder ‘wordpress’. 

Terminal Commands

clear

You can use the clear command to wipe the terminal screen. Super handy if you have a lot of text and want to focus on a clear, fresh start. 

reset

The reset command will remove all the commands and output from the terminal screen and also clear the screen. 

history

This command will show the last 50 commands issued via terminal. Great if you forgot what you were working on or need a refresh on the syntax of a particular command you issued a few minutes ago. 

Other Useful SSH Commands

  • netstat

This command will provide you with the details of the network connection your server’s using and it’s status. 

  • exit

As it says, this command will close the connection to the server. 

Summary

Knowing how to use SSH can save you a lot of time, especially if you get accustomed to working with it. You may find you don’t need to use the cPanel File Manager or your FTP client as much as you did. 

If you enjoyed this article, you’ll enjoy our hosting even more! Check out our Web Hosting plans today! 

You May Also Like
WordPress Security
View Post

WordPress Security Checklist: 12 Steps to Secure Your Website

    Updated on
  • February 25, 2021
  • By Michael Thomas
Productivity Apps
View Post

23 Productivity Apps for Remote Work You Should Check Out in 2021

    Updated on
  • February 16, 2021
  • By Michael Thomas
View Post

Photographing Her Way Around the World – an Interview With Cindy Carlsson

    Updated on
  • January 20, 2021
  • By Michael Thomas
PHP 8
View Post

PHP 8 is Now Available at ChemiCloud

    Updated on
  • January 15, 2021
  • By Michael Thomas
Emails Go to Spam
View Post

10 Reasons Why Your Emails Go to Spam

    Updated on
  • January 18, 2021
  • By Michael Thomas
View Post

How To Enable and Setup Cloudflare on Your Website

    Updated on
  • December 21, 2020
  • By Michael Thomas
WordPress 5.6
View Post

WordPress 5.6 “Simone” Is Here – See What’s New!

    Updated on
  • December 10, 2020
  • By Michael Thomas
View Post

Celtic Calling: Talking Matt Steady’s Latest Album – Nawglan – And Open Hands Compassion

    Updated on
  • January 11, 2021
  • By Michael Thomas

Leave a Reply Cancel reply

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

Recent Posts
  • WordPress Security
    WordPress Security Checklist: 12 Steps to Secure Your Website
      Updated on
    • February 25, 2021
    • 13 minute read
  • Productivity Apps
    23 Productivity Apps for Remote Work You Should Check Out in 2021
      Updated on
    • February 16, 2021
    • 26 minute read
  • Photographing Her Way Around the World – an Interview With Cindy Carlsson
      Updated on
    • January 20, 2021
    • 6 minute read
  • PHP 8
    PHP 8 is Now Available at ChemiCloud
      Updated on
    • January 15, 2021
    • 5 minute read

Subscribe now to our newsletter

  • Web Hosting
  • WordPress Hosting
  • Reseller Hosting
  • VPS Hosting
  • ChemiCloud Coupons
© 2020 CCHOSTING, INC. ALL RIGHTS RESERVED

Input your search keywords and press Enter.

We are using cookies to give you the best experience on our website.

You can find out more about which cookies we are using or switch them off in settings.

ChemiCloud Blog
Powered by  GDPR Cookie Compliance
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognizing you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful. You can read more about our Privacy Policy here.

Strictly Essential Cookies

Enables essential services and functionality, including identity verification, service continuity and site security. Opt-Out is not available.

If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.