“Command-line Computing” Exercise

Before we start learning about Python or software development, it will be helpful for us to achieve a basic level of familiarity with a command-line application. We use the command-line to navigate the computer’s filesystem, execute Python scripts, and perform other tasks using command-line utilities (CLIs).

Commands may differ based on which operating system and command-line application you’re using, but we should encouraged to learn the prevalent “unix-style” commands:

Additional References:

Instructions

Open the Terminal application (Mac) or the Git Bash application (Windows).

After typing each of the commands below, press “enter” to execute it.

Optionally clear previous terminal output at any time by pressing “command + k”, or by typing clear and pressing “enter”.

Current User

Display the current user’s name:

whoami

Present Working Directory

Display the current/present working directory:

pwd

Listing Files in a Directory

List files in the current working directory:

ls

Alternatively, list files using a different display, including file permissions and hidden files:

ls -al

Managing Files

Setup a new directory in which to add some files, and navigate into that directory:

mkdir my_folder
cd my_folder

Create one or more files in the new directory you just created:

touch README.md
touch index.html
touch my_data.csv
touch my_message.txt

Remove a file:

rm index.html

Open a file in a text editor like VS Code (then edit it, and save it):

code my_message.txt

NOTE: Mac users may need to first configure the code command by following these VS Code shell command setup instructions

Display file contents:

cat my_message.txt

Move a file:

mv ~/Desktop/my_folder/my_message.txt ~/Desktop

FYI: If you are into maximum efficiency, press “tab” to auto-complete file paths so you don’t have to type the whole thing. 😺

Copy a file:

cp ~/Desktop/my_message.txt ~/Desktop/my_folder

Copy contents of a file into the clipboard for pasting:

# Mac OS:
pbcopy < ~/Desktop/my_folder/my_message.txt

# Windows OS:
cat ~/Desktop/my_folder/my_message.txt | clip

# ... then just paste as you normally would after copying some text

Further Exploration (Mac Only)

There are many other utilities to use from the command-line. For example, you may optionally try some of the examples below.

Making your computer speak:

say "Hello, I am your computer. Let's be friends."

Tracing the route traveled by a network request:

traceroute google.com
# ... stop after a few seconds if necessary by pressing: control + c

Timing the duration of a network request:

ping google.com
# ... stop after a few seconds if necessary by pressing: control + c

Requesting the contents of a webpage:

curl google.com

curl http://www.google.com

curl https://raw.githubusercontent.com/prof-rossetti/intro-to-python/master/data/products.json