BONUS: learn directory relationships

This is an exercise in how your computer is setup as directories (folders) and files. Everything that happens on the computer ends up in a file in a directory.


preview

I build the relationships below step by step to see how files and folders are related like a family tree, and at the end know how to move around in them because I understand those relationships.

doe
├── .hidden_file_in_doe
├── .hidden_folder_in_doe
├── grandparent_of_mary_jane_doe
├── grandparent_of_lil_john_doe
├── parent_of_jane_doe
├── parent_of_john_doe
├── empty_file_in_doe
├── jane_doe
   ├── child_of_doe
   ├── .hidden_file_in_jane_doe
   ├── .hidden_folder_in_jane_doe
   ├── aunt_of_lil_john_doe
   ├── parent_of_mary_jane_doe
   ├── sibling_of_john_doe
   ├── empty_file_in_jane_doe
   └── mary_jane_doe
       ├── grandchild_of_doe
       ├── .hidden_file_in_mary_jane_doe
       ├── .hidden_folder_in_mary_jane_doe
       ├── child_of_jane_doe
       ├── child_of_sibling_of_john_doe
       ├── cousin_of_lil_john_doe
       └── empty_file_in_mary_jane_doe
└── john_doe
    ├── child_of_doe
    ├── .hidden_file_in_john_doe
    ├── .hidden_folder_in_john_doe
    ├── parent_of_lil_john_doe
    ├── sibling_of_jane_doe
    ├── uncle_of_mary_jane_doe
    ├── empty_file_in_john_doe
    └── lil_john_doe
        ├── grandchild_of_doe
        ├── .hidden_file_in_lil_john_doe
        ├── .hidden_folder_in_lil_john_doe
        ├── child_of_sibling_of_jane_doe
        ├── child_of_john_doe
        ├── cousin_of_mary_jane_doe
        └── empty_file_in_lil_john_doe

10 directories, 30 files

These are all the commands used in this chapter


questions about directory relationships

Here are questions you can answer after going through this chapter


what is a folder?

A folder (directory) is a box for files. It helps organize things, just like a folder in a file cabinet is used to keep files that should be together in one place.

I keep every project I work on in its own folder (directory). All the code in this book is done in a folder named pumping_python.


what is a file?

A file is a collection or container for text, like paper we write or print on and keep in a folder. Their names can end with an extension to show what type of file it is. For example


requirements

I type this in a terminal to make sure the tree program is installed

tree

if it is not installed on the computer, the terminal shows

tree: command not found

if it is installed, the terminal shows

.

0 directories, 0 files

if there is nothing in the directory.

If there is something in the directory, it shows the relationships.

The tree program shows how files and folders on a computer are related, this helps to know how to get from one folder to another, because it shows the way I can go.

It is easier to go where I want if I know where I am.


how to install tree


how to install tree on Linux/Windows Subsystem for Linux


sudo apt update

I always do a full upgrade, you do not have to

sudo apt full-upgrade --yes

type this in the terminal to install tree

sudo apt install tree

continue with how to see what directory I am in


how to install tree on Windows without Windows Subsystem for Linux


tree comes with Windows, no need to install anything.

You are going to use the commands below for the ones I have in the chapter

when you call pwd or tree it shows \ as the separator, not /. For example

...\pumping_python\doe

not

.../pumping_python/doe

Your tree will also look different because of different ways of drawing and sorting. Continue with how to see what directory I am in


how to install tree on Mac OS


type this in the terminal

brew install tree

continue with how to see what directory I am in


how to see what directory I am in

I start by checking where I am in the terminal because It is easier to go where I want if I know where I am. I can do this with the pwd program

pwd

the terminal shows

.../pumping_python

because I am in the pumping_python folder

  • pwd shows the path/address of the folder I am in

  • pwd means print working directory, it prints the directory I am in, to the terminal

  • each / shows a parent-child relationship

  • the first / is for root which is the starting ancestor of all the folders on the computer

  • the first / is at the highest level

    Caution

    if you type this in the terminal it will show you every file and folder on your computer as a tree

    tree /
    

    it runs for a while when you have many files and folders on your computer.

    use ctrl+c on the keyboard if you want to stop it from running.

Note

  • If you see pumping_python when you type pwd, skip to the part where I create doe

  • If you see a different name, continue to the next step - how to change directory


how to change directory

I use the cd program to change directories

cd pumping_python

cd means change directory

the terminal shows

cd: no such file or directory: pumping_python

this means the folder I want to go to is not in the folder where I am


how to make a directory

It is easier to go where I want if I know where I am.


how to see what is in a directory

  • I can use ls to show what is in a directory and see information about the files in it

    ls
    

    the terminal goes back to the command line

    .../pumping_python/doe
    

    this directory is empty

  • ls has a few options. I try ls again with one of them

    ls --all
    

    the terminal shows

    .  ..
    

    Attention

    on MacOS you may get this error

    ls: unrecognized option '--all'
    

    --all is the long form of the option, and there is usually a short form, use -a instead

    ls -a
    

    Note

    on Windows without Windows Subsystem for Linux use dir /ah instead of ls -a

    dir /ah
    

    the terminal does not show . and .. on Windows without Windows Subsystem for Linux

    • --all/-a tells ls to show all the things in the directory even those that start with . ( they are hidden by default)

    • I can hide a file or directory if I put . before its name, for example .hidden

  • I try to change directory to the .

    cd .
    

    the terminal shows

    .../pumping_python/doe
    
  • I try cd with .. to see what happens

    cd ..
    

    the terminal shows

    .../pumping_python
    
    • I am back in the pumping_python directory

    • .. is for the parent of the directory I am in

    • .. is pumping_python when I am in doe

    • pumping_python is the parent of doe

  • I go back to doe

    cd doe
    

    the terminal shows

    .../pumping_python/doe
    

