Typing Unix Commands
To run a Unix command (or any program, for that matter), you normally must type the name of the command/program file followed by any arguments. There is actually a program running that accepts your keystrokes and launches the appropriate program. The program that reads and interprets your keystrokes is called the shell. There are many shells available, all of which offer different features. The default shell for ODU CS is called tcsh, and we’ll concentrate on that.
The command/program name is usually not given as a full file path. Instead, certain directories, such as /bin
, are automatically searched for a program of the appropriate name. This set of directories is referred to as your execution path. New accounts are set up so that the directories holding the most commonly used Unix commands and programs are already in the execution path.
Thus, one can invoke the ls
command as
/bin/ls
but it’s usually simpler to say
ls
1 Command Arguments
Of course, most Unix commands consist not only of the program name but also require one or more command arguments. The command arguments indicate the details of the desired operation, including files to work with, text to use, etc.
Command arguments tend to come in four varieties:
-
Files: If a command needs to operate on one or more files, then we specify those files by giving a path to the file.
As we have already discussed, file paths may be absolute or relative.
-
Directories: these are also specified using absolute or relative paths. In fact, a directory in Unix is also a file – it’s just a binary file whose special contents include a list of other files and their locations on a disk drive. This means that most Unix commands that expect a file can be given a directory as well. But in many cases that won’t do what we want.
Example 1: Try This: Directories versus files in commandsEach of the Try This exercises in this lesson gives you a set of commands that you should enter in a Linux session, carefully observing the results.
If you don’t understand the results that you get, you should ask (in the Forum or via email) before moving on to the assignments in this section.
Enter the following commands:
cd ~ more playing/math.h more playing
As you can see, trying to list the contents of a directory file is not useful.
There are a few commands that work only on directories.
mkdir
(create an empty directory) andrmdir
(remove a directory, if empty) are the most common.In other cases, commands may work with either files or directories, but have slightly different behaviors in the two cases. The
cp
command is a good example of this.Example 2: Try This: Directories versus files in commands (2)cd ~/playing cp stdio.h q1 ls cp math.h q1 ls rm q1 mkdir q1 cp math.h q1 ls ls q1
The three
cp
commands are almost identical.-
In the first case, we copied a file to a path, but the destination path named a file (
q1
) that did not actually exist. Socp
created a copy with that name. -
The second
cp
copies to a path (q1
) that now indicates an existing file.cp
removes that existing file and then creates a new copy with that name. -
The third
cp
, however, names an existing file that happens to be a directory. In this case,cp
does not replaceq1
but writes a copy (namedmath.h
) inside the directoryq1
.
-
-
Plain text: Sometimes commands take ordinary text as parameters. A simple example of that is the command
echo
, which simply prints whatever parameters it is given:Example 3: Try This: Directories versus files in commandsTry the following commands:
echo Hello world! who whoami echo whoami echo I am $USER
The
who
command is normally used to see who is currently logged in to the same machine as you. But the “ami” variant changes it to simply list your own session statistics.The
$USER
is an example of an environment variable, a special placeholder that contains information about your current session on the machine. Environment variables are easily recognized by the opening$
in their name. We’ll learn more about these in a later lesson.A more interesting example is the
grep
command, which searches files for lines containing desired text. For example,Example 4: Try This: Text arguments (searching)more /usr/include/math.h grep def /usr/include/math.h
The
grep
command lists all lines inside /usr/include/math.h containing the string “def”. We’ll shortly see ways to write more complex search patterns. -
Flags: these are special arguments used to alter or control the behavior of a command. Flags for Unix commands are usually written beginning with a “-” or occasionally “–”.
Example 5: Try This: Flag arguments in commandsls ~ ls -a ~ ls /usr/include ls -l /usr/include
Take careful note of how the behavior of the
ls
command is altered by the “-a” and “-l” flags.The “l” in “-l” stands for “long”, and provides a longer listing with more information about each file.
The “a” in “-a” stands for “all”. File names in Unix that begin with a “.” are hidden from normal listings. The “-a” option includes those normally hidden files in the
ls
output.
How do you know what kind of arguments can be accepted by each command? You pretty much have to deal with this on a individual basis, though for any specific command you can consult the on-line manual for that command via man
, e.g.
man ls
man grep
Many common Linux commands have a truly bewildering variety of possible flags. usually, however, there are a small handful of commonly useful ones. I will try to point those out as we go along and will include them in the Try This exercises.
In the assignments for this course, I will always make sure that any command paramaters and flags that you might need will have been covered and included in a Try This. Consequently, it’s generally a bad idea to go hunting up more obscure flags and options on the Internet or in the
man
pages for use in the assignments. It’s an unnecessary waste of your time, and may often distort the command outputs past the ability of my grading scripts to recognize what you are actually doing.Short version: If it’s not in the Lecture Notes and not something you practiced with in a Try This exercise, don’t use it.
2 Special Characters
Usually, the meaning of a character that you type is self-evident. If you type an “a”, it means a and you see the “a” appear on your screen. If you hold the shift key down and type “a”, it means A and you see that upper-case A appear on your screen.
But some characters that you type have a special meaning. The most obvious example would be the Enter or Return key, which sends a character that is interpreted to mean “I’m ending a line now”. Another example would be the $
, which, as we have seen in an earlier Try This, can introduce an environment variable.
This section describes some of the special characters that you may need to use when entering Linux commands. Most of these special characters are entered by holding down the “Control” or “Ctrl” key while typing a letter. By convention, we designate this by placing the symbol “^” in front of the name of the second key. For example, if you have typed zero or more letters of a filename and want to see a list of what filenames begin with what you have typed, you could type ^D, i.e., hold down the “Control” key and type “d”.
2.1 Saving Yourself Some Typing
Many people get turned off by Linux and, more generally, by working at the command line because they think that typing long file paths is tedious and error-prone. While there’s a certain amount of truth in that, experienced programmers generally don’t spend nearly as much effort typing out long commands as you might think. Instead, they take advantages of shortcuts offered by the command shell
2.1.1 Tab Completion
If you have entered the first few letters of a file name and hit the “Tab” key, the shell will examine what you have typed so far and attempt to complete the filename by filling in the remaining characters. If the shell is unable to complete the filename, a bell or beep sound will be given. Even in this case, the shell will fill in as many characters as it can.
Example 6: Try This: Tab CompletionLog in to a Linux server and enter
ls ~/pla
but do not hit the Enter key to end the line. Instead, type the Tab key, wait to see what happens, then hit Enter.
Now enter the following, using the Tab key where I write
<Tab>
and the Enter/Return key where I write<Enter>
. Pause a moment after each<Tab>
to observe what happens.ls ~/pla<Tab>g<Tab><Enter>
Not so bad, now, is it?
This all works nicely if you have entered enough characters to uniquely identify your file or directory name. What if you make a mistake? Try
ls ~/pla<Tab>x<Tab>
Because you do not have a file or directory name in
~/playing
that starts with an ‘x’, this fails and no new characters are added by the shell. Depending on what ssh client you are running and what your settings are, you might hear a bell sound that signals that Tab completion was attempted but could not fill in the remainder.What if there’s more than one file that matches what you have typed so far?
Hit Enter to finish the previous command and try this:
ls ~/pla<Tab>m<Tab>
The Tab after the ‘m’ fills in an ‘a’, giving you “ma”. Theat’s actually not a complete file name.
Hit Tab again. Nothing happens. That’s because there are actually two files in
~/playing/
whose names begin with “ma”.Hit Tab a third time. This time the command shell decides that you need some help and lists the files in
~/playing
whose names begin with “ma”. Pick one of those by typing the third letter in its name and hitting Tab again, and the entire file name gets filled in.
2.1.2 Repeating Prior Commands
Each time you enter a command, the command shell remembers the last several commands that you entered in a “history”.
You can review those commands, moving backwards in time by typing ^P
(hold the Ctrl key and type ‘p’) or, in most cases, the up arrow on your keyboard. Once you have moved a few steps back in time, you can move forward to more recent commands by typing ^N or the down arrow.
You can remember the ‘P’ and ‘N’ in these as “Previous” and “Next”.
Example 7: Try This: Command HistoryIf you are still logged in to a Linux server, log out (
exit
) and then log back in again.Type a few ^P characters. The commands that you see should look familiar.
- It’s interesting to see that the command history has been retained across separate login sessions.
Try your up arrow key and see if it does the same.
Try a few ^N or down arrow keys to reverse the progress.
Stop at any of your recent “ls” commands (from the previous Try This) and hit Enter. The old command from your history will be repeated.
2.1.3 Copy and Paste
You can copy and paste to and from an ssh session much like you would copy and paste in a word processor.
However, Unix was doing this long before it was a common word processor step, before the term “copy and paste” was coined to describe the operation. Consequently the now-almost-universal key sequences ^C and ^V are not used for copying and pasting in Linux. ^C, in particular, already had a long history of use as a “Cancel” command.
Unlike the completion and history commands we have been discussing,, the copy and paste functionality is provided by the ssh client or other terminal window that you are running. So exactly how you do copying and pasting depends on what you are running as your ssh client on your local machine:
Windows (CMD or Powershell)
To copy text in Windows cmd
or Powershell windows, left-click and drag your mouse across the text to be copied. * You can use keyboard shortcuts to copy and paste, but not the Ctrl-c and Ctrl-v that are standard in other applications. * Use Ctrl-Insert to copy that text to the clipboard. * Use Shift-Insert to paste the clipboard into your command line. * Alternatively, click on the small icon in the upper left of the window to drop down the window control menu. Look for “Edit
” – selecting that will give you an option to copy the text to the clipboard or to paste from the clipboard.
Everything Else
To copy text from CygWin, xterm, or from a Linux or MacOS terminal, just left-click and drag your mouse across text to select it. Your selected text is automatically copied to the clipboard, and you can paste it into an e-mail message or Forum post.
To paste text into one of these programs, try:
- clicking the middle mouse button, if you have one.
- The scroll wheel on many mice fills in for a middle mouse button.
- Some of these programs will allow you to simulate a middle click if you click both the left and right mouse buttons simultaneously.
- Hold the shift key and type the “Insert” button.
Example 8: Try This: Copy and Paste
In a Linux session, give the following command by copying it from this web page and pasting it into your ssh session:
ls -ld /usr/include/net
Hit Enter to run the command.
Then copy, from your ssh session, the portion of that command up to the third ‘/’, and paste that back into your ssh session, then hit Enter to run that abbreviated command.
Now, open your favorite email program on your local machine. Start a new message to yourself. In the formatting options for the message body, look for a paragraph style “pre-formatted” or “Fixed width” and select that.
Copy the entire visible text of your ssh session.
Paste it into your email message and send it.
2.2 Special Characters that Say “I’m Done”
Of course, by now you are used to using the Enter key to indicate that you are done entering a line.
-
^D is used when a command/program is reading many lines of input from the keyboard and you want to signal the end of the input, i.e., that you are done entering lines of text to that command.
-
^C is used to “Cancel” or abort a program/command that is running too long or working incorrectly. Beware: aborting a program that is updating a file may leave garbage in that file.
You may think that Unix is being stubborn in not using ^C to mean “copy” as is done in Windows, but the “Cancel” meaning of ^C actually dates back to the definition of the ASCII character set and was a well-established standard long before Unix existed.
-
In many programs, ^Z pauses the program and returns you temporarily to the shell. To return to the paused program, give the command:
fg
2.3 Special Characters for Editing Your Command
We all make mistakes when typing. To correct them:
-
^H, the “Backspace” key, and the “Delete” key all delete the most recently typed character.
-
^B moves the cursor Backwards over what you have just typed, without deleting those characters. This is useful in correcting typing mistakes. The “left” arrow on your keyboard may also do the same thing.
-
^F moves the cursor Forwards over what you have just typed, without deleting those characters. The “right” arrow on your keyboard may also do the same thing.
These can, by the way, are often combined with the History special characters discussed earlier to produce new commands that are similar to, but just slightly different from, earlier commands. For example, if I had a command that I wanted applied to file1.dat
, file2.dat
, … file5.dat
, one way to handle it would be to type out the command for file1.dat
and run it, then use ^P to retrieve that command from history, ^B to move back to the ‘1’, Backspace or Delete to remove the ‘1’, then type ‘2’ in its place and hit Enter. I could repeat this for each of the files I wanted. (For handling very long lists of files, we’ll see better strategies when we cover “scripting” in later lessons.)