1 cp 拷贝、复制

NAME

cp - copy files and directories

SYNOPSIS

cp [OPTION]... [-T] SOURCE DEST        -- cp 源位置 目标位置

cp [OPTION]... SOURCE... DIRECTORY

cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION

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

--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 (redundant if the -n option is used)

如果如已有的目标文件不能打开,删之,在尝试一次拷贝

-i, --interactive 交互式拷贝

prompt before overwrite (overrides a previous -n option) 如果目标文件已经存在,在覆盖时或询问是否覆盖

-H follow command-line 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

测试:

[hadoop3@hadoop3 ~]$ ln -s file01.dat file01-slink.dat ##创建软连接文件file01-slink.dat

[hadoop3@hadoop3 ~]$ ll

total 4

-rw-rw-r--. 1 hadoop3 hadoop3 171 Jul 12 10:51 file01.dat

lrwxrwxrwx. 1 hadoop3 hadoop3 10 Jul 12 10:53 file01-slink.dat -> file01.dat

[hadoop3@hadoop3 ~]$ ln file01.dat file01-hlink.dat ##创建硬连接文件file01-hlink.dat

[hadoop3@hadoop3 ~]$ ll

total 8

-rw-rw-r--. 2 hadoop3 hadoop3 171 Jul 12 10:51 file01.dat

-rw-rw-r--. 2 hadoop3 hadoop3 171 Jul 12 10:51 file01-hlink.dat

lrwxrwxrwx. 1 hadoop3 hadoop3 10 Jul 12 10:53 file01-slink.dat -> file01.dat

[hadoop3@hadoop3 ~]$ cp -P file01-slink.dat backup/ ##非解除参考拷贝

[hadoop3@hadoop3 ~]$ ls -al backup/

total 8

drwxrwxr-x. 2 hadoop3 hadoop3 4096 Jul 12 10:56 .

drwx------. 3 hadoop3 hadoop3 4096 Jul 12 10:55 ..

lrwxrwxrwx. 1 hadoop3 hadoop3 10 Jul 12 10:56 file01-slink.dat -> file01.dat ##因为backup目录中没有file01.dat文件,所以下面的cat报错

[hadoop3@hadoop3 backup]$ cat file01-slink.dat

cat: file01-slink.dat: No such file or directory

[hadoop3@hadoop3 ~]$ cp file01.dat backup/ ##手动把符号链接引用的源文件拷贝到backup目录,cat就可以查看符号连接文件的内容了

[hadoop3@hadoop3 ~]$ cd backup/

[hadoop3@hadoop3 backup]$ ls -ln

total 4

-rw-rw-r--. 1 500 500 171 Jul 12 11:05 file01.dat

lrwxrwxrwx. 1 500 500 10 Jul 12 10:56 file01-slink.dat -> file01.dat

[hadoop3@hadoop3 backup]$ cat file01.dat

file01

-l, --link

link files instead of copying

sdfdsf

dgddsgd

gdfgdfhhgfhhfghfghfgjfjhgjhg

1111111111111111111111111111111111111111111111111

-L, --dereference

always follow symbolic links in SOURCE

[hadoop3@hadoop3 ~]$ cp -L file01-slink.dat backup/ ##软连接文件file01-slink.dat拷贝到backup目录中,得到的目的文件的大小为171个字节

[hadoop3@hadoop3 ~]$ ls -ln backup/ ##cp -L 拷贝软连接时,会把连接引用的源文件的内容一起拷贝过来,得到的是一个普通的文件 -- 即解引用

total 4

-rw-rw-r--. 1 500 500 171 Jul 12 11:08 file01-slink.dat

[hadoop3@hadoop3 ~]$ cat backup/file01-slink.dat

file01

-l, --link

link files instead of copying

sdfdsf

dgddsgd

gdfgdfhhgfhhfghfghfgjfjhgjhg

1111111111111111111111111111111111111111111111111

-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

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 移除每个源参数中的末尾斜杠

-l, --link 硬连接

link files instead of copying