I know how to see what is in a directory


how to look at directory relationships

  • I can use the tree program to see the files and folders in a directory and how they are related. I type it in the terminal to see what is in the doe directory

    tree
    

    the terminal shows

    .
    
    0 directories, 0 files
    

    there is nothing in doe, it is empty

    Note

    on Windows without Windows Subsystem for Linux use tree /F instead of tree

    tree /F
    
  • I try to change directory to jane_doe, a child of doe

    cd jane_doe
    

    the terminal shows

    cd: no such file or directory: jane_doe
    

    jane_doe is not a child of doe, yet

  • I make jane_doe

    mkdir jane_doe
    

    the terminal goes back to the command line

  • I use ls to see what is now in doe

    ls
    

    the terminal shows

    jane_doe
    

    Note

    on Windows without Windows Subsystem for Linux use dir /ah instead of ls -a

    dir /ah
    
  • I use tree to show the doe family tree

    tree
    

    the terminal shows

    .
    └── jane_doe
    
    2 directories, 0 files
    
    • jane_doe is a child of doe

    • . is the working directory, which is doe in this case

    • the line in the tree that goes from . to jane_doe shows I can go from doe right to jane_doe

  • I change directory to jane_doe

    cd jane_doe
    

    the terminal shows

    .../pumping_python/doe/jane_doe
    

    I am in jane_doe

  • I change directory to doe

    cd doe
    

    the terminal shows

    cd: no such file or directory: doe
    

    doe is not a child of jane_doe

  • I change directory back to the parent of jane_doe

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    
    • I am back in doe

    • .. is for the parent of the directory I am in

    • .. is doe when I am in jane_doe

  • I try to change directory to john_doe

    cd john_doe
    

    the terminal shows

    cd: no such file or directory: john_doe
    

    john_doe is not a child of doe, yet

  • I make john_doe

    mkdir john_doe
    

    the terminal goes back to the command line

  • I use ls to see what is in doe

    ls
    

    the terminal shows

    jane_doe  john_doe
    
  • I use tree to show the doe family tree

    tree
    

    the terminal shows

    .
    ├── jane_doe
    └── john_doe
    
    3 directories, 0 files
    
    • john_doe is a child of doe

    • . is the working directory, which is doe in this case

    • the line in the tree that goes from . to john_doe shows I can go from doe right to john_doe

  • I change directory to john_doe

    cd john_doe
    

    the terminal shows

    .../pumping_python/doe/john_doe
    

    I am in john_doe

  • I change directory to doe

    cd doe
    

    the terminal shows

    cd: no such file or directory: doe
    

    doe is not a child of john_doe

  • I change directory back to the parent of john_doe

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    
    • I am back in doe

    • .. is for the parent of the directory I am in

    • .. is doe when I am in john_doe

  • I try to change directory to .hidden_folder_in_doe

    cd .hidden_folder_in_doe
    

    the terminal shows

    cd: no such file or directory: .hidden_folder_in_doe
    

    .hidden_folder is not in doe, yet

  • I make a hidden folder

    mkdir .hidden_folder_in_doe
    

    the terminal goes back to the command line

  • I use ls to see what is in doe

    ls
    

    the terminal shows

    jane_doe  john_doe
    
    • .hidden_folder_in_doe is hidden

    • I can hide a file or directory if I put . before its name

  • I use ls with the -a option to see everything that is in doe

    ls -a
    

    the terminal shows

    .  ..  .hidden_folder_in_doe  jane_doe  john_doe
    
  • I use tree to show the doe family tree

    tree
    

    the terminal shows

    .
    ├── jane_doe
    └── john_doe
    
    3 directories, 0 files
    
    • .hidden_folder_in_doe is hidden

    • I can hide a file or directory if I put . before its name

  • I can also use tree with the -a option

    tree -a
    

    the terminal shows

    .
    ├── .hidden_folder_in_doe
    ├── jane_doe
    └── john_doe
    
    4 directories, 0 files
    
    • . is the working directory, which is doe in this case

    • the line in the tree that goes from . to .hidden_folder_in_doe shows I can go from doe right to .hidden_folder_in_doe

  • I change directory to .hidden_folder_in_doe

    cd .hidden_folder_in_doe
    

    the terminal shows

    .../pumping_python/doe/.hidden_folder_in_doe
    

    I am in .hidden_folder_in_doe

  • I change directory to doe

    cd doe
    

    the terminal shows

    cd: no such file or directory: doe
    

    doe is not a child of .hidden_folder_in_doe

  • I change directory back to the parent of .hidden_folder_in_doe

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    
    • I am back in doe

    • .. is for the parent of the directory I am in

    • .. is doe when I am in .hidden_folder_in_doe


  • I change directory to jane_doe

    cd jane_doe
    

    the terminal shows

    .../pumping_python/doe/jane_doe
    
  • I show what is in the folder

    ls
    

    the terminal goes back to the command line

  • I use ls with the short form of the --all option

    ls -a
    

    the terminal shows

    .  ..
    

    jane_doe has no children

  • I use tree

    tree
    

    the terminal shows

    .
    
    0 directories, 0 files
    

    jane_doe has no children

  • I change directory to a child of jane_doe

    cd mary_jane_doe
    

    the terminal shows

    cd: no such file or directory: mary_jane_doe
    

    jane_doe has no children, yet

  • I make mary_jane_doe

    mkdir mary_jane_doe
    

    the terminal goes back to the command line

  • I try to go to mary_jane_doe again

    cd mary_jane_doe
    

    the terminal shows

    .../doe/jane_doe/mary_jane_doe
    
    • I am in the mary_jane_doe folder

    • mary_jane_doe is a child of jane_doe

    • jane_doe is a child of doe

  • I go up a level to the parent of mary_jane_doe

    cd ..
    

    the terminal shows

    .../doe/jane_doe
    

    I am back in jane_doe

  • I change directory to a hidden folder in jane_doe

    cd .hidden_folder_in_jane_doe
    

    the terminal shows

    cd: no such file or directory: .hidden_folder_in_jane_doe
    

    there is no folder named .hidden_folder_in_jane_doe in jane_doe

  • I make a .hidden_folder_in_jane_doe

    mkdir .hidden_folder_in_jane_doe
    

    the terminal goes back to the command line

  • I try to go to .hidden_folder_in_jane_doe again

    cd .hidden_folder_in_jane_doe
    

    the terminal shows

    .../doe/jane_doe/.hidden_folder_in_jane_doe
    
  • I go up a level to the parent of .hidden_folder_in_jane_doe

    cd ..
    

    the terminal shows

    .../doe/jane_doe
    

    I am back in jane_doe

  • I use tree

    tree
    

    the terminal shows

    .
    └── mary_jane_doe
    
    2 directories, 0 files
    
  • I use tree -a

    tree -a
    

    the terminal shows

    .
    ├── .hidden_folder_in_jane_doe
    └── mary_jane_doe
    
    3 directories, 0 files
    
    • . is jane_doe when I am in jane_doe

    • the line in the tree that goes from . to mary_jane_doe shows I can go from jane_doe right to mary_jane_doe

    • the line in the tree that goes from . to .hidden_folder_in_jane_doe shows I can go from jane_doe right to .hidden_folder_in_jane_doe

  • I go up a level to the parent of jane_doe

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    

    I am back in doe


  • I change directory to john_doe

    cd john_doe
    

    the terminal shows

    .../pumping_python/doe/john_doe
    

    john_doe is a child of doe

  • I show what is in the folder

    ls
    

    the terminal goes back to the command line

  • I use ls with the short form of the --all option

    ls -a
    

    the terminal shows

    .  ..
    

    john_doe has no children

  • I use tree

    tree
    

    the terminal shows

    .
    
    0 directories, 0 files
    

    john_doe has no children, yet

  • I change directory to a child of john_doe

    cd lil_john_doe
    

    the terminal shows

    cd: no such file or directory: lil_john_doe
    

    john_doe has no children

  • I make lil_john_doe

    mkdir lil_john_doe
    

    the terminal goes back to the command line

  • I try to go to lil_john_doe again

    cd lil_john_doe
    

    the terminal shows

    .../doe/john_doe/lil_john_doe
    
    • I am in the lil_john_doe folder

    • lil_john_doe is a child of john_doe

    • john_doe is a child of doe

  • I go up a level to the parent of lil_john_doe

    cd ..
    

    the terminal shows

    .../doe/john_doe
    

    I am back in john_doe

  • I change directory to a hidden folder in john_doe

    cd .hidden_folder_in_john_doe
    

    the terminal shows

    cd: no such file or directory: .hidden_folder_in_john_doe
    

    there is no folder named .hidden_folder_in_john_doe in john_doe

  • I make .hidden_folder_in_john_doe

    mkdir .hidden_folder_in_john_doe
    

    the terminal goes back to the command line

  • I try to go to .hidden_folder_in_john_doe again

    cd .hidden_folder_in_john_doe
    

    the terminal shows

    .../doe/john_doe/.hidden_folder_in_john_doe
    
  • I go up a level to the parent of .hidden_folder_in_john_doe

    cd ..
    

    the terminal shows

    .../doe/john_doe
    

    I am back in john_doe

  • I use tree

    tree
    

    the terminal shows

    .
    └── lil_john_doe
    
    2 directories, 0 files
    
  • I use tree -a

    tree -a
    

    the terminal shows

    .
    ├── .hidden_folder_in_john_doe
    └── lil_john_doe
    
    3 directories, 0 files
    
    • . is john_doe when I am in john_doe

    • the line in the tree that goes from . to lil_john_doe shows I can go from john_doe right to lil_john_doe

    • the line in the tree that goes from . to .hidden_folder_in_john_doe shows I can go from john_doe right to .hidden_folder_in_john_doe

  • I go up a level to the parent of john_doe

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    

    I am back in doe

  • I show the doe family tree

    tree
    

    the terminal shows

    .
    ├── jane_doe
       └── mary_jane_doe
    └── john_doe
        └── lil_john_doe
    
    5 directories, 0 files
    

    Note

    on Windows without Windows Subsystem for Linux use tree /F instead of tree

    tree /F
    
  • I use the -a option with tree

    tree -a
    

    the terminal shows

    .
    ├── .hidden_folder_in_doe
    ├── jane_doe
       ├── .hidden_folder_in_jane_doe
       └── mary_jane_doe
    └── john_doe
        ├── .hidden_folder_in_john_doe
        └── lil_john_doe
    
    8 directories, 0 files
    

    the lines show that

    • mary_jane_doe and lil_john_doe are grandchildren of doe

    • I can go from doe right to .hidden_folder_in_doe

    • I can go from doe right to jane_doe

    • I can go from doe right to john_doe

    • I can go from jane_doe right to .hidden_folder_in_jane_doe

    • I can go from jane_doe right to mary_jane_doe

    • I can go from john_doe right to .hidden_folder_in_john_doe

    • I can go from john_doe right to lil_john_doe

