BONUS: learn directory structure

This is an exercise in how your computer is organized into directories (folders) and files

preview

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

parent
├── a_file_in_parent
├── aka_grandparent_of_child_of_child
├── aka_grandparent_of_child_of_sibling_of_child
├── child
   ├── a_file_in_child
   ├── aunt_or_uncle_of_another_grandchild_of_parent
   └── child_of_child
       ├── a_file_in_child_of_child
       ├── a_grandchild_of_parent
       └── cousin_of_child_of_sibling_of_child
└── sibling_of_child
    ├── a_file_in_sibling_of_child
    ├── aunt_or_uncle_of_a_grandchild_of_parent
    └── child_of_sibling_of_child
        ├── a_file_in_child_of_sibling_of_child
        ├── another_grandchild_of_parent
        └── cousin_of_child_of_child

You will become familiar with these commands

questions about directory structure

Here are questions you can answer after going through this chapter

what is a folder?

A folder (directory) is a container for files. It helps organize things, just like a folder in a file cabinet is used to put files that belong together in one place.

I keep every project I work on in its own folder (directory). All the code from this book is kept 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 usually end with an extension (optionally) to show the type of file. For example

requirements

I open a terminal to make sure the tree program is installed by typing this

tree

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

tree: command not found

when it is installed, the terminal shows a tree of directories and files. The tree program shows how files and folders on a computer are related.

how to install tree

how to install tree on Linux/Windows Subsystem for Linux

sudo apt update

optionally, you can do a full upgrade if you want

sudo apt full-upgrade --yes

type this in the terminal to install tree

sudo apt install tree

continue in how to work in directories

how to install tree on Windows without Windows Subsystem for Linux

tree comes with Windows you do not have to do anything. The following are things you would type in place of what I have in the chapter

The path shown when you call pwd or tree shows \ instead of /, for example

...\pumping_python

instead of

.../pumping_python

continue with how to work in directories

how to install tree on Mac OS

  • install brew (The Missing Package Manager for MacOS), if you do not have it already

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    

    the terminal shows instructions about how to add brew to your path

  • copy and paste the 3 lines it shows in the terminal then hit return to run it, the terminal will not show anything if the commands run successfully

  • use brew to install tree

    brew install tree
    
  • continue with how to work in directories

how to work in directories

how to see the directory I am in

I start by checking where I am in the terminal. 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 to the current folder I am in at the moment

Note

If you see the same name, skip to the part where I create parent. 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

the terminal shows

cd: pumping_python: No such file or directory

this means the folder does not exist where I am

how to make a directory

how to list 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/parent
    

    this directory is empty. ls has a few options, I try it again with one of them

    ls --all
    

    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 it instead

    ls -a
    

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

    dir /ah
    

    the terminal does not show . and ..

    the terminal shows

    .  ..
    
    • --all/-a tells ls to show things in the directory that start with ., these are hidden by default

    • . represents the current directory

  • I try to change directory to the .

    cd .
    

    the terminal shows

    .../pumping_python/parent
    
    • I am still in the same folder

    • . is used for the directory I am in, which is parent in this case

  • I try cd with .. to see what happens

    cd ..
    

    the terminal shows

    .../pumping_python
    
    • .. is used for the parent of a directory where I am

    • pumping_python is the parent of parent

  • I go back to parent

    cd parent
    

    the terminal shows

    .../pumping_python/parent
    

