You are currently viewing Linux Commands – cp

Linux Commands – cp

  • Post author:
  • Post category:Linux

Linux Commands – cp

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed mv command in Linux which is used to move/rename files and directories in Linux.

In this blog post, we will discuss cp command in Linux. cp stands for copy and is used to copy files and directories from the source location to the destination location.

Linux Commands – cp:

You can copy files and directories using cp command. You need to provide the full or relative path of the files/directories of source and destination. In order to copy directories, you need to use cp -R or -r or –recursive which copies directories recursively.

You can copy multiple files and directories at once using cp command. The last parameter will be treated as destination in this case. If the source files or directories have a common pattern, then you can also use regular expression with cp command.

If the destination file/directory already exists, cp command overwrites the destination file/directory with the source file/directory. If destination file/directory does not exist, cp command copies the source file/directory to the destination file/directory.

You can use cp -i or –interactive option to prompt before overwrite (overrides -n option)

You can use cp -n or –no-clobber option for not overwriting destination file if its exist. (overrides a previous -i option)

You can use cp -f or –force option to forcefully copy and replace the destination file if the destination file cannot be opened (redundant if the -n option is used).

You can use cp –remove-destination option to remove each existing destination file before attempting to open it (contrast with –force).

You can use cp -b option to take a backup of each existing destination file.

You can use cp –backup[=CONTROL] option to take a backup of each existing destination file. CONTROL can be none (Never make backups), numbered or t (Make numbered backups), existing or nil (Numbered if numbered backups exist, simple otherwise) and simple or never (Always make simple backups).

You can overwrite the usual backup suffix (~) with cp -S or –suffix=SUFFIX option.

You can get details of what cp command is doing using cp -v or –verbose options.

You can also use cp -u or –update options to copy only when the SOURCE file is newer than the destination file or when the destination file is missing.

You can use cp -s or –symbolic-link option to make symbolic links instead of copying.

You can use cp -H option to follow command-line symbolic links in SOURCE. If a command-line argument specifies a symbolic link, then copy the file it points to rather than the symbolic link itself. However, copy (preserving its nature) any symbolic link that is encountered via recursive traversal.

Use cp -L or –dereference options to always follow symbolic links in SOURCE instead of creating one. With this option, ‘cp’ cannot create a symbolic link.

Use cp -P or –no-dereference options to never follow symbolic links in SOURCE.

Use cp -l or –link options to make hard links instead of copies of non-directories.

Generally, when you copy a file, the file attributes may get changed during the copy. You can use cp –preserve[=ATTR_LIST] to preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all.

You can also use cp -p (same as –preserve=mode,ownership,timestamps) to preserve the source attributes.

You can also use cp -a or –archive options (same as -dR –preserve=all) to archive the source. cp –no-preserve=ATTR_LIST option does the opposite of this command.

You can use cp –parents option to copy full source file name under DIRECTORY.

You can use cp –strip-trailing-slashes option to remove any trailing slashes from each SOURCE argument.

You can use cp –sparse=WHEN control copy a sparse file. By default, cp supports sparse file copy but the source file system should support sparse file. The cp –sparse=always is useful when the source does not support sparse files but has a long sequence of zero in it and the target file system supports sparse files.

You can use cp -t or –target-directory=DIRECTORY to copy all SOURCE arguments into DIRECTORY.

You can use mv -T or –no-target-directory treat DEST as a normal file.

Hope you have enjoyed this article. If an option is left, kindly give a demo in the comment and I will include that in this article. In the next blog post, we will discuss echo command in Linux.