Typing Unix Commands

Last modified: Aug 27, 2023
Contents:

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:

  1. 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.

  2. 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 commands

    Each 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) and rmdir (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. So cp 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 replace q1 but writes a copy (named math.h) inside the directory q1.

  3. 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 commands

    Try 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.

  4. 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 commands
    ls ~
    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 Completion

Log 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 History

If 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:

Example 8: Try This: Copy and Paste
  1. 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.

  2. 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.

  3. 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.

2.3 Special Characters for Editing Your Command

We all make mistakes when typing. To correct them:

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.)