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

相关推荐

  • N25第二周作业

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。     其常用的有: touch、stat、rm、cp、mv、install touch 命令    命令格式: touch [OPTION]… File… 命令参数: -a&n…

    Linux干货 2016-12-12
  • 马哥教育网络班21期+第3周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# who | cut -d' ' -f1 | sort -u 2、取出最后登录到当前系统的用户的相关信息。 [ro…

    Linux干货 2016-07-29
  • bash脚本变量的使用

    bash脚本的变量使用详解 变量的类型包括整数、字符串和数组,但在bash脚本编程中不需要指定类型就可以直接赋值,默认均为字符型,其参与运算会自动进行隐式类型转换。 变量的赋值方式为:name='value',其中“=”两边一定不能有空格,如果变量值中包含有空格则需要使用引号引起来,需要注意的是强引用和弱引用的区分。此外变量的赋值也可以引…

    Linux干货 2016-08-15
  • Linux软件管理(rpm,yum)

    Linux的应用程序安装提供了两中格式的软件包:一种是软件开发者直接提供的源码包;另一种是发行版厂商提供事先编译好打包的软件包;用户需要根据需要来选择何种方式安装软件,作为系统管理运维人员,要想合理应用,必须熟悉这两者之间的内在联系以及各自的特性和差异; *软件包管理工具)   — RPM软件包管理工具  — YUM软件包前端管理工具 软…

    Linux干货 2016-08-29
  • iptables简单概念..

    iptables: 包过滤型的防火墙 Firewall:防火墙,隔离工具;工作于主机或网络边缘,对于进出本主机或本网络的报文根据事先定义的检查规则作匹配检测,对于能够被规则匹配到的报文作出相应处理的组件;    主机防火墙     网络防火墙   &…

    Linux干货 2017-06-19
  • LVS-net模型

    net模型拓扑图 注:rip的网关需指向DIP 搭建LVS net模式 基于httpd服务 首先准备三台主机 主机A 主机B 主机C 主机A(vs主机) 主机A设置两个IP 一个内网一个外网 #yum -y install ipvsadm #echo 1 > /proc/sys/net/ipv4/ip_forward :打开核心转发功能 #iptabl…

    Linux干货 2017-05-17

评论列表(1条)

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

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