I know how to look at directory relationships


how to make an empty file

I can make empty files in a folder with the touch program

  • I make an empty file in doe

    touch empty_file_in_doe
    

    the terminal goes back to the command line

    Note

    on Windows without Windows Subsystem for Linux use New-Item instead of touch

    New-Item empty_file_in_doe
    
  • I make an empty hidden file in doe

    touch .hidden_file_in_doe
    
  • I use ls to see what is in the folder

    ls
    

    the terminal shows

    empty_file_in_doe  jane_doe  john_doe
    
  • I use ls with the -a option

    ls -a
    

    the terminal shows

    .                      .hidden_folder_in_doe  john_doe
    ..                     empty_file_in_doe
    .hidden_file_in_doe  jane_doe
    

    Note

    on Windows without Windows Subsystem for Linux use dir /ah instead of ls -a

    dir /ah
    

    the terminal does not show . and .. and always shows hidden folder and files

  • I change directory to jane_doe

    cd jane_doe
    

    the terminal shows

    .../pumping_python/doe/jane_doe
    
  • I make an empty file with touch

    touch empty_file_in_jane_doe
    

    the terminal goes back to the command line

  • I make an empty hidden file with touch

    touch .hidden_file_in_jane_doe
    

    the terminal goes back to the command line

  • I show what is in the folder

    ls -a
    

    the terminal shows

    .                           .hidden_folder_in_jane_doe
    ..                          empty_file_in_jane_doe
    .hidden_file_in_jane_doe  mary_jane_doe
    
  • I change directory to the parent of jane_doe

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    
  • I change directory to john_doe

    cd john_doe
    

    the terminal shows

    .../pumping_python/doe/john_doe
    
  • I make an empty file with touch

    touch empty_file_in_john_doe
    

    the terminal goes back to the command line

  • I make an empty hidden file with touch

    touch .hidden_file_in_john_doe
    

    the terminal goes back to the command line

  • I show what is in the folder

    ls -a
    

    the terminal shows

    .                           .hidden_folder_in_john_doe
    ..                          empty_file_in_john_doe
    .hidden_file_in_john_doe  lil_john_doe
    
  • I change directory to the parent of john_doe

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    

    I am back in doe

  • I use tree to show the doe family tree

    tree
    

    the terminal shows

    .
    ├── empty_file_in_doe
    ├── jane_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
    └── john_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
    
    5 directories, 3 files
    

    Tip

    Your terminal may use colors to show the difference between directories and files

  • I use tree with the -a option

    tree -a
    

    the terminal shows

    .
    ├── .hidden_file_in_doe
    ├── .hidden_folder_in_doe
    ├── empty_file_in_doe
    ├── jane_doe
       ├── .hidden_file_in_jane_doe
       ├── .hidden_folder_in_jane_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
    └── john_doe
        ├── .hidden_file_in_john_doe
        ├── .hidden_folder_in_john_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
    
    8 directories, 6 files
    

  • I want to make a file in mary_jane_doe. I use cd to go to its parent first

    cd jane_doe
    

    the terminal shows

    /pumping_python/doe/jane_doe
    

    I am in jane_doe

  • I change directory to mary_jane_doe

    cd mary_jane_doe
    

    the terminal shows

    .../doe/jane_doe/mary_jane_doe
    

    I am in mary_jane_doe

  • I make an empty file in mary_jane_doe

    touch empty_file_in_mary_jane_doe
    

    the terminal goes back to the command line

  • I make an empty hidden file in mary_jane_doe

    touch .hidden_file_in_mary_jane_doe
    

    the terminal goes back to the command line

  • I make a hidden folder in mary_jane_doe

    mkdir .hidden_folder_in_mary_jane_doe
    

    the terminal goes back to the command line

  • I use ls to show what is in the folder

    ls -a
    

    the terminal shows

    .
    ..
    .hidden_file_in_mary_jane_doe
    .hidden_folder_in_mary_jane_doe
    empty_file_in_mary_jane_doe
    
  • I go back to the parent of mary_jane_doe

    cd ..
    

    the terminal shows

    .../doe/jane_doe/
    

    I am in jane_doe

  • I go back to the parent of jane_doe

    cd ..
    

    the terminal shows

    ...pumping_python/doe
    

    I am back in doe


  • I want to make a file in lil_john_doe. I use cd to go to its parent first

    cd john_doe
    

    the terminal shows

    /pumping_python/doe/john_doe
    

    I am in john_doe

  • I change directory to lil_john_doe

    cd lil_john_doe
    

    the terminal shows

    .../doe/john_doe/lil_john_doe
    

    I am in lil_john_doe

  • I make an empty file in lil_john_doe

    touch empty_file_in_lil_john_doe
    

    the terminal goes back to the command line

  • I make an empty hidden file in lil_john_doe

    touch .hidden_file_in_lil_john_doe
    

    the terminal goes back to the command line

  • I make a hidden folder in lil_john_doe

    mkdir .hidden_folder_in_lil_john_doe
    

    the terminal goes back to the command line

  • I use ls to show what is in the folder

    ls -a
    

    the terminal shows

    .
    ..
    .hidden_file_in_lil_john_doe
    .hidden_folder_in_lil_john_doe
    empty_file_in_lil_john_doe
    
  • I go back to the parent of lil_john_doe

    cd ..
    

    the terminal shows

    .../doe/john_doe/
    

    I am in john_doe

  • I go back to the parent of john_doe

    cd ..
    

    the terminal shows

    ...pumping_python/doe
    

    I am back in doe

  • I use tree to see what I have so far

    tree
    

    the terminal shows

    .
    ├── empty_file_in_doe
    ├── jane_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            └── empty_file_in_lil_john_doe
    
    5 directories, 5 files
    
  • I use tree with the -a option

    tree -a
    

    the terminal shows

    .
    ├── .hidden_file_in_doe
    ├── .hidden_folder_in_doe
    ├── empty_file_in_doe
    ├── jane_doe
       ├── .hidden_file_in_jane_doe
       ├── .hidden_folder_in_jane_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           ├── .hidden_file_in_mary_jane_doe
           ├── .hidden_folder_in_mary_jane_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── .hidden_file_in_john_doe
        ├── .hidden_folder_in_john_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            ├── .hidden_file_in_lil_john_doe
            ├── .hidden_folder_in_lil_john_doe
            └── empty_file_in_lil_john_doe
    
    10 directories, 10 files
    

