【linux】cp复制文件或者目录

本文介绍了Linux中的`cp`命令用于复制文件或目录的用法。当遇到因权限问题无法直接拖拽文件到远程目录时,可以使用`cp`命令配合相应选项进行复制。`cp`命令的关键选项包括 `-r` 或 `-R` 用于递归复制目录,`-i` 提示用户确认覆盖,以及 `-v` 显示详细过程。示例包括复制文件夹内容到另一文件夹和整个文件夹的复制。注意,目标目录不存在时,`cp`不会自动创建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【linux】cp复制文件或者目录

1、背景

在使用FinalShell开源工具的时候,发现将本地的文件无法拖拽到远程的某些目录,原因是权限的问题。

此时可以通过加权限来解决,最好的方法是拖拽到有权限的目录,
然后通过linux命令cp拷贝过去。

查看cp命令的说明:

root@iZutmw06cp2Z:~# cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
  -a, --archive                same as -dR --preserve=all
      --attributes-only        don't copy the file data, just the attributes
      --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=links
  -f, --force                  if an existing destination file cannot be
                                 opened, remove it and try again (this option
                                 is ignored when the -n option is also used)
  -i, --interactive            prompt before overwrite (overrides a previous -n
                                  option)
  -H                           follow command-line symbolic links in SOURCE
  -l, --link                   hard link files instead of copying
  -L, --dereference            always follow symbolic links in SOURCE
  -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. See below
      --remove-destination     remove each existing destination file before
                                 attempting to open it (contrast with --force)
      --sparse=WHEN            control creation of sparse files. See below
      --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                 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
  -x, --one-file-system        stay on this file system
  -Z                           set SELinux security context of destination
                                 file to default type
      --context[=CTX]          like -Z, or if CTX is specified then set the
                                 SELinux or SMACK security context to CTX
      --help     display this help 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, perform 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, fall back to a standard copy.

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  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.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/cp>
or available locally via: info '(coreutils) cp invocation'
root@iZutmw06cp2Z:~#

注意上面的说明:用户使用该指令复制目录时,必须使用参数 -r 或者 -R 。

2、示例

以上使用细节请自行研究,本博客提供常用的几个示例。
示例1,将一个文件夹内的内容复制到另一个文件夹下。
比如将./Test1下的所有文件复制到./sourceB 中:

$ sudo cp –r ./Test1/*  ./sourceB  

注意:./sourceB不存在则会报错!且点后缀的文件或者文件夹不会被拷贝,比如.git不会被拷贝!

示例2,将一个文件夹复制到另一个文件夹下。
比如将./Test1文件夹复制到./sourceB 中:

$ sudo cp –r ./Test1   ./sourceB  

注意:
如果./sourceB文件夹不存在,则会创建./sourceB 文件夹,并将./Test1里面的内容复制进来。
如果./sourceB文件夹存在,则会将Test1文件夹拷贝至sourceB文件夹中。

建议使用第二种示例的方式复制:

sudo cp –r ./Test1   ./sourceB  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jn10010537

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值