Linux

Linux cp command

1. Introduction to cp

Working with files, from time to time there will be needed to execute some actions on some files like copy, move, rename or delete. These options are available from the user interface by working with a mouse and selecting files and right clicking: “Copy” and then to go in a different location and selecting: “Paste” or just writing a command from the terminal.

For the purpose of this article, we will look over the cp command in Linux with some different syntaxes and the options that this command offers.

If you want to find more information about the cp command type in your terminal: man cp or visit this site.

2. Linux cp Different Syntaxes

The meaning of this cp acronym stands for copy. With this command, you will be able to copy files or directories from one location to another.

There are multiple options for the copy command.

  1. cp [OPTION] SOURCE DESTINATION – the simple command to copy one source to the destination
  2. cp [OPTION] SOURCE DIRECTORY – copy command from a source to a directory
  3. cp [OPTION] ... SOURCE... DIRECTORY – copy multiple files to a directory
Example of copy syntax
Fig.1: Example of copy syntax

3. cp Options

This command offers multiple options to append. Some of the most important options that you can use in your command are:

  • --help – showing the information about this command
  • --version – output the version for the current installer
  • --verbose, -v – showing what is being done
  • -s, --symbolic-link – make symbolic links instead of copying
  • -R, -r, --recursive – copy directories recursively
  • -f, --force – force the copy files or directories. (If the existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)).
  • -n, --no-clobber – do not overwrite an existing file (overrides a previous -i option)
  • -i, --interactive – prompt before overwrite (override a previous -n option)
  • -l, --link – hard link files instead of copying
  • --preserve[=ATTR_LIST] – preserve the specified attributes (default: mode, ownership, timestamps), if possible additional attributes: context, links, xattr, all.
  • -p – same as –preserve=mode,ownership,timestamps
  • --remove-destination – remove each existing destination file before attempting to open it (contrast with --force)
  • --backup – make a backup for the destination file

Besides these options mentioned above, you can find more options just by checking the official documentation or using the help option for the cp command.

4. Conclusion

In this article, we covered the cp command that Linux systems offer. Knowing this command with all the options will be useful for every Linux user. You can copy different files and directories in many ways, from one location to another.

Iulian Timis

My name is Iulian and I graduated Faculty of Electronics, Telecommunications, and Information Technology. My master's degree was in Computer Science, Information Security. I am passionate about technology and the security area. During my career I was working as a Java Developer, on the backend and frontend side, sometimes doing some DevOps tasks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button