I know how to make an empty file


how to use directory relationships

  • I try to go from doe to mary_jane_doe in 1 step

    cd mary_jane_doe
    

    the terminal shows

    cd: no such file or directory: mary_jane_doe
    
  • I use tree with the -d option to show only the directories

    tree -d
    

    the terminal shows

    .
    ├── jane_doe
       └── mary_jane_doe
    └── john_doe
        └── lil_john_doe
    
    5 directories
    
    • there is no line from doe to mary_jane_doe because mary_jane_doe is not a child of doe

    • there is a line from jane_doe to mary_jane_doe because mary_jane_doe is a child of jane_doe

    • there is a line from doe to jane_doe because jane_doe is a child of doe

    • I can go from doe to jane_doe to mary_jane_doe

  • I go from doe to mary_jane_doe in 1 step with its parent

    cd jane_doe/mary_jane_doe
    

    the terminal shows

    .../doe/jane_doe/mary_jane_doe
    

    I cannot go to mary_jane_doe without its parent

  • I can go from mary_jane_doe back to doe in 1 step with ..

    cd ../..
    

    the terminal shows

    .../pumping_python/doe
    

    I am back in doe.

    • .. is for the parent of a directory

    • ../.. is for the parent of the parent, that is a grandparent. I can use as many as I need for each parent, for example ../../../.. is the great great grand parent

    • .. from mary_jane_doe is jane_doe

    • .. from jane_doe is doe

  • I try to go from doe to lil_john_doe in 1 step

    cd lil_john_doe
    

    the terminal shows

    cd: no such file or directory: lil_john_doe
    
  • I use tree with the -d option to show only the directories

    tree -d
    

    the terminal shows

    .
    ├── jane_doe
       └── mary_jane_doe
    └── john_doe
        └── lil_john_doe
    
    5 directories
    
    • there is no line from doe to lil_john_doe because lil_john_doe is not a child of doe

    • there is a line from john_doe to lil_john_doe because lil_john_doe is a child of john_doe

    • there is a line from doe to john_doe because john_doe is a child of doe

    • I can go from doe to john_doe to lil_john_doe

  • I go from doe to lil_john_doe in 1 step with its parent

    cd john_doe/lil_john_doe
    

    the terminal shows

    .../doe/john_doe/lil_john_doe
    

    I cannot go to lil_john_doe without its parent

  • I can go from lil_john_doe back to doe in 1 step with ..

    cd ../..
    

    the terminal shows

    .../pumping_python/doe
    

    I am back in doe.

    • .. is for the parent of a directory

    • ../.. is for the parent of the parent

    • .. from lil_john_doe is john_doe

    • .. from john_doe is doe

