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

相关推荐

  • 马哥教育网络班22期中秋节课程练习1

    #!/bin/sh   set -e CheckRoot() { #check if currect use is root user     CUR_USER=`whoami`     if&nbs…

    Linux干货 2016-10-09
  • 制作本地yum源与编译安装http

    1、制作本地yum源(centos7) [root@centos7 ~]# yum install -y lftp   #安装lftp程序 lftp 10.1.0.1:~> cd pub/Sources/sources/xen/ &n…

    Linux干货 2016-08-25
  • Clonezilla(再升龙)系统备份还原使用

      实验一、单机Centos 系统利用Clonezilla手动备份和还原(VMware vSphere) 实验二、利用Clonezilla+DRBL网络备份和还原   一、简介 DRBL(Diskless Remote Boot in Linux)中文名“企鹅龙”,是基于GNU GPL协议授权下的开源项目,可以实现客户机的远程启动及多客户…

    Linux干货 2015-10-27
  • Linux基础知识之软硬链接

    系统环境:    该博文以CentOS6.8_x86_64系统为基础,Xshell5远程登录CentOS6.8系统,以root身份登录系统。 为什么要学习符号(软)链接和硬链接?    符号(软)链接和硬链接是Linux文件系统中的一个重要的概念,软硬链接的学习过程中会涉及一些文件系统中的索引节点(inode),索引节…

    Linux干货 2016-08-02
  • keepalived双主模型的实现

    一、简介  keepalived 简介:是服务器高可用的一个重要软件,它的核心组件有vrrp ,stack, checker ,ipvs, warpper, watch dog  它是vrrp协议的实现,原生设计目的为高可用ipvs服务;keepalived能够通过配置文件中定义生成ipvs规则 并能够对RS的健康状态进行检测;vrrp_…

    Linux干货 2014-05-22
  • rpm 程序包管理器的基本使用

    目录 安装程序包 卸载 升级 查询 校验 来源合法性和完整性验证 数据库重建 1     安装程序包 rpm -ivh /path/to/package_file -v     显示执行过程的参数。 -h   &nbsp…

    Linux干货 2016-06-22

评论列表(1条)

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

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