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

相關推薦

  • vncserver安裝配置小結

      安裝VNC Server 端:        1. yum install  tigervnc-server         //RHEL6: vnc server的包名          yum &nbs…

    Linux干貨 2016-03-09
  • 馬哥教育網絡班20期+第三周博客作業

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一用戶登錄多次,則只顯示一次即可。 [root@llww3317 ~]# who    #顯示當前用戶登錄信息 root     tty1    &nbsp…

    Linux干貨 2016-06-26
  • N22-第三周作業

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。     首先用who命令查看所有已登錄用戶信息,然后用cut命令取出用戶名,再用sort -u對用戶名排序并刪除重復的用戶名      2、取出最后登錄到當前系統的用戶的相關信息。  &n…

    Linux干貨 2016-09-05
  • 26期全程班-第一周博客作業

    1、描述計算機的組成與功能。 計算機由以下五大單元組成: 一:CPU的算法計算和邏輯判斷單元; 二:CPU的控制單元;協調各設備協同工作。 三:內存:RAM(random accace memory)隨機讀取內存;數據處理的必經路段。 四:輸入設備:鍵盤、鼠標等;負責數據的輸入。 五:輸出設備:屏幕、終端、文件、硬盤等;負責數據的輸出 2、按系列羅列Linu…

    Linux干貨 2016-12-30
  • DNS服務器搭建示例

    DNS服務器搭建示例 負責解析magedu.com域名,能夠對一些主機名進行正向解析和逆向解析 配置主配置文件 [root@slave1 etc]# vim /etc/named.conf options { listen-on port 53 { 192.168.91.132; }; // listen-on-v6 port 53 { ::1; }; di…

    2017-09-16
  • Linux基礎(九)-shell編程練習

    1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現; #!/bin/bash declare -i nologin=0 declare -i login=0 while read l…

    Linux干貨 2016-11-20

評論列表(1條)

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

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

欧美性久久久久