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

相关推荐

  • 文本编辑工具vim

                             vim +#: 打开文件直接处在#行 +/PATTERN:打开文件直接处在第一次匹配到行 vim + file :打开文件直接处在最后行 vim –b file :以二进制打开文件…

    Linux干货 2016-08-12
  • shell编程中的三种流程控制

    流程控制 v 一 、过程式编程语言: 1.顺序执行 2.选择执行 3.循环执行  条件选择if 语句   选择执行:   注意: :if 语句可 嵌套    单分支 if 判断条件;then 条件为真的分支代码 fi    双分支 if 判断条件; then …

    Linux干货 2016-08-21
  • N28 第三周【1】:grep和文本处理工具的使用

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

    Linux干货 2017-12-19
  • 第六周总结

    请详细总结vim编辑器的使用并完成以下练习题 vim: 模块化的编辑器   基本模式: 编辑模式,命令模式 输入模式 末行模式: 打开文件: # vim [options] [file..] +#:打开文件后,直接让光标处于第#行的行首; +/PATTERN:打开文件后,直接让光标处于第一个被PATTERN匹配到的行的行首; 模式转换: 编辑模式:…

    Linux干货 2017-08-07
  • bash脚本编程之数组、高级字符串处理、高级变量操作、用户环境配置

    概述:     承接之前脚本编程部分,本篇将介绍一下脚本编程基础语法的最后一部分内容,具体分为:         1、数组简介         2、高级字符串处…

    Linux干货 2016-08-24
  • grep,egrep及相应的正则表达式用法详解

    目录 1、grep简介     1.1、grep是什么     1.2、grep使用格式     1.3、grep常用选项 2、正则表达式与grep     2.1、正则表达式简介  &nb…

    系统运维 2016-08-22

评论列表(1条)

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

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