Command Line
What is the Command Line?
Everything in a Unix computer is a file. Every program it runs, every little toolbar, every document you own, and every little nuance about the way it runs, is a file. The command line, simply put, is a place where you can navigate through that file system and read/run those files. Within it, you type commands instead of clicking on buttons in a GUI. The command line can be very powerful, complex, and dangerous, but should be lauded and admired, not feared.
In general, these tutorials will take place on a Mac, because of their ease-of-use and unix-like filesystem. Following along with Linux will be very similar, but Windows might cause some problems (use at your own risk).
Mac users — Open the Terminal using Spotlight, or by going into your /Applications/Utilities
folder.
Linux users — You probably already know where your terminal is.
Windows users — Please download and open Babun.
Syntax
A command has three parts: the utility, the flags, and the arguments. The utility is required, and always comes first. The flags are usually optional, and can take both long (--flag
) and short (-f
) forms. The arguments are usually the last part of the command, and are generally required.
Try running this:
ls -l ~/Documents
In the above example, ls
is the utility. It runs a program that lists directory contents. The -l
is an optional flag that tells ls
to display all the file list in long format. ~/Documents
is the argument; it tells ls
to list the directory contents at Users/<username>/Desktop
(or equivalent on your OS).
You can learn how to use any command by typing man
in front of it (man
— for manual — is the utility, and the command you want to learn about is the argument. You can move up and down the documentation with your arrow keys, and exit by typing q
. Often, but not always, you can also learn about a program by using the flag --help
with no arguments, or even just by using the utility with no flags or arguments.
File systems
Every file on your computer is stored in a folder that is part of a larger tree. Let’s look at the following location:
/Users/<username>/Documents/helloWorld.txt
Replace <username>
with your login’s username.
The first /
is the root of that tree. It is where every file and folder on the computer is stored. Go ahead and see for yourself by typing the command cd /
to change your directory to the root /
, and see what’s there by using ls
.
The next part, Users/
, is a folder within /
. Go ahead and cd
into it.
On my computer, <username>/
is a folder within /Users
, and Documents/
is a folder within <username>/
.
Finally, a text file called helloWorld.txt
is within my Documents/
folder.
You can cd
back up a level by doing cd ../
, and can go up multiple levels by chaining those ../
s. Finally, the shortcut for /Users/<username>/
is ~/
— remember that; ~
(tilde) is your home (directory).
Basic Utilities
Below is a list of basic utilities that most people use regularly. Anything in caps with a $
in front of it is a variable that should be replaced with your specific needs.
man $UTIL
displays the manual for the $UTIL.
ls $DIR
lists the contents of the $DIR directory.
cd $DIR
changes directories to $DIR.
pwd
prints the location of the current (working) directory.
cat $FILE
prints the contents of the $FILE to the terminal.
cp $FILE $LOCATION
copies $FILE to $LOCATION.
mv $FILE $LOCATION
moves $FILE to $LOCATION.
rm $FILE
removes the $FILE (deletes it).
$CMD1 | $CMD2
pipes (|
) the output of $CMD1 into the argument of #CMD2.
sudo $CMD
runs the $CMD as the super user (super user do).
sudo
can be dangerous, so only use it if you know what the command you’re elevating is going to do. You can delete the entirety of your computer’s hard drive by running sudo rm -rf
at the root directory (WARNING: DO NOT DO THAT!!).
APIs
Application Program Interfaces are a set of rules dictating how applications should interact. When discussing APIs in the contet of the web, we are usually referring to the process we use to post information to a website or database or pull information from a website or database.
JSON
JavaScript Object Notation is a data format used extensively on the web. It was developed for easy parsing within JavaScipt. Many of the APIs we will be using will return their data in JSON format.
- JSON is written in name|value pairs
"name":"Jake"
- Array:
1 2 3 4 |
{ "employees":[ "John", "Anna", "Peter" ] } |
- Objects can have several sub-object:
1 2 3 4 |
{ "employee":{ "name":"John", "age":30, "city":"New York" } } |
- An object with objects and an array:
1 2 3 4 5 6 7 |
{ "name":"jake", "age" : 34, "friends" : [ "jim", "eric", "katie"], "day" : "wed" } |
Example JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
{ created_at: 'Wed Feb 08 21:37:22 +0000 2017', id: 829444033101586400, id_str: '829444033101586440', text: 'In a Rare Zoo Escape, Sunny the Red Panda Is Still at Large https://t.co/mhOT4z8NIu https://t.co/8vHtTlqbmQ', display_text_range: [ 0, 83 ], source: '<a href="http://publicize.wp.com/" rel="nofollow">WordPress.com</a>', user: { id: 972385555, id_str: '972385555', name: 'Walkileaks', screen_name: 'walkileaks', location: 'United States', url: 'http://www.walkileaks.com', protected: false, utc_offset: -28800, is_translator: false, notifications: null }, geo: null, coordinates: null, place: null, contributors: null, is_quote_status: false, retweet_count: 0, favorite_count: 0, entities: { hashtags: [], urls: [ [Object] ], user_mentions: [], symbols: [], media: [ [Object] ] }, extended_entities: { media: [ [Object] ] }, timestamp_ms: '1486589842473' } |
Example
- In your terminal, change to your home directory
cd ~
- Make a new directory called “test_api”
mkdir test_api
- Go into that directory
cd test_api
- Initialize Node Package Manager in that directory
npm init
- Follow the prompts to get it working
-
Open your text and save a new file called test_api.js in the test_api folder
-
paste this code into the new file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//https://github.com/toddmotto/public-apis var request = require("request"); var url = "http://api.icndb.com/jokes/random/" request(url, function(error, response, body) { //turn the response into something we can use var info = JSON.parse(body); //extract the joke var joke = info["value"]["joke"]; //print the joke console.log("the joke: " + joke); }) |
- We need to know what the JSON will look like before we know how to format it.
- use the this jsonformatter site to check out your request
Exercise
- Find a new API from this list of free, public APIs
- Make a new node.js script that fetches data from the API and parses it