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.

doe
├── a_file_in_doe
├── aka_grandparent_of_baby
├── aka_grandparent_of_lil
├── aka_parent_of_jane
├── aka_parent_of_john
├── jane
   ├── a_child_of_doe
   ├── a_file_in_jane
   ├── aka_aunt_of_lil
   ├── aka_sibling_of_john
   └── baby
       ├── a_file_in_baby
       ├── a_grandchild_of_doe
       ├── aka_child_of_jane
       └── aka_cousin_of_lil
└── john
    ├── a_file_in_john
    ├── aka_sibling_of_jane
    ├── aka_uncle_of_baby
    ├── another_child_of_doe
    └── lil
        ├── a_file_in_lil
        ├── aka_child_of_john
        ├── aka_cousin_of_baby
        └── another_grandchild_of_doe

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

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

the terminal shows

cd: no such file or directory: pumping_python

this means the folder does not exist where I am


how to make a directory


I know how to make a directory


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

    Note

    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/doe
    
    • I am still in the same folder

    • . is used for the directory I am in, which is doe 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 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 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 doe directory

    tree
    

    Note

    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 jane
    

    the terminal shows

    cd: no such file or directory: jane
    

    the directory does not exist

  • I make the folder

    mkdir jane
    

    the terminal goes back to the command line

  • I use ls to see what is now in doe

    ls -a
    

    Note

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

    dir /ah
    

    the terminal shows

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

    tree
    

    the terminal shows

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

    cd john
    

    the terminal shows

    cd: no such file or directory: john
    

    the directory does not exist

  • I make a new folder

    mkdir john
    

    the terminal goes back to the command line

  • I use ls to see what is in doe now

    ls -a
    

    the terminal shows

    .  ..  jane  john
    
  • I use tree to see the structure

    tree
    

    the terminal shows

    .
    ├── jane
    └── john
    
    3 directories, 0 files
    
  • I change directory to one of the children of doe

    cd jane
    

    the terminal shows

    …/pumping_python/doe/jane
    
  • 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

    .  ..
    
  • I use tree

    tree
    

    the terminal shows

    .
    
    0 directories, 0 files
    
  • I change directory

    cd baby
    

    the terminal shows

    cd: no such file or directory: baby
    

    I make the directory

    mkdir baby
    

    the terminal goes back to the command line

  • I try to go to baby again

    cd baby
    

    the terminal shows

    .../doe/jane/baby
    

    I am in the baby folder which is in the jane folder which is in the doe folder

  • I go up a level to the parent of baby

    cd ..
    

    the terminal shows

    .../doe/jane
    

    I am back in jane

  • I go up another level to the parent of jane

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    

    I am back in doe

  • I change directory to the other child of doe

    cd john
    

    the terminal shows

    …/pumping_python/doe/john
    
  • 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

    .  ..
    
  • I use tree

    tree
    

    the terminal shows

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

    cd lil
    

    the terminal shows

    cd: no such file or directory: lil
    

    I make the directory

    mkdir lil
    

    the terminal goes back to the command line

  • I try to go to lil again

    cd lil
    

    the terminal shows

    .../doe/john/lil
    

    I am in the lil folder

  • I go up a level to the parent of lil

    cd ..
    

    the terminal shows

    .../doe/john
    

    I am back in john

  • I go up another level to the parent of john

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    

    I am back in doe

  • I show the directory structure

    tree
    

    Note

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

    tree /F
    

    the terminal shows

    .
    ├── jane
       └── baby
    └── john
        └── lil
    
    5 directories, 0 files
    

I know how to look at directory structure


