How to Copy Files and Directories in Linux using "cp" Command

Copying Files and Directories

You can copy a file or a directory from one place to another using the cp command. The cp command copies files to a specified target file or directory. The target file or directory is the last argument in the command.

Copying Files

You can use the cp command to copy the contents of a file to another file. You can also use the cp command to copy multiple files. You can use the cp command with options and modify the functions of the command. For example, using the -i (interactive) option prevents overwriting existing files when copying files. When you perform the cp command with the -i option, it prompts you for confirmation before the copy overwrites an existing target.

The syntax for the cp command when copying files is:

 cp-option(s) source(s) target

The source option is a file. The target option can be a file or a directory.

The table below describes some options you can use with the cp command when you are copying files and directories.

Option Description
-i Prevents you from accidentally overwriting existing files or directories
-r Includes the contents of a directory, including the contents of all subdirectories, when you copy a directory

Copying a File Within a Directory

To copy a file to a new file name in the same directory, use the cp command with the name of the source file and the target file. To copy the file named file3 to a new file named feathers, within the user1 directory, perform the cp command.

To copy the file named file3 to a new file named feathers, within the user1 directory, perform the cp command:

$ cd
$ pwd
/export/home/user1

$ ls
dante     dir2      dir5      file.3    file3     fruit2
dante_1   dir3      file.1    file1     file4     practice
dir1      dir4      file.2    file2     fruit     tutor.vi

$ cp file3 feathers

$ ls
dante     dir2      dir5      file.2    file2    fruit     tutor.vi
dante_1   dir3      feathers  file.3    file3    fruit2
dir1      dir4      file.1    file1     file4    practice

To copy the feathers file to a new file named feathers_6, within the user1 directory, perform the cp command.

$ cp feathers feathers_6
$ ls
dante       dir3        feathers_6  file1       fruit
dante_1     dir4        file.1      file2       fruit2
dir1        dir5        file.2      file3       practice
dir2        feathers    file.3      file4       tutor.vi

Copying Multiple Files

To copy multiple files to a different directory, use the cp command with multiple file names for the source and use a single directory name for the target. To copy the feathers file and the feathers_6 file from the user1 directory into the dir1 subdirectory, perform the cp command.

$ pwd
/export/home/user1

$ ls dir1
coffees  fruit    trees

$ cp feathers feathers_6 dir1

$ ls dir1
coffees feathers feathers_6 fruit trees

Preventing Overwrites to Existing Files While Copying

To prevent overwriting existing files when copying new files, you can use the cp command with the -i option. When you use the -i option, the system prompts you for a confirmation before overwriting existing files with new ones.

  • A yes response permits the overwrite.
  • A no response prevents the cp command from overwriting the target file.

To copy the feathers file to the feathers_6 file, perform the cp -i command. Because the feathers_6 file already exists, the overwrite prompt appears.

$ cp -i feathers feathers_6
cp: overwrite feathers_6 (yes/no)? y
$

Copying Directories

You can use the cp command with the -r option to copy a directory recursively. If the target directory does not exist, the cp -r command creates a new directory with that name. If the target directory exists already, the cp -r command creates a new sub-directory with that name, below the destination directory.

The syntax for the cp command when copying directories is:

$ cp -option sources target

The source option is one or more directory names. The target option is a single directory name. To copy the contents of the dir3 directory to a new directory named dir10, perform the cp -r command. Both directories are in the user1 directory.

$ cd
$ pwd
/export/home/user1

$ ls dir3
planets

$ cp dir3 dir10
cp:  dir3: is a directory

$ cp -r dir3 dir10
$ ls dir10
planets
$ ls dir3
planets
$

To copy the planets directory from the dir3 directory to a new directory called constellation, perform the cp-r command. The constellation directory is not in the current working directory.

$ cd
$ pwd
/export/home/user1
$ cd dir3

$ cp -r planets ../dir4/constellation
$ ls ../dir4/constellation
mars   pluto
$ cd