Skip to content

Instantly share code, notes, and snippets.

@s2t2
Last active February 15, 2017 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2t2/2b4b055d666e63b9475b9e205ee8ce1b to your computer and use it in GitHub Desktop.
Save s2t2/2b4b055d666e63b9475b9e205ee8ce1b to your computer and use it in GitHub Desktop.

Command-line Computing

Use the command line to manipulate the local filesystem.

Objectives

  1. Familiarize yourself with the command line.

Prerequisites

  1. Obtain access to a personal computer with either Windows or Mac OS.

Instructions

See the following operating system-specific guides in separate files, below.


adapted from source

Terminal (Mac OS)

Open the Terminal application.

Current User

Print the current user's name.

whoami

Present Working Directory

Print the current/present working directory.

pwd

List Files in a Directory

List files in the current working directory.

ls
ls -al # for a different display

Navigate and Manage Directories

Change directories (specifying absolute file path).

cd ~/Desktop

Make a new directory.

mkdir my_folder

Remove a directory.

rm my_folder # triggers an error
rm -rf my_folder # recursively (-r) forces (-f) removal

Manage Files

Change directories (using relative file path).

cd my_folder # first re-create this directory if it doesn't exist, else this will trigger an error

Create a file.

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

Remove a file.

rm index.html

Edit and save a file, using a text editor like notepad.

Print file contents.

cat my_message.txt

Move a file.

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

Copy a file.

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

Copy contents of a file into the clipboard for pasting.

pbcopy < ~/Desktop/my_folder/my_message.txt
# ... then just paste as you normally would after copying some text

Further Exploration

There are many other utilities to use from the command-line.

Make your computer speak:

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

Internet Computing

Trace the route traveled by a network request:

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

Time the duration of a network request:

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

Request the contents of a webpage:

curl google.com
curl http://www.google.com
curl http://hypem.com/playlist/popular/3day/json/1/data.js
curl https://raw.githubusercontent.com/debate-watch/twenty_sixteen/master/lib/twenty_sixteen/candidates.json

Command Prompt (Windows OS)

Open the Command Prompt application.

Current User

Print the current user's name.

whoami

Present Working Directory

Print the current/present working directory.

cd

List Files in a Directory

List files in the current working directory.

dir

Navigate and Manage Directories

Change directories (specifying absolute file path).

cd C:\Users\YOUR_USERNAME\Desktop\ # where YOUR_USERNAME is the name of the user currently operating your local machine

Make a new directory.

mkdir my_folder

Remove a directory.

rmdir my_folder

Manage Files

Change directories (using relative file path).

cd my_folder # first re-create this directory if it doesn't exist, else this will trigger an error

Create a file.

type nul > README.md
type nul > index.html
type nul > my_data.csv
type nul > my_message.txt

Remove/delete a file.

del index.html

Edit and save a file, using a text editor like notepad or notepad++.

Print file contents.

type my_message.txt

Move a file to target location.

move C:\Users\YOUR_USERNAME\Desktop\my_folder\my_message.txt C:\Users\YOUR_USERNAME\Desktop

Copy a file.

xcopy C:\Users\YOUR_USERNAME\Desktop\my_message.txt C:\Users\YOUR_USERNAME\Desktop\my_folder

Copy contents of a file into the clipboard for pasting.

type C:\Users\YOUR_USERNAME\Desktop\my_folder\my_message.txt | clip
# ... then just paste as you normally would after copying some text

Further Exploration

Internet Computing

Trace the route traveled by a network request:

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

Time the duration of a network request:

ping google.com # stop after a few seconds if necessary by pressing: control + c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment