Linux基础知识之文件管理命令(cp、mv、rm)

我们日常工作中对于文件的操作用到最多的几个我觉得应该是是复制、剪切、移动、重命名、删除这五个。因此今天主要介绍关于上面五个操作在中实现涉及到的三个命令:cp、mv、rm。

cp是copy的简写,从名字我们大致就能知道它的作用,它主要是用来复制文件的。虽然这个命令很简单,可是它有很多细节需要我们注意,如果稍有忽略那么命令很有可能无法达到我们想要的结果。

以下信息来自man手册
NAME
      cp – copy files and directories

单源复制(源文件为文本文件) 
其命令格式为:

      cp [OPTION]… [-T] SOURCE DEST
如果DEST不存在:则事先创建此文件,并复制源文件的数据流至DEST中;
如果DEST存在:1、如果DEST是非目录文件,则覆盖目标文件;2、如果DEST是目录文件,则先在DEST目录下创建同名文件并复制数据流

多源复制(源文件为目录文件,且其下有多个文件)
其命令格式:
      cp [OPTION]… SOURCE… DIRECTORY
      cp [OPTION]… -t DIRECTORY SOURCE…
如果DEST不存在:报错
如果DEST存在:1、如果DEST是非目录文件,报错;2、如果目录是目录文件,则分别复制每个文件至目标目录中,并保持原名。

常用选项:
    -i, –interactive 
              prompt before overwrite (overrides a previous -n option)
                    交互式复制,覆盖之前提醒用户确认

    -R, -r, –recursive
              copy directories recursively
                          递归复制目录
    -d    same as –no-dereference –preserve=links
                  复制符号链接文件本身,而非其指向的源文件
    -a, –archive
              same as -dR –preserve=all
                          用于实现归档
    -f, –force
              if  an existing destination file cannot be opened, remove it and try again (this  ignored when the -n option is also used)
                      强制覆盖目标文件
    –preserve[=ATTR_LIST]
              preserve the specified attributes (default: mode,ownership,timestamps), if possible  additional attributes: context, links, xattr, all
            mode:权限                      ownership:属主和属组
            timestamps:戳              context:安全标签
            xattr:扩展属性                  links:符号链接
            all:上述所有属性
    -v, –verbose
              explain what is being done
              详细展示命令的运行过程

——————————————————————————–
看了上面的内容下面通过几个例子来对cp命令及其选项有一个更具体的认识

1、使用别名命令,每日将/etc/目录下所有文件,备份到/testdir/下独立的新目录下,并要求新目录格式为backupYYYY-mm-dd,备份过程可见
[root@localhost ~]# mkdir /testdir
[root@localhost ~]# cp -rdv /etc/ /testdir/bakup$(date +%F)
“/etc/rsyslog.conf” -> “/testdir/bakup2016-08-01/rsyslog.conf”
“/etc/vimrc” -> “/testdir/bakup2016-08-01/vimrc”
“/etc/pulse” -> “/testdir/bakup2016-08-01/pulse”
“/etc/pulse/client.conf” -> “/testdir/bakup2016-08-01/pulse/client.conf”
“/etc/pulse/daemon.conf” -> “/testdir/bakup2016-08-01/pulse/daemon.conf”
“/etc/pulse/default.pa” -> “/testdir/bakup2016-08-01/pulse/default.pa”
“/etc/pulse/system.pa” -> “/testdir/bakup2016-08-01/pulse/system.pa”

..
.
“/etc/screenrc” -> “/testdir/bakup2016-08-01/screenrc”

因为/etc/下文件很多,因此需要等一会。
12 [root@localhost ~]# ls /testdir/
bakup2016-08-01

结果满足要求,下面解释下使用cp那三个选项的原因,给出的要求是首先要将/etc目录下的所有文件都备份,因此需要使用递归选项-r,后面要求备份过程可见,于是又加上-v,至于-d是因为要保持备份文件与源文件的一致性,虽然没有明确要求,不过加上-d显得更好点。