-s, --symbolic-link 软连接

make symbolic links instead of copying 做符号连接(当然是软连接)而不是拷贝

-S, --suffix=SUFFIX

override the usual backup suffix 指定通用的备份前缀

-t, --target-directory=DIRECTORY        目标dest是一个目录

copy all SOURCE arguments into DIRECTORY 拷贝所有源参到目录

-T, --no-target-directory                目标dest是一个文件

treat DEST as a normal file 把DEST参数看作为一个普通的文件

-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, --context=CONTEXT

set security context of copy to CONTEXT

--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. 指定--reflink=auto回到标准拷贝模式

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) 如果version_control环境变量值为none,则拷贝时不做备份(即使指定了--backup选项)

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.

cp使用:

1 同时拷贝多个文件

格式: cp 文件1 文件2 目的目录

[hadoop3@hadoop3 ~]$ ls -ln

total 16

-rw-rw-r--. 1 500 500 176 Jul 12 11:15 a_1.txt

-rw-rw-r--. 1 500 500 137 Jul 12 11:16 a_2.txt

-rw-rw-r--. 1 500 500 106 Jul 12 11:16 a_3.txt

drwxrwxr-x. 2 500 500 4096 Jul 12 11:16 files

[hadoop3@hadoop3 ~]$ cp a_1.txt a_2.txt files/

[hadoop3@hadoop3 ~]$ ls -ln files/

total 8

-rw-rw-r--. 1 500 500 176 Jul 12 11:17 a_1.txt

-rw-rw-r--. 1 500 500 137 Jul 12 11:17 a_2.txt

2 拷贝一个目录

[hadoop3@hadoop3 ~]$ cp -r files files_backup

[hadoop3@hadoop3 ~]$ tree

.

├── a_1.txt

├── a_2.txt

├── a_3.txt

├── files

│   ├── a_1.txt

│   └── a_2.txt

└── files_backup

├── a_1.txt

└── a_2.txt

3硬连接拷贝,而不仅仅是拷贝一个文件(拷贝出来的文件与源文件会同步更新的)

[hadoop3@hadoop3 ~]$ mkdir links

[hadoop3@hadoop3 ~]$ cp -l a_2.txt links

[hadoop3@hadoop3 ~]$ ls -ln links

total 4

-rw-rw-r--. 2 500 500 137 Jul 12 11:16 a_2.txt

[hadoop3@hadoop3 ~]$ echo ' adddd' >> a_2.txt ##向源文件追加字符

[hadoop3@hadoop3 ~]$ cat a_2.txt

22222222222222222222222

2222222222222222222222222222222

222222222222222222222222222222222222

2222222222222222222222222222222222222222222

adddd

[hadoop3@hadoop3 ~]$ cat links/a_2.txt ##硬连接文件的内容同步更新了

22222222222222222222222

2222222222222222222222222222222

222222222222222222222222222222222222

2222222222222222222222222222222222222222222

adddd

4 软连接拷贝 -- 符号链接

[hadoop3@hadoop3 ~]$ cp -s a_3.txt links/