how to look at directory structure

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

    tree
    

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

    tree /F
    

    the terminal shows

    .
    
    0 directories, 0 files
    

    it is empty

  • I change directory

    cd child
    

    the terminal shows

    cd: no such file or directory: child
    

    the directory does not exist

  • I make the folder

    mkdir child
    

    the terminal goes back to the command line

  • I use ls to see what is now in parent

    ls -a
    

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

    dir /ah
    

    the terminal shows

    .  ..  child
    
  • I use tree to see the structure

    tree
    

    the terminal shows

    .
    └── child
    
    2 directories, 0 files
    
  • I try to go to a different directory

    cd sibling_of_child
    

    the terminal shows

    cd: no such file or directory: sibling_of_child
    

    the directory does not exist

  • I make a new folder

    mkdir sibling_of_child
    

    the terminal goes back to the command line

  • I use ls to see what is in parent now

    ls -a
    

    the terminal shows

    .  ..  child  sibling_of_child
    
  • I use tree to see the structure

    tree
    

    the terminal shows

    .
    ├── child
    └── sibling_of_child
    
    3 directories, 0 files
    
  • I change directory to one of the children of parent

    cd child
    

    the terminal shows

    …/pumping_python/parent/child
    
  • I list the contents of 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

    .  ..
    

    I use tree

    tree
    

    the terminal shows

    .
    
    0 directories, 0 files
    
  • I change directory

    cd child_of_child
    

    the terminal shows

    cd: child_of_child: No such file or directory
    

    I make the directory

    mkdir child_of_child
    

    the terminal goes back to the command line

  • I try to go to child_of_child again

    cd child_of_child
    

    the terminal shows

    .../parent/child/child_of_child
    
  • I go up a level to the parent of child_of_child

    cd ..
    

    the terminal shows

    .../parent/child
    

    I am back in child

  • I go up another level to the parent of child

    cd ..
    

    the terminal shows

    .../pumping_python/parent
    

    I am back in parent

  • I change directory to the other child of parent

    cd sibling_of_child
    

    the terminal shows

    …/pumping_python/parent/sibling_of_child
    
  • I list the contents of 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

    .  ..
    

    I use tree

    tree
    

    the terminal shows

    .
    
    0 directories, 0 files
    
  • I change directory to a child of this folder

    cd child_of_sibling_of_child
    

    the terminal shows

    cd: child_of_sibling_of_child: No such file or directory
    

    I make the directory

    mkdir child_of_sibling_of_child
    

    the terminal goes back to the command line

  • I try to go to child_of_sibling_of_child again

    cd child_of_sibling_of_child
    

    the terminal shows

    .../parent/sibling_of_child/child_of_sibling_of_child
    
  • I go up a level to the parent of child_of_sibling_of_child

    cd ..
    

    the terminal shows

    .../parent/sibling_of_child
    

    I am back in sibling_of_child

  • I go up another level to the parent of sibling_of_child

    cd ..
    

    the terminal shows

    .../pumping_python/parent
    

    I am back in parent

  • I show the directory structure

    tree
    

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

    tree /F
    

    the terminal shows

    .
    ├── child
       └── child_of_child
    └── sibling_of_child
        └── child_of_sibling_of_child
    
    5 directories, 0 files
    

how to make an empty file

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

  • I add an empty file to parent

    touch a_file_in_parent
    

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

    New-Item a_file_in_parent
    

    the terminal goes back to the command line

  • I use ls to see what is in the folder now

    ls -a
    

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

    dir /ah
    

    the terminal does not show . and ..

    the terminal shows

    .  ..  a_file_in_parent  child  sibling_of_child
    
  • I change directory to one of the children of parent

    cd child
    

    the terminal shows

    .../pumping_python/parent/child
    
  • I add an empty file with touch

    touch a_file_in_child
    

    the terminal goes back to the command line

  • I list the contents of the folder

    ls -a
    

    the terminal shows

    .  ..  a_file_in_child  child_of_child
    
  • I change directory to the parent of child

    cd ..
    

    the terminal shows

    .../pumping_python/parent
    
  • I change directory to the other child of parent

    cd sibling_of_child
    

    the terminal shows

    .../pumping_python/parent/sibling_of_child
    
  • I add an empty file with touch

    touch a_file_in_sibling_of_child
    

    the terminal goes back to the command line

  • I list the contents of the folder

    ls -a
    

    the terminal shows

    .  ..  a_file_in_sibling_of_child  child_of_sibling_of_child
    
  • I change directory to the parent of sibling_of_child

    cd ..
    

    the terminal shows

    .../pumping_python/parent
    
  • I use tree

    tree
    

    the terminal shows

    .
    ├── a_file_in_parent
    ├── child
       ├── a_file_in_child
       └── child_of_child
    └── sibling_of_child
        ├── a_file_in_sibling_of_child
        └── child_of_sibling_of_child
    
    5 directories, 3 files
    

    Tip

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

  • I want to make a file in child_of_child. I use change directory to go to its parent first

    cd child
    

    the terminal shows

    /pumping_python/parent/child
    

    I change directory to child_of_child

    cd child_of_child
    

    the terminal shows

    .../parent/child/child_of_child
    

how to use directory relationships

  • I can go from child_of_child to parent in 1 step by using ..

    cd ../..
    

    the terminal shows

    .../pumping_python/parent
    

    Since .. is for the parent of a directory, ../.. is for the parent of a parent, that is a grandparent. I can use as many ..’s I need for each parent, for example ../../../.. would be the great great grand parent

  • I try to go from parent to child_of_child in 1 step

    cd child_of_child
    

    the terminal shows

    cd: child_of_child: No such file or directory
    

    I cannot get to child_of_child without its parent

  • I try to go from parent to child_of_child in 1 step with its parent

    cd child/child_of_child
    

    the terminal shows

    .../parent/child/child_of_child
    
  • I make an empty file

    touch a_grandchild_of_parent
    

    the terminal goes back to the command line

  • I use ls to list the contents of the folder

    ls -a
    

    the terminal shows

    .  ..  a_grandchild_of_parent
    
  • I go back to parent

    cd ../..
    

    the terminal shows

    .../pumping_python/parent
    
  • I want to make a file in child_of_sibling_of_child. I use change directory to go to its parent first

    cd sibling_of_child
    

    the terminal shows

    /pumping_python/parent/sibling_of_child
    

    I change directory to child_of_sibling_of_child

    cd child_of_sibling_of_child
    

    the terminal shows

    .../parent/sibling_of_child/child_of_sibling_of_child
    
  • I go from child_of_sibling_of_child to parent in 1 step

    cd ../..
    

    the terminal shows

    .../pumping_python/parent
    
  • I try to go from parent to child_of_sibling_of_child in 1 step

    cd child_of_sibling_of_child
    

    the terminal shows

    cd: child_of_sibling_of_child: No such file or directory
    

    I can only go directly to folders that exist where I am or use the path to the folder I want to go to

  • I go from parent to child_of_sibling_of_child in 1 step with its parent

    cd sibling_of_child/child_of_sibling_of_child
    

    the terminal shows

    .../parent/sibling_of_child/child_of_sibling_of_child
    
  • I make an empty file

    touch another_grandchild_of_parent
    

    the terminal goes back to the command line

  • I use ls to list the contents of the folder

    ls -a
    

    the terminal shows

    .  ..  another_grandchild_of_parent
    
  • I go back to parent

    cd ../..
    

    the terminal shows

    .../pumping_python/parent
    
  • I add an empty file

    touch aka_grandparent_of_child_of_child
    

    the terminal goes back to the command line

  • I make another empty file

    touch aka_grandparent_of_child_of_sibling_of_child
    

    the terminal goes back to the command line

  • I use tree to see what parent looks like now

    tree
    

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

    tree /F
    

    the terminal shows

    .
    ├── a_file_in_parent
    ├── aka_grandparent_of_child_of_child
    ├── aka_grandparent_of_child_of_sibling_of_child
    ├── child
       ├── a_file_in_child
       └── child_of_child
           └── a_grandchild_of_parent
    └── sibling_of_child
        ├── a_file_in_sibling_of_child
        └── child_of_sibling_of_child
            └── another_grandchild_of_parent
    
    5 directories, 7 files
    
  • I can add an empty file in 1 step in any directory as long as

    • I know its path

    • I know its relation to where I am and

    • I have permission to write to the folder

    I add another empty file in child

    touch child/aunt_or_uncle_of_another_grandchild_of_parent
    

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

    New-Item child/aunt_or_uncle_of_another_grandchild_of_parent
    

    the terminal goes back to the command line

  • I add another empty file in sibling_of_child

    touch sibling_of_child/aunt_or_uncle_of_a_grandchild_of_parent
    

    the terminal goes back to the command line

  • I use tree

    tree
    

    the terminal shows

    .
    ├── a_file_in_parent
    ├── aka_grandparent_of_child_of_child
    ├── aka_grandparent_of_child_of_sibling_of_child
    ├── child
       ├── a_file_in_child
       ├── aunt_or_uncle_of_another_grandchild_of_parent
       └── child_of_child
           └── a_grandchild_of_parent
    └── sibling_of_child
        ├── a_file_in_sibling_of_child
        ├── aunt_or_uncle_of_a_grandchild_of_parent
        └── child_of_sibling_of_child
            └── another_grandchild_of_parent
    
    5 directories, 9 files
    
  • I add an empty file in child_of_child

    touch child/child_of_child/a_file_in_child_of_child
    

    the terminal goes back to the command line

  • I add an empty file in child_of_sibling_of_child

    touch sibling_of_child/child_of_sibling_of_child/a_file_in_child_of_sibling_of_child
    

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

    New-Item sibling_of_child/child_of_sibling_of_child/a_file_in_child_of_sibling_of_child
    

    the terminal goes back to the command line

  • I change directory to the parent of parent

    cd ..
    

    the terminal shows

    .../pumping_python
    
  • I use tree to show what is in parent

    tree parent
    

    the terminal shows

    parent
    ├── a_file_in_parent
    ├── aka_grandparent_of_child_of_child
    ├── aka_grandparent_of_child_of_sibling_of_child
    ├── child
       ├── a_file_in_child
       ├── aunt_or_uncle_of_another_grandchild_of_parent
       └── child_of_child
           ├── a_file_in_child_of_child
           └── a_grandchild_of_parent
    └── sibling_of_child
        ├── a_file_in_sibling_of_child
        ├── aunt_or_uncle_of_a_grandchild_of_parent
        └── child_of_sibling_of_child
            ├── a_file_in_child_of_sibling_of_child
            └── another_grandchild_of_parent
    
    5 directories, 11 files
    
  • I type pwd to show where I am

    pwd
    

    the terminal shows

    .../pumping_python
    
  • I can list the contents of any folder once I know its path or relation to where I am

    ls -a parent
    

    the terminal shows

    .                 aka_grandparent_of_child_of_child             sibling_of_child
    ..                aka_grandparent_of_child_of_sibling_of_child
    a_file_in_parent  child
    
  • I list the contents of child

    ls -a parent/child
    

    the terminal shows

    .   a_file_in_child                                child_of_child
    ..  aunt_or_uncle_of_another_grandchild_of_parent
    
  • I list the contents of child_of_child

    ls -a parent/child/child_of_child
    

    the terminal shows

    .  ..  a_file_in_child_of_child  a_grandchild_of_parent
    
  • I list the contents of sibling_of_child

    ls -a parent/sibling_of_child
    

    the terminal shows

    .   a_file_in_sibling_of_child               child_of_sibling_of_child
    ..  aunt_or_uncle_of_a_grandchild_of_parent
    
  • I list the contents of child_of_sibling_of_child

    ls -a parent/sibling_of_child/child_of_sibling_of_child
    

    the terminal shows

    .  ..  a_file_in_child_of_sibling_of_child  another_grandchild_of_parent
    
  • I change directory to child_of_child

    cd parent/child/child_of_child
    
  • I want to list the contents of child_of_sibling_of_child from inside child_of_child in 1 step. ../.. is parent and I can go from parent to child_of_sibling_of_child, I use the relationship with ls

    ls -a ../../sibling_of_child/child_of_sibling_of_child
    

    the terminal shows

    .  ..  a_file_in_child_of_sibling_of_child  another_grandchild_of_parent
    
  • I add an empty file to child_of_sibling_of_child from child_of_child

    touch ../../sibling_of_child/child_of_sibling_of_child/cousin_of_child_of_child
    

    the terminal goes back to the command line

  • I use tree this time

    tree ../../sibling_of_child/child_of_sibling_of_child
    

    the terminal shows

    ../../sibling_of_child/child_of_sibling_of_child
    ├── a_file_in_child_of_sibling_of_child
    ├── another_grandchild_of_parent
    └── cousin_of_child_of_child
    
    1 directory, 3 files
    
  • I can use the same thing to change directory to child_of_sibling_of_child from child_of_child

    cd ../../sibling_of_child/child_of_sibling_of_child
    

    the terminal shows

    .../parent/sibling_of_child/child_of_sibling_of_child $
    
  • I want to list the contents of child_of_child from inside child_of_sibling_of_child in 1 step. Since ../.. is parent and I can go from parent to child_of_child, I use the relationship with ls

    ls -a ../../child/child_of_child
    

    the terminal shows

    .  ..  a_file_in_child_of_child  a_grandchild_of_parent
    
  • I add an empty file to child_of_child from child_of_sibling_of_child

    touch ../../child/child_of_child/cousin_of_child_of_sibling_of_child
    

    the terminal goes back to the command line

  • I use tree to show what is in child_of_child now

    tree ../../child/child_of_child
    

    the terminal shows

    ../../child/child_of_child
    ├── a_file_in_child_of_child
    ├── a_grandchild_of_parent
    └── cousin_of_child_of_sibling_of_child
    
    1 directory, 3 files
    
  • I look at the structure of parent again, this time from inside child_of_sibling_of_child

    tree ../../../parent
    

    the terminal shows

    ../../../parent
    ├── a_file_in_parent
    ├── aka_grandparent_of_child_of_child
    ├── aka_grandparent_of_child_of_sibling_of_child
    ├── child
       ├── a_file_in_child
       ├── aunt_or_uncle_of_another_grandchild_of_parent
       └── child_of_child
           ├── a_file_in_child_of_child
           ├── a_grandchild_of_parent
           └── cousin_of_child_of_sibling_of_child
    └── sibling_of_child
        ├── a_file_in_sibling_of_child
        ├── aunt_or_uncle_of_a_grandchild_of_parent
        └── child_of_sibling_of_child
            ├── a_file_in_child_of_sibling_of_child
            ├── another_grandchild_of_parent
            └── cousin_of_child_of_child
    
    5 directories, 13 files
    
  • I change directory to the parent of parent

    cd ../../..
    

    the terminal shows

    .../pumping_python
    

    I show the current working directory (where I am)

    pwd
    

    the terminal shows

    .../
    

how to remove a directory and all its contents

  • I remove parent and all its descendants

    Danger

    This is a desctructive operation that CANNOT be undone on MacOS or Linux/Windows Subsystem for Linux, use it wisely

    rm -rf parent
    

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

    Remove-Item -Path parent -Recurse -Force
    

    the terminal goes back to the command line

    • rm is used to remove files and folders

    • -r/-Recurse means remove child directories and what is in them recursively, it goes through each child directory and removes everything include their children

    • -f/-Force means “force”, do not ask me any questions, just remove the file or folder and all its children with extreme prejudice

  • I try to go back to the parent directory

    cd parent
    

    the terminal shows

    cd: no such file or directory: parent
    

review

I ran the following commands to play with folder (directory) structure

How many questions can you 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?

Would you like to see me make a Test Driven Development Environment


please leave a review