how to make an empty file


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

  • I add an empty file to doe

    touch a_file_in_doe
    

    Note

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

    New-Item a_file_in_doe
    

    the terminal goes back to the command line

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

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

    the terminal shows

    .  ..  a_file_in_doe  jane  john
    
  • I change directory to one of the children of doe

    cd jane
    

    the terminal shows

    .../pumping_python/doe/jane
    
  • I add an empty file with touch

    touch a_file_in_jane
    

    the terminal goes back to the command line

  • I show what is in the folder

    ls -a
    

    the terminal shows

    .  ..  a_file_in_jane  baby
    
  • I change directory to the parent of jane

    cd ..
    

    the terminal shows

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

    cd john
    

    the terminal shows

    .../pumping_python/doe/john
    
  • I add an empty file with touch

    touch a_file_in_john
    

    the terminal goes back to the command line

  • I show what is in the folder

    ls -a
    

    the terminal shows

    .  ..  a_file_in_john  lil
    
  • I change directory to the parent of john

    cd ..
    

    the terminal shows

    .../pumping_python/doe
    

    I am back in doe

  • I use tree

    tree
    

    the terminal shows

    .
    ├── a_file_in_doe
    ├── jane
       ├── a_file_in_jane
       └── baby
    └── john
        ├── a_file_in_john
        └── lil
    
    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 baby. I use change directory to go to its parent first

    cd jane
    

    the terminal shows

    /pumping_python/doe/jane
    

    I change directory to baby

    cd baby
    

    the terminal shows

    .../doe/jane/baby
    
  • I make an empty file

    touch a_file_in_baby
    

    the terminal goes back to the command line

  • I use ls to show what is in the folder

    ls -a
    

    the terminal shows

    .  ..  a_file_in_baby
    
  • I go back to the parent of baby

    cd ..
    

    the terminal shows

    .../doe/jane/
    
  • I go back to the parent of jane

    cd ..
    

    the terminal shows

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

    cd john
    

    the terminal shows

    /pumping_python/doe/john
    

    I change directory to lil

    cd lil
    

    the terminal shows

    .../doe/john/lil
    
  • I make an empty file

    touch a_file_in_lil
    

    the terminal goes back to the command line

  • I use ls to show what is in the folder

    ls -a
    

    the terminal shows

    .  ..  a_file_in_lil
    
  • I go back to the parent of lil

    cd ..
    

    the terminal shows

    .../doe/john/
    
  • I go back to the parent of john

    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

    .
    ├── a_file_in_doe
    ├── jane
       ├── a_file_in_jane
       └── baby
           └── a_file_in_baby
    └── john
        ├── a_file_in_john
        └── lil
            └── a_file_in_lil
    
    5 directories, 5 files
    

I know how to add empty files to folders


