Linux and UNIX cp command help and examples

advertisement
Linux and UNIX cp command help and examples
http://www.computerhope.com/unix/ucp.htm
Linux and Unix cp command
1 von 7
Quick links
About cp
Syntax
Examples
Related commands
Linux and Unix main page
About cp
Copies files and directories.
Overview
cp is the command which makes a copy of your files or directories. For instance, let's say you have a
file named picture.jpg in your working directory, and you want to make a copy of it called picture02.jpg. You would run the command:
cp picture.jpg picture-02.jpg
...and the file will be copied. Here, picture.jpg is the source of the copy operation, and picture02.jpg is the destination. Both files now exist in your working directory.
The source and destination files may also reside in different directories. For instance,
cp /home/chuck/pictures/picture.jpg /home/chuck/backup/picture.jpg
...will make a copy of the file /home/chuck/pictures/picture.jpg in the directory /home/chuck
/backup. The destination file will also be named picture.jpg.
(If you are the user chuck, you can abbreviate your home directory ("/home/chuck") using a tilde
("~"). For instance,
14.01.2015 08:58
Linux and UNIX cp command help and examples
2 von 7
http://www.computerhope.com/unix/ucp.htm
cp ~/pictures/picture.jpg ~/backup/picture.jpg
...functions the same as the above command when it is run by chuck.)
Copying Multiple Files To A Directory
Or, perhaps you want to copy multiple files into another directory. To accomplish this, you can
specify multiple files as the source, and a directory name as the destination. Let's say you are the user
sally, and you have a bunch of files in the directory /home/sally/pictures/ named picture-01.jpg,
picture-02.jpg... and you want to copy them into the directory /home/sally/picture-backup/. This
command will do the trick:
cp ~/pictures/picture-*.jpg ~/picture-backup
Here, we use a wildcard (the asterisk, "*") to indicate that the source files are all the files in the
directory /home/sally/pictures whose name starts with "picture-" and has the extension ".jpg". They
will be copied into the directory /home/sally/picture-backup, assuming that directory already exists.
(If it doesn't exist, cp will give you an error message, and no files will be copied.)
You can also specify multiple source files one after the other, and cp will expect that the final
argument is a directory name, and copy them all there. For instance,
cp ~/pictures/picture-01.jpg ~/pictures/picture-02.jpg ~/picture-backup
...will copy only those two files, /home/sally/picture-01.jpg and /home/sally/picture-02.jpg, into the
directory /home/sally/picture-backup.
Copying Files Recursively
You can use cp to copy entire directory structures from one place to another using the -R option to
perform a recursive copy. Let's say you are the user steve and you have a directory, /home/steve/files,
which contains many files and subdirectories. You want to copy all those files, and all the
subdirectories (and the files and subdirectories they contain), to a new location, /home/steve/filesbackup. You can copy all of them using the command:
14.01.2015 08:58
Linux and UNIX cp command help and examples
http://www.computerhope.com/unix/ucp.htm
cp -R ~/files ~/files-backup
...and the entire directory structure will be copied to the directory /home/steve/files-backup. When
performing a recursive copy:
» if the directory files-backup already exists, the directory files will be placed
inside;
» if files-backup does not already exist, it will be created and the contents of
the files directory will be placed inside it.
Creating Symbolic Links Instead Of Copying Data
Another useful trick is to use cp to create symbolic links to your source files. You may already be
familiar with using the ln command to create symlinks; cp is a great way to create multiple symlinks
all at once.
cp will create symbolic links if you specify the -s option. So, for instance,
cp -s file.txt file2.txt
...will create a symbolic link, file2.txt, which points to file.txt.
You can also create symbolic links from multiple source files, specifying a directory as the
destination.
Note: In order to create symbolic links in another directory, cp needs you to specify the full
pathname, including the full directory name, in your source filename(s). Relative paths will not work.
Let's say you are user melissa and you have a set of files, file01.txt, file02.txt... in the directory
/home/melissa/myfiles. You want to create symbolic links to these files in the existing directory
/home/melissa/myfiles2. This command will do the trick:
cp -s ~/myfiles/file*.txt ~/myfiles2
The directory myfiles2 will now contain symbolic links to the file*.txt in the directory /home/melissa
3 von 7
14.01.2015 08:58
Linux and UNIX cp command help and examples
http://www.computerhope.com/unix/ucp.htm
/myfiles. The myfiles2 directory must already exist for the operation to succeed; if it doesn't exist, cp
will give you an error message and nothing will be copied.
This will work with a recursive copy, as well. So the command:
cp -R -s ~/myfiles ~/myfiles2
...will re-create the directory structure of /home/melissa/myfiles, including any subdirectories and
their contents; any files will be created as symlinks to the originals, but the directories will not be
symbolic links, just regular directories. If myfiles2 already exists, cp will create a directory inside it
called myfiles which contains the directory structure and symlinks; if myfiles2 does not already exist,
it will be created, and contain the subdirectories and symlinks to the files that myfiles contains.
There are many other options you can provide to cp which will affect its behavior. These are listed,
along with the precise command syntax, in the following sections.
Command Syntax
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
Technical Description
cp copies SOURCE to the destination DEST, or multiple SOURCE(s) to directory DIRECTORY.
Options
4 von 7
-a, --archive
Same as -dR --preserve=ALL.
--attributes-only
Don't copy the file data, just create a file with the same attributes.
--backup[=CONTROL]
Make a backup of each existing destination file. CONTROL
specifies the version control method to use; see below for more
14.01.2015 08:58
Linux and UNIX cp command help and examples
http://www.computerhope.com/unix/ucp.htm
details.
5 von 7
-b
Like --backup, but does not accept a CONTROL argument; the
default control method is always used.
--copy-contents
When operating recursively, copy contents of special files.
-d
Same as --no-dereference --preserve=links.
-f, --force
If an existing destination file cannot be opened, remove it and try
again. (This option has no effect if the -n option is used.)
-i, --interactive
Prompt before overwrite (overrides a previous -n option).
-H
Follow command-line symbolic links in SOURCE.
-l, --link
Create hard links to files instead of copying them.
-L, --dereference
Always follow symbolic links in SOURCE. In other words, if
SOURCE is a symlink, copy the file linked to rather than the
symlink itself.
-n, --no-clobber
Do not overwrite an existing file (overrides a previous -i option).
-P, --no-dereference
Never follow symbolic links in SOURCE.
-p
Same as --preserve=mode,ownership,timestamps.
--preserve[=ATTR_LIST]
Preserve the specified attributes (default:
mode,ownership,timestamps), if possible additional attributes:
context, links, xattr, all.
--no-preserve=ATTR_LIST
Don't preserve the specified attributes.
--parents
Use full source file name under DIRECTORY.
-R, -r, --recursive
Copy directories recursively.
--reflink[=WHEN]
Control clone/CoW copies.
--remove-destination
Remove each existing destination file before attempting to open it
(contrast with the --force option).
--sparse=WHEN
Control creation of sparse files.
--strip-trailing-slashes
Remove any trailing slashes from each SOURCE argument.
-s, --symbolic-link
Make symbolic links instead of copying.
-S, --suffix=SUFFIX
Override the usual backup suffix.
-t, --target-
Copy all SOURCE arguments into DIRECTORY
14.01.2015 08:58
Linux and UNIX cp command help and examples
6 von 7
http://www.computerhope.com/unix/ucp.htm
directory=DIRECTORY
-T, --no-target-directory
Treat DEST as a normal file
-u, --update
Copy only when the SOURCE file is newer than the destination
file or when the destination file is missing.
-v, --verbose
Verbose mode; explain what is being done.
-x, --one-file-system
Stay on this file system.
--help
Display a help message and exit.
--version
Output version information and exit.
By default, sparse SOURCE files are detected by a crude heuristic and the corresponding DEST file is
made sparse as well. That is the behavior selected by --sparse=auto. Specify --sparse=always to
create a sparse DEST file whenever the SOURCE file contains a long enough sequence of zero bytes.
Use --sparse=never to inhibit creation of sparse files.
When --reflink[=always] is specified, cp performs a lightweight copy, where the data blocks are
copied only when modified. If this is not possible, the copy fails; or, if --reflink=auto is specified, cp
falls back to a standard copy operation.
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version
control method may be selected via the --backup=CONTROL option or through the
VERSION_CONTROL environment variable. Here are the values of
CONTROL/VERSION_CONTROL:
none, off
never make backups (even if --backup is given).
numbered, t
make numbered backups.
existing, nil
numbered if numbered backups exist, simple otherwise.
simple, never
always make simple backups.
As a special case, cp makes a backup of SOURCE when the force and backup options are given and
SOURCE and DEST are the same name for an existing, regular file.
Examples
14.01.2015 08:58
Linux and UNIX cp command help and examples
http://www.computerhope.com/unix/ucp.htm
cp file1.txt newdir
Copies the file1.txt in the current directory to the newdir subdirectory.
cp /home/public_html/mylog.txt /home/public_html/backup/mylog.bak
Copies the file mylog.txt in the public_html directory into the public_html/backup directory as
mylog.bak.
cp -u *.txt newdir
Copy all files ending in .txt into the newdir directory, but only if the files do not already exist in the
new directory, or if the files being copied are newer.
cp -R /home/hope/files/* /home/hope/backup
Recursively copies all the files, directories, and subdirectories in the /home/hope/files directory into
the /home/hope/backup directory. If the directory backup does not exist in the directory
/home/hope, it will be created.
Related commands
dd — Copy and convert the encoding of files.
ln — Create a link, or a symbolic link, to a file or directory.
mv — Move files and directories from one location to another, and optionally rename them.
7 von 7
14.01.2015 08:58
Download