Command Line#
command line & shell commands
file paths (absolute and relative)
scripts and modules
Jupyter Notebooks are a helpful tool, but they’re bulky.
File Systems#
When you click through the folders (directories) on your computer, you’re interacting with this hierarchical system.
Command Line#
The command line allows us to:
create files
edit files
run python scripts
etc.
…without clicking on anything.
The Terminal#
The terminal is where you can type these commands into the command line.
Accessing the terminal…
possible on your computer
Windows: “Command Prompt”
Mac or Linux: “Terminal”
and on datahub
Shell Commands#
…can be run in the terminal and Jupyter notebooks
In jupyter Notebooks, do this by starting with !
Check current directory#
(note that this command will be cd
(without anything afterwards) on Windows machines and pwd
on Linux/Mac pachines)
# print working directory
!cd
# on datahub this would be:
# !pwd
An important aside: File Paths#
When using a Graphical User Interface (GUI), you click on directories to access subdirectories and finally find the file you’re interested in.
When using the command line, you specify a file’s path explicitly with text.
Absolute vs. Relative Paths#
The two ways to specify the path to your file of interest allow for flexibility in programming.
Absolute Paths#
root specifies the ‘highest’ directory in the file structure (the start).
An absolute file path starts with a slash /
(in Mac OS and Linux) or a drive letter and backslash C:\
(in Windows) specifying the root directory.
## absolute path
## this is specific to my computer
## look at the path output above for you computer
!cd
# on datahub this would be:
# !pwd
Relative Paths#
Note that directories are separated with \
in Windows and /
in Linux and Mac OS
# remind us of our current working directory
!cd
# on datahub this would be:
# !pwd
# relative path
# this is specific to my computer
!dir ..\fall_2024
# on datahub this would be:
# !ls ../fall_2024
..
specify you want to move one directory up in your hierarchy\fall_2024
specifies the path to the directory I want to list files ineach directory is separated with a backslash or slash (
\
or/
)
This relative path does not start with a leading slash (b/c it’s not an absolute path).
Class Question #1#
Given the following file structure:
/
scripts/
cool_thing.py
super_cool_thing.py
images/
image1.png
image2.png
notebooks/
00_intro.ipynb
01_variables.ipynb
If your current working directory is notebooks
, what is the absolute path to cool_thing.py
?
A)
/scripts/cool_thing.py
B)
scripts/cool_thing.py
C)
cool_thing.py
D)
../scripts/cool_thing.py
E) ¯\_(ツ)_/¯
Class Question #2#
Given the same file structure:
/
scripts/
cool_thing.py
super_cool_thing.py
images/
image1.png
image2.png
notebooks/
00_intro.ipynb
01_variables.ipynb
If your current working directory is notebooks
, what is the relative path to cool_thing.py
?
A)
/notebooks/../scripts/cool_thing.py
B)
scripts/cool_thing.py
C)
/scripts/cool_thing.py
D)
../scripts/cool_thing.py
E) ¯\_(ツ)_/¯
Shell Commands#
…can be run in the terminal and Jupyter notebooks
Check current directory#
# print working directory
!cd
# on datahub this would be:
# !pwd
Change directory#
# change directory
!cd ..\fall_2024
# same on datahub
# which directory are we in now?
!cd
# on datahub this would be:
# !pwd
List files in a directory#
dir
in Windows, ls
in Linux/Mac
# list files (list segments)
!dir
# on datahub this would be:
# !ls
More Shell Commands#
Make a new directory#
# make directory
!mkdir dir_name
# same on datahub
Create a file#
Works in Linux/Mac, not in Windows
# create an empty file (this will work on Linux/Mac/datahub but not in Windows)
!touch new_file.py
Move a file#
# move file
# notice the relative file path
!move new_file.py dir_name\
# on datahub this would be:
# !mv new_file.py dir_name\
And Some More#
Print out a message#
!echo Hello World!
Print the contents of a file#
!type dir_name\new_file.py
# on datahub this would be:
# !cat dir_name/new_file.py
Open to see and edit contents of a file#
# will not work on datahub
!open dir_name/new_file.py
Class Question #4#
Which creates a file?
A)
mkdir
B)
pwd
C)
cd
D)
touch
E)
cat
Windows Command Prompt#
dir
: lists files in current directorymove
: moves a filecopy
: copies a filerename
: renames a filetype
: can be used to print out a file
Note that pwd
, cd
, mkdir
, echo
are all the same in Windows command prompt and Linux/Mac terminal.
If you want to make a new empty file, you can do:
'' > my_file.py
^This construction puts an empty string into a file. If the filename is not found, it will create a new (empty) file.
Linux/Mac Command Prompt#
ls
orls -l
: lists files in current directorymv
: moves a file or renames it if you move to a new name in the same directorycp
: copies a filecat
: can be used to print out a filetouch
: creates an empty file
Note that pwd
, cd
, mkdir
, echo
are all the same in Windows command prompt and Linux/Mac terminal.
Python Files#
Script vs. Module File#
Scripts#
Module Files#
Remember: if you’re writing code, you cannot just click through to the file you want. You need to specify using code where the file you want is.
This is where understanding file paths is critically important.
Text Editors#
Terminal Based Text Editors#
There are text editors designed to be used within a terminal, such as vim
, emacs
, or nano
.
vim#
vim
is a terminal based text-editor. Type vim filename
in command line to open a file in vim.
vim
has different modes:
click
escape
+i
to enter edit mode (insert mode).This will let you write text / code into the file
To escape vim, press
escape
then type:wq
and enter to save and quit vimIf you want to exit without saving, you can do
:!q
to force quit without saving
Non-Terminal Text Editors#
For writing code (outside of notebooks and the terminal), you probably want a code focused stand-alone text editor, like Sublime
. If you are doing more extensive Python coding, you may want PyCharm
instead.
Executing Python Files#
From the command line, you can execute a Python script using the python
command:
python dir_name/new_file.py
Class Question #5#
To create a file and see its contents, which command line commands would you use (and in which order)?
A)
mkdir
>cd
B)
pwd
>ls
C)
cd
>pwd
D)
touch
>cat
E)
cat
>touch
Class Question #6#
Given the file structure from earlier:
/
scripts/
cool_thing.py
super_cool_thing.py
images/
image1.png
image2.png
notebooks/
00_intro.ipynb
01_variables.ipynb
Your currently working within notebooks
and you want to execute the code in ‘cool_thing.py’ from the command line. How would you do that?
A)
python cool_thing.py
B)
python ../notebooks/cool_thing.py
C)
python ../scripts/cool_thing.py
D)
python ../cool_thing.py
E) ¯\_(ツ)_/¯