cp和scp是Linux/Unix上常有物文件备份、文件复制命令。它们的主要区别在于cp为本地磁盘间的文件复制,scp则为跨系统的基于ssh远程登录的的Linux系统间的文件复制。
cp和scp在参数选项上除了用于ssh登录的,基本一致。
常见的参数:
-r -R, -r, --recursive,递归子目录;
-f, --force(cp命令):强制复制全部内容,但要注意一般系统使用cp的别名,去掉了-f,以免目标文件被替换。如果必须使用,可以使用 \cp 执行。
cp的常用参数表:
用法:cp [选项]... [-T] 源 目的
或:cp [选项]... 源... 目录
或:cp [选项]... -t 目录 源...
将<源>文件复制至<目的>,或将多个<源>复制至<目录>。
长选项必须用的参数在使用短选项时也是必须的。
-a, --archive same as -dR --preserve=all
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
--copy-contents copy contents of special files when recursive
-d same as --no-dereference --preserve=link
-f, --force if an existing destination file cannot be
opened, remove it and try again
-i, --interactive prompt before overwrite
-H follow command-line symbolic links
-l, --link 链接文件而不复制
-L, --dereference 总是跟随符号链接
-P, --no-dereference 从不跟随符号链接
-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
-c same as --preserve=context
--no-preserve=ATTR_LIST don't preserve the specified attributes
--parents use full source file name under DIRECTORY
-R, -r, --recursive 复制目录及目录内的所有项目
--remove-destination 尝试打开目的地文件前先删除已存在的目的地
文件 (与 --force 选项作对比)
--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-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-u, --update 只在<源>文件比目的地文件新,或目的地文件
不存在时才进行复制
-v, --verbose 详细显示进行的步骤
-x, --one-file-system 不会跨越文件系统进行操作
-Z, --context=CONTEXT set security context of copy to CONTEXT
--help 显示此帮助信息并退出
--version 输出版本信息并退出
默认使用模式中,<源>文件是否 sparse 文件会由一种粗略的方式决定,而且相应
的<目的地>文件也会是 sparse 文件。此方式等于使用 --sparse=auto 选项。指定
--sparse=always 则只要<源>文件含有足够长的 0 字节都会产生 sparse 的
<目的地>文件。
使用 --sparse=never 会禁止产生 sparse 文件。
备份文件的后缀为“~”,除非以 --suffix 选项或是 SIMPLE_BACKUP_SUFFIX
环境变量指定。版本控制的方式可透过 --backup 选项或 VERSION_CONTROL 环境
变量来选择。以下是可用的变量值:
none, off 不会进行备份 (即使使用了 --backup 选项)
numbered, t 备份文件会加上数字
existing, nil 若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
simple, never 永远使用普通方式备份
有一个特别情况:如果同时指定 --force 和 --backup 选项,而且<源>和<目的地>
是同一个已存在的一般文件的话,cp 会将<源>文件备份。
scp的常用参数表:
NAME
scp - secure copy (remote file copy program)
选项:
scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port]
[-S program] [[user@]host1:]file1 [...] [[user@]host2:]file2
介绍:
scp copies files between hosts on a network. It uses ssh(1) for data transfer, and uses the same authentication
and provides the same security as ssh(1). Unlike rcp(1), scp will ask for passwords or passphrases if they are
needed for authentication.
Any file name may contain a host and user specification to indicate that the file is to be copied to/from that
host. Copies between two remote hosts are permitted.
When copying a source file to a target file which already exists, scp will replace the contents of the target
file (keeping the inode).
If the target file does not yet exist, an empty file with the target file name is created, then filled with the
source file contents. No attempt is made at "near-atomic" transfer using temporary files.
选项说明:
-1 Forces scp to use protocol 1 (使用SSH1).
-2 Forces scp to use protocol 2 (使用SSH2).
-4 Forces scp to use IPv4 addresses only (使用IPv4).
-6 Forces scp to use IPv6 addresses only (使用IPv6).
-B Selects batch mode (prevents asking for passwords or passphrases).
-C Compression enable. Passes the -C flag to ssh(1) to enable compression.
-c cipher
Selects the cipher to use for encrypting the data transfer. This option is directly passed to ssh(1).
-F ssh_config
Specifies an alternative per-user configuration file for ssh. This option is directly passed to ssh(1).
-i identity_file
Selects the file from which the identity (private key) for RSA authentication is read. This option is
directly passed to ssh(1).
-l limit
Limits the used bandwidth, specified in Kbit/s.
-o ssh_option
Can be used to pass options to ssh in the format used in ssh_config(5). This is useful for specifying
options for which there is no separate scp command-line flag. For full details of the options listed
below, and their possible values, see ssh_config(5).
AddressFamily
BatchMode
BindAddress
ChallengeResponseAuthentication
CheckHostIP
Cipher
Ciphers
Compression
CompressionLevel
ConnectionAttempts
ConnectTimeout
ControlMaster
ControlPath
GlobalKnownHostsFile
GSSAPIAuthentication
GSSAPIDelegateCredentials
HashKnownHosts
Host
HostbasedAuthentication
HostKeyAlgorithms
HostKeyAlias
HostName
IdentityFile
IdentitiesOnly
KbdInteractiveDevices
LogLevel
MACs
NoHostAuthenticationForLocalhost
NumberOfPasswordPrompts
PasswordAuthentication
Port
PreferredAuthentications
Protocol
ProxyCommand
PubkeyAuthentication
RekeyLimit
RhostsRSAAuthentication
RSAAuthentication
SendEnv
ServerAliveInterval
ServerAliveCountMax
SmartcardDevice
StrictHostKeyChecking
TCPKeepAlive
UsePrivilegedPort
User
UserKnownHostsFile
VerifyHostKeyDNS
-P port
Specifies the port to connect to on the remote host. Note that this option is written with a capital
‘P’, because -p is already reserved for preserving the times and modes of the file in rcp(1).
-p Preserves modification times, access times, and modes from the original file.
-q Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh(1).
-r Recursively copy entire directories.
-S program
Name of program to use for the encrypted connection. The program must understand ssh(1) options.
-v Verbose mode. Causes scp and ssh(1) to print debugging messages about their progress. This is helpful
in debugging connection, authentication, and configuration problems.
DIAGNOSTICS
scp exits with 0 on success or >0 if an error occurred.
IPV6
IPv6 address can be used everywhere where IPv4 address. In all entries must be the IPv6 address enclosed in
square brackets. Note: The square brackets are metacharacters for the shell and must be escaped in shell.
SEE ALSO
rcp(1), sftp(1), ssh(1), ssh-add(1), ssh-agent(1), ssh-keygen(1), ssh_config(5), sshd(8)
HISTORY
scp is based on the rcp(1) program in BSD source code from the Regents of the University of California.