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#

Computers use a hierarchical file systems that organizes files & folders.

When you click through the folders (directories) on your computer, you’re interacting with this hierarchical system.

Command Line#

A command line interface is a way to interact with a computer through written commands.

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#

The specific location of a file or folder on your computer.

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#

Absolute paths specify the full path for a given file system (starting from the root directory).

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#

Relative paths specify the path to a file from your current working directory (where your computer is working right now).

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 in

  • each 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#

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 directory

  • move : moves a file

  • copy : copies a file

  • rename : renames a file

  • type : 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 or ls -l : lists files in current directory

  • mv : moves a file or renames it if you move to a new name in the same directory

  • cp : copies a file

  • cat : can be used to print out a file

  • touch : creates an empty file

Note that pwd, cd, mkdir, echo are all the same in Windows command prompt and Linux/Mac terminal.

Python Files#

Python files are plain-text files, with Python code in them, that can be executed and/or imported from.

Script vs. Module File#

Scripts#

A script is a Python file that can be run to execute a particular task.

Module Files#

A module file is a file with Python code (typically functions & classes) that we can import and use.

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#

Text-editors are programs made for editing text. Many text-editors are designed for writing code specifically.

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 vim

    • If 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) ¯\_(ツ)_/¯