Note

  • I can only go right to folders that are where I am (children)

  • I can use the path/address of a folder to go to it

  • It is easier to go where I want if I know where I am.

I know how to use directory relationships


how to use touch with directory relationships

  • I show the doe family tree

    tree
    

    the terminal shows

    .
    ├── empty_file_in_doe
    ├── jane_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            └── empty_file_in_lil_john_doe
    
    5 directories, 5 files
    
  • I make an empty file in jane_doe from inside doe

    touch jane_doe/child_of_doe
    
  • I make an empty file in mary_jane_doe from inside doe

    touch jane_doe/mary_jane_doe/grandchild_of_doe
    
  • I make an empty file in john_doe from inside doe

    touch john_doe/child_of_doe
    
  • I make an empty file in lil_john_doe from inside doe

    touch john_doe/lil_john_doe/grandchild_of_doe
    
  • I show the doe family tree

    tree
    

    the terminal shows

    .
    ├── empty_file_in_doe
    ├── jane_doe
       ├── child_of_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           ├── grandchild_of_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── child_of_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            ├── grandchild_of_doe
            └── empty_file_in_lil_john_doe
    
    5 directories, 9 files
    

  • I change directory to jane_doe

    cd jane_doe
    
  • I make an empty file in mary_jane_doe from inside jane_doe

    touch mary_jane_doe/child_of_jane_doe
    
  • I make an empty file in doe from inside jane_doe

    touch ../parent_of_jane_doe
    
  • I make an empty file in john_doe from inside jane_doe

    touch ../john_doe/sibling_of_jane_doe
    
    • .. is the parent of jane_doe which is doe

    • john_doe is a child of doe

  • I make an empty file in lil_john_doe from inside jane_doe

    touch ../john_doe/lil_john_doe/child_of_sibling_of_john_doe
    
    • .. is the parent of jane_doe which is doe

    • john_doe is a child of doe

    • lil_john_doe is a child of john_doe

  • I go back to the parent of jane_doe

    cd ..
    
  • I show the doe family tree

    tree
    

    the terminal shows

    .
    ├── parent_of_jane_doe
    ├── empty_file_in_doe
    ├── jane_doe
       ├── child_of_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           ├── grandchild_of_doe
           ├── child_of_jane_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── child_of_doe
        ├── sibling_of_jane_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            ├── grandchild_of_doe
            ├── child_of_sibling_of_john_doe
            └── empty_file_in_lil_john_doe
    
    5 directories, 13 files
    

  • I change directory to john_doe

    cd john_doe
    
  • I make an empty file in lil_john_doe from inside john_doe

    touch lil_john_doe/child_of_john_doe
    
  • I make an empty file in doe from inside john_doe

    touch ../parent_of_john_doe
    
  • I make an empty file in jane_doe from inside john_doe

    touch ../jane_doe/sibling_of_john_doe
    
    • .. is the parent of john_doe which is doe

    • jane_doe is a child of doe

  • I make an empty file in mary_jane_doe from inside john_doe

    touch ../jane_doe/mary_jane_doe/child_of_sibling_of_jane_doe
    
    • .. is the parent of john_doe which is doe

    • jane_doe is a child of doe

    • mary_jane_doe is a child of jane_doe

  • I go back to the parent of john_doe

    cd ..
    
  • I show the doe family tree

    tree
    

    the terminal shows

    .
    ├── parent_of_jane_doe
    ├── parent_of_john_doe
    ├── empty_file_in_doe
    ├── jane_doe
       ├── child_of_doe
       ├── sibling_of_john_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           ├── grandchild_of_doe
           ├── child_of_jane_doe
           ├── child_of_sibling_of_jane_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── child_of_doe
        ├── sibling_of_jane_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            ├── grandchild_of_doe
            ├── child_of_john_doe
            ├── child_of_sibling_of_john_doe
            └── empty_file_in_lil_john_doe
    
    5 directories, 17 files
    

