Linux上文件管理类命令实例讲解

下面介绍三个文件cp, mv, rm管理命令:

  1. cp命令:copy,复制命令
    命令格式: cp 源文件 目标文件
    复制又分为单源复制和多源复制两种情况:

    • 单源复制
      • 如果目标文件不存在,创建此文件,并复制数据流到此文件;
        [root@localhost tmp]# cp yum.log ok
        [root@localhost tmp]# ls -l
        total 4
        drwxr-xr-x. 2 root root 21 Feb 28 02:38 ok
        -rw-------. 1 root root 24 Feb 28 02:29 yum.log
        [root@localhost tmp]# ls -l ok/
        total 4
        -rw-------. 1 root root 24 Feb 28 02:38 yum.log
      • 如果目标文件存在
        • 目标文件是非目录文件,覆盖目标文件;
          [root@localhost tmp]# cat ok/yum.log 
          !!!!!
          [root@localhost tmp]# cp yum.log ok/
          cp: overwrite ‘ok/yum.log’? y
          [root@localhost tmp]# cat ok/yum.log 
          aaaaa
          bbbbb
          ccccc
          ddddd
        • 目标文件是目录文件,先在目录下创建此文件,并复制数据流到此文件;
          [root@localhost tmp]# tree
          .
          ├── ok
          └── yum.log
          [root@localhost tmp]# cp yum.log ok/
          [root@localhost tmp]# tree
          .
          ├── ok
          │   └── yum.log
          └── yum.log
          1 directory, 2 files
    • 多源复制
      • 目标不存在;错误提示;
        [root@localhost tmp]# tree
        .
        ├── ok
        ├── yum1.log
        └── yum.log
        1 directory, 2 files
        [root@localhost tmp]# cp {yum.log,yum1.log} ok1
        cp: target ‘ok1’ is not a directory
      • 目标存在:
        • 目标非目录,错误;
          [root@localhost tmp]# ls -l
          total 12
          -rw-r--r--. 1 root root  7 Feb 28 03:11 aaa
          drwxr-xr-x. 2 root root  6 Feb 28 03:05 ok
          -rw-------. 1 root root 30 Feb 28 03:02 yum1.log
          -rw-------. 1 root root 24 Feb 28 02:29 yum.log
          [root@localhost tmp]# cp {yum.log,yum1.log} aaa
          cp: target ‘aaa’ is not a directory
        • 目标是目录文件,分别复制每个文件到目录中,保持原名
          [root@localhost tmp]# tree
          .
          ├── aaa
          ├── ok
          ├── yum1.log
          └── yum.log
          1 directory, 3 files
          [root@localhost tmp]# cp {yum.log,yum1.log} ok/
          [root@localhost tmp]# tree
          .
          ├── aaa
          ├── ok
          │   ├── yum1.log
          │   └── yum.log
          ├── yum1.log
          └── yum.log
          1 directory, 5 files
  2. mv命令:move,移动命令
    命令格式:mv 源文件 目标文件 (除了复制成功后删除源文件,其他都和复制命令相同)

    • 剪切到ok目录下
      [root@localhost tmp]# tree
      .
      ├── aaa
      ├── ok
      ├── yum1.log
      └── yum.log
      1 directory, 3 files
      [root@localhost tmp]# mv {yum.log,yum1.log} ok/
      [root@localhost tmp]# tree
      .
      ├── aaa
      └── ok
        ├── yum1.log
        └── yum.log
      1 directory, 3 files
    • ok目录改名为yes目录
      [root@localhost tmp]# tree
      .
      ├── aaa
      └── ok
        ├── yum1.log
        └── yum.log
      1 directory, 3 files
      [root@localhost tmp]# mv ok/ yes/
      [root@localhost tmp]# tree
      .
      ├── aaa
      └── yes
        ├── yum1.log
        └── yum.log
      1 directory, 3 files
  3. rm命令:remove,删除命令
    [root@localhost tmp]# tree
    .
    ├── aaa
    └── yes
      ├── yum1.log
      └── yum.log
    1 directory, 3 files
    [root@localhost tmp]# rm -rf yes/
    [root@localhost tmp]# tree
    .
    └── aaa
    0 directories, 1 file

    危险操作:rm -rf /*
    注意:不用的文件单独放到独立目录,不要轻易删除,否则找不回来;

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/91778

(0)
华龙华龙
上一篇 2018-02-28 15:17
下一篇 2018-02-28 20:15

相关推荐

  • 如何安装CentOS 6.8 ?

    一 ,首先我要先在虚拟机的平台上搭建一下我们需要的环境。 1,选择创建一个新的虚拟机。之后选择自定义安装。点击下一步 2,选择硬件兼容性workstation12.0,(根据情况你也可以选择下面的几个版本,不过要记得选择版本之后,不能再这个版本以下打开虚拟机) 3,点击下一步选择安装程序光盘映像文件项。点击下一步。 4,之后创建一下用户信息,点击下一步。 5…

    Linux干货 2017-02-14
  • grep的使用和正则表达式

           grep作为Linux中的文本编辑的三剑客之一,它的功能很强大,并不是虚传,学会了grep在文本中找我们要找的的字符串总是能很轻易地找到,grep不仅仅就这一点功能,它在我们工作上也会对我们有很大的帮助。下面我来介绍一下grep的简单的功能。 grep :文本过滤( 模式:patter…

    Linux干货 2016-08-10
  • linux-系统启动和内核管理

    一、Linux 组成及启动过程 Linux: kernel+rootfs    kernel:进程管理、内存管理、网络管理、驱动程序、文件系统、安全功能      IPC:Inter Process Communication  &nbsp…

    Linux干货 2016-09-26
  • Linux误删大文件的一个可能解救办法

    描述:某个网友说他在进行md5指纹对比某文件的时候,另外一个同事在另外一个窗口删除了该文件,然后顺嘴跟他说了下,这时候他意识到同事可能是误删除了,于是,他利用了以下办法来进行解救: 操作顺序如下: 1)网友的操作: [root@C67-X64-A0 ~]# ls -l /test.img  -rw-r–r-…

    Linux干货 2016-08-02
  • 脚本练习

      1、写一个脚本,使用ping命令探测10.1.8.1-10.1.8.10之间所有主机的在线状态,在线的主机使用绿色显示,不在线的主机使用红色显示。     #!/bin/bash     #Test host whether onlin…

    Linux干货 2016-12-18
  • grep命令

    ]# chmod -R 700 /home/tuser1 ]# ll /home/tuser1/ total 0 drwx——. 2 root root 72 Nov 20 17:33 skel 新增用户与组: 显示/proc/meminfo: 显示shell 非 /sbin/nologin 的用户 找出/etc/passwd文件中一…

    Linux干货 2016-11-20

评论列表(1条)

  • 马哥教育
    马哥教育 2018-03-20 21:49

    尽量把一周作业写在一个博客上。