Commandline Intro
The commandline is a magical place of wizards & wizardry. It's talking to a computer where special incantations make "things happen." It's also a deep, dungeon full of beasties and evil powers that require great responsibility! All is not so scary though, if you are willing to dig into it's arcane mysteries you find a powerful way to work with your computer.
A Little History
The origins of the commandline are traced back to the Teletype, a mechanical typewriter that could send and receive text over (originally) telegraph & (later) telephone lines. You could record keystrokes to a paper tape and then play them back while transmitting to another terminal or you could send information live, although this would take much longer since the keys were hard to press!
Pushing the keys was good exercise; the keys traveled a good half inch before making contact, and resistance was considerable. - Frank da Cruz, Columbia University Computing History
Each key & associated character had a simple number of electrical pulses representing it which became the origin for the ASCII character standard. The Bell, Line Feed, & Carriage Return characters owe their origin to the mechanical nature of the original Teletypes.
When digital computers were being developed, it was natural to use the existing Teletype interface as a way to talk to a computer. Many of the first commercially available mainframes came with Teletype terminals. In the image above, Dennis Ritchie, the inventor of Unix, is using a Teletype with a DEC PDP 11 mainframe.
As computers became more powerful and started to support multiple people using them simultaneously, the Teletype terminal evolved into the mainframe terminal: a keyboard with a CRT display instead of the typewriter for output. This form factor then evolved into the familiar computer of today.
The commandline itself may have been supplanted by the GUI (Graphical User Interface), but it still exists on all major desktop operating systems in the form of "terminal emulators", ie. programs that create a virtual representation of the original mainframe terminals. For the major operating systems, the following programs fill this role:
- Windows: DOS prompt, Powershell
- OSX: Terminal.app
- Linux: xterm, Konsole, Gnome Terminal/VTE, etc
Basic Idea
The commandline is essentially a text-driven interface for computing. You have a conversation with the computer by telling it to do something and then wait for a reply.
This communication takes the form:
program [options] arguments...
Cheat Sheet
Print the working directory aka the current location
pwd
List the contents of the working dir
ls
List the contents of the working dir, including hidden files
ls -a
Change directory
cd
Change directory up one level
cd ../
Change to the user home directory
cd ~/
Change to the previous directory you were in
cd -
Make a directory
mkdir newdir
Updates the timestamp for a file or creates an empty file
touch newfile
Copy a file from one location to another
cp old/path/file new/path
Copy a folder & all it's contents from one location to another
cp -R old/path new/path
Move a file from one place to another
mv old/path/file new/path
Rename a file (move in place)
mv file newfile
Open the current location in Finder (OSX)
open .
Open the current location in Explorer (OSX)
explorer.exe .
Open a file using the default program for that file type (OSX)
open file
Open a file using a specific program(OSX)
open -a TextEdit file
Open a text file in vim (simple commandline text editor)
vim somefile.txt
vim commands:
- ESC - exit INSERT mode
i
- enter INSERT mode:q
- to quit (when not in INSERT mode):q!
- to quit and ignore any changes made (when not in INSERT mode):w
- to write any changes made (when not in INSERT mode):wq
- to write any changes made and quit (when not in INSERT mode)
Examples
Some fun examples:
What's the date?
date
Who runs a website?
whois http://somewebsite.com
Reverse all text inside a text file:
cat input.txt | rev > output.text
Speak any webpage (grab with curl, strip html using sed with a regex, & speak with OSX say command):
curl http://link/to/some/webpage | sed 's/<[^>]*>//g' | say
Connect to a remote telnet server (see a list of some interesting servers here:
# ascii star wars
telnet towel.blinkenlights.nl
Useful Stuff
- twurl, a curl for the Twitter API