cp: `links/a_3.txt': can make relative symbolic links only in current directory

##跨目录不能创建相对符号连接

[hadoop3@hadoop3 ~]$ cp -s /home/hadoop3/a_3.txt links

[hadoop3@hadoop3 ~]$ ls -ln links

total 4

-rw-rw-r--. 2 500 500 145 Jul 12 11:28 a_2.txt

lrwxrwxrwx. 1 500 500 21 Jul 12 11:34 a_3.txt -> /home/hadoop3/a_3.txt

cp拷贝的更多相关文章

  1. ubuntu mount u盘以及cp拷贝文件夹

    如果是ubuntu桌面环境的话,不用mount,接入的U盘就可以直接被系统识别,访问起来非常方便,但如果没有桌面环境呢,比如在ubuntu server端,如何访问U盘呢? 第一步:查看U盘信息sud ...

  2. linux cp 拷贝文件或目录

    cp 拷贝文件或目录 默认不能拷贝目录 常用来备份: [root@MongoDB ~]# cp a.txt /tmp/ [root@MongoDB ~]# cp /root/a.txt /tmp/ c ...

  3. Linux命令(2)--cp拷贝、mv剪切、head、tail追踪、tar归档

    文章目录 一.知识回顾 ls cd 二.Linux基本操作(二) 1.cp 拷贝 2.mv 移动(剪切) 3.head 头部 4.tail 追踪(尾部) 5.tar 归档 查看 压缩 解压 总结 一. ...

  4. linux 命令cp拷贝

    linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...

  5. shell cp拷贝的用法

    个人觉得这个记录的比较全 自己查阅: cp [options] <source file or directory> <target file or directory> 或 ...

  6. linux cp 拷贝不覆盖源文件

    cp 参数 CP() User Commands CP() NAME cp - copy files and directories SYNOPSIS cp [OPTION]... [-T] SOUR ...

  7. docker cp 拷贝文件 和 进入容器

    进入正在运行的容器 # 进入容器 新开一个终端 # docker exec -it 容器id /bin/bash docker exec -it eaac94ef6926 /bin/bash # 进入 ...

  8. cp -拷贝文件出现错误

    对于cp -a最主要的用法是在保留原文件属性的前提下复制文件. 如果出现了拷贝文件错误,在文件前面加上-a 即可

  9. Linux 命令 - cp: 拷贝文件和目录

    命令格式 cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY ...

随机推荐

  1. Hdu 1025(LIS)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  2. TypeScript类型检查机制

    类型推断 指不需要指定变量的类型,TS编译器可以根据某些规则自动推断出类型. 什么时候会有类型推断? 声明变量时没有指定类型 函数默认参数 函数返回值 ...... let a; // 这时自动推断为 ...

  3. Python数据分析与展示[第一周]

    ipython 中的问号 获得相关的描述信息 %run 系统文件 执行某一个文件 ipython的模式命令 %magic 显示所有的魔术命令 %hist    命令历史输入信息 %pdb    异常发 ...

  4. Hibernate QBC 简单收集

    Hibernate QBC 介绍: QBC(Query By Criteria)通过 API 来检索对象 主要由 Criteria 接口.Criterion 接口和 Exception 类组成,支持在 ...

  5. OSI七层模型,作用及其对应的协议

    物理层(Physical Layer):利用传输介质为数据链路层提供物理连接,实现比特流的透明传输 数据链路层(Data Link Layer):负责建立和管理节点间的链路 网络层(Network L ...

  6. 使用淘宝镜像cnpm安装Vue.js

    简介: Vue.js是前端一个比较火的MVVM框架, 是一套构建用户界面的渐进式框架. Vue 只关注视图层, 采用自底向上增量开发的设计. Vue 的目标是通过尽可能简单的 API 实现响应的数据绑 ...

  7. buffer的相关小知识

    php与mysql的连接有三种方式,mysql,mysqli,pdo.不管使用哪种方式进行连接,都有使用buffer和不使用buffer的区别. 什么叫使用buffer和不使用buffer呢? 客户端 ...

  8. R语言-组间差异的非参数检验

    R语言-组间差异的非参数检验 7.5 组间差异的非参数检验 如果数据无法满足t检验或ANOVA的参数假设,可以转而使用非参数方法.举例来说,若结果变量在本质上就严重偏倚或呈现有序关系,那么你可能会希望 ...

  9. 在Eclipse打开文件目录

    1.在Eclipse中使用外部命令 2.配置外部命令 3.设置使用的命令和参数 4.设置使用notepad++打开文件

  10. 探索云数据库最佳实践 阿里云开发者大会数据库专场邀你一起Code up!

    盛夏.魔都.科技 三者在一起有什么惊喜? 7月24日,阿里云峰会·上海——开发者大会将在上海世博中心盛大启程,与未来世界的开发者们分享数据库.云原生.开源大数据等领域的技术干货,共同探讨前沿科技趋势, ...