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
mkdir - to make directories
cd - to change directories
ls - to show what is in directories
tree - to show the relationships between directories
rm - to remove directories
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
.pyfor a Python module.txtfor a plain text file.shfor a bash file.ps1for a PowerShell file.docfor a Word Document.xlsfor an Excel Spreadsheet
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
tree /Ffor treedirforls --all/-aRemove-Item for rm
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 means
print working directory, it prints the directory I am in, to the terminaleach
/shows a parent-child relationshipthe first
/is forrootwhich is the starting ancestor of all the folders on the computerthe first
/is at the highest level
Note
If you see
pumping_pythonwhen you typepwd, skip to the part where I createdoeIf 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
I use the mkdir program to make a folder (directory)
mkdir pumping_pythonthe terminal goes back to the command line
mkdirmeansmake directory
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 it. 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
doe, I try to change directory todoecd doethe terminal shows
cd: no such file or directory: doedoeis not in thepumping_pythondirectory, yetI make
doemkdir doethe terminal shows
.../pumping_pythonI change directory to
doecd 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
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
lsthe terminal goes back to the command line
.../pumping_python/doethis directory is empty
ls has a few options. I try ls again with one of them
ls --allthe terminal shows
. ..Attention
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-ainsteadls -aNote
on Windows without Windows Subsystem for Linux use
dir /ahinstead ofls -adir /ahthe terminal does not show
.and..on Windows without Windows Subsystem for LinuxI try to change directory to the
.cd .the terminal shows
.../pumping_python/doeI try cd with
..to see what happenscd ..the terminal shows
.../pumping_pythonI go back to
doecd doethe terminal shows
.../pumping_python/doe
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
doedirectorytreethe terminal shows
. 0 directories, 0 filesthere is nothing in
doe, it is emptyI try to change directory to
jane_doe, a child ofdoecd jane_doethe terminal shows
cd: no such file or directory: jane_doejane_doeis not a child ofdoe, yetI make
jane_doemkdir jane_doethe terminal goes back to the command line
I use ls to see what is now in
doelsthe terminal shows
jane_doeI use tree to show the
doefamily treetreethe terminal shows
. └── jane_doe 2 directories, 0 filesjane_doeis a child ofdoe.is the working directory, which isdoein this casethe line in the tree that goes from
.tojane_doeshows I can go fromdoeright tojane_doe
I change directory to
jane_doecd jane_doethe terminal shows
.../pumping_python/doe/jane_doeI am in
jane_doeI change directory to
doecd doethe terminal shows
cd: no such file or directory: doedoeis not a child ofjane_doeI change directory back to the parent of
jane_doecd ..the terminal shows
.../pumping_python/doeI am back in
doe..is for the parent of the directory I am in..isdoewhen I am injane_doe
I try to change directory to
john_doecd john_doethe terminal shows
cd: no such file or directory: john_doejohn_doeis not a child ofdoe, yetI make
john_doemkdir john_doethe terminal goes back to the command line
I use ls to see what is in
doelsthe terminal shows
jane_doe john_doeI use tree to show the
doefamily treetreethe terminal shows
. ├── jane_doe └── john_doe 3 directories, 0 filesjohn_doeis a child ofdoe.is the working directory, which isdoein this casethe line in the tree that goes from
.tojohn_doeshows I can go fromdoeright tojohn_doe
I change directory to
john_doecd john_doethe terminal shows
.../pumping_python/doe/john_doeI am in
john_doeI change directory to
doecd doethe terminal shows
cd: no such file or directory: doedoeis not a child ofjohn_doeI change directory back to the parent of
john_doecd ..the terminal shows
.../pumping_python/doeI am back in
doe..is for the parent of the directory I am in..isdoewhen I am injohn_doe
I try to change directory to
.hidden_folder_in_doecd .hidden_folder_in_doethe terminal shows
cd: no such file or directory: .hidden_folder_in_doe.hidden_folderis not indoe, yetI make a hidden folder
mkdir .hidden_folder_in_doethe terminal goes back to the command line
I use ls to see what is in
doelsthe terminal shows
jane_doe john_doeI use ls with the
-aoption to see everything that is indoels -athe terminal shows
. .. .hidden_folder_in_doe jane_doe john_doeI use tree to show the
doefamily treetreethe terminal shows
. ├── jane_doe └── john_doe 3 directories, 0 filesI can also use tree with the
-aoptiontree -athe terminal shows
. ├── .hidden_folder_in_doe ├── jane_doe └── john_doe 4 directories, 0 files.is the working directory, which isdoein this casethe line in the tree that goes from
.to.hidden_folder_in_doeshows I can go fromdoeright to.hidden_folder_in_doe
I change directory to
.hidden_folder_in_doecd .hidden_folder_in_doethe terminal shows
.../pumping_python/doe/.hidden_folder_in_doeI am in
.hidden_folder_in_doeI change directory to
doecd doethe terminal shows
cd: no such file or directory: doedoeis not a child of.hidden_folder_in_doeI change directory back to the parent of
.hidden_folder_in_doecd ..the terminal shows
.../pumping_python/doeI am back in
doe..is for the parent of the directory I am in..isdoewhen I am in.hidden_folder_in_doe
I change directory to
jane_doecd jane_doethe terminal shows
.../pumping_python/doe/jane_doeI 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
. ..jane_doehas no childrenI use tree
treethe terminal shows
. 0 directories, 0 filesjane_doehas no childrenI change directory to a child of
jane_doecd mary_jane_doethe terminal shows
cd: no such file or directory: mary_jane_doejane_doehas no children, yetI make
mary_jane_doemkdir mary_jane_doethe terminal goes back to the command line
I try to go to
mary_jane_doeagaincd mary_jane_doethe terminal shows
.../doe/jane_doe/mary_jane_doeI am in the
mary_jane_doefoldermary_jane_doeis a child ofjane_doejane_doeis a child ofdoe
I go up a level to the parent of
mary_jane_doecd ..the terminal shows
.../doe/jane_doeI am back in
jane_doeI change directory to a hidden folder in
jane_doecd .hidden_folder_in_jane_doethe terminal shows
cd: no such file or directory: .hidden_folder_in_jane_doethere is no folder named
.hidden_folder_in_jane_doeinjane_doeI make a
.hidden_folder_in_jane_doemkdir .hidden_folder_in_jane_doethe terminal goes back to the command line
I try to go to
.hidden_folder_in_jane_doeagaincd .hidden_folder_in_jane_doethe terminal shows
.../doe/jane_doe/.hidden_folder_in_jane_doeI go up a level to the parent of
.hidden_folder_in_jane_doecd ..the terminal shows
.../doe/jane_doeI am back in
jane_doeI use tree
treethe terminal shows
. └── mary_jane_doe 2 directories, 0 filesI use
tree -atree -athe terminal shows
. ├── .hidden_folder_in_jane_doe └── mary_jane_doe 3 directories, 0 files.isjane_doewhen I am injane_doethe line in the tree that goes from
.tomary_jane_doeshows I can go fromjane_doeright tomary_jane_doethe line in the tree that goes from
.to.hidden_folder_in_jane_doeshows I can go fromjane_doeright to.hidden_folder_in_jane_doe
I go up a level to the parent of
jane_doecd ..the terminal shows
.../pumping_python/doeI am back in
doe
I change directory to
john_doecd john_doethe terminal shows
.../pumping_python/doe/john_doejohn_doeis a child ofdoeI 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
. ..john_doehas no childrenI use tree
treethe terminal shows
. 0 directories, 0 filesjohn_doehas no children, yetI change directory to a child of
john_doecd lil_john_doethe terminal shows
cd: no such file or directory: lil_john_doejohn_doehas no childrenI make
lil_john_doemkdir lil_john_doethe terminal goes back to the command line
I try to go to
lil_john_doeagaincd lil_john_doethe terminal shows
.../doe/john_doe/lil_john_doeI am in the
lil_john_doefolderlil_john_doeis a child ofjohn_doejohn_doeis a child ofdoe
I go up a level to the parent of
lil_john_doecd ..the terminal shows
.../doe/john_doeI am back in
john_doeI change directory to a hidden folder in
john_doecd .hidden_folder_in_john_doethe terminal shows
cd: no such file or directory: .hidden_folder_in_john_doethere is no folder named
.hidden_folder_in_john_doeinjohn_doeI make
.hidden_folder_in_john_doemkdir .hidden_folder_in_john_doethe terminal goes back to the command line
I try to go to
.hidden_folder_in_john_doeagaincd .hidden_folder_in_john_doethe terminal shows
.../doe/john_doe/.hidden_folder_in_john_doeI go up a level to the parent of
.hidden_folder_in_john_doecd ..the terminal shows
.../doe/john_doeI am back in
john_doeI use tree
treethe terminal shows
. └── lil_john_doe 2 directories, 0 filesI use
tree -atree -athe terminal shows
. ├── .hidden_folder_in_john_doe └── lil_john_doe 3 directories, 0 files.isjohn_doewhen I am injohn_doethe line in the tree that goes from
.tolil_john_doeshows I can go fromjohn_doeright tolil_john_doethe line in the tree that goes from
.to.hidden_folder_in_john_doeshows I can go fromjohn_doeright to.hidden_folder_in_john_doe
I go up a level to the parent of
john_doecd ..the terminal shows
.../pumping_python/doeI am back in
doeI show the
doefamily treetreethe terminal shows
. ├── jane_doe │ └── mary_jane_doe └── john_doe └── lil_john_doe 5 directories, 0 filesI use the
-aoption with treetree -athe 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 filesthe lines show that
mary_jane_doeandlil_john_doeare grandchildren ofdoeI can go from
doeright to.hidden_folder_in_doeI can go from
doeright tojane_doeI can go from
doeright tojohn_doeI can go from
jane_doeright to.hidden_folder_in_jane_doeI can go from
jane_doeright tomary_jane_doeI can go from
john_doeright to.hidden_folder_in_john_doeI can go from
john_doeright tolil_john_doe
how to make an empty file
I can make empty files in a folder with the touch program
I make an empty file in
doetouch empty_file_in_doethe terminal goes back to the command line
Note
on Windows without Windows Subsystem for Linux use New-Item instead of
touchNew-Item empty_file_in_doeI make an empty hidden file in
doetouch .hidden_file_in_doeI use ls to see what is in the folder
lsthe terminal shows
empty_file_in_doe jane_doe john_doeI use ls with the
-aoptionls -athe terminal shows
. .hidden_folder_in_doe john_doe .. empty_file_in_doe .hidden_file_in_doe jane_doeNote
on Windows without Windows Subsystem for Linux use
dir /ahinstead ofls -adir /ahthe terminal does not show
.and..and always shows hidden folder and filesI change directory to
jane_doecd jane_doethe terminal shows
.../pumping_python/doe/jane_doeI make an empty file with touch
touch empty_file_in_jane_doethe terminal goes back to the command line
I make an empty hidden file with touch
touch .hidden_file_in_jane_doethe terminal goes back to the command line
I show what is in the folder
ls -athe terminal shows
. .hidden_folder_in_jane_doe .. empty_file_in_jane_doe .hidden_file_in_jane_doe mary_jane_doeI change directory to the parent of
jane_doecd ..the terminal shows
.../pumping_python/doeI change directory to
john_doecd john_doethe terminal shows
.../pumping_python/doe/john_doeI make an empty file with touch
touch empty_file_in_john_doethe terminal goes back to the command line
I make an empty hidden file with touch
touch .hidden_file_in_john_doethe terminal goes back to the command line
I show what is in the folder
ls -athe terminal shows
. .hidden_folder_in_john_doe .. empty_file_in_john_doe .hidden_file_in_john_doe lil_john_doeI change directory to the parent of
john_doecd ..the terminal shows
.../pumping_python/doeI am back in
doeI use tree to show the
doefamily treetreethe 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 filesTip
Your terminal may use colors to show the difference between directories and files
I use tree with the
-aoptiontree -athe 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 firstcd jane_doethe terminal shows
/pumping_python/doe/jane_doeI am in
jane_doeI change directory to
mary_jane_doecd mary_jane_doethe terminal shows
.../doe/jane_doe/mary_jane_doeI am in
mary_jane_doeI make an empty file in
mary_jane_doetouch empty_file_in_mary_jane_doethe terminal goes back to the command line
I make an empty hidden file in
mary_jane_doetouch .hidden_file_in_mary_jane_doethe terminal goes back to the command line
I make a hidden folder in
mary_jane_doemkdir .hidden_folder_in_mary_jane_doethe terminal goes back to the command line
I use ls to show what is in the folder
ls -athe terminal shows
. .. .hidden_file_in_mary_jane_doe .hidden_folder_in_mary_jane_doe empty_file_in_mary_jane_doeI go back to the parent of
mary_jane_doecd ..the terminal shows
.../doe/jane_doe/I am in
jane_doeI go back to the parent of
jane_doecd ..the terminal shows
...pumping_python/doeI am back in
doe
I want to make a file in
lil_john_doe. I use cd to go to its parent firstcd john_doethe terminal shows
/pumping_python/doe/john_doeI am in
john_doeI change directory to
lil_john_doecd lil_john_doethe terminal shows
.../doe/john_doe/lil_john_doeI am in
lil_john_doeI make an empty file in
lil_john_doetouch empty_file_in_lil_john_doethe terminal goes back to the command line
I make an empty hidden file in
lil_john_doetouch .hidden_file_in_lil_john_doethe terminal goes back to the command line
I make a hidden folder in
lil_john_doemkdir .hidden_folder_in_lil_john_doethe terminal goes back to the command line
I use ls to show what is in the folder
ls -athe terminal shows
. .. .hidden_file_in_lil_john_doe .hidden_folder_in_lil_john_doe empty_file_in_lil_john_doeI go back to the parent of
lil_john_doecd ..the terminal shows
.../doe/john_doe/I am in
john_doeI go back to the parent of
john_doecd ..the terminal shows
...pumping_python/doeI am back in
doeI use tree to see what I have so far
treethe 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 filesI use tree with the
-aoptiontree -athe 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
how to use directory relationships
I try to go from
doetomary_jane_doein 1 stepcd mary_jane_doethe terminal shows
cd: no such file or directory: mary_jane_doeI use tree with the
-doption to show only the directoriestree -dthe terminal shows
. ├── jane_doe │ └── mary_jane_doe └── john_doe └── lil_john_doe 5 directoriesthere is no line from
doetomary_jane_doebecausemary_jane_doeis not a child ofdoethere is a line from
jane_doetomary_jane_doebecausemary_jane_doeis a child ofjane_doethere is a line from
doetojane_doebecausejane_doeis a child ofdoeI can go from
doetojane_doetomary_jane_doe
I go from
doetomary_jane_doein 1 step with its parentcd jane_doe/mary_jane_doethe terminal shows
.../doe/jane_doe/mary_jane_doeI cannot go to
mary_jane_doewithout its parentI can go from
mary_jane_doeback todoein 1 step with..cd ../..the terminal shows
.../pumping_python/doeI 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..frommary_jane_doeisjane_doe..fromjane_doeisdoe
I try to go from
doetolil_john_doein 1 stepcd lil_john_doethe terminal shows
cd: no such file or directory: lil_john_doeI use tree with the
-doption to show only the directoriestree -dthe terminal shows
. ├── jane_doe │ └── mary_jane_doe └── john_doe └── lil_john_doe 5 directoriesthere is no line from
doetolil_john_doebecauselil_john_doeis not a child ofdoethere is a line from
john_doetolil_john_doebecauselil_john_doeis a child ofjohn_doethere is a line from
doetojohn_doebecausejohn_doeis a child ofdoeI can go from
doetojohn_doetolil_john_doe
I go from
doetolil_john_doein 1 step with its parentcd john_doe/lil_john_doethe terminal shows
.../doe/john_doe/lil_john_doeI cannot go to
lil_john_doewithout its parentI can go from
lil_john_doeback todoein 1 step with..cd ../..the terminal shows
.../pumping_python/doeI am back in
doe...is for the parent of a directory../..is for the parent of the parent..fromlil_john_doeisjohn_doe..fromjohn_doeisdoe
Note
how to use touch with directory relationships
I show the
doefamily treetreethe 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 filesI make an empty file in
jane_doefrom insidedoetouch jane_doe/child_of_doeI make an empty file in
mary_jane_doefrom insidedoetouch jane_doe/mary_jane_doe/grandchild_of_doeI make an empty file in
john_doefrom insidedoetouch john_doe/child_of_doeI make an empty file in
lil_john_doefrom insidedoetouch john_doe/lil_john_doe/grandchild_of_doeI show the
doefamily treetreethe 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_doecd jane_doeI make an empty file in
mary_jane_doefrom insidejane_doetouch mary_jane_doe/child_of_jane_doeI make an empty file in
doefrom insidejane_doetouch ../parent_of_jane_doeI make an empty file in
john_doefrom insidejane_doetouch ../john_doe/sibling_of_jane_doe..is the parent ofjane_doewhich isdoejohn_doeis a child ofdoe
I make an empty file in
lil_john_doefrom insidejane_doetouch ../john_doe/lil_john_doe/child_of_sibling_of_john_doe..is the parent ofjane_doewhich isdoejohn_doeis a child ofdoelil_john_doeis a child ofjohn_doe
I go back to the parent of
jane_doecd ..I show the
doefamily treetreethe 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_doecd john_doeI make an empty file in
lil_john_doefrom insidejohn_doetouch lil_john_doe/child_of_john_doeI make an empty file in
doefrom insidejohn_doetouch ../parent_of_john_doeI make an empty file in
jane_doefrom insidejohn_doetouch ../jane_doe/sibling_of_john_doe..is the parent ofjohn_doewhich isdoejane_doeis a child ofdoe
I make an empty file in
mary_jane_doefrom insidejohn_doetouch ../jane_doe/mary_jane_doe/child_of_sibling_of_jane_doe..is the parent ofjohn_doewhich isdoejane_doeis a child ofdoemary_jane_doeis a child ofjane_doe
I go back to the parent of
john_doecd ..I show the
doefamily treetreethe 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_doea child ofjohn_doeand a child ofjohn_doe'ssibling?How is
mary_jane_doea child ofjane_doeand a child ofjane_doe'ssibling?
I made mistakes
how to rename a file or directory
I go to
mary_jane_doecd jane_doe/mary_jane_doeI use the mv program to change
child_of_sibling_of_jane_doetochild_of_sibling_of_john_doemv child_of_sibling_of_jane_doe child_of_sibling_of_john_doeI go to
lil_john_doecd ../../john_doe/lil_john_doe..is the parent ofmary_jane_doewhich isjane_doe../..is the parent of the parent ofmary_jane_doewhich isdoejohn_doeis a child ofdoelil_john_doeis a child ofjohn_doe
I use the mv program to change
child_of_sibling_of_john_doetochild_of_sibling_of_jane_doemv child_of_sibling_of_john_doe child_of_sibling_of_jane_doethe terminal goes back to the command line
I go back to
doecd ../..I show the family tree
treethe 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 filesbetter.
mv means move, it takes two arguments
mv source target
sourceis the path/address I want to move the original file or folder fromtargetis the path/address I want to move the original file or folder tothis allows me to rename a file or folder in one step, the other way would be to
I change directory to
mary_jane_doefromdoecd jane_doe/mary_jane_doeI make an empty file in
jane_doefrom insidemary_jane_doetouch ../parent_of_mary_jane_doeI make an empty file in
doefrom insidemary_jane_doetouch ../../grandparent_of_mary_jane_doeI make an empty file in
john_doefrom insidemary_jane_doetouch ../../john_doe/aunt_of_mary_jane_doeI made a mistake -
john_doeis not the aunt ofmary_jane_doehe is the uncleI make an empty file in
lil_john_doefrom insidemary_jane_doetouch ../../john_doe/lil_john_doe/cousin_of_mary_jane_doeI look at the
doefamily tree from insidemary_jane_doetree ../..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_doefrommary_jane_doecd ../../john_doe/lil_john_doethe terminal shows
.../doe/john_doe/lil_john_doeI make an empty file in
john_doefrom insidelil_john_doetouch ../parent_of_lil_john_doeI make an empty file in
doefrom insidelil_john_doetouch ../../grandparent_of_lil_john_doeI make an empty file in
jane_doefrom insidelil_john_doetouch ../../jane_doe/uncle_of_lil_john_doeI made another mistake -
jane_doeis not the uncle ofmary_jane_doeshe is the auntI make an empty file in
mary_jane_doefrom insidelil_john_doetouch ../../jane_doe/mary_jane_doe/cousin_of_lil_john_doeI look at the
doefamily tree from insidelil_john_doethrough the parent ofdoetree ../../../doe..is for the parent oflil_john_doewhich isjohn_doe../..is for the parent of the parent oflil_john_doewhich isdoe../../..is for the parent of the parent of the parent oflil_john_doewhich ispumping_pythondoeis a child ofpumping_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_doetoaunt_of_lil_john_doemv ../../jane_doe/uncle_of_lil_john_doe ../../jane_doe/aunt_of_lil_john_doewhy does that work?
I change
aunt_of_mary_jane_doetouncle_of_mary_jane_doemv ../aunt_of_mary_jane_doe ../uncle_of_mary_jane_doethe terminal goes back to the command line
I use tree to show what is in
doetree ../..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 filesall 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
doecd ../../..the terminal shows
.../pumping_pythonI am in
pumping_pythonI show what is in
doels -a doethe 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_doeI show what is in
jane_doels -a doe/jane_doethe 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_doeI show what is in
mary_jane_doels -a doe/jane_doe/mary_jane_doethe 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_doeI change directory to
mary_jane_doecd doe/jane_doe/mary_jane_doeI show what is in
john_doefrommary_jane_doels -a ../../john_doethe 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_doeI show what is in
lil_john_doefrommary_jane_doels -a ../../john_doe/lil_john_doethe 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_doeI use tree to show what is in
lil_john_doetree ../../john_doe/lil_john_doethe 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 filesI use tree to show what is in
john_doetree ../../john_doethe 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 filesI change directories from
mary_jane_doetolil_john_doecd ../../john_doe/lil_john_doethe terminal shows
.../doe/john_doe/lil_john_doeI am in
lil_john_doeI look at what is in
mary_jane_doefrom insidelil_john_doels -a ../../jane_doe/mary_jane_doethe 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_doeI use tree to show what is in
mary_jane_doetree ../../jane_doe/mary_jane_doethe 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 filesI use tree to show what is in
jane_doetree ../../jane_doethe 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 filesI go to the parent of
doecd ../../..the terminal shows
.../pumping_pythonI show the
doefamily tree and all its hidden secretstree -a doethe 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
I know their path/address
I know their relation to where I am and
I can write to the folder
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
doeand all its children and their childrenrm doethe terminal shows
rm: cannot remove 'doe': Is a directoryI cannot remove a directory this way
I remove
doeand all its children and their children with the-r/--recursiveoptionDanger
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 doethe terminal goes back to the command line
Note
on Windows without Windows Subsystem for Linux use
Remove-Item -Recurse -Forceinstead ofrm --recursiveRemove-Item -Path doe -Recurse -Forcermmeansremove-r/--recursive/-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-Forcemeans do not ask me any questions, just remove the file or folder
I try to go back to
doecd doethe terminal shows
cd: no such file or directory: doethe
doefamily is gone!
review
I ran these commands to play with folders (directories)
mkdir to make directories
cd to change directories
tree to show a directory and its sub directories as a tree
rm to remove files or directories
How many questions do you think you can answer after going through this chapter?
code from the 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