Difference between revisions of "Introduction to the Command Line"
(Start transcribing command line introduction page.) |
(Continue transcribing introduction to command line.) |
||
Line 61: | Line 61: | ||
:[[Image:Windows-icons.jpg|256px]] [[Image:Windows-thumbnails.jpg|256px]] [[Image:Mac-folders.png|320px]] | :[[Image:Windows-icons.jpg|256px]] [[Image:Windows-thumbnails.jpg|256px]] [[Image:Mac-folders.png|320px]] | ||
+ | |||
+ | On a command line, the folders displayed by the windows above are expressed, respectively, as: | ||
+ | /WINDOWS | ||
+ | /Users/Public/Pictures/Sample Pictures | ||
+ | /Users/dondi/Documents | ||
+ | (some operating systems hide the ''Users'' folder from the window, and that’s why you might not see ''Users'' in the screenshot) | ||
+ | |||
+ | The way to read a path is to separate out the slash ('''/''') characters; starting from the left, each slash marks off a folder that is inside another one. The very last item after the last slash represents the actual file or folder that is indicated by the path. Thus, you would interpret a path like: | ||
+ | /home/dondi/public_html | ||
+ | To actually mean the ''public_html'' file or folder inside the ''dondi'' folder inside the ''home'' folder, which in turn sits at the "top" or "root" of the file system—that is, it's a folder that isn't inside any other folder. As a tree (or an upside-down tree for biologists, since the “root” is at the top!), the path above looks like this: | ||
+ | / (root) | ||
+ | | | ||
+ | +-home | ||
+ | | | ||
+ | +-dondi | ||
+ | | | ||
+ | +-public_html | ||
+ | * If there is a folder called ''data'' within the folder above, its path would be: | ||
+ | /home/dondi/public_html/data | ||
+ | * If there is a file called ''prokaryote.txt'' within the above ''data'' folder, then its path would be: | ||
+ | /home/dondi/public_html/data/prokaryote.txt | ||
+ | |||
+ | === Your “Working” Directory: pwd === | ||
+ | |||
+ | On everyday computers, we frequently see a window—typically when we're trying to save our work or browse our desktop—that gives us a concept of “where” we are among all of our files. While we don’t have such a window on the command line, the computer ''does'' keep track of what folder we’re ''in''. This folder is called the ''working directory''. | ||
+ | |||
+ | :'''Terminology alert:''' The word ''directory'' is a synonym for what everyday computers call a ''folder''. They mean the same thing—a folder or directory is an entity on the computer that keeps files, some of which may themselves be more folders or directories. For this write-up, ''folder'' and ''directory'' will be used interchangeably, to get you accustomed to seeing both words. | ||
+ | |||
+ | If, at any time, you forget "where" you are in your files, this command will display your working directory: | ||
+ | pwd | ||
+ | You'll notice that, because commands involve a lot of typing, they tend to be brief or even abbreviated. While this makes them harder to remember, they still resemble what they mean to do; for example, '''pwd''' stands for '''print working directory'''—which is exactly what that command does. | ||
+ | |||
+ | === Your “Home” Directory === | ||
+ | |||
+ | When you connect to a command line for the first time, the working directory typically starts at your user account’s ''home directory'' for that command line. This is the folder in which your user account is allowed to create files and otherwise do all kinds of other work. Most computers today have this concept; on Windows computers, your home directory is typically displayed as a folder icon with a name like ''Joe's Documents''. On macOS computers, the home directory is typically displayed as a house icon with the same name as your login. Mobile devices don’t typically show files as much as personal computers do, but they’re there, with an implicit home directory for the mobile user. | ||
+ | |||
+ | :'''Terminology alert:''' The word ''invoke'' is commonly used to mean “make the computer perform a command,” in this case by typing the command and hitting the '''enter''' or '''return''' key. | ||
+ | |||
+ | === Getting Around === | ||
+ | |||
+ | On everyday computers, you move from folder to folder by clicking or double-clicking the folder icon that you like. Some systems have a “folder up” button that you can use to move to the folder that contains your current folder. | ||
+ | |||
+ | The command line has similar commands, with the difference being that they are typed instead of invoked with a mouse. | ||
+ | |||
+ | ==== List Files: ls -F ==== | ||
+ | |||
+ | To ''see'' the files inside your current folder (i.e., your working directory), type this: | ||
+ | ls -F | ||
+ | Here, think of '''ls -F''' as “list files.” While the exact contents of your home directory may vary, '''ls -F''' typically produces something like this: | ||
+ | Desktop/ Downloads/ Pictures/ public_html/ Templates/ | ||
+ | Documents/ Music/ Public/ sandbox.txt Videos/ | ||
+ | Each name is a file inside your folder—so it’s really a lot like the screenshots shown before, but without icons. Since we don’t have icons, how can we tell if a name represents another folder? That is indicated by the slash ('''/''') at the end of the name. In the listing above, every entry except for ''sandbox.txt'' is a folder. | ||
+ | |||
+ | ==== Change Directory: cd ==== | ||
+ | |||
+ | Let’s say that you want to change your working directory to something else. The command for this is: | ||
+ | cd <new directory> | ||
+ | To remember this, '''cd''' stands for ''change directory''. The ''<new directory>'' part in the command above is the name of the directory that you want to change ''to''. Thus, if you want to “go” to the ''public_html'' directory/folder shown previously, you would type: | ||
+ | cd public_html | ||
+ | After doing this, type: | ||
+ | pwd | ||
+ | ...to convince yourself that you did successfully go to the new directory; type: | ||
+ | ls -F | ||
+ | ...to see what's in there (it might be empty). | ||
+ | |||
+ | What about that "folder up" button? That is indicated by the special name '''..''' (two periods one after the other). To “go up” a directory, type: | ||
+ | cd .. | ||
+ | If you then type: | ||
+ | pwd | ||
+ | You should be back in the folder that contained ''public_html''. | ||
+ | |||
+ | A few shortcuts are available when the directory you want is someone (or your own) home directory. | ||
+ | * If you type '''cd''' by itself: <pre>cd</pre> ...you will “teleport” back to your home directory, no matter where you are. | ||
+ | * If you type '''cd''' followed by '''~<username>''', where you substitute '''<username>''' for another account in the system: <pre>cd ~dondi</pre> ...you will “teleport” to that user’s home directory, no matter where you are. '''~<username>''' (that first “squiggly” symbol is called a ''tilde'') is the aforementioned shorthand for expressing a user’s home directory. | ||
+ | |||
{{Under Construction}} | {{Under Construction}} |
Revision as of 00:19, 11 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.
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:
On a command line, the folders displayed by the windows above are expressed, respectively, as:
/WINDOWS /Users/Public/Pictures/Sample Pictures /Users/dondi/Documents
(some operating systems hide the Users folder from the window, and that’s why you might not see Users in the screenshot)
The way to read a path is to separate out the slash (/) characters; starting from the left, each slash marks off a folder that is inside another one. The very last item after the last slash represents the actual file or folder that is indicated by the path. Thus, you would interpret a path like:
/home/dondi/public_html
To actually mean the public_html file or folder inside the dondi folder inside the home folder, which in turn sits at the "top" or "root" of the file system—that is, it's a folder that isn't inside any other folder. As a tree (or an upside-down tree for biologists, since the “root” is at the top!), the path above looks like this:
/ (root) | +-home | +-dondi | +-public_html
- If there is a folder called data within the folder above, its path would be:
/home/dondi/public_html/data
- If there is a file called prokaryote.txt within the above data folder, then its path would be:
/home/dondi/public_html/data/prokaryote.txt
Your “Working” Directory: pwd
On everyday computers, we frequently see a window—typically when we're trying to save our work or browse our desktop—that gives us a concept of “where” we are among all of our files. While we don’t have such a window on the command line, the computer does keep track of what folder we’re in. This folder is called the working directory.
- Terminology alert: The word directory is a synonym for what everyday computers call a folder. They mean the same thing—a folder or directory is an entity on the computer that keeps files, some of which may themselves be more folders or directories. For this write-up, folder and directory will be used interchangeably, to get you accustomed to seeing both words.
If, at any time, you forget "where" you are in your files, this command will display your working directory:
pwd
You'll notice that, because commands involve a lot of typing, they tend to be brief or even abbreviated. While this makes them harder to remember, they still resemble what they mean to do; for example, pwd stands for print working directory—which is exactly what that command does.
Your “Home” Directory
When you connect to a command line for the first time, the working directory typically starts at your user account’s home directory for that command line. This is the folder in which your user account is allowed to create files and otherwise do all kinds of other work. Most computers today have this concept; on Windows computers, your home directory is typically displayed as a folder icon with a name like Joe's Documents. On macOS computers, the home directory is typically displayed as a house icon with the same name as your login. Mobile devices don’t typically show files as much as personal computers do, but they’re there, with an implicit home directory for the mobile user.
- Terminology alert: The word invoke is commonly used to mean “make the computer perform a command,” in this case by typing the command and hitting the enter or return key.
Getting Around
On everyday computers, you move from folder to folder by clicking or double-clicking the folder icon that you like. Some systems have a “folder up” button that you can use to move to the folder that contains your current folder.
The command line has similar commands, with the difference being that they are typed instead of invoked with a mouse.
List Files: ls -F
To see the files inside your current folder (i.e., your working directory), type this:
ls -F
Here, think of ls -F as “list files.” While the exact contents of your home directory may vary, ls -F typically produces something like this:
Desktop/ Downloads/ Pictures/ public_html/ Templates/ Documents/ Music/ Public/ sandbox.txt Videos/
Each name is a file inside your folder—so it’s really a lot like the screenshots shown before, but without icons. Since we don’t have icons, how can we tell if a name represents another folder? That is indicated by the slash (/) at the end of the name. In the listing above, every entry except for sandbox.txt is a folder.
Change Directory: cd
Let’s say that you want to change your working directory to something else. The command for this is:
cd <new directory>
To remember this, cd stands for change directory. The <new directory> part in the command above is the name of the directory that you want to change to. Thus, if you want to “go” to the public_html directory/folder shown previously, you would type:
cd public_html
After doing this, type:
pwd
...to convince yourself that you did successfully go to the new directory; type:
ls -F
...to see what's in there (it might be empty).
What about that "folder up" button? That is indicated by the special name .. (two periods one after the other). To “go up” a directory, type:
cd ..
If you then type:
pwd
You should be back in the folder that contained public_html.
A few shortcuts are available when the directory you want is someone (or your own) home directory.
- If you type cd by itself:
cd
...you will “teleport” back to your home directory, no matter where you are. - If you type cd followed by ~<username>, where you substitute <username> for another account in the system:
cd ~dondi
...you will “teleport” to that user’s home directory, no matter where you are. ~<username> (that first “squiggly” symbol is called a tilde) is the aforementioned shorthand for expressing a user’s home directory.