Absolute and Relative Pathnames

Objective:

The objective of this lab is to introduce absolute and relative addressing.

Notes

Absolute Pathnames

  • All absolute paths begin with /
  • All absolute pathnames to a file (or directory) are unique
  • Absolute pathnames can be used to specify a location unambiguously to any user.

Relative Pathnames

  • Relative pathnames are relative to the:
    • present working directory (pwd) also called current directory
    • home directory (~ denotes the home directory).
    • parent directory relative to current directory is denoted by '..'
    • current, or present, directory is denoted by '.' (one dot)
  • Relative pathnames begin with anything other than /
  • Sometimes relative paths can be shorter compared to absolute paths
  • Relative to home pathnames begin with ~
  • Relative pathnames may not be the same for all users because relative-to-home '~' is different for every user. Also current, or present, directory '.' and parent directory '..' is dependent on pwd.

Examples

ls command is used to show list of files, with different paths

  • Absolute path examples
    • ls /
    • ls /etc
    • ls /usr/bin
  • Relative path examples
    • relative to present directory
      • ls .
      • ls
    • relative to parent directory

      ls ..

    • relative to home of logged in user (may not be mark.fernandes)

      ls ~

    • relative to user mark.fernandes' home

      ls ~mark.fernandes

Tips and Advice

  • You need to know what you are looking for before you can find/search for it.
  • Experiment by running all files in the /bin directory. Look up Wikipedia/Google on each of those commands. Some might seem useful, others not so much but every one of them serves some purpose otherwise they would not be there.
  • Use man to get more information about a command.
  • Every file whose pathname is located by / is unique, meaning it is not possible to have two files with the same name in the same directory.
  • Home directory for every user on the system is unique, just as the login username must be unique. It is under /home and has the same name as the user name. It is also the location that the user is in after logging onto the system.
  • When you make a mistake with a command you can quit it by pressing <CTRL>+C, which is the universal way in Linux/UNIX to interrupt a command.

Practice Questions

  1. In your Debian VM and using just two commands make the following directory structure in the /tmp directory of your VM. Lines in the diagram below that end with / are directories, rest are files.

    rough/
    ├── play/
    │   ├── games/
    │   │   │
    │   │   └── tetris
    │   │
    │   └── movies/
    │       │
    │       └── matrix
    └── work/
        ├── reports/
        │   │
        │   └── project.txt
        │
        └── scripts/
            └── ops105.bash
    
  2. Assume your pwd, at the start of each of the questions below, is movies.


    Copy scripts and its files into games. This means after each copy command completes you have a sub-directory within games called scripts with file ops105.bash in scripts.


    Copy using cp scripts into games, the following three different ways (you may delete scripts in games after copying, so that the directory tree is returned to the same start state each time):


    • when using cp to copy scripts into games use absolute paths
    • when using cp to copy scripts into games use relative-to-home directory (~)
    • when using cp to copy scripts into games use relative-to-username directory (~user)
    • when using cp to copy scripts into games use parent directory (..)
    • when using cp to copy scripts into games use current directory (.)
    • Which of the three paths above (a,b, or c) is the shortest.
    • If instead of copying to games, you copied to your present working directory (movies) how would that further shorten the relative path? Did it shorten the path?


  3. Delete the sub-directory scripts (and its files) that you copied in games using absolute path.
Last Updated: 2021-Apr-14 Wed 15:07