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
下一篇 2018-02-28

相关推荐

  • N22-第三周课堂练习

    — 1.列出当前系统上所有已经登录的用户的用户名,注意:用一个用户登录多次,则只显示一次即可。     who | awk '{print $1}' | uniq 2.取出最后登录到当前系统的用户相关信息。   last | head -n 1 3.取出当前系统上被用户当作其默认shell的最多的…

    Linux干货 2016-08-30
  • 一键搭建mysql集群系列二

    mysql 5.7  主从复制 本节是在上一节的基础之上做的操作,上节我们通过脚本实现了一键自动化安装mysql5.7,这次我们要在三台机器上部署msyql5.7,并实现主从复制,实现的宗旨是,能自动化,就尽量自动化,实在不能自动化,那就手工化 用的脚本和文件说明1.install_mysql.sh 自动化安装mysql脚本2.ntpdate.sh 时间同步…

    2017-05-21
  • CentOS 7和CentOS 6 使用gpg工具实现公钥加密和解密

    运行环境: [root@Shining ~]# uname -a Linux Shining.ACG 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x…

    Linux干货 2016-12-01
  • 22期第十四周课堂练习

    系统的INPUT和OUTPUT默认策略为DROP; [root@localhost ~]# iptables -P INPUT DROP [root@localhost ~]# iptables -P OUTPUT DROP 1、限制本地主机的web服务器在周…

    Linux干货 2017-03-15
  • 文件查找与解压缩

    文件查找 脚本文件名的查询 which(寻找执行文件) which [-a] command -a :将所有由PATH目录中可以找到的命令均列出,而不只是第一个被找到的命令名称 文件名的查找 文件查找:实时查找:遍历所有文件进行条件匹配;(find)非实时查找:根据索引查找;(whereis、locate) whereis whereis [-bmsu] […

    Linux干货 2017-04-08
  • N22-妙手-第八周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。     网桥:         一种网络设备,负责网络桥接,将网络的多个网段在数据链路层连接起来。     集线器: &…

    Linux干货 2016-11-07

评论列表(1条)

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

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