wait a minute!

  • How is lil_john_doe a child of john_doe and a child of john_doe's sibling?

  • How is mary_jane_doe a child of jane_doe and a child of jane_doe's sibling?

I made mistakes

I know how to use touch with directory relationships


how to rename a file or directory

  • I go to mary_jane_doe

    cd jane_doe/mary_jane_doe
    
  • I use the mv program to change child_of_sibling_of_jane_doe to child_of_sibling_of_john_doe

    mv child_of_sibling_of_jane_doe child_of_sibling_of_john_doe
    
  • I go to lil_john_doe

    cd ../../john_doe/lil_john_doe
    
    • .. is the parent of mary_jane_doe which is jane_doe

    • ../.. is the parent of the parent of mary_jane_doe which is doe

    • john_doe is a child of doe

    • lil_john_doe is a child of john_doe

  • I use the mv program to change child_of_sibling_of_john_doe to child_of_sibling_of_jane_doe

    mv child_of_sibling_of_john_doe child_of_sibling_of_jane_doe
    

    the terminal goes back to the command line

  • I go back to doe

    cd ../..
    
  • I show the family tree

    tree
    

    the terminal shows

    .
    ├── parent_of_jane_doe
    ├── parent_of_john_doe
    ├── empty_file_in_doe
    ├── jane_doe
       ├── child_of_doe
       ├── sibling_of_john_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           ├── grandchild_of_doe
           ├── child_of_jane_doe
           ├── child_of_sibling_of_john_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── child_of_doe
        ├── sibling_of_jane_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            ├── grandchild_of_doe
            ├── child_of_sibling_of_jane_doe
            ├── child_of_john_doe
            └── empty_file_in_lil_john_doe
    
    5 directories, 17 files
    

    better.

mv means move, it takes two arguments

mv source target
  • source is the path/address I want to move the original file or folder from

  • target is the path/address I want to move the original file or folder to

  • this allows me to rename a file or folder in one step, the other way would be to

I know how to rename a file


  • I change directory to mary_jane_doe from doe

    cd jane_doe/mary_jane_doe
    
  • I make an empty file in jane_doe from inside mary_jane_doe

    touch ../parent_of_mary_jane_doe
    
  • I make an empty file in doe from inside mary_jane_doe

    touch ../../grandparent_of_mary_jane_doe
    
  • I make an empty file in john_doe from inside mary_jane_doe

    touch ../../john_doe/aunt_of_mary_jane_doe
    

    I made a mistake - john_doe is not the aunt of mary_jane_doe he is the uncle

  • I make an empty file in lil_john_doe from inside mary_jane_doe

    touch ../../john_doe/lil_john_doe/cousin_of_mary_jane_doe
    
  • I look at the doe family tree from inside mary_jane_doe

    tree ../..
    

    the terminal shows

    ../..
    ├── grandparent_of_mary_jane_doe
    ├── parent_of_jane_doe
    ├── parent_of_john_doe
    ├── empty_file_in_doe
    ├── jane_doe
       ├── child_of_doe
       ├── parent_of_mary_jane_doe
       ├── sibling_of_john_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           ├── grandchild_of_doe
           ├── child_of_jane_doe
           ├── child_of_sibling_of_john_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── child_of_doe
        ├── aunt_of_mary_jane_doe
        ├── sibling_of_jane_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            ├── grandchild_of_doe
            ├── child_of_sibling_of_jane_doe
            ├── child_of_john_doe
            ├── cousin_of_mary_jane_doe
            └── empty_file_in_lil_john_doe
    
    5 directories, 21 files
    

  • I change directory to lil_john_doe from mary_jane_doe

    cd ../../john_doe/lil_john_doe
    

    the terminal shows

    .../doe/john_doe/lil_john_doe
    
  • I make an empty file in john_doe from inside lil_john_doe

    touch ../parent_of_lil_john_doe
    
  • I make an empty file in doe from inside lil_john_doe

    touch ../../grandparent_of_lil_john_doe
    
  • I make an empty file in jane_doe from inside lil_john_doe

    touch ../../jane_doe/uncle_of_lil_john_doe
    

    I made another mistake - jane_doe is not the uncle of mary_jane_doe she is the aunt

  • I make an empty file in mary_jane_doe from inside lil_john_doe

    touch ../../jane_doe/mary_jane_doe/cousin_of_lil_john_doe
    
  • I look at the doe family tree from inside lil_john_doe through the parent of doe

    tree ../../../doe
    
    • .. is for the parent of lil_john_doe which is john_doe

    • ../.. is for the parent of the parent of lil_john_doe which is doe

    • ../../.. is for the parent of the parent of the parent of lil_john_doe which is pumping_python

    • doe is a child of pumping_python

    the terminal shows

    ../../../doe
    ├── grandparent_of_mary_jane_doe
    ├── grandparent_of_lil_john_doe
    ├── parent_of_jane_doe
    ├── parent_of_john_doe
    ├── empty_file_in_doe
    ├── jane_doe
       ├── child_of_doe
       ├── parent_of_mary_jane_doe
       ├── sibling_of_john_doe
       ├── uncle_of_lil_john_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           ├── grandchild_of_doe
           ├── child_of_jane_doe
           ├── child_of_sibling_of_john_doe
           ├── cousin_of_lil_john_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── child_of_doe
        ├── aunt_of_mary_jane_doe
        ├── parent_of_lil_john_doe
        ├── sibling_of_jane_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            ├── grandchild_of_doe
            ├── child_of_sibling_of_jane_doe
            ├── child_of_john_doe
            ├── cousin_of_mary_jane_doe
            └── empty_file_in_lil_john_doe
    
    5 directories, 25 files
    

