Copying files within the current directory – HP NonStop G-Series User Manual

Page 101

Advertising
background image

The general format of the cp command is:

cp source destination

source

is the name of the file to be copied, destination is the name of the file to which you

want to copy source. source and destination can be filenames in your current directory or
pathnames to different directories.

source

can also be a series of one or more filenames, in which case destination must be a

pathname that ends with the name of the target directory.

In the source entry, you can also use pattern-matching characters, as described in

Chapter 4

(page 53)

.

Copying Files Within the Current Directory

The cp command creates the destination file if it does not already exist. However, if a file with the
same name as the destination file does exist, cp copies the source file over the existing destination
file.

CAUTION:

If the destination file exists, the contents of that file will be erased before the source

file is copied. As a result, be certain that you do not need the contents of the destination file, or
that you have a backup copy of the file, before you use it as the destination file for the cp command.

In the following example, the destination file does not exist, so the cp command creates it. First,
list the contents of your home directory:

$ ls

file1 file2 file3 project

Now, copy the source file, file2, into a new destination file, file2x:

$ cp file2 file2x

List the contents of the directory to verify that the copying process was successful:

$ ls

file1 file2 file2x file3 project

Copying Files From One Directory to Another Directory

You need a new subdirectory to do the following example, so create one called /reports with
the mkdir (make directory) command:

$ mkdir reports

$ ls

file1 file2 file2x file3 project reports

To copy the file file2 into the directory /reports, enter:

$ cp file2 reports

Now, list the contents of /reports to verify that it contains a copy of file2:

$ ls reports

file2

You can also use the cp command to copy multiple files from one directory into another directory.
The general format of the command is the following:

cp filename1 filename2 dirname

In the following example, enter the cp command to copy both file2 and file3 into the
/reports

directory, and then list the contents of that directory:

$ cp file2 file3 reports

Copying Files (cp)

101

Advertising
This manual is related to the following products: