How to use the command line interface - CLI

With the terminal you may very easily navigate your computer directories and even access the Internet. In fact it is easier to work with folders and files with the terminal than it is with a Graphical User Interface (GUI - a program where you click the folders in order to open them).

Let's learn the basics of navigating using the terminal.

Identification: user@computer:~$

First things first. For you to navigate, you should know where you are. Have you ever noticed that text in the beginning of each new line in the terminal that cannot delete? That is the identification of the current user, the current computer and the current folder. This is very important because, as you'll learn, using the terminal you can actually use different users, different computers and, of course, different folders.

Let's go for an example: say the user is "joe", the computer name (the computer name is set during installation process) is "compute" and that he is now in the folder /home/jose/Documents/. Terninal will look like this:


joe@computer:~/Documents$

The tilde symbol "~" means the home folder of the current current user.

If the user is in the folder /var/www, terminal will look like this:


joe@computer:/var/www$

When you're using terminal and want to know where you are, simply check the path after the colon.

The dollar symbol $ indicates you are using a regular user. The hash symbol #, would indicate you are using the super user, or "root". So, when you find a tutorial saying something like this:


root@computer:/home/user/# apt-get install hex-a-hop 

Notice the hash symbol! It means this command must be used as root. You can use the sudo program to act as super user (sudo = su [super user] do [act, do something]).

 user@computer:~/$ sudo apt-get
install hex-a-hop 

Navigating

There are some basic commands to navigate using the terminal. Here are some of them:

cd : used to enter a directory, examples: access the "Documents" folder that is inside the current folder:


user@computer:~/$ cd Documents

Access the same folder using its full path. Notice that when you place a "/" before the first folder you mean that you are indicating the full path, the path from the root directory.

 user@computer:~/$ cd
/home/user/Documents 

Access the "texts" folder inside Documents folder which is in the current folder. :


user@computer:~/$ cd Documents/texts
Notice you don't need to enter a folder first to enter an inner folder afterwards. You may simply give the path.

Leave the current folder and go back to home:


user@computer:~/$ cd 

Go back to the the parent directory of the current directory:


user@computer:~/$ cd ..

Go back two parent directories:


user@computer:~/$ cd ../..

Check the content of the directory. It would be no use navigate if you cannot see what is in the folder. Here comes the ls command (ls = list)


user@computer:~/$ ls

You may list the content of other folders as well, giving ls the path of the folder you want to list. This will not change your current directory, but will show the contents of the other folder:


user@computer:~/$ ls /var/www/

You can list contents and their permissions.


user@computer:~/$ ls -l

And of course, list the contents and permissions of another folder:


user@computer:~/$ ls -l /var/www/

You can also filter the results of your listing. Sometimes there are simply way too much results for you to check manually. Simply give a part of the name you want to find and use these two special chars:

*means any characters, any number of them
?means any character, only one

Let's go to some examples:

List only files with the extension .odt


user@computer:~/$ ls *.odt

List only files with the name "important" with any extension:


user@computer:~/$ ls important.???
Jutsu ls

The technique you just learned will give you much power. Consider this: You want to list all .odt files in both folders "Documents" and "Downloads"


user@computer:~/$ ls Do*s/*.odt

Or you want to find all png images in any folder in the current directory

user@computer:~/$ ls */*.png

And here is a bonus: if you place a -R after the command, ls will list recursively (meaning it will also list the contents of any folder found inside the given folders). So, if you want to list everything inside everything a folder contains, use a -R. You will need a new kind of filter though.

Use grep and you have a very powerful filter. Simply place a pipe grep after the command ( |grep) followed by the term you want to filter by. Let's list all .odt files of our entire system:

user@computer:~/$ ls / -R | grep .odt

How to access folders with composed names (with spaces)?

Have you noticed that when you try to use the command cd with a folder that has a space in its name it doesn't work?

user@computer:~/$ cd important folder/

Terminal answers that "important" folder does not exist. The space indicates, in terminal, that the name is over. We need to tell terminal that this time the space will not mean the end of the name. Use a "\" before the space to tell that the next space means only a space:

user@computer:~/$ cd important\ folder/

I am lost! Where am I?

If the terminal you are using is not showing the path you are in, you can use the command pwd (Present Working Directory) to find out where you are:

user@computer:~/$ pwd

Terminal will show you the current folder.

These commands may be very useful for a basic navigation. Get used to them and then try to learn some more advanced techniques. You will soon realize terminal will make your life a lot easier.

Jumping around

You may find yourself switching between some directories often. You can use a stack of folders to help you out with this. Imagine that after navigating through some folders you now need to go back to the one you were five steps before. And then you may need to come back to the present folder. Well the pushd, popd and dirs will help you do it quite easily.

Use pushd to navigate instead of cd and each folder you visit will be pushed to a stack. Use dirs -v to see the stack and cd followed by a tilda and a number (~1, ~2, etc) to visit that item of the stack.

comments powered by Disqus