Skip to content
Snippets Groups Projects
bash-3-navigating.md 7.33 KiB
Newer Older
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
# Navigating

Back to the directory structure... this time we will learn to `navigate between directories` and the difference between **absolute** and **relative** paths.

??? quote "First click and follow the instructions below only if you start the course at this stage! Otherwise skip this step!"
    {%
    include-markdown "pages/bash/bash-0-setup.md"
    %}
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed


## Where are we?

**Below is represented the sub-part of the directory structure we will work on (keep this picture aside, it will help!):**

![](../images/3-navigation/tree.png){: style="height:500px"}

!!! warning
    `Home` is for Linux, it will be called `Users` on MacOSX

!!! question "Could you tell where our terminal is located currently?"

??? example "Click to show the solution"
    To check where we are located we must use the `pwd` command.

!!! tip
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
    By default the termnial open in your `home`. The complete path is `/home/username`.
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed

!!! question "In unix world, it exists a special character which is a shortcut for your home. Which character is that?"

??? example "Click to show the solution"
    Yes the `~` charcater is also your home! 


## Move into a child directory (Move down)

Check what files and directorys are presents in the current directory using the `ls` command.
Check if you can see the {{extra.working_directory}} directory. If you see it you can move in it using the `cd` command. This command means `Change Directory`.

```bash
cd {{extra.working_directory}}
```

!!! question "How can you be sure you are in the {{extra.working_directory}} now?"

??? example "Click to show the solution"
    You can look at your prompt or check with the `pwd` command. 

## File or directory?

Do you remember the `ls` command to list the content of a directory? Good! Did you remember how you differentiate a file from a directory? Run the `ls` command if you are unsure.

??? example "Click to show the solution"
    Well actually it is not possible like that.

Bu using the `-F` you can tell ls to classify the output by adding a marker to file and directory names to indicate what they are:

  * a trailing / indicates that this is a directory
  * @ indicates a link
  * \* indicates an executable

!!! tip
    Depending on your shell’s default settings, the shell might also use colors to indicate whether each entry is a file or directory.

!!! question "Is there any file then in our current directory?"
    
??? example "Click to show the solution"
    Yes there is a `README` file.

## The hidden files and directories

`ls` command has many options. The `-a` allows you to display the hidden files and directories. They are prefixed with a `.`.

```bash
ls -F -a
```

!!! question "What special directory do you observe?"
    ??? example "Click to show the solution"
    We can see a `.` directory as well as a `..` directory.

`.` means the **current working directory**  
`..` means the **parent directory**

!!! tip
    Most command line tools accept multiple options by combining them with a single - and no spaces between. `ls -F -a` is equivalent to `ls -Fa`.

## Move in the parent directory (Move up)

Now try to move in the parent directory.

??? example "Click to show the solution"
    `cd ..`

You should be now back to your home.

!!! tip
    `cd ..` brings you up in the directory structure

## Move back

To move into a directory we have seen we can use the command `cd` and a directory name. If you don't remember which directory you come from you can use `cd -`. It will mover back to the previous directory.

!!! tip
    `cd -` brings you back in the directory structure.
    If you execute `cd -` twice, you will end up back in the starting directory.


## Relative path

A `relative` path is a path that specifies the location of a file or directory with respect to the current working directory. Any path that does not begin with a separator character ("/") is a relative path. 
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed

!!! tip
    Relative path:  

    * Path related to the present working directory (where the user is working)
    * Never starts with `/`
    * Depends on where the user is working

At this point you should be in your `home` directory. You can check using `pwd` or move into your home with `cd ~` to be sure.  

Now try to move into the `data` directory.

![](../images/3-navigation/tree_exo1.png){: style="height:250px"}

??? example "Click to show the solution"
    `cd {{extra.working_directory}}/data`

Actually what we have done so far is to use a relative path.
Let's continue and move into the `script` directory. How would you do it?

![](../images/3-navigation/tree_exo2.png){: style="height:250px"}

!!! tip
    It is impossible from the `data` directory to move into `{{extra.working_directory}}` using `cd {{extra.working_directory}}`!! To move up you  must use `..`!

??? example "Click to show the solution"
    `cd ../script`

## Absolute path
 
An `absolute` path that refers to a particular location in a file system. Absolute paths are usually written with respect to the file system’s root directory, and begin with `/` in Unix machines.

!!! tip
    Absolute path:  

    * Complete path of a file starting from the root directory `/`
    * starts always with `/`
    * always good wherever user is working

Let's do the same exercise as for relative path. To do so we need to start by being in the `home` directory. How you do that already?

??? example "Click to show the solution"
    `cd ~`

Now try to move into the `data` directory using an absolute path.

![](../images/3-navigation/tree_exo3.png){: style="height:250px"}

??? example "Click to show the solution"
    `cd /home/<userName>/{{extra.working_directory}}/data`

Let's now move into the `script` directory using the absolute path. How would you do it?

![](../images/3-navigation/tree_exo4.png){: style="height:250px"}

??? example "Click to show the solution"
    `cd /home/<userName>/{{extra.working_directory}}/script`

## Excercise relative vs absolute path

We are now in the `script` directory.

![](../images/3-navigation/tree_exo5.png){: style="height:250px"}

!!! question "How would you move into your home directory?"
    1. cd .
    2. cd ..
    3. cd ../..
    4. cd /home/<userName\>
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
    5. cd ~
    6. cd home
    7. cd ~/data/..
    9. cd /

??? example "Click to show the solution"
    3,4,5

jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
## File path Autocompletion
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
It exist a wonderful behavior in Bash called the autocompletion. What it is? 
Autocompletion refers to a feature that allows you to automatically complete the names of commands and **file paths** by pressing the Tab key. This feature can significantly improve efficiency and reduce the need to type out long or complex names manually.

Bash can complete file paths based on what exists in the current directory or any specified path.

As you type a file or directory name and press Tab, Bash will complete the name if it's unique. If not, it will show a list of possible matches.

!!! exercise 
    Move to `data` directory via the absolute path using the autocompletion.   


## A final navigation tip

You are supposed to be in the `data` directory now. Try the command `cd` without any argument. What happens? (check with `pwd`)
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed

??? example "Click to show the solution"
    Right you moved in the `home` directory. This command is even shorter than `cd ~`.

!!! Success "Quick recap"
    In this section we've learned:

    - How to navigate in the Unix directory structure
    - The difference between absolute and relative path
    - Special character `~`, `..`, `.`