Unix shell: 1

Never used a terminal before? This article will help you get started.

Aim: after working through this article you will be able to use your terminal to navigate your way around your filesystem, and to list what is in your current directory (folder).

The commands we will learn are: ls, pwd and cd.

First you will need to open your terminal. If you are working on a mac you just need to search spotlight (the magnifying glass in the menu bar) for terminal.app. If you are working on Ubuntu linux press Ctrl - Alt + T (other linux distributions may vary).

A box will appear - it will look something a little like this:

                    $
                    

This is your terminal. The line ending in a "$" is known as the command prompt and this is where we will type our instructions. Your terminal will most likely start in your home directory. First we will look at the ls command which lists everything in your current directory. Type ls into your terminal and press enter.

                    $ ls
                    Applications     Desktop
                    Documents        Downloads
                    Dropbox          Google Drive
                    Library          Movies
                    Music            Pictures
                    $
                    

All the files and directories in your current directory will now be listed in your terminal, which will look something like the above. Notice that after executing the ls command we again have a command prompt, waiting for a new instruction from us.

We can verify what directory we are currently in by using the pwd (present working directory) command.

                    $ ls
                    Applications     Desktop
                    Documents        Downloads
                    Dropbox          Google Drive
                    Library          Movies
                    Music            Pictures
                    $ pwd
                    /Users/MyName
                    $
                    

Lets move into the "Desktop" directory to make things easier. We do this using the cd Desktop command, where the second argument is the name of the directory we want to go to. We can verify where we are again by using pwd.

                    $ cd Desktop
                    $ pwd
                    /Users/MyName/Desktop
                    $
                    

Now, go to your Desktop outside of the terminal and create a directory, say called "TestDirectory" (don't put any white spaces in the name).

In your terminal verify you are in the Desktop directory and the use ls to list all contents.

                    $ pwd
                    /Users/MyName/Desktop
                    $ ls
                    TestDirectory    somethingElse.txt    anotherThing.doc
                    $
                    

We can now cd into our new directory, and, if we wanted to go back to the parent directory Desktop we can use the cd .. command.

                    $ cd TestDirectory
                    $ pwd
                    /Users/MyName/Desktop/TestDirectory
                    $ cd ..
                    $ pwd
                    /Users/MyName/Desktop
                    $
                    

We have now learned how to move around the filesystem. Here are some useful extensions to the commands we learned:

  • cd ~ will move you back to your home directory

  • cd ../.. will move you to the parent of your parent directory (this can be extended as far as you like)

  • ls -a will list everything in your current directory including hidden files
  • ls -l will list everything in your current directory with additional information

  • ls -al perform both the above functions