Some useful commands in SSH

How to move between directories in SSH?

When you are connected to your server, through Putty or your Linux/MacOS terminal, you are sent to the home of the user with which you are connected. However you may want to go to a specific directory, for instance, in order to edit or remove a file.

The specific command to move between the directories on your server is “cd”.

First let’s take an example of a directory architecture (a directories tree):

/home/
   - superuser/
            - myspace/
                       - website1/
                       - website2/

This tree shows that the “home” directory is at the root of the machine (computer or server), because of the “/” before its name, and contains the directory named “superuser” which contains the directory “myspace” which contains the directories “website2” and “website1” which may contain the directory and files of your website. Obviously these names can change from a Linux distribution, or any other OS, to another. We will keep this example to illustrate our article.

There are several ways to use “cd”:

  • moving one step above to a directory which is above the current directory
    cd ../

    In this command “../” means “the directory above”. And if we want to go two steps above, we can use:

    cd ../../

    and so on for x steps above. Thus, if you are located in the “website1” folder, and you enter this last command, you will be sent in the “superuser” folder.

  • moving in a directory which is in your current location:
    Let’s say we are in the directory called “myspace” which contains the directories “website1” and “website2”, and you want to enter “website1”:

    cd website1/

    but now let’s say we have another directory in “website1” called “images”, and you want to enter “images”, you have two ways to enter “images” from your current location (“myspace”):

    • you can enter “website1” first, and then enter “images”. In this case the commands would be:
      cd website1/

      and then, once you are in “website1”:

      cd images/
    • the other way is to go directly to “images”:
      cd website1/images/

Now, as an exercise, let’s say you are in the “images” folder and you want to go back and enter the “website2” directory. Remember that this “website2” directory is located two steps above and you want to enter it. The command would be:

cd ../../website2/

In this command we say “go back two steps above (../../) and enter “website2”.

Now you know how to move between your files and directories with SSH. A last point which is quite important: You have noted in our previous example that we entered the “website1” directory from the “myspace” directory with:

cd website1/

The “/” at the end means “website1” is a directory. However you will see and use sometimes the “/” at the beginning of a directory, in addition to the “/” at the end. Like in our tree of directories:

cd /home/

If you use this “/” at the beginning of a directory that means this directory is at the root of your server. Remember that “/” at the beginning of a directory always means that this directory is at the root of your server. What’s the useful knowledge here? A big one. That means, wherever you are located you can always retrieve your path, or go to the root of your server directly by using this “/” at the beginning. Let’s take an example: Imagine you are lost, you don’t know exactly where you are on your server, but you have to go to the root of your server to a directory called “etc” (this “etc” directory always exists on Linux operating systems actually, and so you may have to go to this directory one day!):

/etc/

No matter where you are on your server at the moment, you can enter:

cd /etc/

and you will be in this “etc” directory. What I mean is you don’t have to ask yourself, how many steps is this directory away from my current location? How many “../” do I have to enter to get to this directory? No, just enter “cd /etc/” and you will be sent to this directory.

How to list all the files which are in a directory in SSH?

Ok so now we know how to move between directories and go to a specific directory located at the root of the server, we would now like to list all the files and directories which are in a specific directory.

This command is “ls”, for “list” (quite easy to remember!). So, in our previous example, when you are located in the “myspace” directory which contains the “website1” and “website2” directories, you can see these two directories with:

ls

This command will display this:

website1 website2

Like what we’ve just seen with the “cd” command, we can also see what contains a specific directory located elsewhere from our current location:

ls website1/images/

or, if we are in the “images” folder and want to list what is in the “myspace” folder:

ls ../../

There are different options that you can use to get a more readable list which would make directories and files listed a different way. For instance, “ls -la” command will make you a list of all files and directories but with additional information. If done in the “myspace” directory:

ls -l

it would list:

drwxr-xr-x    3 guysintech  psaserv        102 16 jui  2:34 website1

drwxr-xr-x    5 guysintech  psaserv       170 15 mar 1:18 website2

You can see it shows the directories but also other information like the permissions on these directories (“d” for directory and then “rwx” for the user, “rx” for the group, and “rx” for the others – remember the part about users and groups in a LAMP stack!). It also displays the user, “guysintech”, and the group, “psaserv”, which “own” the directories, the weight of the directories and the last date they have been changed. As you can see the list has much more information with the “ls -l” command than with “ls” only.

On some Linux OS distributions you can also use “ll” rather than “ls -l”.

How to change permissions on a file or a directory?

You may need to change the permissions on a file or a directory. To do that you can use the command “chmod”. For example, if we are in the “myspace” directory:

chmod 775 website1/

Or, if we are in the “images” directory and want to change the permissions of the “myspace” directory:

chmod 775 ../../../myspace/

