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
.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. 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
returnto run it, the terminal will not show anything if the commands run successfully-
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
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 now 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’s 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
parentcd parentthe terminal shows
cd: no such file or directory: parent-
mkdir parentthe terminal shows
.../pumping_python I change directory to the folder to do some work in it
cd parentthe terminal shows
.../pumping_python/parentI am in the
parentfolder I just madeI use pwd to see where I am
pwdthe terminal shows
.../pumping_python/parent
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
lsthe terminal goes back to the command line
.../pumping_python/parentthis 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 -aon 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/parentI 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 ofparent
I go back to
parentcd parentthe 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
parentdirectorytreeon Windows without Windows Subsystem for Linux use
tree /Finstead oftreetree /Fthe terminal shows
. 0 directories, 0 filesit is empty
-
cd childthe terminal shows
cd: no such file or directory: childthe directory does not exist
I make the folder
mkdir childthe terminal goes back to the command line
I use ls to see what is now in
parentls -aon Windows without Windows Subsystem for Linux use
dir /ahinstead ofls -adir /ahthe terminal shows
. .. childI use tree to see the structure
treethe terminal shows
. └── child 2 directories, 0 filesI try to go to a different directory
cd sibling_of_childthe terminal shows
cd: no such file or directory: sibling_of_childthe directory does not exist
I make a new folder
mkdir sibling_of_childthe terminal goes back to the command line
I use ls to see what is in
parentnowls -athe terminal shows
. .. child sibling_of_childI use tree to see the structure
treethe terminal shows
. ├── child └── sibling_of_child 3 directories, 0 filesI change directory to one of the children of
parentcd childthe terminal shows
…/pumping_python/parent/childI list the contents of 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 child_of_childthe terminal shows
cd: child_of_child: No such file or directorymkdir child_of_childthe terminal goes back to the command line
I try to go to
child_of_childagaincd child_of_childthe terminal shows
.../parent/child/child_of_childI go up a level to the parent of
child_of_childcd ..the terminal shows
.../parent/childI am back in
childI go up another level to the parent of
childcd ..the terminal shows
.../pumping_python/parentI am back in
parentI change directory to the other child of
parentcd sibling_of_childthe terminal shows
…/pumping_python/parent/sibling_of_childI list the contents of 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 child_of_sibling_of_childthe terminal shows
cd: child_of_sibling_of_child: No such file or directorymkdir child_of_sibling_of_childthe terminal goes back to the command line
I try to go to
child_of_sibling_of_childagaincd child_of_sibling_of_childthe terminal shows
.../parent/sibling_of_child/child_of_sibling_of_childI go up a level to the parent of
child_of_sibling_of_childcd ..the terminal shows
.../parent/sibling_of_childI am back in
sibling_of_childI go up another level to the parent of
sibling_of_childcd ..the terminal shows
.../pumping_python/parentI am back in
parentI show the directory structure
treeon Windows without Windows Subsystem for Linux use
tree /Finstead oftreetree /Fthe 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
parenttouch a_file_in_parenton Windows without Windows Subsystem for Linux use New-Item instead of
touchNew-Item a_file_in_parentthe terminal goes back to the command line
I use ls to see what is in the folder now
ls -aon Windows without Windows Subsystem for Linux use
dir /ahinstead ofls -adir /ahthe terminal does not show
.and..the terminal shows
. .. a_file_in_parent child sibling_of_childI change directory to one of the children of
parentcd childthe terminal shows
.../pumping_python/parent/childI add an empty file with touch
touch a_file_in_childthe terminal goes back to the command line
I list the contents of the folder
ls -athe terminal shows
. .. a_file_in_child child_of_childI change directory to the parent of
childcd ..the terminal shows
.../pumping_python/parentI change directory to the other child of
parentcd sibling_of_childthe terminal shows
.../pumping_python/parent/sibling_of_childI add an empty file with touch
touch a_file_in_sibling_of_childthe terminal goes back to the command line
I list the contents of the folder
ls -athe terminal shows
. .. a_file_in_sibling_of_child child_of_sibling_of_childI change directory to the parent of
sibling_of_childcd ..the terminal shows
.../pumping_python/parentI use tree
treethe 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 filesTip
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 firstcd childthe terminal shows
/pumping_python/parent/childI change directory to
child_of_childcd child_of_childthe terminal shows
.../parent/child/child_of_child
how to use directory relationships
I can go from
child_of_childtoparentin 1 step by using..cd ../..the terminal shows
.../pumping_python/parentSince
..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 parentI try to go from
parenttochild_of_childin 1 stepcd child_of_childthe terminal shows
cd: child_of_child: No such file or directoryI cannot get to
child_of_childwithout its parentI try to go from
parenttochild_of_childin 1 step with its parentcd child/child_of_childthe terminal shows
.../parent/child/child_of_childI make an empty file
touch a_grandchild_of_parentthe terminal goes back to the command line
I use ls to list the contents of the folder
ls -athe terminal shows
. .. a_grandchild_of_parentI go back to
parentcd ../..the terminal shows
.../pumping_python/parentI want to make a file in
child_of_sibling_of_child. I use change directory to go to its parent firstcd sibling_of_childthe terminal shows
/pumping_python/parent/sibling_of_childI change directory to
child_of_sibling_of_childcd child_of_sibling_of_childthe terminal shows
.../parent/sibling_of_child/child_of_sibling_of_childI go from
child_of_sibling_of_childtoparentin 1 stepcd ../..the terminal shows
.../pumping_python/parentI try to go from
parenttochild_of_sibling_of_childin 1 stepcd child_of_sibling_of_childthe terminal shows
cd: child_of_sibling_of_child: No such file or directoryI 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
parenttochild_of_sibling_of_childin 1 step with its parentcd sibling_of_child/child_of_sibling_of_childthe terminal shows
.../parent/sibling_of_child/child_of_sibling_of_childI make an empty file
touch another_grandchild_of_parentthe terminal goes back to the command line
I use ls to list the contents of the folder
ls -athe terminal shows
. .. another_grandchild_of_parentI go back to
parentcd ../..the terminal shows
.../pumping_python/parentI add an empty file
touch aka_grandparent_of_child_of_childthe terminal goes back to the command line
I make another empty file
touch aka_grandparent_of_child_of_sibling_of_childthe terminal goes back to the command line
I use tree to see what
parentlooks like nowtreeon Windows without Windows Subsystem for Linux use
tree /Finstead oftreetree /Fthe 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 filesI 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
childtouch child/aunt_or_uncle_of_another_grandchild_of_parenton Windows without Windows Subsystem for Linux use New-Item instead of
touchNew-Item child/aunt_or_uncle_of_another_grandchild_of_parentthe terminal goes back to the command line
I add another empty file in
sibling_of_childtouch sibling_of_child/aunt_or_uncle_of_a_grandchild_of_parentthe terminal goes back to the command line
I use tree
treethe 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 filesI add an empty file in
child_of_childtouch child/child_of_child/a_file_in_child_of_childthe terminal goes back to the command line
I add an empty file in
child_of_sibling_of_childtouch sibling_of_child/child_of_sibling_of_child/a_file_in_child_of_sibling_of_childon 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_childthe terminal goes back to the command line
I change directory to the parent of
parentcd ..the terminal shows
.../pumping_pythonI use tree to show what is in
parenttree parentthe 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 filesI type pwd to show where I am
pwdthe terminal shows
.../pumping_pythonI can list the contents of any folder once I know its path or relation to where I am
ls -a parentthe terminal shows
. aka_grandparent_of_child_of_child sibling_of_child .. aka_grandparent_of_child_of_sibling_of_child a_file_in_parent childI list the contents of
childls -a parent/childthe terminal shows
. a_file_in_child child_of_child .. aunt_or_uncle_of_another_grandchild_of_parentI list the contents of
child_of_childls -a parent/child/child_of_childthe terminal shows
. .. a_file_in_child_of_child a_grandchild_of_parentI list the contents of
sibling_of_childls -a parent/sibling_of_childthe terminal shows
. a_file_in_sibling_of_child child_of_sibling_of_child .. aunt_or_uncle_of_a_grandchild_of_parentI list the contents of
child_of_sibling_of_childls -a parent/sibling_of_child/child_of_sibling_of_childthe terminal shows
. .. a_file_in_child_of_sibling_of_child another_grandchild_of_parentI change directory to
child_of_childcd parent/child/child_of_childI want to list the contents of
child_of_sibling_of_childfrom insidechild_of_childin 1 step.../..isparentand I can go fromparenttochild_of_sibling_of_child, I use the relationship with lsls -a ../../sibling_of_child/child_of_sibling_of_childthe terminal shows
. .. a_file_in_child_of_sibling_of_child another_grandchild_of_parentI add an empty file to
child_of_sibling_of_childfromchild_of_childtouch ../../sibling_of_child/child_of_sibling_of_child/cousin_of_child_of_childthe terminal goes back to the command line
I use tree this time
tree ../../sibling_of_child/child_of_sibling_of_childthe 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 filesI can use the same thing to change directory to
child_of_sibling_of_childfromchild_of_childcd ../../sibling_of_child/child_of_sibling_of_childthe terminal shows
.../parent/sibling_of_child/child_of_sibling_of_child $I want to list the contents of
child_of_childfrom insidechild_of_sibling_of_childin 1 step. Since../..isparentand I can go fromparenttochild_of_child, I use the relationship with lsls -a ../../child/child_of_childthe terminal shows
. .. a_file_in_child_of_child a_grandchild_of_parentI add an empty file to
child_of_childfromchild_of_sibling_of_childtouch ../../child/child_of_child/cousin_of_child_of_sibling_of_childthe terminal goes back to the command line
I use tree to show what is in
child_of_childnowtree ../../child/child_of_childthe 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 filesI look at the structure of
parentagain, this time from insidechild_of_sibling_of_childtree ../../../parentthe 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 filesI change directory to the parent of
parentcd ../../..the terminal shows
.../pumping_pythonI show the current working directory (where I am)
pwdthe terminal shows
.../
how to remove a directory and all its contents
I remove
parentand all its descendantsDanger
This is a desctructive operation that CANNOT be undone on MacOS or Linux/Windows Subsystem for Linux, use it wisely
rm -rf parenton Windows without Windows Subsystem for Linux use
Remove-Item -Recurse -Forceinstead ofrm -rfRemove-Item -Path parent -Recurse -Forcethe terminal goes back to the command line
-r/-Recursemeans remove child directories and what is in them recursively, it goes through each child directory and removes everything include their children-f/-Forcemeans “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
parentdirectorycd parentthe 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
what is next?
Would you like to see me make a Test Driven Development Environment