Getting to Grips With SSH – Part 3
By Ollie Armstrong Thursday, 24th June 2010
Welcome to the third guide in the series of blog posts helping you to understand basic SSH commands to aid your development.
In the previous part, you learned how to connect to your vCluster using SSH. This is a good starting point, but not very useful on its own. In this part we will be learning how to navigate your vCluster using SSH commands. It's pretty easy though!
Getting Started
First you will need to connect to your vCluster, you can refer back to the previous part if needs be. Once you've got to the SSH prompt you are able to type anything you like - but only certain commands will actually do something. Anything else will just return a helpful message saying that whatever you typed isn't recognised. The basic commands used for navigating around your vCluster files and folders are simple. Here we go...
Changing Directories - cd
This is perhaps one of the most important and useful SSH commands to remember. You honestly wouldn't get very far without it, It's that important! However it's also one of the easiest commands to remember, when you think to yourself 'I want to change directory' you will remember:
cd
That's all there is to it! But to use it you will need to type...
cd
...followed by a space, and then the directory name. For example:
cd www
Guess what that will do. Yep, it will change to the 'www' directory. Or you could use
cd http
No prizes for guessing what that one does. Yes, it changes to the 'http' directory.
But this is all relative to the directory you are currently in. So, you couldn't change to a directory called 'http', when you are inside a folder that doesn't contain a directory called 'http'. The directory you want to change to must be in the currently active directory. Get that into your head. There's an image below showing multiple nested directories, refer to that in this example. If you are in the 'images' folder and you wanted to change to the 'http' folder, you cannot just type...
cd http...because it will be looking for an 'http' folder inside the active folder (images). You are going to have to use the command below, in the Taking it Further section.
Taking it Further - Moving up Directories
Firstly, lets establish what I mean by moving up directories. The directory tree could have a folder called 'www', with a folder called 'http' in it. This 'http' folder could then have an 'images' folder. They are all nested in eachother. I've got a little diagram to explain this to you a little bit better.
Let's start in the 'images' folder, the deepest one, and you want to move up to the 'http' folder, you would use the command
cd ..
Notice those two dots after the cd command? These tell the shell you want to go back to the containing folder. Don't worry yourself if you don't completely understand this, it will all click when you suddenly realise you need to move up a directory. (In this example I have used another command, 'pwd'. This just shows the file path so it is easier to understand moving up folders, but it will be covered in the next part).
Listing Files and Directories - ls
Another very useful command. It simply will return the files and directories that exist in the active directory. It's easier to understand with an example. We have navigated to a folder that contains a file called 'index.html' and another folder called 'images', this is what the ls command does.
ls
As you can see, it puts the folders in blue and the files in white. That's cool. Combine this command with the cd command, and you are the master of SSH navigation.
Taking it Further - A Switch
So, you understand how to view the files/directories in a folder, but did you know that by default it won't display hidden files/folders? To do this is incredibly simple, you just add...
-a
...to the end of the command. So it would look like...
ls -a
Remember the space though, that's important. I have created an extra hidden file called .readme in the example directory we used above. Running the regular command would display exactly the same stuff as it did before, but adding the -a 'switch' displays hidden files.
You see the .readme file? I suppose you're also wondering about those strange blue dots at the start, but don't worry about those for now.
Making Directories - mkdir
Yet another really useful command. This one gives you the ability to create directories. It is simply an abbreviation of make directory.
mkdir
As with the 'cd' command, we have to put the directory name after the command. So an example command to create a directory called 'images', would be
mkdir images
It really is that easy. Yet an important point is the created directory won't automatically be switched to, you have to then run...
cd images...to switch to it.
Let's Recap!
Changing directories:cdListing files and directories:
lsMaking directories:
mkdir
With cd and mkdir, the directory name needs to be added at the end, following a space. With the ls command an optional -a can be added to the end to show hidden files and folders. Here is a shot of me using all of these commands to navigate around and create folders. See if you can follow what is happening.
All done! Wasn't too hard I hope. You can always leave a comment if you're confused and I will help you out.
Next up in the series we will be tackling a few more navigation commands, stay tuned!
Posted in Guides, SSH |
No Comments »