You may also want to change the permissions recursively for all the subdirectories and files which are in a specific directory. For example, let’s say we are in the “myspace” directory and we want to change the permissions of the “website1” directory and all of the items it contains. We are going to use the argument “-R”:

chmod -R 755 website1/

This will change the permissions of the “website1” folder and also of all the items it contains 755 permissions (rwx, r-x, r-x). Remember we have seen the file permissions on the previous article)

Use this command with caution because there is no way to go back once you have pressed “enter”! It can break all your website, web app or other software. Especially never use this command to change the permissions of a root directory, like the “etc” directory we’ve seen before.

How to change users and groups of a file or a directory?

Now you know how to change the permissions of a file and a directory, you may need to change the “ownership” of a file or a directory. This can be done with the “chown” command:

chown user:group directory/
chown user:group file
chown user:group ../../directory/file

And, like for the “chmod” command, you can also apply your changes recursively:

chown -R user:group directory/

And, like for the “chmod” command, use this command with caution!

How to find a file or a directory with SSH?

If you are looking for a file or a directory there is also a command for that. The command is “find”. It can be described like this:

find "directory from which you want to search" -type "file or directory" -name "name of the file or the directory"

Let’s take the previous example, we are looking for a file called “Darkmatter.php” in the “website1” directory:

find website1/ -type f -name Darkmatter.php

“f” is for file, if we were looking for a directory we would have written “d” as the type.

If you want to search everywhere on your server as you don’t know where your file or directory is located, here is an example if you look for a directory called “website”

find / -type d -name website

The “/” means you are looking from the root of your server.

If you want to find a file from the directory you are located in you can use “.”, like this:

find . -type d -name website

How to edit a file with SSH?

There are different editors in SSH. The best way is to use several ones and then to choose the one that suits you the best. My personal favorite is “Vim”, but another one which is very popular is called “nano”.

Note that if one of these two editors is not available when you try to use them, you can install them with a simple command:

On Ubuntu/Debian:

  • for Vim:
apt-get install vim
  • for Nano:
apt-get install nano

On CentOS/Fedora:

  • for Vim:
yum install vim
  • for Nano:
yum install nano

Then to edit a file, this is a very simple command:

vim file
nano file

You just have to replace “file” by the name of the file you want to edit. Of course, in the above command, we suppose we are in the same folder as the file we want to edit. If the file is one step above and we want to edit it without moving to this step above:

vim ../file or nano ../file

Or, to keep the example we use since the beginning of this article, if we are in the “myspace” directory and we want to edit a file called “portrait.jpeg” which is in the “images” directory, the command, with vim, would be:

vim website1/images/portrait.jpeg

You can move with the arrows of your keyboard to the line or the string you want to edit.

Some key commands when using Vim

Once you are in a file with Vim and you want to edit something, here are the commands to use:

  • Entering in edition mode:
    Just type “i” on your keyboard (for “insert”), then press “Esc” on your keyboard once you have inserted your code or text to exit the “insert” mode.
  • Searching for a string:
    Type “/” and enter the string you are looking for and press enter. To search for the next occurence of the string, type “n”. To exit the “search” mode, just press “Esc” on your keyboard.
  • Saving and exiting the file:
    There are two options – The first option is to enter “:w” + “enter” on your keyboard. In this case the file will be saved but will still be open if you want to proceed with another insertion. Note that as long as your file is open you can remove your insertion by typing the key “u” on your keyboard (“u” for “undo”).
    The second option is to save and exit the file: You just have to enter “:wq”. Be careful, this will save and exit, and so you cannot “undo”!
    A last option is to exit the file without saving your changes. To do that, just type “:q!” and you will exit the file without saving your changes.
  • Scrolling down directly to the bottom of the file:
    Just type “GG”
  • Going back to the top of a file:
    Just type “gg”
  • Going directly to a specific line of the file:
    Just type “:” and the line that you want to reach
  • Cutting an entire line:
    Go to the beginning of the line and type “dd”

Some key commands when using Nano

Nano displays a menu with the key commands that you can use with this editor:

nano-menu-commands

Unlike with Vim you don’t have to enter in “insert” mode to begin typing your information. Once you are in the file and in the appropriate line you can delete or add your content directly with the usual keyboard keys. In the screenshot above you can see the most used commands. The “^” means the “ctrl” key on your keyboard, so, for example, if you want to scroll down the file you can use “ctrl+V”, you can search a specific string with “ctrl+W”, and you can exit the file with “ctrl+X”.

How to remove a file or a directory with SSH?

The command to remove something is “rm” (for “remove”). Enter this command if you want to remove a file:

rm file

You just have to replace “file” by the name of the file you want to edit. Of course, in the command above, we suppose we are in the same folder than the file we want to remove. If the file is one step above and we want to remove it without moving to this step above:

rm ../file

Or, to keep the example we use since the beginning of this article, if we are in the “myspace” directory and we want to remove a file called “portrait.jpeg” which is in the “images” directory, the command would be:

