3.12 Linux cp命令:复制文件和目录
-
cp 命令,主要用来复制文件和目录,同时借助某些选项,还可以实现复制整个目录,以及比对两文件的新旧而予以升级等功能。
-
cp 命令的基本格式如下:
[root@CncLucZK ~]# cp [选项] 源文件 目标文件
- 选项:
- -a:相当于 -d、-p、-r 选项的集合,这几个选项我们一一介绍;
- -d:如果源文件为软链接(对硬链接无效)即链接档的属性(link file),则复制出的目标文件也为软链接非文件本身;
- -f: 为强制(force)的意思,若目标文件已经存在且无法开启,则移除后再尝试一次;
- -i:询问,如果目标文件已经存在,则会询问是否覆盖;
- -l:把目标文件建立为源文件的硬链接文件,而不是复制源文件;
- -s:把目标文件建立为源文件的软链接文件,而不是复制源文件;
- -p:复制后目标文件保留源文件的属性(包括所有者、所属组、权限和时间),而非使用默认属性(备份常用);
- -r:递归复制,用于复制目录;
- -u:若目标文件比源文件有差异,则使用该选项可以更新目标文件,此选项可用于对文件的升级和备用。
注意,源文件可以有多个,但这种情况下,目标文件必须是目录才可以。
软链接,类似于 Windows 系统中的快捷方式,而硬链接则是透过文件系统的 inode 号产生一个新的文件名。无论是复制软链接还是硬链接,都不是复制源文件
- cp 命令既可以复制文件,也可以复制目录。我们先来看看如何复制文件
123456[root@CncLucZK test]# cp tmp.txt user/ #把源文件不改名复制到 user/ 目录下
[root@CncLucZK test]# ls -l
total 24
-rw-r--r-- 1 root root 90 Oct 5 00:15 demo.txt
-rw-r--r-- 2 root root 6 Oct 8 22:28 hardlink.txt
-rw-r--r-- 1 root root 0 Oct 8 14:54 softlink
-rw-r--r-- 1 root root 381 Oct 5 18:14 test1.sh
-rwxr-xr-x 1 root root 117 Oct 5 18:05 test.sh
lrwxrwxrwx 1 root root 8 Oct 8 14:54 tmp -> softlink
-rw-r--r-- 2 root root 6 Oct 8 22:28 tmp.txt
drwx--x--x 3 root root 4096 Oct 8 22:36 user
[root@CncLucZK test]# ls -l user
total 8
drwxr-xr-x 2 root root 4096 Oct 6 09:09 tmp
-rw-r--r-- 1 root root 6 Oct 8 22:36 tmp.txt
- 可以改名复制,则命令如下:
[root@CncLucZK test]# cp tmp.txt user/demo.txt #改名复制
[root@CncLucZK test]# ls -l user
total 12
-rw-r--r-- 1 root root 6 Oct 8 22:38 demo.txt
drwxr-xr-x 2 root root 4096 Oct 6 09:09 tmp
-rw-r--r-- 1 root root 6 Oct 8 22:36 tmp.txt
- 如果复制的目标位置已经存在同名的文件,则会提示是否覆盖,因为 cp 命令默认执行的是“cp -i”的别名,例如:
[root@CncLucZK test]# cp tmp.txt user/tmp.txt
cp: overwrite 'user/tmp.txt'? #目标位置有同名文件,所以会提示是否覆盖 y:表示同意操作,n:表示否定操作
- 复制目录只需使用“-r”选项即可
[root@CncLucZK test]# cp -r user users
[root@CncLucZK test]# ls -l
total 28
-rw-r--r-- 1 root root 90 Oct 5 00:15 demo.txt
-rw-r--r-- 2 root root 6 Oct 8 22:28 hardlink.txt
-rw-r--r-- 1 root root 0 Oct 8 14:54 softlink
-rw-r--r-- 1 root root 381 Oct 5 18:14 test1.sh
-rwxr-xr-x 1 root root 117 Oct 5 18:05 test.sh
lrwxrwxrwx 1 root root 8 Oct 8 14:54 tmp -> softlink
-rw-r--r-- 2 root root 6 Oct 8 22:28 tmp.txt
drwx--x--x 3 root root 4096 Oct 8 22:38 user
drwx--x--x 3 root root 4096 Oct 8 22:42 users
[root@CncLucZK test]# ls -l users
total 12
-rw-r--r-- 1 root root 6 Oct 8 22:42 demo.txt
drwxr-xr-x 2 root root 4096 Oct 8 22:42 tmp
-rw-r--r-- 1 root root 6 Oct 8 22:42 tmp.txt
- 复制软链接的属性
[root@CncLucZK test]# ll tmp #源文件本身就是一个软链接文件
lrwxrwxrwx 1 root root 8 Oct 8 14:54 tmp -> softlink
[root@CncLucZK test]# cp tmp tmp_nod #复制软链接文件,但是不加"-d"选项
[root@CncLucZK test]# ll tmp_nod
-rw-r--r-- 1 root root 9 Oct 8 22:50 tmp_nod #不加"-d"选项,实际复制的是软链接的源文件,而不是软链接文件
[root@CncLucZK test]# cp -d tmp tmp_d #复制软链接文件,加入"-d"选项
[root@CncLucZK test]# ll tmp_d
lrwxrwxrwx 1 root root 8 Oct 8 22:51 tmp_d -> softlink
#加入了"-d"选项,则会复制软链接文件
- 保留源文件属性复制,在执行复制命令后,目标文件的时间会变成复制命令的执行时间,而不是源文件的时间
[root@CncLucZK test]# cp -d tmp tmp_d
[root@CncLucZK test]# ll tmp
lrwxrwxrwx 1 root cnc 8 Oct 8 14:54 tmp -> softlink
#注意源文件的时间和所属组
[root@CncLucZK test]# ll tmp_d
lrwxrwxrwx 1 root root 8 Oct 8 22:51 tmp_d -> softlink
#由于复制命令由root用户执行,所以目标文件的所属组为了root,而且时间也变成了复制命令的执行时间
- 执行备份、曰志备份的时候,这些文件的时间可能是一个重要的参数,这就需执行 “-p” 选项了。这个选项会保留源文件的属性,包括所有者、所属组和时间。
[root@CncLucZK test]# cp -dp tmp tmp_p
#使用"-p"选项
[root@CncLucZK test]# ll tmp tmp_p
lrwxrwxrwx 1 root root 8 Oct 8 14:54 tmp -> softlink
lrwxrwxrwx 1 root root 8 Oct 8 14:54 tmp_p -> softlink
#源文件和目标文件的所有属性都一致,包括时间
可以使用 “-a” 选项来取代 “-d、-p、-r” 选项更加方便
- 使用 “-l” 选项,则目标文件会被建立为源文件的硬链接;而如果使用了 “-s” 选项,则目标文件会被建立为源文件的软链接。这两个选项和 “-d” 选项是不同的,“d” 选项要求源文件必须是软链接,目标文件才会复制为软链接;而 “-l” 和 “-s” 选项的源文件只需是普通文件,目标文件就可以直接复制为硬链接和软链接。
[root@CncLucZK test]# ll -i tmp.txt
1048585 -rw-r--r-- 3 root root 6 Oct 8 22:28 tmp.txt
#源文件只是一个普通文件,而不是软链接文件
[root@CncLucZK test]# cp -l tmp.txt tmp_h.txt
[root@CncLucZK test]# cp -s tmp.txt tmp_s.txt
[root@CncLucZK test]# ll
total 36
-rw-r--r-- 1 root root 90 Oct 5 00:15 demo.txt
-rw-r--r-- 3 root root 6 Oct 8 22:28 hardlink.txt
-rw-r--r-- 1 root root 9 Oct 8 22:45 softlink
-rw-r--r-- 1 root root 381 Oct 5 18:14 test1.sh
-rwxr-xr-x 1 root root 117 Oct 5 18:05 test.sh
-rw-r--r-- 3 root root 6 Oct 8 22:28 tmp_h.txt
#目标文件 /tmp_h 为源文件的硬链接文件
lrwxrwxrwx 1 root root 7 Oct 8 23:18 tmp_s.txt -> tmp.txt
#目标文件 /tmp_s 为源文件的软链接文件
-rw-r--r-- 3 root root 6 Oct 8 22:28 tmp.txt
drwx--x--x 3 root root 4096 Oct 8 22:38 user
drwx--x--x 3 root root 4096 Oct 8 22:42 users
3.13 Linux rm命令:删除文件或目录
- rm 是强大的删除命令,它可以永久性地删除文件系统中指定的文件或目录。在使用 rm 命令删除文件或目录时,系统会产生提示信息。此命令的基本格式为:
[root@CncLucZK test]# rm[选项] 文件或目录
- 选项:
- -f:强制删除(force),和 -i 选项相反,使用 -f,系统将不再询问,而是直接删除目标文件或目录。
- -i:和 -f 正好相反,在删除文件或目录之前,系统会给出提示信息,使用 -i 可以有效防止不小心删除有用的文件或目录,默认删除时的选项。
- -r:递归删除,主要用于删除目录,可删除指定目录及包含的所有内容,包括所有的子目录和文件。
注意,rm 命令是一个具有破坏性的命令,因为 rm 命令会永久性地删除文件或目录,这就意味着,如果没有对文件或目录进行备份,一旦使用 rm 命令将其删除,将无法恢复
- rm 命令如果任何选项都不加,则默认执行的是”rm -i 文件名”,也就是在删除一个文件之前会先询问是否删除
[root@CncLucZK test]# rm tmp.txt
rm: remove regular file 'tmp.txt'? #是否删除普通空文件
#删除前会询问是否删除
- 如果需要删除目录,则需要使用”-r”选项。例如:
[root@CncLucZK test]# rm users
rm: cannot remove 'users': Is a directory
#如果不加"-r"选项,则会报错
[root@CncLucZK test]# rm -r users
rm: descend into directory 'users'? y
rm: remove directory 'users/tmp'? y
rm: remove regular file 'users/demo.txt'? y
rm: remove regular file 'users/tmp.txt'? y
rm: remove directory 'users'? y
#会分别询问是否进入子目录、是否删除子目录
- 强制删除:如果要删除的目录中有 上万个子目录或子文件,那么普通的 rm 删除最少需要确认上万次。所以,在真正删除文件的时候,我们会选择强制删除
[root@CncLucZK test]# rm -rf users
加入了强制功能之后,删除就会变得很简单,但是需要注意,数据强制删除之后无法恢复,除非依赖第三方的数据恢复工具
虽然 “-rf” 选项是用来删除目录的,但是删除文件也不会报错。所以,为了使用方便,一般不论是删除文件还是删除目录,都会直接使用 “-rf” 选项。
3.14 Linux mv命令:移动文件、目录或改名
- mv 命令(move 的缩写),既可以在不同的目录之间移动文件或目录,也可以对文件和目录进行重命名。该命令的基本格式如下:
[root@CncLucZK ~]# mv 【选项】 源文件 目标文件
[root@CncLucZK ~]# mv 【选项】 source1 source2 source3 .... directory
- 选项:
- -f:强制覆盖,如果目标文件已经存在,则不询问,直接强制覆盖;
- -i:交互移动,如果目标文件已经存在,则询问用户是否覆盖(默认选项);
- -n:如果目标文件已经存在,则不会覆盖移动,而且不询问用户;
- -v:显示文件或目录的移动过程;
- -u:若目标文件已经存在,但两者相比,源文件更新,则会对目标文件进行升级,只替换更新过的文件;
注意,同 rm 命令类似,mv 命令也是一个具有破坏性的命令.
- 移动文件或目录
[root@CncLucZK test]# mkdir config
[root@CncLucZK test]# ll
total 40
drwxr-xr-x 2 root root 4096 Oct 8 23:49 config
-rw-r--r-- 1 root root 90 Oct 5 00:15 demo.txt
-rw-r--r-- 3 root root 6 Oct 8 22:28 hardlink.txt
-rw-r--r-- 1 root root 9 Oct 8 22:45 softlink
-rw-r--r-- 1 root root 381 Oct 5 18:14 test1.sh
-rwxr-xr-x 1 root root 117 Oct 5 18:05 test.sh
-rw-r--r-- 3 root root 6 Oct 8 22:28 tmp_h.txt
lrwxrwxrwx 1 root root 7 Oct 8 23:18 tmp_s.txt -> tmp.txt
-rw-r--r-- 3 root root 6 Oct 8 22:28 tmp.txt
drwx--x--x 3 root root 4096 Oct 8 22:38 user
drwx--x--x 3 root root 4096 Oct 8 22:38 users
[root@CncLucZK test]# mv tmp_h.txt users
[root@CncLucZK test]# mv config users
#移动之后,源文件会被删除,类似剪切
[root@CncLucZK test]# ll
total 32
-rw-r--r-- 1 root root 90 Oct 5 00:15 demo.txt
-rw-r--r-- 3 root root 6 Oct 8 22:28 hardlink.txt
-rw-r--r-- 1 root root 9 Oct 8 22:45 softlink
-rw-r--r-- 1 root root 381 Oct 5 18:14 test1.sh
-rwxr-xr-x 1 root root 117 Oct 5 18:05 test.sh
lrwxrwxrwx 1 root root 7 Oct 8 23:18 tmp_s.txt -> tmp.txt
-rw-r--r-- 3 root root 6 Oct 8 22:28 tmp.txt
drwx--x--x 3 root root 4096 Oct 8 22:38 user
drwx--x--x 4 root root 4096 Oct 8 23:50 users
[root@CncLucZK test]# ll users
total 20
drwxr-xr-x 2 root root 4096 Oct 8 23:49 config
-rw-r--r-- 1 root root 6 Oct 8 22:38 demo.txt
drwxr-xr-x 2 root root 4096 Oct 6 09:09 tmp
-rw-r--r-- 3 root root 6 Oct 8 22:28 tmp_h.txt
-rw-r--r-- 1 root root 6 Oct 8 22:36 tmp.txt
#也可以移动目录。和 rm、cp 不同的是,mv 移动目录不需要加入 "-r" 选项
- 如果移动的目标位置已经存在同名的文件,则同样会提示是否覆盖,因为 mv 命令默认执行的也是 “mv -i” 的别名,例如:
[root@CncLucZK test]# cp -a tmp.txt tmp_h.txt
#重新建立文件
[root@CncLucZK test]# mv tmp_h.txt users
mv: overwrite 'users/tmp_h.txt'? n
#由于 /users 目录下已经存在 tmp_h.txt 文件,所以会提示是否覆盖,需要手工输入 y 覆盖移动,n不移动
- 强制移动:如果目标目录下已经存在同名文件,则会提示是否覆盖,需要手工确认。这时如果移动的同名文件较多,则需要一个一个文件进行确认,很不方便。
[root@CncLucZK test]# mv -f tmp_h.txt users
#就算 /users/ 目录下已经存在同名的文件,由于"-f"选项的作用,所以会强制覆盖
- 不覆盖移动:既然可以强制覆盖移动,那也有可能需要不覆盖的移动。如果需要移动几百个同名文件,但是不想覆盖,这时就需要 “-n” 选项的帮助了。例如:
[root@CncLucZK test]# cd user
[root@CncLucZK user]# ll
total 12
-rw-r--r-- 1 root root 6 Oct 8 22:38 demo.txt
drwxr-xr-x 2 root root 4096 Oct 6 09:09 tmp
-rw-r--r-- 1 root root 6 Oct 8 22:36 tmp.txt
[root@CncLucZK user]# ll ../users
total 20
drwxr-xr-x 2 root root 4096 Oct 8 23:49 config
-rw-r--r-- 1 root root 6 Oct 8 22:38 demo.txt
drwxr-xr-x 2 root root 4096 Oct 6 09:09 tmp
-rw-r--r-- 3 root root 6 Oct 8 22:28 tmp_h.txt
-rw-r--r-- 1 root root 6 Oct 8 22:36 tmp.txt
[root@CncLucZK user]# cd ../users
[root@CncLucZK users]# mv -vn config demo.txt tmp.txt ../user
renamed 'config' -> '../user/config'
#向 /user/ 目录中移动同名文件,如果使用了 "-n" 选项,则可以看到只移动了config目录,由于 /user 目录下已经存在 demo.txt tmp.txt文件 并没有移动("-v" 选项用于显示移动过程)
- 改名:如果源文件和目标文件在同一目录中,那就是改名.目录也可以按照同样的方法改名。
[root@CncLucZK test]# mv tmp_h.txt tmp_h1.txt
[root@CncLucZK test]# ll
total 36
-rw-r--r-- 1 root root 90 Oct 5 00:15 demo.txt
-rw-r--r-- 3 root root 6 Oct 8 22:28 hardlink.txt
-rw-r--r-- 1 root root 9 Oct 8 22:45 softlink
-rw-r--r-- 1 root root 381 Oct 5 18:14 test1.sh
-rwxr-xr-x 1 root root 117 Oct 5 18:05 test.sh
-rw-r--r-- 1 root root 6 Oct 8 22:28 tmp_h1.txt
lrwxrwxrwx 1 root root 7 Oct 8 23:18 tmp_s.txt -> tmp.txt
-rw-r--r-- 3 root root 6 Oct 8 22:28 tmp.txt
drwx--x--x 4 root root 4096 Oct 9 00:44 user
drwx--x--x 3 root root 4096 Oct 9 00:44 users
- 显示移动过程:如果我们想要知道在移动过程中到底有哪些文件进行了移动,则可以使用 “-v” 选项来查看详细的移动信息
[root@CncLucZK test]# ll -a
total 44
drwxr-xr-x 4 root root 4096 Oct 9 00:49 .
dr-xr-xr-x. 23 root root 4096 Oct 9 00:50 ..
-rw-r--r-- 1 root root 90 Oct 5 00:15 demo.txt
-rw-r--r-- 3 root root 6 Oct 8 22:28 hardlink.txt
-rw-r--r-- 1 root root 9 Oct 8 22:45 softlink
-rw-r--r-- 1 root root 381 Oct 5 18:14 test1.sh
-rwxr-xr-x 1 root root 117 Oct 5 18:05 test.sh
-rw-r--r-- 1 root root 6 Oct 8 22:28 tmp_h1.txt
lrwxrwxrwx 1 root root 7 Oct 8 23:18 tmp_s.txt -> tmp.txt
-rw-r--r-- 3 root root 6 Oct 8 22:28 tmp.txt
drwx--x--x 4 root root 4096 Oct 9 00:44 user
drwx--x--x 3 root root 4096 Oct 9 00:44 users
[root@CncLucZK test]# mv -v *.sh user
renamed 'test1.sh' -> 'user/test1.sh'
renamed 'test.sh' -> 'user/test.sh'
#加入"-v"选项,可以看到有哪些文件进行了移动
参考文献:
Linux 文件与目录管理
Linux cp命令:复制文件和目录
Linux rm命令:删除文件或目录
下一篇:Linux学习-09-Linux ln命令:建立链接(硬链接和软链接)文件
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/123783.html