I can add a file to any folder when I know its path or relation to where I am, if I have permission to write to the folder. It is easier to go where I want if I know where I am.


Time to fix my mistakes

  • I change uncle_of_lil_john_doe to aunt_of_lil_john_doe

    mv ../../jane_doe/uncle_of_lil_john_doe ../../jane_doe/aunt_of_lil_john_doe
    

    why does that work?

  • I change aunt_of_mary_jane_doe to uncle_of_mary_jane_doe

    mv ../aunt_of_mary_jane_doe ../uncle_of_mary_jane_doe
    

    the terminal goes back to the command line

  • I use tree to show what is in doe

    tree ../..
    

    the terminal shows

    ../..
    ├── grandparent_of_mary_jane_doe
    ├── grandparent_of_lil_john_doe
    ├── parent_of_jane_doe
    ├── parent_of_john_doe
    ├── empty_file_in_doe
    ├── jane_doe
       ├── child_of_doe
       ├── aunt_of_lil_john_doe
       ├── parent_of_mary_jane_doe
       ├── sibling_of_john_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           ├── grandchild_of_doe
           ├── child_of_jane_doe
           ├── child_of_sibling_of_john_doe
           ├── cousin_of_lil_john_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── child_of_doe
        ├── parent_of_lil_john_doe
        ├── sibling_of_jane_doe
        ├── uncle_of_mary_jane_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            ├── grandchild_of_doe
            ├── child_of_sibling_of_jane_doe
            ├── child_of_john_doe
            ├── cousin_of_mary_jane_doe
            └── empty_file_in_lil_john_doe
    
    5 directories, 25 files
    

    all is well


how to use ls with directory relationships

