Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Copying Files (cp)

To make a copy of a file use the cp command. The basic syntax of the command is

cp source destination

Here source is the name of the file that is copied and destination is the name of the copy. For example, the following command makes a copy of the file test_results and places the copy in a file named test_results.orig:

$ cp test_results test_results.orig

Common Errors

There is no output from the cp command, unless it encounters an error. Two common errors occur when

  The source is a directory
  The source does not exists

An example of the first case is the command

$ cp work docs

This causes an error message similar to the following:

cp: work: is a directory

An example of the second case is the command

$ cp test_relsuts test_results.orig

Here I have mistyped the filename test_results as test_relsuts and cp gives the following error:

cp: cannot access test_relsuts: No such file or directory

Interactive Mode


No error message is generated if the destination already exists. In this case, the destination file is automatically overwritten. This can lead to serious problems.

To avoid this behavior you can specify the -i (i as in interactive) options to cp.

If the file test_results.orig exists, the command

$ cp -i test_results test_results.orig

results in a prompt something like the following:

overwrite test_results.orig? (y/n)

If you choose y (yes), the file will is overwritten. If you choose n (no), the file test_results.orig isn’t changed.

Copying Files to a Different Directory

If the destination is a directory, the copy has the same name as the source but is located in the destination directory. For example, the command

$ cp test_results work/

places a copy of the file test_results in the directory work.

Multiple Inputs

If more than two inputs are given, cp treats the last argument as the destination and the other files as sources. This works only if the sources are files and the destination is a directory, as in the following example:

$ cp res.01 res.02 res.03 work/

If one or more of the sources are directories the following error message is produced. For example, the command

$ cp res.01 work/ docs/ pub/

produces the following error:

cp: work: is a directory
cp: docs: is a directory

Although cp reports errors, the source file, in this case res.01, is correctly copied to the directory pub.

If the destination is a file, but multiple inputs are given, as in the following example,

$ cp hw1 hw2 hw3

an error message similar to the following

cp: hw3: No such file or directory

is generated. In this case no files are copied.

Renaming Files (mv)


To change the name of a file use the mv command. Its basic syntax is
mv source destination

Here source is the original name of the file and destination is the new name of the file. As an example,

$ mv test_result test_result.orig

changes the name of the file test_result to test_result.orig. A new file called test_result.orig is not produced like in cp; only the name of the file is changed. There is no output from mv if the name change is successful.

If the source does not exist, as in the following example,

$ mv test_reslut test_result.orig

an error similar to the following is reported:

mv: test_reslut: cannot access: No such file or directory

Interactive Mode

Like cp, mv does not report an error if the destination already exists: it simply overwrites the file. to avoid this problem you can specify the -i option.

For example, if the file ch07.bak already exists, the following command

$ mv -i ch07 ch07.bak

results in a confirmation prompt:

remove ch07.bak? (n/y)

If you choose n (no), the destination file is not touched. If you choose y (yes), the destination file is removed and the source file is renamed.

The actual prompt varies between the different versions of UNIX.

Removing Files (rm)


To remove files use the rm command. The syntax is
rm files

Here files is a list of one or more files to remove. For example, the command

$ rm res.01 res.02

removes the files res.01 and res.02.


Common Errors

The two most common errors using rm are

  One of the specified files does not exist
  One of the specified files is a directory

As an example of the first case, the command

$ rm res.01 res.02 res.03

produces an error message if the file res.02 does not exist:

rm: res.02 non-existent

The other two files are removed.

An example of the second case is the command

$ rm res.01 res.03 work/

This command produces another error message:

rm: work directory

The two files are removed.

Interactive Mode

Because there is no way to recover a file that has been deleted using rm, you can specify the -i option. In interactive mode, rm prompts you for every file that is requested for deletion.

For example, the command

$ rm -i hw1 hw2 hw3

produces confirmation prompts similar to the following:

hw1: ? (n/y) y
hw2: ? (n/y) n
hw3: ? (n/y) y

In this case I answer y to deleting hw1 and hw3, but I answer n to deleting hw2.


Previous Table of Contents Next