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.www58058.com/91778

(0)
華龍華龍
上一篇 2018-02-28 15:17
下一篇 2018-02-28 20:15

相關推薦

  • scp和rsync的使用

    通過一些簡單需求了解scp和rsync的使用

    2017-09-18
  • find文件查找

    文件查找      在文件系統上查找符合條件的文件:      實現工具:locate,find locate:      構建于實現構建好的索引庫:/var/lib/mlocate/mlocate.db   &nbsp…

    Linux干貨 2016-08-22
  • N22-第2周作業

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示 cd、pwd、ls、alias、cat、more、less、tail、cut、wc、touch、mv、cp、rm pwd:用戶當前所在目錄 [xuc@localhost ~]$ pwd /home/xuc cd:切換目錄  cd  [/PATH…

    Linux干貨 2016-08-22
  • 磁盤管理之磁盤分區,主引導分區表修復

    磁盤管理: I/O Ports: I/O設備地址 一切皆文件: open(), read(), write(), close() 設備類型: 塊設備:block,存取單位“塊”,磁盤 字符設備:char,存取單位“字符”,鍵盤 設備文件:關聯至一個設備驅動程序,進而能夠跟與之對應硬件設備進行通信 設備號碼: 主設備號:major number, 標識設備類型…

    Linux干貨 2016-08-25
  • wk_02 作業

    Linux 文件管理命令 cp命令 功能 Linux 系統中cp命令是用來復制目錄/文件的。 概要 單源復制 cp [OPTION]… [-T] SOURCE DEST DEST不存在則事先創建此文件,并復制源文件的數據流至DEST中; DEST存在 DEST是非目錄文件:則覆蓋目標文件; DEST是目錄文件:則先…

    Linux干貨 2016-12-11
  • Linux文件系統及文件管理相關命令

    一、Linux文件系統介紹: Linux文件系統叫做根文件系統(rootfs): root filesystem。文件系統從根目錄下開始,用“ /”表示。文件和目錄被組織成一個單根倒置樹結構。文件系統分層遵循FHS(Filesystem Hierarchy Stantartd)。標準的Linux文件系統,文件名嚴格區分大小寫。使用“/”分割路徑。 …

    Linux干貨 2016-07-29

評論列表(1條)

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

    盡量把一周作業寫在一個博客上。

欧美性久久久久