“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:
- On Mac OS, the default Terminal application will allow students to use unix-style commands.
- On Windows OS, the default Command Prompt application uses different commands, but installing the Git Bash application will allow students to use unix-style commands.
Additional References:
Walkthrough Video
Instructions
Open the Terminal application (Mac) or the Git Bash application (Windows).
After typing each of the commands below, press “enter” to execute it.
NOTE: You can copy and paste the commands, however on Git Bash you will need to right click to copy or paste, as keyboard shortcuts like “control + c” or “control + v” don’t work on Git Bash.
NOTE: You can optionally clear previous terminal output at any time by pressing “command + k” (Mac), or by typing
clearand pressing “enter”.
Current User
Display the current user’s name:
whoamiPresent Working Directory
Display the current/present working directory:
pwdListing Files in a Directory
List files in the current working directory:
lsAlternatively, list files using a different display, including file permissions and hidden files:
ls -alManaging Files
Setup a new directory in which to add some files, and navigate into that directory:
mkdir my_folder
cd my_folderCreate one or more files in the new directory you just created:
touch README.md
touch index.html
touch my_data.csv
touch my_message.txtRemove a file:
rm index.htmlOpen a file in a text editor like VS Code (then edit it, and save it):
code my_message.txtNOTE: Mac users may need to first configure the
codecommand by following these VS Code shell command setup instructions
Display file contents:
cat my_message.txtMove a file:
mv ~/Desktop/my_folder/my_message.txt ~/DesktopFYI: 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_folderCopy 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 textFurther 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 + cTiming the duration of a network request:
ping google.com
# ... stop after a few seconds if necessary by pressing: control + cRequesting 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