how to use directory relationships


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

    cd baby
    

    the terminal shows

    cd: no such file or directory: baby
    

    I cannot get to baby without its parent

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

    cd jane/baby
    

    the terminal shows

    .../doe/jane/baby
    
  • I can go from baby to doe in 1 step with ..

    cd ../..
    

    the terminal shows

    .../pumping_python/doe
    

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

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

    cd lil
    

    the terminal shows

    cd: no such file or directory: lil
    

    I cannot get to lil without its parent

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

    cd john/lil
    

    the terminal shows

    .../doe/john/lil
    
  • I go back to doe in 1 step with ..

    cd ../..
    

    the terminal shows

    .../pumping_python/doe
    

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

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

    cd jane/baby
    

    the terminal shows

    .../doe/jane/baby
    
  • I make another empty file

    touch aka_child_of_jane
    

    the terminal goes back to the command line

  • I use ls to show what is in the folder

    ls -a
    

    the terminal shows

    .  ..  a_file_in_baby  aka_child_of_jane
    
  • I go back to doe

    cd ../..
    

    the terminal shows

    .../pumping_python/doe
    
  • I go from doe to lil in 1 step with its parent

    cd john/lil
    

    the terminal shows

    .../doe/john/lil
    
  • I make an empty file

    touch aka_child_of_john
    

    the terminal goes back to the command line

  • I use ls to show what is in the folder

    ls -a
    

    the terminal shows

    .  ..  a_file_in_lil  aka_child_of_john
    
  • I go back to doe

    cd ../..
    

    the terminal shows

    .../pumping_python/doe
    
  • I add 2 empty files

    touch aka_parent_of_jane aka_parent_of_john
    

    the terminal goes back to the command line

  • I use tree to see what doe looks like now

    tree
    

    Note

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

    tree /F
    

    the terminal shows

    .
    ├── a_file_in_doe
    ├── aka_parent_of_jane
    ├── aka_parent_of_john
    ├── jane
       ├── a_file_in_jane
       └── baby
           ├── a_file_in_baby
           └── aka_child_of_jane
    └── john
        ├── a_file_in_john
        └── lil
            ├── a_file_in_lil
            └── aka_child_of_john
    
    5 directories, 9 files
    
  • I can add empty files in 1 step in any directory as long as

    I add 2 empty files in jane

    touch jane/a_child_of_doe jane/aka_sibling_of_john
    

    Note

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

    New-Item jane/a_child_of_doe jane/aka_sibling_of_john
    

    the terminal goes back to the command line

  • I add 2 empty files in john

    touch john/another_child_of_doe john/aka_sibling_of_jane
    

    the terminal goes back to the command line

  • I add an empty file in baby

    touch jane/baby/a_grandchild_of_doe
    

    the terminal goes back to the command line

  • I add an empty file in lil

    touch john/lil/another_grandchild_of_doe
    

    the terminal goes back to the command line

  • I change directory to the parent of doe

    cd ..
    

    the terminal shows

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

    tree doe
    

    the terminal shows

    doe
    ├── a_file_in_doe
    ├── aka_parent_of_jane
    ├── aka_parent_of_john
    ├── jane
       ├── a_child_of_doe
       ├── a_file_in_jane
       ├── aka_sibling_of_john
       └── baby
           ├── a_file_in_baby
           ├── a_grandchild_of_doe
           └── aka_child_of_jane
    └── john
        ├── a_file_in_john
        ├── aka_sibling_of_jane
        ├── another_child_of_doe
        └── lil
            ├── a_file_in_lil
            ├── aka_child_of_john
            └── another_grandchild_of_doe
    
    5 directories, 15 files
    
  • I type pwd to show where I am

    pwd
    

    the terminal shows

    .../pumping_python
    

    I am in the pumping_python folder

  • I can see what is in any folder when I know its path or relation to where I am

    ls -a doe
    

    the terminal shows

    .  ..  a_file_in_doe  aka_parent_of_jane  aka_parent_of_john  jane  john
    
  • I show what is in jane

    ls -a doe/jane
    

    the terminal shows

    .  ..  a_child_of_doe  a_file_in_jane  aka_sibling_of_john  baby
    
  • I show what is in baby

    ls -a doe/jane/baby
    

    the terminal shows

    .  ..  a_file_in_baby  a_grandchild_of_doe  aka_child_of_jane
    
  • I show what is in john

    ls -a doe/john
    

    the terminal shows

    .  ..  a_file_in_john  aka_sibling_of_jane  another_child_of_doe  lil
    
  • I show what is in lil

    ls -a doe/john/lil
    

    the terminal shows

    .  ..  a_file_in_lil  aka_child_of_john  another_grandchild_of_doe
    
  • I change directory to baby

    cd doe/jane/baby
    
  • I want to see what is in lil from inside baby in 1 step. ../.. is doe and I can go from doe to lil, I use this relationship with ls

    ls -a ../../john/lil
    

    the terminal shows

    .  ..  a_file_in_lil  aka_child_of_john  another_grandchild_of_doe
    
  • I add an empty file to lil from baby

    touch ../../john/lil/aka_cousin_of_baby
    

    the terminal goes back to the command line

  • I use tree to show the structure of lil

    tree ../../john/lil
    

    the terminal shows

    ../../john/lil
    ├── a_file_in_lil
    ├── aka_child_of_john
    ├── aka_cousin_of_baby
    └── another_grandchild_of_doe
    
    1 directory, 4 files
    
  • I add 2 empty files from inside baby, one to doe and another to john

    touch ../../aka_grandparent_of_baby ../../john/aka_uncle_of_baby
    

    the terminal goes back to the command line

  • I can use the relationship to change directory to lil from baby

    cd ../../john/lil
    

    the terminal shows

    .../doe/john/lil
    
  • I want to see what is in baby from inside lil in 1 step. I use their relationship with ls

    ls -a ../../jane/baby
    

    the terminal shows

    .  ..  a_file_in_baby  a_grandchild_of_doe  aka_child_of_jane
    
  • I add an empty file to baby from lil

    touch ../../jane/baby/aka_cousin_of_lil
    

    the terminal goes back to the command line

  • I use tree to show what is in baby now

    tree ../../jane/baby
    

    the terminal shows

    ../../jane/baby
    ├── a_file_in_baby
    ├── a_grandchild_of_doe
    ├── aka_child_of_jane
    └── aka_cousin_of_lil
    
    1 directory, 4 files
    
  • I add 2 empty files from inside lil, one to doe and another to jane

    touch ../../aka_grandparent_of_lil ../../jane/aka_aunt_of_lil
    

    the terminal goes back to the command line

  • I look at the structure of doe again, this time from inside lil

    tree ../../../doe
    

    the terminal shows

    ../../../doe
    ├── a_file_in_doe
    ├── aka_grandparent_of_baby
    ├── aka_grandparent_of_lil
    ├── aka_parent_of_jane
    ├── aka_parent_of_john
    ├── jane
       ├── a_child_of_doe
       ├── a_file_in_jane
       ├── aka_aunt_of_lil
       ├── aka_sibling_of_john
       └── baby
           ├── a_file_in_baby
           ├── a_grandchild_of_doe
           ├── aka_child_of_jane
           └── aka_cousin_of_lil
    └── john
        ├── a_file_in_john
        ├── aka_sibling_of_jane
        ├── aka_uncle_of_baby
        ├── another_child_of_doe
        └── lil
            ├── a_file_in_lil
            ├── aka_child_of_john
            ├── aka_cousin_of_baby
            └── another_grandchild_of_doe
    
    5 directories, 21 files
    
  • I change directory to the parent of doe

    cd ../../..
    

    the terminal shows

    .../pumping_python
    

I know how to use directory relationships


how to remove a directory and all its contents


  • I remove doe and all its children and their children

    Danger

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

    rm -rf doe
    

    Note

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

    Remove-Item -Path doe -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 until there is nothing left, it goes through each child directory and removes everything including their children

    • -f/-Force means do not ask me any questions, just remove the file or folder and everything inside it until there is nothing left

  • I try to go back to the doe directory

    cd doe
    

    the terminal shows

    cd: no such file or directory: doe
    

review

I ran these 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?

Do you have a Windows computer with Windows Subsystem for Linux? Click Here to install Windows Subsystem for Linux

If you have MacOS or already have Windows Subsystem for Linux installed then you can 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