2011年6月13日星期一

unix - copy

From: http://en.wikipedia.org/wiki/Cp_%28Unix%29

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.

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.

没有评论:

发表评论