7.1 Advanced Directory and File Management Using the Command Line
7.1.1 Copying Files
The cp (copy) command is used to copy files and directories. We will focus on copying files in this first section. Copying files is a normal occurrence when working with the file system. A common use of the copy command is to make a backup of an existing file for safekeeping so the original can be modified.

Files can be copied two different ways:

  • A new file can be created with a different name in the same directory.
  • Files can be copied to a different location in the directory hierarchy with the same or different name. They can also be copied to a different disk such as a floppy or to a centralized server.

The command format below shows two different formats of the copy command depending on your goal.

Copying Files Within A Directory
If you wish to copy a file to another file name within the same directory, use the format in Figure
.

The format in Figure will copy from an existing filename (source_file) to a new file name (destination_file) in the same directory.

Note: You cannot have two files of the same name in one directory.

This method of copying files can be thought of as copying from OLD to NEW in the same directory. Figure illustrates copying a file within a directory.

Copying Files to Another Directory
If you wish to copy one or more files to another directory, use the format in Figure .

The format in Figure copies the existing file(s) (what you want to copy) to another directory in the directory structure (where you want to copy it to). When copying a file this way, it will normally have the same name. You can add a slash and a file name after the destination directory to give the file a different name if desired. This version can be thought of as copying WHAT to WHERE. Figure shows copying several files using this format. The cp command assumes the last entry (dir1) is a directory name and the two prior entries (feathers and feathers_6) are files.

Note: If a file is copied and the name of the destination directory does not exist, a new file is created with that name, otherwise the file is copied to the directory specified. 

The copy command can also be used with metacharacters or wildcards such as the asterisk (*) and question mark (?) to copy groups of files at the same time. Figure also shows the use of the asterisk to copy the same files using an alternative command line.

Preventing Overwriting of Files
If you copy a file and the target name already exists, you will overwrite or "clobber" the file and will not receive a warning. To prevent overwriting an existing file when copying, use
cp -i (interactive) option as a security measure. The -i option only prompts you if you are about to overwrite an existing file and gives you a choice. Answering "y" will overwrite the file; answering "n" will return the shell prompt without copying. It is a good idea to get into the habit of using cp -i since it can prevent accidental mistakes. Figure shows an example of the cp -i command.

Interactive Media Activity (Flash, 95 kB)
  Copying Files. 

In this media activity, you are logged in as user2 and your current working directory is: /home/user2/dir2. Refer to the Class File Tree Structure by clicking the 'tree' button located on the Menu Bar and type the commands that would accomplish the requested objectives. 
Note: Be sure to press enter after each one. Click on step 1 to begin.