This is a quick tutorial on how to use the Terminal application and get around a computer using the command line. The tutorial also applies to terminals (or xterms) in the X-windows environment on Unix/Linux/MacOS or Cygwin (X-window emulator on Windows).
Open up the Terminal application, which is a text-based method of using your computer.
A terminal is simply a text-based interface to the computer. In a terminal, you can type commands, manipulate files, execute programs, and open documents. When working in a terminal, the current directory is called your working directory. A terminal will usually start in the top-level directory of your account.
Directories are separated by a backslash /. The topmost directory is indicated by a single backslash. The total directory tree is all relative to the top level directory.
On MacOS, there is a Volumes directory at the top that lists all mounted drives (networked or local). So if you have mounted your network directory, the path to it will look like the following if you substitute your username for mine.
/Volumes/Personal/bmaxwell
In a terminal, you generally type a command and any modifiers or arguments the command requires, then hit return and the command is executed. For example, to set your current (working) directory to your home directory, type cd and the prompt and hit return. Below is an example of a terminal and some commands.
Note that pretty much all of these things you can do using the Finder on a mac or the desktop on Windows. Once you learn the command set, however, you may find that certain actions are much faster using a terminal (opening or deleting all files in the current directory that end with .png, for example). As you begin to use the commands, relate then to actions you already know how to do.
Some common useful terminal commands include the following.
- cd <directory> - change directory. If you use the command with no arguments, it changes the directory back to your top-level home directory. The current directory is specified as . and the parent directory of the current working directory is specified using ..
- cd .. - go up one directory in the file tree.
- ls - lists the files in the current directory
- pwd - tells you the complete pathname of the current directory
- mv <from> <to> - move a file, including just renaming it in the current directory. The mv command will remove the file in the old location after copying it to the new location.
- cp <from> <to> - copy a file to a new location or name
- mkdir <directory name> - make a directory
- rmdir <directory name> - remove a directory (the directory must be empty)
- less <filename> - scroll through a file
- cat <filename> - send the file to standard output
- echo "a string" write the text within the string to standard output
- touch <filename> touch either updates the modification date on a file or creates an empty file if the named file does not exist. This can be useful in various situations, like when you are learning to create, rename, and delete files using a terminal.
-
Piping output: you can send anything sent to the standard output by
a program into another program by using the vertical slash character
|
echo "print 'you are a smart student'" | python
-
Directing output: you can send stuff to a file by using the greater than symbol >
echo "print 'you are a smarter student'" > smart.py
-
Wildcard characters: the star character * is a wildcard
character. If you want to see all the files in a directory that
start with A, you can type:
ls A*
A possibly bad thing to do is to type rm *
-
Tab completion: when you have typed part of the name of a file or
program, hitting the tab key will complete the filename as far as
possible while the choice is unique. For example, if you have only
one file that starts with the letter b, then typing b and then the
tab key will complete the filename. Learning to use the tab key can
save you a lot of time. For example, if I type
/V<tab>P<tab>bma>tab< then I can type the whole
string /Volumes/Personal/bmaxwell in 9 keystrokes.
If you have two or more files that start with the same letter or initial sequence, tab completion will beep at you. If you hit tab again, then the terminal will show you the options (all the files that start with the keystrokes you've typed so far).
- Hitting return - you don't have to be at the end of the line to hit return. If you go back and edit something in a terminal command, you can hit return and execute the whole line no matter where the cursor is located. Try it.
- You can get to the beginning of a command line using cntl-a, and get to the end using cntl-e. You can go forward a character using cntl-f and back a character using cntl-b. These are good shortcuts to know because it means you can move your cursor around while leaving your hands in the standard typing position. (I also tend to swap my caps lock and my control key so that my pinky doesn't have to stretch as much to get to the control key.
Useful Text Editors
-
nano/pico - nano (or pico on some systems) is a very simple
text editor that works in a terminal. All of the commands use the
control key and are listed at the bottom of the screen. To open up a
file, just type the name of the editor followed by the name of the
file.
nano <filename>
Open up the file smart.py and add a second print statement. Then quit nano and run the file by using:
cat smart.py | python
Alternatively, you can run the file by calling python with the file as its argument.
-
emacs - emacs is the battery-powered swiss-army knife with
optional nuclear reactor of the editor world
(emacs humor).
By default, if you type something like:
emacs myfile.c
then emacs will open up in a new window with a blank file called myfile.c. One nice thing about emacs is that it knows a lot about C and will do syntax highlighting, automatic indenting, and other functions (like most IDEs). If you open emacs using the nw flag
emacs -nw myfile.c
then emacs will not create a new window, but will start up in the terminal itself.
If you open emacs, the opening screen tells you how to access a tutorial. The most important thing to know is how to save and get out. To save, hold the control key down and type x then s (C-x C-s). It will ask you for a filename (and give you a default if you opened a file) and then save it. To exit, hold down the control key and type x then c (C-x C-c).
- BBEdit - BBEdit is a very nice GUI editor designed for writing code and html (web pages). Colby has a license for it.
- JEdit - a java-based editor that is cross-platform and free. You can download it from jedit.org.
python smart.py