目录
cp 和 dd 都有拷贝文件的功能,但是两者究竟有啥不同呢?下面我们逐个来分析。
一、cp
cp可以用来拷贝文件或者目录。其功能偏向于在文件系统层次上的拷贝,比如你可以一次拷贝多个文件、递归的拷贝目录及其里面的文件、只拷贝文件属性、还可以用来产生文件的硬链接或者软链接、当原文件比目的文件新时才拷贝替换,拷贝文件链接或者其原文件等等。
cp - copy files and directories
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
1、递归拷贝目录
-R, -r, --recursive
copy directories recursively
[root@yglocal test]# ls
1.txt aaa aaa1
[root@yglocal home]# cp -r test test_tmp/
[root@yglocal home]# tree test_tmp/
test_tmp/
└── test
├── 1.txt
├── aaa
└── aaa1
1 directory, 3 files
2、只拷贝文件属性,不拷贝文件内容
--attributes-only
don't copy the file data, just the attributes
[root@yglocal test]# ll -i
total 16
17780954 -rw-r--r--. 1 root root 7323 Dec 10 20:39 aaa
[root@yglocal test]#
[root@yglocal test]# cp --attributes-only aaa aaa_attr
[root@yglocal test]# ll -i
total 16
17780954 -rw-r--r--. 1 root root 7323 Dec 10 20:39 aaa
17780965 -rw-r--r--. 1 root root 0 Dec 10 22:10 aaa_attr
可以看出copy的aaa_attr文件,大小为0,文件属性同原文件aaa。
3、创建文件硬链接
-l, --link
hard link files instead of copying
[root@yglocal test]# ll -i
total 16
17780954 -rw-r--r--. 1 root root 7323 Dec 10 20:39 aaa
17780965 -rw-r--r--. 1 root root 0 Dec 10 22:10 aaa_attr
[root@yglocal test]# cp -l aaa aaa_hardlink
[root@yglocal test]# ll -i
total 24
17780954 -rw-r--r--. 2 root root 7323 Dec 10 20:39 aaa
17780965 -rw-r--r--. 1 root root 0 Dec 10 22:10 aaa_attr
17780954 -rw-r--r--. 2 root root 7323 Dec 10 20:39 aaa_hardlink
创建了原文件aaa的硬链接文件aaa_hardlink文件,可以看出这俩文件的inode号相同,ref_count都变成了2。
4、创建文件软链接
-s, --symbolic-link
make symbolic links instead of copying
[root@yglocal test]# cp -s aaa aaa_symlink
[root@yglocal test]# ll -i
total 24
17780954 -rw-r--r--. 2 root root 7323 Dec 10 20:39 aaa
17780965 -rw-r--r--. 1 root root 0 Dec 10 22:10 aaa_attr
17780954 -rw-r--r--. 2 root root 7323 Dec 10 20:39 aaa_hardlink
17780966 lrwxrwxrwx. 1 root root 3 Dec 10 22:22 aaa_symlink -> aaa
可以看出,aaa_symlink -> aaa,俩文件inode号不同,ref_count为1。
关于文件的软链接跟硬链接的区别可以看之前文章。
5、拷贝文件链接
-P, --no-dereference
never follow symbolic links in SOURCE
[root@yglocal test]# cp -P aaa_symlink aaa_symlink1
[root@yglocal test]# ll -i
total 16
17780954 -rw-r--r--. 2 root root 7323 Dec 10 20:39 aaa
17780965 -rw-r--r--. 1 root root 0 Dec 10 22:10 aaa_attr
17780954 -rw-r--r--. 2 root root 7323 Dec 10 20:39 aaa_hardlink
17780966 lrwxrwxrwx. 1 root root 3 Dec 10 22:22 aaa_symlink -> aaa
17780953 lrwxrwxrwx. 1 root root 3 Dec 10 22:36 aaa_symlink1 -> aaa
6、随文件链接拷贝文件内容
-L, --dereference
always follow symbolic links in SOURCE
[root@yglocal test]# cp -L aaa_symlink aaa_sym_content
[root@yglocal test]# ll -i
total 24
17780954 -rw-r--r--. 2 root root 7323 Dec 10 20:39 aaa
17780965 -rw-r--r--. 1 root root 0 Dec 10 22:10 aaa_attr
17780954 -rw-r--r--. 2 root root 7323 Dec 10 20:39 aaa_hardlink
17780955 -rw-r--r--. 1 root root 7323 Dec 10 22:44 aaa_sym_content
17780966 lrwxrwxrwx. 1 root root 3 Dec 10 22:22 aaa_symlink -> aaa
17780953 lrwxrwxrwx. 1 root root 3 Dec 10 22:36 aaa_symlink1 -> aaa
可以看到,aaa_sym_content是aaa_symlink对应的文件aaa的拷贝。
7、原文件比目标文件新时,才拷贝替换
-u, --update
copy only when the SOURCE file is newer than the destination
file or when the destination file is missing
[root@yglocal test]# cp aaa ../
[root@yglocal test]# vi aaa
testseteststest
execve("/usr/bin/cp", ["cp", "../1.txt", "."], [/* 25 vars */]) = 0
....省略文件其它内容
[root@yglocal test]# ll -i
total 24
17780954 -rw-r--r--. 2 root root 7339 Dec 10 22:55 aaa
17780965 -rw-r--r--. 1 root root 0 Dec 10 22:10 aaa_attr
17780954 -rw-r--r--. 2 root root 7339 Dec 10 22:55 aaa_hardlink
17780955 -rw-r--r--. 1 root root 7323 Dec 10 22:44 aaa_sym_content
17780966 lrwxrwxrwx. 1 root root 3 Dec 10 22:22 aaa_symlink -> aaa
17780953 lrwxrwxrwx. 1 root root 3 Dec 10 22:36 aaa_symlink1 -> aaa
[root@yglocal test]# cp -vu ../aaa aaa
[root@yglocal test]# ll -i
total 24
17780954 -rw-r--r--. 2 root root 7339 Dec 10 22:55 aaa
17780965 -rw-r--r--. 1 root root 0 Dec 10 22:10 aaa_attr
17780954 -rw-r--r--. 2 root root 7339 Dec 10 22:55 aaa_hardlink
17780955 -rw-r--r--. 1 root root 7323 Dec 10 22:44 aaa_sym_content
17780966 lrwxrwxrwx. 1 root root 3 Dec 10 22:22 aaa_symlink -> aaa
17780953 lrwxrwxrwx. 1 root root 3 Dec 10 22:36 aaa_symlink1 -> aaa
[root@yglocal test]#
可以看出,../aaa文件是旧文件,此时-u拷贝不生效。
二、dd
dd命令的主要用途是读取块数据或者用于备份磁盘数据,它偏向于更底层的拷贝和数据转换,主要针对数据内容的处理。它可以从源数据拷贝n块,每次m字节,也可以拷贝到目的文件的指定偏移量处,并可以转换数据等等。
Copy a file, converting and formatting according to the operands
Actually, it stands for ‘Copy and Convert’ and was renamed to dd only
because cc was reserved for the C compiler!
1、Using “dd”, it’s possible to directly read and/or write from/to different files provided that the function is already implemented in the respected drivers.
2、It’s super useful for purposes like backing up the boot sector, obtaining random data etc.
3、Data conversion, for example, converting ASCII to EBCDIC encoding.
1、备份或恢复mbr分区
[root@yglocal home]# dd if=/dev/sda bs=512 count=1 of=mbr.img
1+0 records in
1+0 records out
512 bytes (512 B) copied, 9.326e-05 s, 5.5 MB/s
[root@yglocal home]# ll -i mbr.img
35466117 -rw-r--r--. 1 root root 512 Dec 10 23:25 mbr.img
[root@yglocal home]# dd if=mbr.img of=/dev/sda
2、擦除磁盘或某文件数据
//用0填充,擦除sda磁盘数据
dd if=/dev/zero bs=1M of=/dev/sda conv=noerror,sync
//随机数填充
dd if=/dev/urandom bs=1M of=/dev/sda conv=noerror,sync
3、备份或恢复磁盘数据,也可以测试磁盘读写速率
dd if =/dev/sda2 of=~/hdadisk.img bs=64k conv=noerror,sync
dd if=hdadisk.img of=/dev/sdb2
4、测试网速
dd if=/dev/zero bs=4096 count=1048576 | ssh user@host.tld 'cat > /dev/null'
dd还要很多其它选项,就不细说了,man dd可以看到:
感兴趣的话可以关注我的微信公众号【大胖聊编程】,我的公众号中有更多文章分享,也可以在公众号中联系到我,加好友一起交流学习。