Linux Copy Command Examples

htt‮sp‬://www.lautturi.com
Linux Copy Command Examples

The cp command is a Linux command for copying files and directories. Here are some examples of how to use the cp command:

  1. Copy a file to a different directory:
$ cp file.txt /path/to/destination/directory

This will copy the file file.txt to the destination directory /path/to/destination/directory.

  1. Copy a file and rename it:
$ cp file.txt /path/to/destination/directory/new_file_name.txt

This will copy the file file.txt to the destination directory /path/to/destination/directory and rename it to new_file_name.txt.

  1. Copy multiple files to a different directory:
$ cp file1.txt file2.txt file3.txt /path/to/destination/directory

This will copy the files file1.txt, file2.txt, and file3.txt to the destination directory /path/to/destination/directory.

  1. Copy a directory and its contents:
$ cp -r source_directory /path/to/destination/directory

This will copy the directory source_directory and all of its contents to the destination directory /path/to/destination/directory. The -r option tells the cp command to copy directories recursively.

  1. Copy a file and preserve its permissions:
$ cp -p file.txt /path/to/destination/directory

This will copy the file file.txt to the destination directory /path/to/destination/directory and preserve its permissions. The -p option tells the cp command to preserve the file's permissions.

  1. Copy a file and overwrite an existing file with the same name:
$ cp -f file.txt /path/to/destination/directory

This will copy the file file.txt to the destination directory /path/to/destination/directory and overwrite an existing file with the same name if it exists. The -f option tells the cp command to force the copy operation and overwrite any existing files.

These are just a few examples of how to use the cp command. There are many other options and features that you can use with the cp command. You can use the cp --help command to see a complete list of options and usage examples.

Created Time:2017-10-29 22:08:52  Author:lautturi