Difference between revisions of "Introduction to the Command Line"
(Start transcribing command line introduction page.) |
(No difference)
|
Revision as of 08:33, 10 September 2017
- For much of the computer work in this course, we'll be using a computer interaction style that's very different from what's familiar to most of us: the command line.
- The reason for this is that the command line actually offers a surprising wealth of text processing tools, some of which are more powerful than the applications that we're accustomed to using.
- It is clear, however, that one needs a lot more "up-front" training with the command line, and that's what this page hopes to provide.
- To put things in context—everything “old” becomes new again: using the command line is ultimately a lot like “talking” to current digital assistants like Alexa, Cortana, and Siri, but with typing and reading instead of talking and listening.
Contents
Command Line Basics
Working with a command line is a cycle (i.e., a loop) of:
- The computer indicating that it is ready for the next command (via a prompt)
- The user (you) typing in a command
- While typing a command, you may use the arrow, backspace, and other keys to edit what you've typed so far
- To completely start over, hold down the control key then hit c (i.e., "control-c"); you'll go back to a fresh prompt
- When you're ready with your command, hit the Enter or Return key
- Many users experience some fear with typing a command—this is understandable, but rest assured:
- Typically, the worst thing that happens is that the computer did not "understand" the command, thus doing nothing
- While there are harmful commands, (a) the likelihood of your typing one at random is very low, and (b) most modern operating systems will prevent you from doing the really dangerous stuff anyway
- The computer performing the command, then showing you the result of that command
- "Rinse and repeat"
If all goes well, then with each command you type, you get closer to accomplishing your goal.
The Command History
To assist with this command entry cycle, modern command lines keeps track of the commands you type—essentially, a command history. This history shows up in a number of ways:
- If you press the up or down arrow keys at the command prompt, you will move back and forth through your history. If you see the command you'd like to perform, press Enter or Return, and the computer will try to perform that command
- You can do "variations" on past commands by immediately editing what shows up; that is, you can press the up or down arrow keys until you see a command that's similar to the one you want to do, then use the left/right arrow keys, backspace, and other keys to edit it
- The control-c shortcut is always there if you want to start over
“Autocomplete” with the Tab Key
While working on the command line, the Tab key (the one to the left of the Q key) provides a convenient “autocomplete” function. No matter where you are in your command, it can be a good idea to tap the Tab key if you're in the middle of typing something out.
- If the computer concludes that what you’ve typed in so far can mean only one thing, then it will “autocomplete” what you have by spelling everything out.
- If the computer concludes that what you’ve typed in so far can still mean multiple choices, the first Tab will do nothing (though on some systems you may hear a beep); hitting Tab a second time will show you the computer’s “guesses” as to what you might be typing.
- You can keep typing a few letters, then hitting Tab, until there is only one choice and the computer spells everything out for you. We’ll see instances of this later, in terms of the other commands.
In general, it’s a good idea to periodically type the Tab key, either when you can only remember the first few letters of a command or file name, or if you want to save some typing and know that you've typed in enough letters so that only one choice is available.
Your First Command: exit
The first command to try is:
exit
Typing this command ends your “command session” with the computer. Most of the time, this closes the window into which you’ve been typing your commands (typically called a “terminal”). Sometimes, you get a message that your session is finished, but you still need to close the window manually. In any case, exit means you’re done, and you can quit whatever program you were using to get to the command line (e.g., Terminal, PuTTY, etc.).
- Since we just mentioned the Tab key, try this—type only the first two letters of exit:
ex
- Now hit the Tab key twice. You should see a list of commands that start with ex, similar (but not identical) to this:
ex exec execsnoop exit expand expect export expr extcheck
- Now type the letter i:
exi
- If you hit the Tab key twice again, you’ll see that the list would be narrowed down just to commands that start with exi
- If there is just one command that matches what you’ve typed so far, the command line then “autocompletes” to that command—saving you some typing!
Of course, this might not be useful for a command as short as exit, but in any case this helps demonstrate the Tab key’s functionality.
Files and Folders on the Command Line
Before we move on into actual text processing commands, let’s look at some key concepts and commands for just “getting around” the files on a computer using the command line.
How Files and Folders Look on a Command Line
Just like with the computers we use everyday, we can access our files and folders on the command line. Unlike the computers that we use everyday, we don't see any icons, folders, or pictures; instead, command lines represent files and folders as text expressions called paths.
You've probably seen displays like the ones below on your computer. Click on them to see them full-sized: