Difference between revisions of "Cp"

From wikieduonline
Jump to navigation Jump to search
 
(39 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<code>[[w:Cp_(Unix)|cp]]</code><ref>http://man7.org/linux/man-pages/man1/cp.1.html</ref> command is used for copying files and directories. It lacks progress bar indicator<ref>https://unix.stackexchange.com/questions/65077/is-it-possible-to-see-cp-speed-and-percent-copied</ref>, but you can use <code>[[progress]] -w</code> command to display the percentage of copied data or use [[linux/Basic commands/rsync|rsync]]<ref>https://stackoverflow.com/questions/6339287/copy-or-rsync-command</ref> command.
+
{{lc}}
 +
<code>[[wikipedia:Cp_(Unix)|cp]]</code><ref>http://man7.org/linux/man-pages/man1/cp.1.html</ref> command is used for copying [[file]]s and [[directory|directories]]. It lacks [[progress bar]] indicator<ref>https://unix.stackexchange.com/questions/65077/is-it-possible-to-see-cp-speed-and-percent-copied</ref>, but you can use <code>[[progress]] -w</code> or <code>[[progress]] -m</code>  command to display the percentage of copied data or use [[rsync]]<ref>https://stackoverflow.com/questions/6339287/copy-or-rsync-command</ref> command.
  
[[DevOps/Ansible|Ansible]] also includes a cp module with limited scalability (>hundreds of files) <ref>https://docs.ansible.com/ansible/latest/modules/copy_module.html</ref>
+
[[Ansible]] also includes a cp module with limited scalability (>hundreds of files) <ref>https://docs.ansible.com/ansible/latest/modules/copy_module.html</ref>
  
 
== Options ==
 
== Options ==
:<code>-a, --archive</code>
+
: <code>[[-a]], --archive</code>
:<code>-l, --link</code> [[hard link]] files instead of copying
+
: <code>-l, --link</code> [[hard link]] files instead of copying
 +
: <code>-p</code>    same as <code>--preserve=mode,ownership,timestamps</code>
 +
: <code>-R, [[-r]], --[[recursive]]</code>
 +
: <code>--reflink[=WHEN]</code> control clone/CoW copies. --reflink=always
 +
: <code>-v</code>    verbose, showing files as they are copied.
 +
 
 +
cp --help
 +
cp: illegal option -- -
 +
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-aclpsvXx] source_file target_file
 +
        cp [-R [-H | -L | -P]] [-fi | -n] [-aclpsvXx] source_file ... target_directory
 +
 
 +
== Examples ==
 +
cp -a /source/. /dest/
 +
cp -rp /home/origin /home/destination
 +
cp -rp /home/origin/* /home/destination/
 +
cp file.txt file-`date --iso`.txt
 +
 
 +
* cp using [[Bash]] [[brace expansion]]:
 +
<code>cp /etc/httpd/httpd.{,.BAK}</code>
 +
 
 +
[[cp -R -af]]
  
 
== Activities ==
 
== Activities ==
# Understand the differences between [[linux/Basic commands/cp|cp]], [[scp]] and [[linux/Basic commands/rsync|rsync]]: https://stackoverflow.com/questions/20244585/how-does-scp-differ-from-rsync
+
# Understand the differences between [[cp]], [[scp]] and [[rsync]]: https://stackoverflow.com/questions/20244585/how-does-scp-differ-from-rsync
 
# Understand <code>--sparse</code> option and usage cases
 
# Understand <code>--sparse</code> option and usage cases
# Learn how to not overwrite a file with <code>-n, --no-clobber</code> option<ref>https://stackoverflow.com/questions/9392735/linux-how-to-copy-but-not-overwrite</ref> or using <code>[[linux/Basic commands/rsync|rsync]] -a -v --ignore-existing src dst</code>
+
# Learn how to not overwrite a file with <code>-n, --no-clobber</code> option<ref>https://stackoverflow.com/questions/9392735/linux-how-to-copy-but-not-overwrite</ref> or using <code>[[rsync]] -a -v --ignore-existing src dst</code>
 +
 
 +
== Related commands ==
 +
* <code>[[rsync]]</code>
 +
* <code>[[mv]]</code>
 +
* <code>[[progress -m]]</code>
 +
* <code>[[copy]]</code> ([[Windows]])
 +
* <code>[[nfs-cp]]</code>
 +
* <code>[[docker cp]]</code>
 +
* <code>[[minikube cp]]</code>
 +
* <code>[[multipass transfer]]</code>
 +
* <code>[[xcopy]]</code> (Windows)
 +
* <code>[[ADD]]</code> in <code>[[Dockerfile]]</code>
 +
* <code>[[aws s3 cp]]</code>
 +
* <code>[[gsutil cp]]</code>
 +
* <code>[[sftp cp]]</code>, since Aug 2022
  
 
== See also ==
 
== See also ==
 
* {{cp}}
 
* {{cp}}
* <code>progress</code> <ref>https://github.com/Xfennec/progress</ref>: Linux tool to show progress for cp, mv, dd. See also: [[pv]]<ref>https://linux.die.net/man/1/pv</ref> (Pipe Viewer). Example: <code>progress -m</code>
+
* <code>[[progress]]</code> <ref>https://github.com/Xfennec/progress</ref>: Linux tool to show progress for cp, mv, dd. See also: [[pv]]<ref>https://linux.die.net/man/1/pv</ref> (Pipe Viewer). Example: <code>progress -m</code>
* <code>[[scp]]</code>, <code>[[dd (Unix)|dd]]</code>, <code>[[mv]]</code>
+
* <code>[[dd]]</code>
  
  

Latest revision as of 06:07, 30 August 2022

cp[1] command is used for copying files and directories. It lacks progress bar indicator[2], but you can use progress -w or progress -m command to display the percentage of copied data or use rsync[3] command.

Ansible also includes a cp module with limited scalability (>hundreds of files) [4]

Options[edit]

-a, --archive
-l, --link hard link files instead of copying
-p same as --preserve=mode,ownership,timestamps
-R, -r, --recursive
--reflink[=WHEN] control clone/CoW copies. --reflink=always
-v verbose, showing files as they are copied.
cp --help
cp: illegal option -- -
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-aclpsvXx] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-aclpsvXx] source_file ... target_directory

Examples[edit]

cp -a /source/. /dest/
cp -rp /home/origin /home/destination
cp -rp /home/origin/* /home/destination/
cp file.txt file-`date --iso`.txt

cp /etc/httpd/httpd.{,.BAK}

cp -R -af

Activities[edit]

  1. Understand the differences between cp, scp and rsync: https://stackoverflow.com/questions/20244585/how-does-scp-differ-from-rsync
  2. Understand --sparse option and usage cases
  3. Learn how to not overwrite a file with -n, --no-clobber option[5] or using rsync -a -v --ignore-existing src dst

Related commands[edit]

See also[edit]

Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy.

Source: https://en.wikiversity.org/wiki/Linux/Basic_commands/cp

Advertising: