Linux / Command line intro
- Almost everything here should work for both Linux and MacOS.
- Python, Ruby, and many other languages can run in REPL mode
- _R_ead _E_xecute _P_rint _L_oop.
- demonstrate Python in REPL mode (e.g., use it as a calculator)
- Terminal vs. shell
- Terminal is the program that opens a window, runs a program, and allows text I/O.
- e.g.,
Terminal.app
- Launch
XQuartz then run xterm ./hello_world
xterm provides a window in which to run hello_world
- The “shell” is a specific program that runs in the terminal that allows you to tell the operating system what to do.
- Common shells include
sh, bash, csh, tcsh, zsh.
- They are more alike than different.
- I will use
bash for this course.
- The shell is a lot like a programming language.
- But, we’ll get to that later.
- For now, just think of it as something you use to issue commands.
- There are two types of commands:
- Directions to the shell specifically
- (e.g.,
cd, echo, source, type)
- run
type -a cmd to check whether cmd is built-in
- programs to execute.
- If you type something, you either (a) run the built-in command, or run a program with that name.
python runs the program named python.
File system
- Groups of files are called directories
- Directories and folders are effectively the same thing.
- Files are organized in a hierarchical tree.
- Just like folders are nested withing folders, directories are nested within directories.
- Open inClass 6. Browse file system graphically.
- In a terminal, we do this with text commands
ls: List the files
cd: _c_hange _d_irectory
ls -l: long: Show file properties.
ls -a: show all files – including hidden files.
- Hidden files begin with
.
- Create hidden file and show that it only appears when using
-a
- Two special hidden files:
.: current directory
..: parent directory
cd .. to move up.
pwd: print working directory
cat: displays the contents of a file
head: displays the first few lines
- A path is a sequence of directories that identifies a file.
- Two types
- Absolute:
- a “full” name
- path shows how to get to the file from the root.
- Relative
- A “local” name
- path shows how to get to the file from the current working directory.
- When you use
cat or head (or many other programs) you supply a path of some kind.
- Give examples.
- current directory (with and without
.)
- parent directory
- sibling directory
- absolute path
- Which type of path you use depends on the context
- Just as whether you use GPS coordinates, or “how to get there” directions depends on the context.
- Key idea: You can reference the desired file from anywhere
- You only need to change directories if you want to.
- show how wildcards/globs work
Running Python as a program
- Although Jupyter notebooks are becoming increasingly popular, many python programs are
intended to “stand alone” like the other programs on your computer.
- Put the desired code in a file with the .py extension.
- run
python _path_to_file
- Give an example.
Misc
- If extra time,
- show wildcards/glob
- show mac Terminal and EOS