Cp Command in Linux

superior_hosting_service

Cp Command in Linux (Copy Files)

Copy-Command-in-Linux.jpg

When working on Linux and Unix systems, copying files and directories is one of the most common tasks you’ll perform on a daily basis.

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 cp command

The general syntax for the cp command is as follows:

cp [OPTIONS] SOURCE... DESTINATION

The SOURCE can contain one or more files or directories as arguments, and the DESTINATION argument can be a single file or directory .

  • When the SOURCE and DESTINATION arguments are both files, the cp command copies the first file to the second one. If the file doesn’t exists the command creates it.
  • When the SOURCE has multiple files or directories as arguments, the DESTINATION argument must be a directory. In this situation, the SOURCE files and directories are moved to the DESTINATION directory.
  • When the SOURCE and DESTINATION arguments are both directories, the cp command copies the first directory into the second one.

To copy files and directories, you must have at least read permissions on the source file and write permission on the destination directory. Otherwise, a permission denied error is shown.

Copying Files with cp Command

The most basic scenario of using cp is to copy a file in the current working directory . For example, to copy a file named file.txt to file_backup.txt you wound run the following command:

$ cp file file_backup

or

$ cp file{,_backup}

To copy a file to another directory, specify the absolute or the relative path to the destination directory.

When only the directory name is specified as a destination, the copied file have the same name as the original file.

In the following example we are copying the file file.txt to the /backup directory:

$ cp file.txt /backup

If you want to copy the file under a different name you need to specify the desired file name. The command below will copy the file to the specified directory as new_file.txt.

$ cp file.txt /backup/new_file.txt

By default, if the destination file exists, it is overwritten. The -n option tells cp not to overwrite an existing file.

To force cp to prompt for confirmation, use the -i option.

$ cp -i file.txt file_backup.txt

If you want to copy the file only if it is newer than the destination use the -u option:

$ cp -u file.txt file_backup.txt

When copying a file, the new file is owned by the user running the command. Use the -p option to preserve the file mode, ownership , and timestamps :

$ cp -p file.txt file_backup.txt

Another option that can be useful is -v, which tells cp to print verbose output:

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

Copying Directories with cp Command

To copy a directory, including all its files and subdirectories, use the -R or -r option.

In the following example we are copying the directory Pictures to Pictures_backup:

$ cp -R Pictures Pictures_backup

The command above creates the destination directory, and recursively copy all files and subdirectories from the source to the destination directory.

If the destination directory already exists, the source directory itself and all its content is copied inside the destination directory.

To copy only the files and subdirectories but not the source directory, use the -T option:

$ cp -RT Pictures Pictures_backup

Another way to copy only the directory’s content but not the directory itself is to use the wildcard character (*). The disadvantage of the following command is that it does not copy the hidden files and directories (the ones starting with a dot .):

$ cp -RT Pictures/* Pictures_backup/

All the options we used in the previous section when copying files can also be used when copying directories. The main difference is that when copying directories. you always need to use the -R option.

Copy Multiple Files and Directories

To copy multiple files and directories at once, specify their names and use the destination directory as the last argument:

$ cp file.txt dir file1.txt  dir1

When copying multiple files, the destination must be a directory.

Conclusion

Copying files and directories with the cp command is a simple task. For information about all available cp options, type man cp in your terminal. To copy files over the network, use the rsync and scp utilities.

If you have any questions or feedback, feel free to leave a comment.