cp command

In this tutorial, you will learn how to use the cp command to copy files and directories in Linux. You will learn how to specify the source and destination of the copy, how to use the -r option to copy directories and their contents, and how to use options such as -p, -f, and -u to customise the behaviour of the cp command.

The cp command is a basic Linux command that is used to copy files and directories. It is an essential tool for creating backups or duplicating files and directories.

Here is a tutorial on using the cp command:

To copy a file, simply type cp followed by the source file and the destination file or directory, and press enter:

$ cp source destination

For example, to copy a file called file.txt to a directory called documents, you would use the following command:

$ cp file.txt documents

This will create a copy of the file.txt file in the documents directory. If the documents directory does not exist, an error will be displayed.

You can also specify the path to the source and destination files and directories if they are not in the current working directory. For example, to copy a file from the /home/user directory to the /tmp directory, you would use the following command:

$ cp /home/user/file.txt /tmp

To copy a directory and its contents, you can use the -r option, which stands for “recursive.” This option tells the cp command to copy the directory and all of its subdirectories and files.

Introduction to the cp Command

The cp command is a basic Linux command used to copy files and directories. It is useful for creating backups or duplicating files and directories.

Basic Usage of the cp Command

To copy a file, type cp followed by the source file and the destination file or directory, then press enter:

$ cp source destination

For example, to copy a file called file.txt to a directory called documents, use the following command:

$ cp file.txt documents

This will create a copy of the file.txt file in the documents directory. If the documents directory does not exist, an error will be displayed.

Copying a Directory and Its Contents

To use the -r option, simply include it as an argument to the cp command, like this:

$ cp -r source destination

For example, to copy the documents directory and all of its contents to the /tmp directory, you would use the following command:

$ cp -r documents /tmp

Other Options for the cp Command

There are several other options that can be used to customise the behaviour of the cp command:

  • -p: This option preserves the file attributes, such as the permissions, ownership, and timestamps, of the source file when creating the copy.
  • -f: This option forces the cp command to overwrite any existing files or directories in the destination.
  • -u: This option only copies the source file to the destination if the destination file does not exist or if the source file is newer than the destination file.

To use these options, simply include them as arguments to the cp command, like this:

$ cp -p source destination
$ cp -f source destination
$ cp -u source destination

Multiple options can also be combined by grouping them together after a single dash:

$ cp -pf source destination

Conclusion

This tutorial covered the basic usage and some useful options of the cp command in Linux. Remember to use it carefully to avoid overwriting important files or directories.