cp (Unix)

cp (Unix)

cp is a UNIX command used to copy a file. Files can be copied either to the same directory or to a completely different directory, possibly on a different file system or hard disk drive. If the file is copied to the same directory, the new file must have a different name to the original; in all other cases, the new file may have the same or a different name. The original file remains unchanged.

The cp command now has a number of variations; two major variations are POSIX cp and GNU cp. GNU cp has many additional options.[1]

Contents

Usage

To copy a file to another file

 cp [-f] [-H] [-i] [-p][--] SourceFile TargetFile

To copy a file to a directory

 cp [-f] [-H] [-i] [-p] [-r | -R] [--] SourceFile ... TargetDirectory

To copy a directory to a directory (-r or -R must be used)

 cp [-f] [-H] [-i] [-p] [--] { -r | -R } SourceDirectory ... TargetDirectory

Flags

-f (force) – specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by the cp command.

-L (dereference) – makes the cp command follow symbolic links (symlinks) so that the destination has the target file rather than a symlink to the target.

-i (interactive) – prompts you with the name of a file to be overwritten. This occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in the SourceFile or SourceDirectory parameter. If you enter y or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file.

-p (preserve) – duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory:

  • The time of the last data modification and the time of the last access.
  • The user ID and group ID (only if it has permissions to do this)
  • The file permission bits and the SUID and SGID bits.

-R or -r (recursive) – copy directories (recursively copying all the contents)

Examples

To make a copy of a file in the current directory, enter:

     cp prog.c prog.bak

This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces its contents with the contents of the prog.c file.

To copy a file in your current directory into another directory, enter:

     cp jones /home/nick/clients

This copies the jones file to /home/nick/clients/jones.

To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:

     cp -p smith smith.jr

This copies the smith file to the smith.jr file. Instead of creating the file with the current date and time stamp, the system gives the smith.jr file the same date and time as the smith file. The smith.jr file also inherits the smith file's access control protection.

To copy all the files in a directory to a new directory, enter:

     cp /home/janet/clients/* /home/nick/customers

This copies only the files in the clients directory to the customers directory.

To copy a directory, including all its files and subdirectories, to another directory, enter:

     cp -R /home/nick/clients /home/nick/customers

This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory. Be careful about including a trailing slash in the source directory, however. If you run cp -R /home/nick/clients/ /home/nick/customers on a GNU-based system, it does the same thing as without the slash; however, if you run the same thing on a BSD-based system, it will copy all the contents of the "clients" directory over, instead of the "clients" directory itself. The same will happen in both GNU-based and BSD-based systems if the path of the source directory ends in . or .. (with or without trailing slash).

To copy a specific set of files to another directory, enter:

     cp jones lewis smith /home/nick/clients

This copies the jones, lewis, and smith files in your current working directory to the /home/nick/clients directory.

To use pattern-matching characters to copy files, enter:

     cp programs/*.c .

This copies the files in the programs directory that end with .c to the current directory, signified by the single . (dot). You must type a space between the c and the final dot.

Copying a file to an existing file is done by opening the existing file in update mode which requires write access and results in the target file retaining the permissions it had originally.

Related Unix commands

  • cpio – copy an entire directory structure from one place to another
  • tar – create an archive of files
  • link – system call to create a link to a file or directory
  • ln – create a link to a file or directory
  • mv – move a file or directory
  • rm – remove a file or directory
  • unlink – system call to remove a file or directory
  • chmod – change the mode (aka permissions) on a file or directory
  • chown – change ownership on a file or directory
  • chgrp – change group on a file or directory
  • uucp – unix to unix copy
  • scp – secure copy over SSH

See also

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Unix — (officially trademarked as UNIX, sometimes also written as Unix with small caps) is a computer operating system originally developed in 1969 by a group of AT T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Douglas McIlroy, and… …   Wikipedia

  • UNIX — Ken Thompson und Dennis Ritchie Basisdaten Entwickler …   Deutsch Wikipedia

  • Unix — Kommandozeile (Unix Prompt) Basisdaten Entwickler Ken Thompso …   Deutsch Wikipedia

  • Unix — (registrado oficialmente como UNIX®) es un sistema operativo portable, multitarea y multiusuario; desarrollado, en principio, en 1969 por un grupo de empleados de los laboratorios Bell de AT T, entre los que figuran Ken Thompson, Dennis Ritchie y …   Wikipedia Español

  • UNIX — UNIX, parfois écrit « Unix », avec des petites capitales, est un système d exploitation multitâche et multi utilisateur créé en 1969, conceptuellement ouvert et fondé sur une approche par laquelle il offre de nombreux petits outils… …   Wikipédia en Français

  • Unix — (marque déposée officiellement comme UNIX, parfois aussi écrit comme Unix avec les petites capitalisations) est le nom d un système d exploitation multitâche et multi utilisateur créé en 1969, conceptuellement ouvert et fondé sur une approche par …   Wikipédia en Français

  • Unix time — Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1 1970, not counting leap seconds. It is widely used not only on Unix like… …   Wikipedia

  • Unix-подобная операционная система — Генеалогическое дерево UNIX подобных ОС UNIX подобная операционная система (иногда сокр. *nix)  система, которая образовалась под влиянием UNIX. Термин включает свободные/открытые операционные системы, образованные от UNIX компании …   Википедия

  • UNIX — 〈[ju:nıx] EDV〉 Betriebssystem für Computer, das zum großen Teil in der Programmiersprache C geschrieben ist [engl.] * * * UNIX   [ursprünglich UNICS, Abk. für Uniplexed Information and Computing System, dt. »nicht multiplextes (vielseitiges)… …   Universal-Lexikon

  • UNIX-Kommandos — Unix Systeme zeichnen sich durch eine Vielzahl von Kommandos aus, mit denen sich über eine Shell das Betriebssystem bedienen lässt. Die Syntax dieser Kommandos weicht unter den verschiedenen Systemen voneinander ab. Es existieren die beiden… …   Deutsch Wikipedia

  • Unix-Befehle — Unix Systeme zeichnen sich durch eine Vielzahl von Kommandos aus, mit denen sich über eine Shell das Betriebssystem bedienen lässt. Die Syntax dieser Kommandos weicht unter den verschiedenen Systemen voneinander ab. Es existieren die beiden… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”