Linux cp command Detailed

When working on Linux and Unix systems, copy files and directories is one of the most common tasks that you and almost every day to perform.

cp is a command line utility for copying files and directories on Unix and Linux systems. In this article, we will explain how to use the cp command.

How to Use the cp command

The general syntax of the cp command is as follows:

cp [OPTIONS] SOURCE... DESTINATION

SOURCE can have one or more files or directories as an argument, and can have as a parameter a single file or directory in DESTINATION.

  • When the SOURCE and DESTINATION parameters are file, cp command to copy the first file to the second file. If the file does not exist, the command creates it.
  • When SOURCE multiple files or directories as arguments, DESTINATION parameter must be a directory. In this case, SOURCE files and directories will be moved to the DESTINATION directory.
  • When the SOURCE and DESTINATION parameters are directories, cp command will first be copied to a directory in the second directory.

To copy files and directories, you must have at least read permissions and write permissions to the target directory of the source file. Otherwise, you will receive a permission denied error.

Use the cp command to copy files

The most basic method is to use cp to copy files in the current working directory. For example, to copy a file file.txt to file_backup.txt file, run the following command:

cp file file_backup

or

cp file{,_backup}

To copy files to another directory, specify the path to the destination directory absolute or relative path. When only the name of the directory specified as the destination, the copied file will have the same name as the original file.

In the following example, we will copy the file.txt file into the / backup directory:

cp file.txt /backup

If you want to copy the file to another name, you need to specify the desired file name. The following command will copy files to a specified directory new_file.txt.

cp file.txt /backup/new_file.txt

By default, if the target file exists, it will be overwritten. -n option tells cp do not overwrite existing files. To prompted for confirmation, use the -i option.

cp -i file.txt file_backup.txt

If you want to only copy files when the file is newer than the target, use the -u option:

cp -u file.txt file_backup.txt

When you copy a file, the new file will have the user running the command. Use the -p option to retain file mode, ownership and timestamps:

cp -p file.txt file_backup.txt

Another possible option is useful -v, he told cp print verbose output:

cp -v file.txt file_backup.txt
'file.txt' -> 'file_backup.txt'

Use cp command to copy a directory

To copy a directory (including all files and subdirectories), use the -R or -r option. In the following example, we will copy the Pictures directory to Pictures_backup:

cp -R Pictures Pictures_backup

The above command will create a destination directory and recursively copy all files and subdirectories from the source directory to the target directory.

If the target directory already exists, then the source directory itself and all its contents will be copied to the target directory. To copy only the files and subdirectories, without copying the target directory, use the -T option:

cp -RT Pictures Pictures_backup

Another way to copy only the contents of the directory rather than the directory itself is to use a wildcard (*). The following command drawback is that it does not copy hidden files and directories (point to the beginning of the file and directory):

cp -RT Pictures/* Pictures_backup/

When you copy a file, we can use all the options on one used when copying files. The main difference is always necessary to use the -R option when copying directories.

Copy multiple files and directories

To copy multiple files and directories, and specify its name and use the target directory as the last parameter:

cp file.txt dir file1.txt  dir1

When you copy multiple files, the goal must be a directory.

to sum up

Use the cp command to copy files and directories is a simple task. If you want to view information about all the options available in the cp terminal, enter man cp.

If you want to copy files over a network, use the scp and rsync commands.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159913.htm