Skip to content
Snippets Groups Projects
bash-3-navigating.md 7.8 KiB
Newer Older
# Navigating files and directories

This section is to familiarize with the directory structure... 
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!"
## First steps : prompt & pwd

**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"}

!!! question "What is the current/working directory just by looking the prompt?"

??? example "Click to show the solution"
    In unix world, it exists a special character which is a shortcut for your home: `~`
!!! question "Check the name of your working directory"
    To check where we are located we must use the `pwd` command (print working directory)
    By default the terminal opens in your `home`. The complete path is `/home/username`.
!!! warning
    `Home` is for Linux, it will be called `Users` on MacOSX


## List files and directories (ls)

The `ls` command allows to list the content of a directory.  

!!! question "Check what files and directories are present in the current directory using the `ls` command."

??? example "Click to show the solution"
    `ls`

Check if you can see the `{{extra.working_directory}}` directory.

!!! question "Can you list the files and directories present inside the `{{extra.working_directory}}` directory?"

??? example "Click to show the solution"
    `ls {{extra.working_directory}}`

## Change directory (cd)
If you see `{{extra.working_directory}}` 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. 

## List files with options (ls -F)

!!! question "How you differentiate between a file and a directory? Run the `ls` command if you are unsure."
    Well actually, it is not possible to differenciate with only `ls`.
By using the option `-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.

## List hidden files (ls -a)
`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
Now try to move into the parent directory.

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

You should be now back to your home.


## Move back (cd -)
To move back into the directory you come from, you can use `cd -`. It will move back to the previous directory. Let's try and look at your working 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. 

!!! 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.  

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

!!! question "From your home, try to move into the `data` directory."

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

Actually what we have done so far is to use a relative path.

!!! question "Using relative path, move into the `script` directory. How would you do it?"
![](../images/3-navigation/tree_exo2.png){: style="height:250px"}
!!! question "From the `script` directory, list the content of the `data` directory"

??? example "Click to show the solution"
    `ls ../data`


## 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 exercises as for relative path. To do so, we need to be in the `home` directory. 

!!! question "Go back to your `home` directory"
!!! question "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`

!!! question "Move into the `script` directory using the absolute path"

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

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


!!! question "List the content of the `data` directory"

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

## Relative vs absolute path

We are now in the `script` directory.

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

!!! question "Which commands allow to move into your home directory?"
    1. cd .
    2. cd ..
    3. cd ../..
    4. cd /home/<userName\>
    5. cd ~
    6. cd home
    7. cd ~/data/..
    9. cd /

??? example "Click to show the solution"
## Autocompletion
It exists 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 `script` directory now. Try the command `cd` without any argument. What happens? (check with `pwd`)

??? 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 `~`, `..`, `.`