Ryans Tutorials
More great tutorials at RyansTutorials

Basic Navigation!

Let's explore the system.

Introduction

In this section, we'll learn the basics of moving around the system. Many tasks rely on being able to get to, or reference the correct location in the system. As such, this stuff really forms the foundation of being able to work effectively in Linux. Make sure you understand it well.

So where are we?

The first command we are going to learn is pwd which stands for Print Working Directory. (You'll find that a lot of commands in linux are named as an abbreviation of a word or words describing them. This makes it easier to remember them.) The command does just that. It tells you what your current or present working directory is. Give it a try now.

  1. pwd
  2. /home/ryan

A lot of commands on the terminal will rely on you being in the right location. As you're moving around, it can be easy to lose track of where you are at. Make use of this command often so as to remind yourself where you presently are.

What's in Our Current Location?

It's one thing to know where we are. Next we'll want to know what is there. The command for this task is ls. It's short for list. Let's give it a go.

  1. ls
  2. bin Documents public_html

Whereas pwd is just run by itself with no arguments, ls is a little more powerful. We have run it here with no arguments in which case it will just do a plain listing of our current location. We can do more with ls however. Below is an outline of its usage:

ls [options] [location]

In the above example, the square brackets ( [ ] ) mean that those items are optional, we may run the command with or without them. In the terminal below I have run ls in a few different ways to demonstrate.

  1. ls
  2. bin Documents public_html
  3. ls -l
  4. total 3
  5. drwxr-xr-x  2 ryan users 4096 Mar 23 13:34 bin
  6. drwxr-xr-x 18 ryan users 4096 Feb 17 09:12 Documents
  7. drwxr-xr-x  2 ryan users 4096 May 05 17:25 public_html
  8. ls /etc
  9. a2ps.cfg aliases alsa.d cups fonts my.conf systemd
  10. ...
  11. ls -l /etc
  12. total 3
  13. -rwxr-xr-x  2 root root 123 Mar 23 13:34 a2ps.cfg
  14. -rwxr-xr-x 18 root root 78 Feb 17 09:12 aliases
  15. drwxr-xr-x  2 ryan users 4096 May 05 17:25 alsa.d
  16. ...

Let's break it down:

  • Line 1 - We ran ls in it's most basic form. It listed the contents of our current directory.
  • Line 4 - We ran ls with a single command line option ( -l ) which indicates we are going to do a long listing. A long listing has the following:
    • First character indicates whether it is a normal file ( - ) or directory ( d )
    • Next 9 characters are permissions for the file or directory (we'll learn more about them in section 6).
    • The next field is the number of blocks (don't worry too much about this).
    • The next field is the owner of the file or directory (ryan in this case).
    • The next field is the group the file or directory belongs to (users in this case).
    • Following this is the file size.
    • Next up is the file modification time.
    • Finally we have the actual name of the file or directory.
  • Line 10 - We ran ls with a command line argument ( /etc ). When we do this it tells ls not to list our current directory but instead to list that directories contents.
  • Line 13 - We ran ls with both a command line option and argument. As such it did a long listing of the directory /etc.
  • Lines 12 and 18 just indicate that I have cut out some of the commands normal output for brevities sake. When you run the commands you will see a longer listing of files and directories.

Paths

In the previous commands we started touching on something called a path. I would like to go into more detail on them now as they are important in being proficient with Linux. Whenever we refer to either a file or directory on the command line, we are in fact referring to a path. ie. A path is a means to get to a particular file or directory on the system.

Absolute and Relative Paths

There are 2 types of paths we can use, absolute and relative. Whenever we refer to a file or directory we are using one of these paths. Whenever we refer to a file or directory, we can, in fact, use either type of path (either way, the system will still be directed to the same location).

To begin with, we have to understand that the file system under linux is a hierarchical structure. At the very top of the structure is what's called the root directory. It is denoted by a single slash ( / ). It has subdirectories, they have subdirectories and so on. Files may reside in any of these directories.

Absolute paths specify a location (file or directory) in relation to the root directory. You can identify them easily as they always begin with a forward slash ( / )

Relative paths specify a location (file or directory) in relation to where we currently are in the system. They will not begin with a slash.

Here's an example to illustrate:

  1. pwd
  2. /home/ryan
  3. ls Documents
  4. file1.txt file2.txt file3.txt
  5. ...
  6. ls /home/ryan/Documents
  7. file1.txt file2.txt file3.txt
  8. ...
  • Line 1 - We ran pwd just to verify where we currently are.
  • Line 4 - We ran ls providing it with a relative path. Documents is a directory in our current location. This command could produce different results depending on where we are. If we had another user on the system, bob, and we ran the command when in their home directory then we would list the contents of their Documents directory instead.
  • Line 7 - We ran ls providing it with an absolute path. This command will provide the same output regardless of our current location when we run it.

More on Paths

You'll find that a lot of stuff in Linux can be achieved in several different ways. Paths are no different. Here are some more building blocks you may use to help build your paths.

  • ~ (tilde) - This is a shortcut for your home directory. eg, if your home directory is /home/ryan then you could refer to the directory Documents with the path /home/ryan/Documents or ~/Documents
  • . (dot) - This is a reference to your current directory. eg in the example above we referred to Documents on line 4 with a relative path. It could also be written as ./Documents (Normally this extra bit is not required but in later sections we will see where it comes in handy).
  • .. (dotdot)- This is a reference to the parent directory. You can use this several times in a path to keep going up the hierarchy. eg if you were in the path /home/ryan you could run the command ls ../../ and this would do a listing of the root directory.

So now you are probably starting to see that we can refer to a location in a variety of different ways. Some of you may be asking the question, which one should I use? The answer is that you can use any method you like to refer to a location. Whenever you refer to a file or directory on the command line you are actually referring to a path and your path can be constructed using any of these elements. The best approach is whichever is the most convenient for you. Here are some examples:

  1. pwd
  2. /home/ryan
  3. ls ~/Documents
  4. file1.txt file2.txt file3.txt
  5. ...
  6. ls ./Documents
  7. file1.txt file2.txt file3.txt
  8. ...
  9. ls /home/ryan/Documents
  10. file1.txt file2.txt file3.txt
  11. ...
  12. ls ../../
  13. bin boot dev etc home lib var
  14. ...
  15. ls /
  16. bin boot dev etc home lib var
  17. ...

After playing about with these on the command line yourself they will start to make a bit more sense. Make sure you understand how all of these elements of building a path work as you'll use all of them in future sections.

Let's Move Around a Bit

In order to move around in the system we use a command called cd which stands for change directory. It works as follows:

cd [location]

If you run the command cd without any arguments then it will always take you back to your home directory.

The command cd may be run without a location as we saw in the shortcut above but usually will be run with a single command line argument which is the location we would like to change into. The location is specified as a path and as such may be specified as either an absolute or relative path and using any of the path building blocks mentioned above. Here are some examples.

  1. pwd
  2. /home/ryan
  3. cd Documents
  4. ls
  5. file1.txt file2.txt file3.txt
  6. ...
  7. cd /
  8. pwd
  9. /
  10. ls
  11. bin boot dev etc home lib var
  12. ...
  13. cd ~/Documents
  14. pwd
  15. /home/ryan/Documents
  16. cd ../../
  17. pwd
  18. /home
  19. cd
  20. pwd
  21. /home/ryan

Tab Completion

Typing out these paths can become tedious. If you're like me, you're also prone to making typos. The command line has a nice little mechanism to help us in this respect. It's called Tab Completion.

When you start typing a path (anywhere on the command line, you're not just limited to certain commands) you may hit the Tab key on your keyboard at any time which will invoke an auto complete action. If nothing happens then that means there are several possibilities. If you hit Tab again it will show you those possibilities. You may then continue typing and hit Tab again and it will again try to auto complete for you.

It's kinda hard to demonstrate here so it's probably best if you try it yourself. If you start typing cd /hTab/<beginning of your username>Tab you'll get a feel for how it works.

Summary

pwd
Print Working Directory - ie. Where are we currently.
ls
List the contents of a directory.
cd
Change Directories - ie. move to another directory.
Relative path
A file or directory location relative to where we currently are in the file system.
Absolute path
A file or directory location in relation to the root of the file system.

Activities

Right, now let's put this stuff into practice. Have a go at the following:

  • Let's start by getting familiar with moving around. Use the commands cd and ls to explore what directories are on your system and what's in them. Make sure you use a variety of relative and absolute paths. Some interesting places to look at are:
    • /etc - Stores config files for the system.
    • /var/log - Stores log files for various system programs. (You may not have permission to look at everything in this directory. Don't let that stop you exploring though. A few error messages never hurt anyone.)
    • /bin - The location of several commonly used programs (some of which we will learn about in the rest of this tutorial.
    • /usr/bin - Another location for programs on the system.
  • Now go to your home directory using 4 different methods.
  • Make sure you are using Tab Completion when typing out your paths too. Why do anything you can get the computer to do for you?