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
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...
Print the working directory aka the current location
List the contents of the working dir
List the contents of the working dir, including hidden files
Change directory up one level
Change to the user home directory
Change to the previous directory you were in
Make a directory
Updates the timestamp for a file or creates an empty file
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 the current location in Explorer (OSX)
Open a file using the default program for that file type (OSX)
Open a file using a specific program(OSX)
open -a TextEdit file
Open a text file in vim (simple commandline text editor)
- 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)
Some fun examples:
What's the date?
Who runs a website?
Reverse all text inside a text file:
cat input.txt | rev > output.text
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
- twurl, a curl for the Twitter API