rm website1/images/portrait.jpeg

This will display a prompt to ask for your confirmation about removing this file. If you want to avoid this prompt, you can add the argument “f” (“f” for “force”) in addition to “rm”:

rm -f website1/images/portrait.jpeg

But use it with caution as it will remove the file definitely with no option to go back.

For a directory you have to add the argument “-r” in addition to “rm”. For example if you want to remove the directory “images”:

rm -r website1/images/

You can also use the “f” argument to avoid the prompt for your confirmation:

rm -rf website1/images/

How to move or rename a file with SSH?

It is the same command to move a file or to rename a file:

mv website1/ newname/

In this command we have renamed the directory “website1” to “newname”. If we just want to move a file or a directory without renaming it, we just have to give it the same name:

mv website1/ ../website1/

In this command we have moved website1/ from the current location to one step above.

How to find a file containing a specific string with SSH?

This command is very useful if you don’t know which file contains a specific item you want to modify. If you know the name of this specific item you can find the file(s) that contain it. For instance, let’s say you are looking for a specific div of your website. With the inspector of your browser you see a specific css class that makes this div looking not the way you want. You want to remove this css class so that this div is not concerned by the CSS styles applied to this class:

<div class="widget theme_services_list widget-theme_services_list">

In this example above we want to remove the class “widget” so that this div is not concerned anymore by the CSS applied to the class “widget”. We have to find from the files of your website on your server, where is the file which creates this webpage. You can use this command to find the file:

grep . -rnw -e widget

The “grep” command will return all the files which are containing “widget”. Obviously the list of files can be long because “widget” is a very generic term, but it’s up to you to enter this command from the appropriate directory on your server to reduce the number of files. What I mean here is, if you look for “widget” while you are at the root of your website, there should be many files which contain “widget” while if you enter this command while you are in the directory that manages the webpages of the specific section you want to modify the number of files should be smaller. We will see this in some exercises.

The trick is also to look for a string which is less used than “widget” but also used by your webpage. In this example, if you look for the files that use “theme_services_list” the list may be smaller:

grep . -rnw -e theme_services_list

And in the list of files returned by this command you will also get the file that contains this div. Then just edit the file with “vim” or “nano” and remove “widget” in the appropriate div. Save and exit and it’s done!

How to transfer a file through SSH?

The idea here is to know how to transfer a file or a directory from one server to another, or from your computer to a server with SSH. It is useful in case, for example, you are performing a backup from one server to another. We are going to see the command called “rsync”,however there are also other commands. The basic way to use rsync is the following:

rsync -argument location destination

“argument” is a list of specific arguments, like “v” for verbose (it returns the information of the transfer in your terminal so that you can follow the transfer and check if everything is going well.

“location” is where the file or directory is located on the machine (server or your computer) from which you perform the request.

“destination” is where you want to send the file or directory to.

Here is an example if we want to move a file from your local computer to a distant server:

rsync -av Downloads/myfile.zip user@123.456.78.9:/home/superuser/public_html/mywebsite/

If you perform this command on your computer it will send the file “myfile.zip”, which is in the Downloads/myfile.zip on your computer, to the directory “mywebsite” located at “/home/superuser/public_html/” on a server at 123.456.78.9.

You can do the same with a directory:

rsync -av Downloads user@123.456.78.9:/home/superuser/public_html/mywebsite/

This command will send the folder “Downloads” on the machine you are connected to (the server or the computer) to the distant server in the “mywebsite” folder.

Please note that in all the commands above, we stated that the “Downloads” folder is in the directory you are already located in when you perform the command, otherwise you have to enter the full path to this directory from the place you are located in. As an example, let’s say you open a terminal on your computer, and by default, the terminal opens with your location in the “Desktop” folder of your computer; the tree structure of your folders on your computer may look like this:

User
  --Desktop
  --Downloads
  --Documents

If you are located in the “Desktop” folder and you want to send the “Downloads” folder, you have two options:

  • The “noob” one:
    • First, you choose to move to the upper directory (“User”):
cd ../
    • And then you enter a command like this:
rsync -av Downloads user@123.456.78.9:/home/superuser/public_html/mywebsite/
  • The “good” one:
    • You directly enter your command from your current location:
rsync -av ../Downloads user@123.456.78.9:/home/superuser/public_html/mywebsite/

The only difference here is that we have added “../” before “Downloads”. But just understand the logic and master this way to do it, and this will save you a lot of time compared to having to move from one location to another with the “cd” command each time you have to run a command!

Et voilà! Now you know the basic commands and you can train yourself. There are many more commands that you can perform. Each time you ask yourself “how can I do that in SSH” you can be sure to find an answer by browsing the web. But be careful in using the commands in SSH as, most of the times, there is no option to get back a file that has been removed or changed, etc.