I can see what is in any folder when I know its path or relation to where I am. It is easier to get to where I want to go if I know where I am

  • I go to the parent of doe

    cd ../../..
    

    the terminal shows

    .../pumping_python
    

    I am in pumping_python

  • I show what is in doe

    ls -a doe
    

    the terminal shows

    .                        parent_of_jane_doe
    ..                       parent_of_john_doe
    .hidden_file_in_doe    empty_file_in_doe
    .hidden_folder_in_doe  jane_doe
    grandparent_of_mary_jane_doe  john_doe
    grandparent_of_lil_john_doe
    
  • I show what is in jane_doe

    ls -a doe/jane_doe
    

    the terminal shows

    .                         aunt_of_lil_john_doe
    ..                        parent_of_mary_jane_doe
    child_of_doe            sibling_of_john_doe
    .hidden_file_in_jane_doe    empty_file_in_jane_doe
    .hidden_folder_in_jane_doe  mary_jane_doe
    
  • I show what is in mary_jane_doe

    ls -a doe/jane_doe/mary_jane_doe
    

    the terminal shows

    .                         child_of_jane_doe
    ..                        child_of_sibling_of_john_doe
    grandchild_of_doe       cousin_of_lil_john_doe
    .hidden_file_in_mary_jane_doe    empty_file_in_mary_jane_doe
    .hidden_folder_in_mary_jane_doe
    
  • I change directory to mary_jane_doe

    cd doe/jane_doe/mary_jane_doe
    
  • I show what is in john_doe from mary_jane_doe

    ls -a ../../john_doe
    

    the terminal shows

    .                         parent_of_lil_john_doe
    ..                        sibling_of_jane_doe
    child_of_doe            uncle_of_mary_jane_doe
    .hidden_file_in_john_doe    empty_file_in_john_doe
    .hidden_folder_in_john_doe  lil_john_doe
    
  • I show what is in lil_john_doe from mary_jane_doe

    ls -a ../../john_doe/lil_john_doe
    

    the terminal shows

    .                        child_of_sibling_of_jane_doe
    ..                       child_of_john_doe
    grandchild_of_doe      cousin_of_mary_jane_doe
    .hidden_file_in_lil_john_doe    empty_file_in_lil_john_doe
    .hidden_folder_in_lil_john_doe
    
  • I use tree to show what is in lil_john_doe

    tree ../../john_doe/lil_john_doe
    

    the terminal shows

    ../../john_doe/lil_john_doe
    ├── grandchild_of_doe
    ├── child_of_sibling_of_jane_doe
    ├── child_of_john_doe
    ├── cousin_of_mary_jane_doe
    └── empty_file_in_lil_john_doe
    
    1 directory, 5 files
    
  • I use tree to show what is in john_doe

    tree ../../john_doe
    

    the terminal shows

    ../../john_doe
    ├── child_of_doe
    ├── parent_of_lil_john_doe
    ├── sibling_of_jane_doe
    ├── uncle_of_mary_jane_doe
    ├── empty_file_in_john_doe
    └── lil_john_doe
        ├── grandchild_of_doe
        ├── child_of_sibling_of_jane_doe
        ├── child_of_john_doe
        ├── cousin_of_mary_jane_doe
        └── empty_file_in_lil_john_doe
    
    2 directories, 10 files
    
  • I change directories from mary_jane_doe to lil_john_doe

    cd ../../john_doe/lil_john_doe
    

    the terminal shows

    .../doe/john_doe/lil_john_doe
    

    I am in lil_john_doe

  • I look at what is in mary_jane_doe from inside lil_john_doe

    ls -a ../../jane_doe/mary_jane_doe
    

    the terminal shows

    .                         child_of_jane_doe
    ..                        child_of_sibling_of_john_doe
    grandchild_of_doe       cousin_of_lil_john_doe
    .hidden_file_in_mary_jane_doe    empty_file_in_mary_jane_doe
    .hidden_folder_in_mary_jane_doe
    
  • I use tree to show what is in mary_jane_doe

    tree ../../jane_doe/mary_jane_doe
    

    the terminal shows

    ../../jane_doe/mary_jane_doe
    ├── grandchild_of_doe
    ├── child_of_jane_doe
    ├── child_of_sibling_of_john_doe
    ├── cousin_of_lil_john_doe
    └── empty_file_in_mary_jane_doe
    
    1 directory, 5 files
    
  • I use tree to show what is in jane_doe

    tree ../../jane_doe
    

    the terminal shows

    ../../jane_doe
    ├── child_of_doe
    ├── aunt_of_lil_john_doe
    ├── parent_of_mary_jane_doe
    ├── sibling_of_john_doe
    ├── empty_file_in_jane_doe
    └── mary_jane_doe
        ├── grandchild_of_doe
        ├── child_of_jane_doe
        ├── child_of_sibling_of_john_doe
        ├── cousin_of_lil_john_doe
        └── empty_file_in_mary_jane_doe
    
    2 directories, 10 files
    
  • I go to the parent of doe

    cd ../../..
    

    the terminal shows

    .../pumping_python
    
  • I show the doe family tree and all its hidden secrets

    tree -a doe
    

    the terminal shows

    doe
    ├── .hidden_file_in_doe
    ├── .hidden_folder_in_doe
    ├── grandparent_of_mary_jane_doe
    ├── grandparent_of_lil_john_doe
    ├── parent_of_jane_doe
    ├── parent_of_john_doe
    ├── empty_file_in_doe
    ├── jane_doe
       ├── child_of_doe
       ├── .hidden_file_in_jane_doe
       ├── .hidden_folder_in_jane_doe
       ├── aunt_of_lil_john_doe
       ├── parent_of_mary_jane_doe
       ├── sibling_of_john_doe
       ├── empty_file_in_jane_doe
       └── mary_jane_doe
           ├── grandchild_of_doe
           ├── .hidden_file_in_mary_jane_doe
           ├── .hidden_folder_in_mary_jane_doe
           ├── child_of_jane_doe
           ├── child_of_sibling_of_john_doe
           ├── cousin_of_lil_john_doe
           └── empty_file_in_mary_jane_doe
    └── john_doe
        ├── child_of_doe
        ├── .hidden_file_in_john_doe
        ├── .hidden_folder_in_john_doe
        ├── parent_of_lil_john_doe
        ├── sibling_of_jane_doe
        ├── uncle_of_mary_jane_doe
        ├── empty_file_in_john_doe
        └── lil_john_doe
            ├── grandchild_of_doe
            ├── .hidden_file_in_lil_john_doe
            ├── .hidden_folder_in_lil_john_doe
            ├── child_of_sibling_of_jane_doe
            ├── child_of_john_doe
            ├── cousin_of_mary_jane_doe
            └── empty_file_in_lil_john_doe
    
    10 directories, 30 files
    

I can do things with files and folders in 1 step as long as

It is easier to go where I want if I know where I am. I know how to use directory relationships


how to remove a directory and all its contents

  • I try to remove doe and all its children and their children

    rm doe
    

    the terminal shows

    rm: cannot remove 'doe': Is a directory
    

    I cannot remove a directory this way

  • I remove doe and all its children and their children with the -r/--recursive option

    Danger

    This is a destructive operation takes a lot of effort and time to undo on MacOS or Linux/Windows Subsystem for Linux. Do you want to do it?

    rm --recursive doe
    

    the terminal goes back to the command line

    Note

    on Windows without Windows Subsystem for Linux use Remove-Item -Recurse -Force instead of rm --recursive

    Remove-Item -Path doe -Recurse -Force
    
    • rm is used to remove files and folders

    • rm means remove

    • -r/--recursive/-Recurse means remove child directories and what is in them until there is nothing left. It goes through each child directory and removes everything including their children

    • -Force means do not ask me any questions, just remove the file or folder

  • I try to go back to doe

    cd doe
    

    the terminal shows

    cd: no such file or directory: doe
    

    the doe family is gone!

review

I ran these commands to play with folders (directories)

How many questions do you think you can answer after going through this chapter?


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

You know

Homework: use what you have learned - mkdir, cd, ls, tree and touch to make your family tree. Send it to me when you are done.

Click Here to see me make a Python Test Driven Development Environment


rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too