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
.txtfor a plain text file.ps1for a PowerShell file.pyfor a Python module
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
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
doeIf 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 use the mkdir program to make a folder (directory)
mkdir pumping_pythonthe terminal goes back to the command line
I use cd to change directory again
cd pumping_pythonthe terminal shows I am in the
pumping_pythonfolder (directory).../pumping_pythonTip
to make sure I can see the
pumping_pythonfolder in my Integrated Development Environment (IDE) I have to open the folder. Here is how to do that with Visual Studio Codecode .a new Visual Studio Code window opens in the
pumping_pythondirectoryI want to work in a directory named
doecd doethe terminal shows
cd: no such file or directory: doe-
mkdir doethe terminal shows
.../pumping_python I change directory to the folder to do some work in it
cd doethe terminal shows
.../pumping_python/doeI am in the
doefolder I just madeI use pwd to see where I am
pwdthe terminal shows
.../pumping_python/doe
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
lsthe terminal goes back to the command line
.../pumping_python/doethis directory is empty. ls has a few options, I try it again with one of them
ls --allAttention
on MacOS you may get this error
ls: unrecognized option '--all'--allis the long form of the option, and there is usually a short form, use it insteadls -aNote
on Windows without Windows Subsystem for Linux use
dir /ahinstead ofls -adir /ahthe terminal does not show
.and..the terminal shows
. ..--all/-atells 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/doeI try cd with
..to see what happenscd ..the terminal shows
.../pumping_python..is used for the parent of a directory where I ampumping_pythonis the parent ofdoe
I go back to
doecd doethe terminal shows
.../pumping_python/doe
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
doedirectorytreethe terminal shows
. 0 directories, 0 filesit is empty
-
cd janethe terminal shows
cd: no such file or directory: janethe directory does not exist
I make the folder
mkdir janethe terminal goes back to the command line
I use ls to see what is now in
doels -athe terminal shows
. .. janeI use tree to see the structure
treethe terminal shows
. └── jane 2 directories, 0 filesI try to go to a different directory
cd johnthe terminal shows
cd: no such file or directory: johnthe directory does not exist
I make a new folder
mkdir johnthe terminal goes back to the command line
I use ls to see what is in
doenowls -athe terminal shows
. .. jane johnI use tree to see the structure
treethe terminal shows
. ├── jane └── john 3 directories, 0 filesI change directory to one of the children of
doecd janethe terminal shows
…/pumping_python/doe/janeI show what is in the folder
lsthe terminal goes back to the command line
I use ls with the short form of the
--alloptionls -athe terminal shows
. ..I use tree
treethe terminal shows
. 0 directories, 0 files-
cd babythe terminal shows
cd: no such file or directory: babymkdir babythe terminal goes back to the command line
I try to go to
babyagaincd babythe terminal shows
.../doe/jane/babyI am in the
babyfolder which is in thejanefolder which is in thedoefolderI go up a level to the parent of
babycd ..the terminal shows
.../doe/janeI am back in
janeI go up another level to the parent of
janecd ..the terminal shows
.../pumping_python/doeI am back in
doeI change directory to the other child of
doecd johnthe terminal shows
…/pumping_python/doe/johnI show what is in the folder
lsthe terminal goes back to the command line
I use ls with the short form of the
--alloptionls -athe terminal shows
. ..I use tree
treethe terminal shows
. 0 directories, 0 filesI change directory to a child of this folder
cd lilthe terminal shows
cd: no such file or directory: lilmkdir lilthe terminal goes back to the command line
I try to go to
lilagaincd lilthe terminal shows
.../doe/john/lilI am in the
lilfolderI go up a level to the parent of
lilcd ..the terminal shows
.../doe/johnI am back in
johnI go up another level to the parent of
johncd ..the terminal shows
.../pumping_python/doeI am back in
doeI show the directory structure
treethe terminal shows
. ├── jane │ └── baby └── john └── lil 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
doetouch a_file_in_doeNote
on Windows without Windows Subsystem for Linux use New-Item instead of
touchNew-Item a_file_in_doethe terminal goes back to the command line
I use ls to see what is in the folder now
ls -aNote
on Windows without Windows Subsystem for Linux use
dir /ahinstead ofls -adir /ahthe terminal does not show
.and..the terminal shows
. .. a_file_in_doe jane johnI change directory to one of the children of
doecd janethe terminal shows
.../pumping_python/doe/janeI add an empty file with touch
touch a_file_in_janethe terminal goes back to the command line
I show what is in the folder
ls -athe terminal shows
. .. a_file_in_jane babyI change directory to the parent of
janecd ..the terminal shows
.../pumping_python/doeI change directory to the other child of
doecd johnthe terminal shows
.../pumping_python/doe/johnI add an empty file with touch
touch a_file_in_johnthe terminal goes back to the command line
I show what is in the folder
ls -athe terminal shows
. .. a_file_in_john lilI change directory to the parent of
johncd ..the terminal shows
.../pumping_python/doeI am back in
doeI use tree
treethe terminal shows
. ├── a_file_in_doe ├── jane │ ├── a_file_in_jane │ └── baby └── john ├── a_file_in_john └── lil 5 directories, 3 filesTip
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 firstcd janethe terminal shows
/pumping_python/doe/janeI change directory to
babycd babythe terminal shows
.../doe/jane/babyI make an empty file
touch a_file_in_babythe terminal goes back to the command line
I use ls to show what is in the folder
ls -athe terminal shows
. .. a_file_in_babyI go back to the parent of
babycd ..the terminal shows
.../doe/jane/I go back to the parent of
janecd ..the terminal shows
...pumping_python/doeI want to make a file in
lil. I use change directory to go to its parent firstcd johnthe terminal shows
/pumping_python/doe/johnI change directory to
lilcd lilthe terminal shows
.../doe/john/lilI make an empty file
touch a_file_in_lilthe terminal goes back to the command line
I use ls to show what is in the folder
ls -athe terminal shows
. .. a_file_in_lilI go back to the parent of
lilcd ..the terminal shows
.../doe/john/I go back to the parent of
johncd ..the terminal shows
...pumping_python/doeI am back in
doeI use tree to see what I have so far
treethe 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
how to use directory relationships
I try to go from
doetobabyin 1 stepcd babythe terminal shows
cd: no such file or directory: babyI cannot get to
babywithout its parentI try to go from
doetobabyin 1 step with its parentcd jane/babythe terminal shows
.../doe/jane/babyI can go from
babytodoein 1 step with..cd ../..the terminal shows
.../pumping_python/doeSince
..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 parentI try to go from
doetolilin 1 stepcd lilthe terminal shows
cd: no such file or directory: lilI cannot get to
lilwithout its parentI try to go from
doetolilin 1 step with its parentcd john/lilthe terminal shows
.../doe/john/lilI go back to
doein 1 step with..cd ../..the terminal shows
.../pumping_python/doeI 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
doetobabyin 1 step with its parentcd jane/babythe terminal shows
.../doe/jane/babyI make another empty file
touch aka_child_of_janethe terminal goes back to the command line
I use ls to show what is in the folder
ls -athe terminal shows
. .. a_file_in_baby aka_child_of_janeI go back to
doecd ../..the terminal shows
.../pumping_python/doeI go from
doetolilin 1 step with its parentcd john/lilthe terminal shows
.../doe/john/lilI make an empty file
touch aka_child_of_johnthe terminal goes back to the command line
I use ls to show what is in the folder
ls -athe terminal shows
. .. a_file_in_lil aka_child_of_johnI go back to
doecd ../..the terminal shows
.../pumping_python/doeI add 2 empty files
touch aka_parent_of_jane aka_parent_of_johnthe terminal goes back to the command line
I use tree to see what
doelooks like nowtreethe 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 filesI can add empty files in 1 step in any directory as long as
I know its path
I know its relation to where I am and
I can write to the folder
I add 2 empty files in
janetouch jane/a_child_of_doe jane/aka_sibling_of_johnNote
on Windows without Windows Subsystem for Linux use New-Item instead of
touchNew-Item jane/a_child_of_doe jane/aka_sibling_of_johnthe terminal goes back to the command line
I add 2 empty files in
johntouch john/another_child_of_doe john/aka_sibling_of_janethe terminal goes back to the command line
I add an empty file in
babytouch jane/baby/a_grandchild_of_doethe terminal goes back to the command line
I add an empty file in
liltouch john/lil/another_grandchild_of_doethe terminal goes back to the command line
I change directory to the parent of
doecd ..the terminal shows
.../pumping_pythonI use tree to show what is in
doetree doethe 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 filesI type pwd to show where I am
pwdthe terminal shows
.../pumping_pythonI am in the
pumping_pythonfolderI can see what is in any folder when I know its path or relation to where I am
ls -a doethe terminal shows
. .. a_file_in_doe aka_parent_of_jane aka_parent_of_john jane johnI show what is in
janels -a doe/janethe terminal shows
. .. a_child_of_doe a_file_in_jane aka_sibling_of_john babyI show what is in
babyls -a doe/jane/babythe terminal shows
. .. a_file_in_baby a_grandchild_of_doe aka_child_of_janeI show what is in
johnls -a doe/johnthe terminal shows
. .. a_file_in_john aka_sibling_of_jane another_child_of_doe lilI show what is in
lills -a doe/john/lilthe terminal shows
. .. a_file_in_lil aka_child_of_john another_grandchild_of_doeI change directory to
babycd doe/jane/babyI want to see what is in
lilfrom insidebabyin 1 step.../..isdoeand I can go fromdoetolil, I use this relationship with lsls -a ../../john/lilthe terminal shows
. .. a_file_in_lil aka_child_of_john another_grandchild_of_doeI add an empty file to
lilfrombabytouch ../../john/lil/aka_cousin_of_babythe terminal goes back to the command line
I use tree to show the structure of
liltree ../../john/lilthe terminal shows
../../john/lil ├── a_file_in_lil ├── aka_child_of_john ├── aka_cousin_of_baby └── another_grandchild_of_doe 1 directory, 4 filesI add 2 empty files from inside
baby, one todoeand another tojohntouch ../../aka_grandparent_of_baby ../../john/aka_uncle_of_babythe terminal goes back to the command line
I can use the relationship to change directory to
lilfrombabycd ../../john/lilthe terminal shows
.../doe/john/lilI want to see what is in
babyfrom insidelilin 1 step. I use their relationship with lsls -a ../../jane/babythe terminal shows
. .. a_file_in_baby a_grandchild_of_doe aka_child_of_janeI add an empty file to
babyfromliltouch ../../jane/baby/aka_cousin_of_lilthe terminal goes back to the command line
I use tree to show what is in
babynowtree ../../jane/babythe terminal shows
../../jane/baby ├── a_file_in_baby ├── a_grandchild_of_doe ├── aka_child_of_jane └── aka_cousin_of_lil 1 directory, 4 filesI add 2 empty files from inside
lil, one todoeand another tojanetouch ../../aka_grandparent_of_lil ../../jane/aka_aunt_of_lilthe terminal goes back to the command line
I look at the structure of
doeagain, this time from insideliltree ../../../doethe 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 filesI change directory to the parent of
doecd ../../..the terminal shows
.../pumping_python
how to remove a directory and all its contents
I remove
doeand all its children and their childrenDanger
This is a desctructive operation that CANNOT be undone on MacOS or Linux/Windows Subsystem for Linux, use it wisely
rm -rf doeNote
on Windows without Windows Subsystem for Linux use
Remove-Item -Recurse -Forceinstead ofrm -rfRemove-Item -Path doe -Recurse -Forcethe terminal goes back to the command line
-r/-Recursemeans 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/-Forcemeans 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
doedirectorycd doethe 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
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