2、先创建/testdir/rootdir目录,再复制/root所有下文件到该目录内,并要求保留原有权限。
[root@localhost ~]# mkdir /testdir/rootdir
[root@localhost ~]# cp -r –preserve=mode,ownership /root /testdir/rootdir/
[root@localhost ~]#  ll /root /testdir/rootdir/root/ 
/root:
总用量 20
-rw——-. 1 root root 1172 7月  20 00:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 7月  21 18:52 CST
-rw-r–r–. 1 root root  14 8月  1 10:49 file
-rw——-. 1 root root 1220 7月  19 16:46 initial-setup-ks.cfg
-rw-r–r–. 1 root root 4282 8月  1 12:17 test.txt
-rw-r–r–. 1 root root    0 8月  1 12:20 tr
drwxr-xr-x. 2 root root    6 7月  22 09:52 公共
drwxr-xr-x. 2 root root    6 7月  22 09:52 模板
drwxr-xr-x. 2 root root    6 7月  22 09:52 视频
drwxr-xr-x. 2 root root    6 7月  22 09:52 图片
drwxr-xr-x. 2 root root    6 7月  22 09:52 文档
drwxr-xr-x. 2 root root    6 7月  22 09:52 下载
drwxr-xr-x. 2 root root    6 7月  22 09:52 音乐
drwxr-xr-x. 2 root root    6 7月  22 09:52 桌面
/testdir/rootdir/root/:
总用量 20
-rw——-. 1 root root 1172 8月  1 22:06 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 8月  1 22:06 CST
-rw-r–r–. 1 root root  14 8月  1 22:06 file
-rw——-. 1 root root 1220 8月  1 22:06 initial-setup-ks.cfg
-rw-r–r–. 1 root root 4282 8月  1 22:06 test.txt
-rw-r–r–. 1 root root    0 8月  1 22:06 tr
drwxr-xr-x. 2 root root    6 8月  1 22:06 公共
drwxr-xr-x. 2 root root    6 8月  1 22:06 模板
drwxr-xr-x. 2 root root    6 8月  1 22:06 视频
drwxr-xr-x. 2 root root    6 8月  1 22:06 图片
drwxr-xr-x. 2 root root    6 8月  1 22:06 文档
drwxr-xr-x. 2 root root    6 8月  1 22:06 下载
drwxr-xr-x. 2 root root    6 8月  1 22:06 音乐
drwxr-xr-x. 2 root root    6 8月  1 22:06 桌面

依然是复制目录依然是使用递归选项-r,这次使用–preserve主要是为了满足保留权限这一要求。由结果可知满足要求。

3、复制/etc/system-release文件,比较加-d与不加-d有何区别

udast

[root@localhost ~]# cp /etc/system-release /test/t1
[root@localhost ~]# cp -d /etc/system-release /test/t2
[root@localhost ~]# ll /test/
总用量 2
-rw-r–r–. 1 root root    38 8月  1 22:22 t1
lrwxrwxrwx. 1 root root    14 8月  1 22:22 t2 -> -release
[root@localhost test]# cat t1 
release 7.2.1511 (Core) 
[root@localhost test]# cat t2
cat: t2: 没有那个文件或目录

    由结果的比较我们可以知道加-d我们复制的是原始文件,若目标是链接文件我们复制的只是链接文件,而不是原始文件。

——————————————————————————–
mv 是move缩写,主要作用是移动和重命名文件

  mv [OPTION]… [-T] SOURCE DEST
 mv [OPTION]… SOURCE… DIRECTORY
  mv [OPTION]… -t DIRECTORY SOURCE…
在同一路径下,mv作用为重命名
在不同路径下,mv作用为移动文件或剪切
常用选项:
      -v, –verbose
              explain what is being done
      -i    prompt before every removal 
以上两种选项上文已有例子说明,这里不再具体演示。

——————————————————————————–
下面以实验说明mv的具体作用

1、在/test/目录下更改t1文件名为t3
[root@localhost test]# ll
总用量 0
-rw-r–r–. 1 root root 0 8月  1 22:36 t1
[root@localhost test]# mv t1 t3
[root@localhost test]# ll
总用量 0
-rw-r–r–. 1 root root 0 8月  1 22:36 t3

2、将/test/目录下的t3文件移动至/testdir/目录下

[root@localhost test]# ll /testdir/
总用量 0
[root@localhost test]# mv t3 /testdir/
[root@localhost test]# ll
总用量 0
[root@localhost test]# ll /testdir/
总用量 0
-rw-r–r–. 1 root root 0 8月  1 22:36 t3

——————————————————————————–
rm – remove files or directories 移除文件或目录

格式: rm [OPTION]… FILE…
常用选项:
    -f, –force  强制删除
              ignore nonexistent files and arguments, never prompt
    -i    prompt before every removal  交互模式

    -r, -R, –recursive    递归删除
              remove directories and their contents recursively
    -v, –verbose          显示删除详细过程
              explain what is being done

——————————————————————————–
下面以实验来具体演示rm的作用

1、删除/test/目录下t1文件
[root@localhost test]# rm t1
rm:是否删除普通空文件 “t1″?y
[root@localhost test]# ll
总用量 0
drwxr-xr-x. 2 root root 18 8月  1 22:46 file

在root用户下其默认情况下“cp=cp -i”
2、删除/test/目录下的file文件夹
[root@localhost test]# rm file/
rm: 无法删除”file/”: 是一个目录
[root@localhost test]# rm -f file/
rm: 无法删除”file/”: 是一个目录

我们发现仅仅使用rm且加上强制删除选项也无法将目录给删除。

删除目录需要加上-r递归,删除目录连带将其下面的所有文件都给删掉。
[root@localhost test]# rm -rfv file/
已删除”file/file1″
已删除目录:”file/”

其删除过程是先从最底层的文件一个个删除之后向上再次删除一直删到最上层为止。

更多内容请长按二维码关注(更有不定期发红包活动吆^0^):
致儒先生

转载自:https://www.linuxidc.com/Linux/2016-08/134036.htm

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《Linux基础知识之文件管理命令(cp、mv、rm)
本文地址:https://www.zhiletu.com/archives-8052.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微