How to Copy Files and Directories in Linux using "cp" Command
Copying Files and Directories
You can copy a ﬁle or a directory from one place to another using the cp command. The cp command copies ﬁles to a speciﬁed target ﬁle or directory. The target ﬁle or directory is the last argument in the command.
You can use the cp command to copy the contents of a ﬁle to another ﬁle. You can also use the cp command to copy multiple ﬁles. 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 ﬁles when copying ﬁles. When you perform the cp command with the -i option, it prompts you for conﬁrmation before the copy overwrites an existing target.
The syntax for the cp command when copying ﬁles is:
cp-option(s) source(s) target
The source option is a ﬁle. The target option can be a ﬁle or a directory.
The table below describes some options you can use with the cp command when you are copying ﬁles and directories.
|-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 ﬁle to a new ﬁle name in the same directory, use the cp command with the name of the source ﬁle and the target ﬁle. To copy the ﬁle named file3 to a new ﬁle named feathers, within the user1 directory, perform the cp command.
To copy the ﬁle named file3 to a new ﬁle 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 ﬁle to a new ﬁle 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 ﬁles to a different directory, use the cp command with multiple ﬁle names for the source and use a single directory name for the target. To copy the feathers ﬁle and the feathers_6 ﬁle 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 ﬁles when copying new ﬁles, you can use the cp command with the -i option. When you use the -i option, the system prompts you for a conﬁrmation before overwriting existing ﬁles with new ones.
- A yes response permits the overwrite.
- A no response prevents the cp command from overwriting the target ﬁle.
To copy the feathers ﬁle to the feathers_6 ﬁle, perform the cp -i command. Because the feathers_6 ﬁle already exists, the overwrite prompt appears.
$ cp -i feathers feathers_6 cp: overwrite feathers_6 (yes/no)? y $
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