đź“ť Edit page
âž• Add page
cp
The copy shell command
Usage
cp [OPTIONS] SOURCE DESTINATION
cp [OPTIONS] SOURCE_A SOURCE_B [...] DESTINATION
Common situations
Copy files
Be careful of trailing slash when copying to or from a directory name. I find it works better without. Or maybe that is only rsync
where is makes a difference?
$ cp foo.txt bazz
Copy multiple sources.
$ cp foo.txt bar.txt bazz
$ cp *.mp3 bazz
Copy to current location.
$ cp ~/foo.txt .
Copy directories
Use the -r
flag for recursive. If you omit the flag then an error message will suggest it. Note that -R
is similar but not the same.
$ cp -r SOURCE DEST
Copy foo
itself into bazz
.
$ cp -r foo bazz
Copy contents of foo
into bazz
.
$ cp -r foo/ bazz
Copy paths with a directory. You will see -r
flag if there are directories in foo
and not just files.
$ cp foo/* bazz
Copy interactively
Prevent overwriting of targets. You’ll have to enter y
or n
for each file.
$ cp -i SOURCE DEST
To always use:
alias cp='cp -i'
And then to skip interactive mode selectively:
$ command cp SOURCE DEST
Note that the -f
flag doesn’t help here.
Verbose
$ cp -v SOURCE DEST
Help
Linux manpage - link.
cp
Copy one or more files to another location.
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Syntax
cp [options]... Source Dest
cp [options]... Source... Directory
Key
-a, --archive same as -dpR
-b, --backup Make backup before removal.
If the copy will overwrite a file in the destination, then
the original file will be backed up as 'filename~' before
being overwritten.
-d, --no-dereference preserve links
-f, --force remove existing destinations, never prompt
-i, --interactive prompt before overwrite
-l, --link link files instead of copying
-p, --preserve preserve file attributes if possible
-P, --parents append source path to DIRECTORY
-r copy recursively, non-directories as files
--sparse=WHEN control creation of sparse files
-R, --recursive copy directories recursively
-s, --symbolic-link make symbolic links instead of copying
-S, --suffix=SUFFIX override the usual backup suffix
-u, --update copy only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
-V, --version-control=WORD override the usual version control
-x, --one-file-system stay on this file system
--help display this help and exit
--version output version information and exit.