linux学习笔记7---命令cp

cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一。

cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录。它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下。cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,否则将出现错误。

一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数。但是如果是在shell脚本中执行cp时,没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有些不同。

1.命令格式:

用法:

cp [选项]... [-T] 源 目的

或:cp [选项]... 源... 目录

或:cp [选项]... -t 目录 源...

2.命令功能:

将源文件复制至目标文件,或将多个源文件复制至目标目录。

3.命令参数:

-a, --archive    等于-dR --preserve=all

--backup[=CONTROL    为每个已存在的目标文件创建备份,新的文件或者目录和原文件或者目录具有相同的创建时间。

-b                类似--backup 但不接受参数

--copy-contents        在递归处理是复制特殊文件内容

-d                等于--no-dereference --preserve=links

-f, --force        如果目标文件无法打开则将其移除并重试(当 -n 选项

存在时则不需再选此项)

-i, --interactive        覆盖前询问(使前面的 -n 选项失效)

-H                跟随源文件中的命令行符号链接

-l:对源文件建立硬连接,而非复制文件;

-s:对源文件建立符号连接,而非复制文件;

-L, --dereference   总是跟随符号链接

-n, --no-clobber   不要覆盖已存在的文件(使前面的 -i 选项失效)

-P, --no-dereference   不跟随源文件中的符号链接

-p                等于--preserve=模式,所有权,时间戳.  -p:保留源文件或目录的属性;

--preserve[=属性列表   保持指定的属性(默认:模式,所有权,时间戳),如果

可能保持附加属性:环境、链接、xattr 等

-R, -r, --recursive  复制目录及目录内的所有项目

4.命令实例:

实例一:复制单个文件到目标目录,文件在目标文件中不存在

命令:

cp log.log test5

输出:

[[email protected] test]# cp log.log test5

[[email protected] test]# ll

-rw-r--r-- 1 root root    0 10-28 14:48 log.log

drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxrwx 2 root root 4096 10-28 14:47 test3

drwxr-xr-x 2 root root 4096 10-28 14:53 test5

[[email protected] test]# cd test5

[[email protected] test5]# ll

-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log

-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log

-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log

-rw-r--r-- 1 root root 0 10-28 14:53 log.log

说明:

在没有带-a参数时,两个文件的时间是不一样的。在带了-a参数时,两个文件的时间是一致的。

实例二:目标文件存在时,会询问是否覆盖

命令:

cp log.log test5

输出:

[[email protected] test]# cp log.log test5

cp:是否覆盖“test5/log.log”? n

[[email protected] test]# cp -a log.log test5

cp:是否覆盖“test5/log.log”? y

[[email protected] test]# cd test5/

[[email protected] test5]# ll

-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log

-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log

-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log

-rw-r--r-- 1 root root 0 10-28 14:48 log.log

说明:

目标文件存在时,会询问是否覆盖。这是因为cp是cp -i的别名。目标文件存在时,即使加了-f标志,也还会询问是否覆盖。

实例三:复制整个目录

命令:

输出:

目标目录存在时:

[[email protected] test]# cp -a test3 test5

[[email protected] test]# ll

-rw-r--r-- 1 root root    0 10-28 14:48 log.log

drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxrwx 2 root root 4096 10-28 14:47 test3

drwxr-xr-x 3 root root 4096 10-28 15:11 test5

[[email protected] test]# cd test5/

[[email protected] test5]# ll

-rw-r--r-- 1 root root    0 10-28 14:46 log5-1.log

-rw-r--r-- 1 root root    0 10-28 14:46 log5-2.log

-rw-r--r-- 1 root root    0 10-28 14:46 log5-3.log

-rw-r--r-- 1 root root    0 10-28 14:48 log.log

drwxrwxrwx 2 root root 4096 10-28 14:47 test3

目标目录不存在时候:

[[email protected] test]# cp -a test3 test4

[[email protected] test]# ll

-rw-r--r-- 1 root root    0 10-28 14:48 log.log

drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxrwx 2 root root 4096 10-28 14:47 test3

drwxrwxrwx 2 root root 4096 10-28 14:47 test4

drwxr-xr-x 3 root root 4096 10-28 15:11 test5

[[email protected] test]#

说明:

注意目标目录存在与否结果是不一样的。目标目录存在时,整个源目录被复制到目标目录里面。如果目标目录不存在,整个源目录被复制到一个新的目录里,有点类似复制了一个;但是必须加上参数-a,否则没法复制。

实例四:复制的 log.log 建立一个连结档 log_link.log

命令:

cp -s log.log log_link.log

输出:

[[email protected] test]# cp -s log.log log_link.log

[[email protected] test]# ll

lrwxrwxrwx 1 root root    7 10-28 15:18 log_link.log -> log.log

-rw-r--r-- 1 root root    0 10-28 14:48 log.log

drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxrwx 2 root root 4096 10-28 14:47 test3

drwxrwxrwx 2 root root 4096 10-28 14:47 test4

drwxr-xr-x 3 root root 4096 10-28 15:11 test5

说明:

那个 log_link.log 是由 -s 的参数造成的,建立的是一个『快捷方式』,所以您会看到在文件的最右边,会显示这个文件是『连结』到哪里去的!

我们在Linux下使用cp命令复制文件时候,有时候会需要覆盖一些同名文件,覆盖文件的时候都会有提示:需要不停的按Y来确定执行覆盖。文件数量不多还好,但是要是几百个估计按Y都要吐血了,于是折腾来半天总结了一个方法: cp aaa/* /bbb 复制目录aaa下所有到/bbb目录下,这时如果/bbb目录下有和aaa同名的文件,需要按Y来确认并且会略过aaa目录下的子目录。 cp -r aaa/* /bbb 这次依然需要按Y来确认操作,但是没有忽略子目录。 cp -r -a aaa/* /bbb 依然需要按Y来确认操作,并且把aaa目录以及子目录和文件属性也传递到了/bbb。 \cp -r -a aaa/* /bbb 成功,没有提示按Y、传递了目录属性、没有略过目录。

我常用的cp命令:

1.cp sourcefile dirname :将一个文件复制到目录diraname下面。

2.cp  dir/*    dir     :将一个目录下的所有文件复制到目录diraname下面。

3.cp -pR  dir  dir2     :将一个文件夹下面的所有文件复制到目录dir2下面。

4.cp -a dir1 dir2    :将一个文件复制到目录diraname下面,而且新路径下的文件夹有源文件的相同属性。

如果在软连接和硬链接的文件里写入内容的话,源文件也会发生变化。

时间: 2024-08-02 11:32:11

linux学习笔记7---命令cp的相关文章

linux学习笔记-type命令

语法: type [-tpa] name 参数: type:    不加任何参数时,type会显示出name是外部命令还是内部bash内置命令 -t:      当加入-t参数时,type会将name以下面这些字眼显示出它的意义 file:表示为外部命令 alias:表示该命令为命令别名所设置的名称 builtin:表示该命令为bash内置的命令功能 -p:     如果后面接的name为外部命令时,才显示完整文件名 -a:     会有path变量定义的路径中,将所有含有name的命令都列出来

Linux学习笔记:命令(一)

linux常用命令 1.关机 shutdown -h now 立刻关机 poweroff shutdown -r now 立刻重启 reboot logout 注销 2.进入图形界面 startx 3.vi编辑器 [vi] [path]/[file]: [i]进入编辑模式: [esc]进入命令模式: [:wq]保存并退出:[:q!]退出不保存. 4.显示列表 ls 列表 ls -l 详细列表 长列表格式 ls -a 显示隐藏文件 ll 详细列表 ls -l /bin/*sh 列表bin目录中以s

Linux学习笔记——常用命令(一)

Linux分区的四个基本步骤: 1)分区:硬盘划分为逻辑分区 2)格式化逻辑分区(写入文件系统) 3)分区设备文件名:给每个分区定义设备文件名 4)挂载点:给每个分区分配挂载点 注意事项: 1)必须分区: /   /boot  /swap 2)一块硬盘最多4个分区,最多1个扩展分区,扩展分区又可以包含多个逻辑分区 设置密码原则: 1)复杂性 2)易记性 3)时效性 目录usr(unlix software resource) 防火墙:用来过滤,制定一系列的规则(IP.MAC.端口等) Linux

linux学习笔记——基础命令、快捷键与认识虚拟机

虚拟机[[email protected] Desktop]$ rht-vmctl start desktop     ###开启Starting desktop.[[email protected] Desktop]$ rht-vmctl view desktop    ##显示[[email protected] Desktop]$ rht-vmctl stop desktop    ##正常关闭虚拟机[[email protected] Desktop]$ rht-vmctl powero

Linux学习笔记——find命令

哎呀,随着年龄的增大记性越来越差了之前学过的一些命令也就依稀记得写命令名字了(大写的监介,你懂得),趁着年前还有点时间就先把find命令简单整理一下吧,其实都知道葫芦里卖的什么药(手动哈哈)! 有人可能会说了记得名字其他不就好办了吗,直接命令行真男人(man)一下就可以拯救世界了,话虽如此,谁让本人只对女人感兴趣咧~,不扯了开始正题. 先看下find用法格式: find   [path...] [expression] 常用选项 -name:指明要查询的文件名: -type :指明所要查询的文件

Linux学习笔记--which命令(搜索命令的命令)

which,哪一个的意思,作用是从PATH环境变量指定的路径中,搜索命令所在位置及命令别名. which命令特点: 1) "which" 命令只能查找系统命令,不能搜索普通文件. 2) "which" 命令是从PATH环境变量指定的路径中,搜索某个系统命令的位置, 并且返回第一个搜索结果. PATH环境变量: PATH:决定了shell将到哪些目录中寻找命令或程序,PATH的值是一系列目录,用":"隔开,当您运行一个命令时,Linux在这些目录下

Linux学习笔记--locate命令(文件搜索命令)

locate,定位的意思,作用是让使用者可以快速的搜寻系统中是否有指定的文件. locate 命令特点: 1) "locate"的速度比"find"快,因为它并不是真的查找文件,而是查数据库. 2) 新建的文件,我们立即用"locate"命令去查找,一般是找不到的, 因为数据库的更新不是实时的,数据库的更新时间由系统维护. 3) "locate"命令所搜索的后台数据库在"/var/lib/mlocate"这

Linux学习笔记9--touch命令使用

NAME       touch - change file timestamps --改变文件的时间记录 SYNOPSIS       touch [OPTION]... FILE... DESCRIPTION       Update  the  access and modification times of each FILE to the current       time. --使用touch指令可更改文件或目录的日期时间,包括存取时间和更改时间. Mandatory argume

Linux学习笔记--rm命令(删除文件或目录)

rm:英文名remove,删除的意思. 1. 命令格式: rm [选项] 文件或目录 2. 常用选项: "rm -f" 强行删除,忽略不存在的文件,不提示确认.(f为force的意思) "rm -i" 进行交互式删除,即删除时会提示确认.(i为interactive的意思) "rm -r" 将参数中列出的全部目录和子目录进行递归删除.(r为recursive的意思) "rm -v" 详细显示删除操作进行的步骤.(v为verbo