Copying files in GNU/Linux with cp Command

GNU/Linux comes with a versatile cp command to copy files and folders. Typical invocation of cp involves a source and a target. Source can be file or directory and target can also be file or directory. However, if source is a directory then target must be a directory. A synopsis of cp command is given below:

cp [option]… [-T] source dest
cp [option]… source… directory
cp [option]… -t directory source…

If two filenames are given, cp copies first file to second. If source is a directory, -r or -R i.e. recursive copy or -a i.e. archive; must be supplied as an argument and target must be a directory. If source is a file but target is a directory, source file is copied to the target directory.

Consider we have two folders: one and two. one contains two files a and b while two is empty. Let us also assume that these two folders are in our home directory and our working directory is also home directory. To copy file a from one to two we can issue the command: cp one/a two. We can also supply full paths like cp ~/one/a ~/two where ~ signifies home directory. To copy entire folder one into two we can give following command: cp -r one two

Now we will see some of the most useful options of cp command which we can use to perform various different tasks while copying files and folders.

Archiving files and folders

You can use -a or --archive to achieve this. It preserves as much as possible of the structure and attributes of the original files in the copy (but do not attempt to preserve internal directory structure; i.e., ls -U may list the entries in a copied directory in a different order). It tries to preserve SELinux security context and extended attributes (xattr), but ignore any failure to do that and print no corresponding diagnostic. Equivalent to -dR --preserve=all with the reduced diagnostics.

Creating backups

You can use -b or --backup=[method] to create backups of files and folders. As mentioned in my previous post, backup files have ~ at the end of their names. method here can be one of none, off, numbered, t, existing, nil, simple and never. none and off mean never make backup, numbered and t mean make numbered backup, existing and nil mean make numbered backup of files that already have them and simple and never mean always make simple backups.

Consider we have two folders one and two. Both contain a filename called f. If we copy file like cp -b one/f two, then a backup of file will be created in folder two.

--copy-contents copies contents of special files like FIFOs as if they were regular files, if copying contents recursively. It is usually a mistake to use this option. It does not affect the copying of symbolic links.

Preserving file attributes

-p or --preserve=[attribute-list] where attribute list includes mode, ownership, timestamps, links, context(SELinux), xattr and all. Attribute list can be comma seprated list of one of more of above keywords.

Copying links

-d option should be used to copy symbolic links as links rather than copying what they are pointing to. It also preserves hard links between source files and copies.

Forcing copying

-f or --force forces copying. When copying without this option and an existing destination file cannot be opened for writing, the copy fails. However, with --force, when a destination file cannot be opened, cp then tries to recreate the file by first removing it. Note --force alone will not remove dangling symlinks. When this option is combined with --link (-l) or --symbolic-link (-s), the destination link is replaced, and unless --backup (-b) is also given there is no brief moment when the destination does not exist. This option is independent of the --interactive or -i option: neither cancels the effect of the other. This option is ignored when the --no-clobber or -n option is also used.

Copying contents of a symbolic link

-H options will make cp copy the contents a symbolic link is pointing to. However, if symbolic link points to a directory then further encountered symbolic link will be copied as it it.

Asking before overwriting or interactive copying

-i or --interactive option achieves this. It will make cp ask for confirmation before overwriting files in destination. It also overrides previous -n option.

Making links of non-directories

With -l or --link cp makes hard links instead of copies of non-directories.

Dereferencing symbolic links

With -L or --dereference cp will follow symbolic links when copying from them. With this option, cp cannot create a symbolic link. For example, a symlink (to regular file) in the source tree will be copied to a regular file in the destination tree.

Not overwriting files in target or destination

With -n or --no-clobber cp will not overwrite files and will be silent for the operation. This overrides any previous -i option. It is also mutually-exclusive with backup option.

Copying symbolic links as symbolic links

-P or --no-dereference will make cp copy symbolic links as symbolic links rather than copying files they are pointing to. This only affects symbolic links in the source. Symbolic links in destination are always followed, if possible.

With this we have covered most important options of cp command. For a complete reference please refer to coreutils documentation installed on your system or https://www.gnu.org/software/coreutils/manual/coreutils.html#cp-invocation

Leave a comment

Your email address will not be published. Required fields are marked *