Academic Computing Services * Simon Fraser University

HOW TO

Record, Reissue and Rename Unix Commands

© January 27, 1994 B-12



Often when using Unix you will repeat a series of commands or need to correct a mistyped command. This how-to describes several Unix shortcuts to reduce the amount of typing necessary, and also tells how to make a transcript of your entire Unix session.

Saving keystrokes when typing file & directory names

When typing a command, if you have typed enough letters of a file or directory name to uniquely identify it, press the Esc key and Unix will fill in the rest of the name.

Recording and reissuing commands

The history mechanism

One way to reduce typing is to use the history mechanism, available in the C shell. (For an explanation of Unix shells, see how-to B-14, Customize Your Unix Environment.) The C shell can keep a list of past commands you have entered if it is instructed to do so. The command

set history=100

tells the C shell to begin keeping a record of commands entered, but to remember only the last 100 commands. ACS Unix accounts are preconfigured with the above command in an initialization file. (Initialization files are also described in how-to B-14.)

Assume that we have logged in to Unix and performed a typical sequence of actions: list all files recursively (showing contents of directories), make a new directory, change into that directory, copy a file from another directory to our current directory (abbreviated . ), and look at a file using cat. We can display this sequence of past commands by typing:

history

The most recent commands we entered are shown, each with a number at the left:

1  set history=20

2  ls -R

3  mkdir project2

4  cd project2

5  cp /home/jsmythe/project1/outline .

6  cat /home/jsmythe/project1/outline

7  ls -l

It may be useful to be able to re-use some of these commands without having to type the full command.

Reissuing commands

There are several ways to reissue previous commands without having to retype them. The easiest way is to type

!!

which repeats the most recent command (the command is both displayed on the screen and executed). In the example above, typing !! would repeat the ls -l command.

To use a particular command from the history list, type

!#

where # is the number of the command in the history list. For example,

!2

would repeat the ls -R command numbered 2 in our history list above.

To repeat the last command that began with a certain letter or letters, type an exclamation mark followed by enough letters to uniquely identify the command:

!letters

In our case, typing

!cp

would repeat the command

cp /home/jsmythe/project1/outline .

If we didn't type enough letters to uniquely identify the command, for example, if we had typed

!l

the most recent command that began with that letter or letters would be repeated, in our case, command number 7,

ls -l

Changing previous commands

Sometimes we need to use only part of a previously-issued command. Let's assume that the most recent command we issued was

cat /home/jsmythe/project1/outline

Now we want to edit the same file using the emacs editor. We would type

emacs !$

The symbol !$ means "take the last word in the previous command and insert it in the current command at the position of this symbol". To Unix, the last word in the previous command is the pathname for the file. So Unix would interpret this short form as

emacs /home/jsmythe/project1/outline

and would execute this command.

Correcting previous commands

It is possible to make spelling mistakes in long commands and not notice until the command has been issued. For example, suppose we wished to view the file /home/jsmythe/project1/agenda, but we accidently typed

moe /home/jsmythe/project1/agenda

when of course we meant more instead of moe.

To correct the spelling mistake without having to type the whole command again, we would type

^mo^mor^

This command tells Unix to find the first occurrence of the letters mo in the previous command, substitute the letters mor for them, and re-execute the complete command.

This feature can also be used to remove unwanted portions of a command. For example, if we had typed

more /home/jsmythe/project1/agenda

but the file agenda was in jsmythe's home directory and not in directory project1, we could issue the command

^/project1^^

to remove /project1 (in effect to replace it with nothing) and reissue the command as

more /home/jsmythe/agenda

Recording your entire Unix session

To keep a copy of everything displayed on your terminal during a Unix session, type

script

at the beginning of the session. This causes all commands you type and all responses from Unix to these commands to be recorded in a file called typescript in your current directory (as well as being displayed on your screen). To stop the recording, type

exit

You can then view or print the file typescript. A caveat: typescript will contain end-of-line and backspace characters which can be removed using the Unix utility tr.

Renaming commands (using aliases)

There may be times when you wish to rename a Unix command. For example, if you are familiar with the DOS operating system for PC-compatibles, you may prefer to call the ls command by its DOS equivalent, dir. Or, if you regularly misspell a command (like moe for more in the last section) you might wish Unix would act on moe as if it were more.

It is possible to give existing commands a new name, or alias. These aliases can be made temporary (for one session only) or permanent.

Creating a temporary alias for a Unix command

Without an alias, typing the command dir will produce:

dir: Command not found.

In order to have Unix give us the information that ls displays when we type dir, we create an alias, as follows:

alias dir ls

Now, typing either dir or ls will display the same short listing of files. (See how-to B-5, List Contents and Navigate Unix Directories, for more on ls.)

If we want to have the long format of the ls command displayed whenever we type ll, we can create another alias:

alias ll ls -l

This makes typing ll equivalent to typing the ls command with the -l option. (ACS Unix accounts are preconfigured with this particular alias.)

If you almost always use a certain option with a command, you may create an alias for that command which includes the option. For example, for safety's sake you may usually use the -i option with the rm command, so that Unix asks you to confirm each file to be removed. You may create an alias for rm as follows:

alias rm rm -i

Now you have redefined the command rm to automatically include the -i option.

Checking aliases

Some aliases are set up when your ACS Unix account was created. To see which aliases are currently in effect, type the alias command with no arguments:

alias

The Unix shell will display a list of aliases in the left-hand column and the meaning of each alias in the right-hand column, for example:

help    man

ll      ls -l

ls      ls -aC

Creating permanent aliases

The aliases we created in the previous section are temporary aliases. They are abandoned each time you log out of your Unix account. If you wish to use the same aliases during each Unix session, you can use a Unix editor like emacs or vi to add your alias commands to an initialization file called .mycshrc. Unix will automatically read .mycshrc at the beginning of each session. For details on initialization files, see how-to B-14, Customize Your Unix Environment.

Removing an alias

Once you create a temporary alias, you may remove it by using the command unalias. To remove the alias you created above called dir, type

unalias dir

To remove a permanent alias, edit your initialization file .mycshrc which contains it.


* * * * * * * * * * * * * * *

This page written and maintained by Academic Computing Services, Simon Fraser University.
Please e-mail questions or comments to help@sfu.ca.