第二週作業

第二週作業內容:

1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。
【cp命令】使用方法:
    語法
        cp(選項)(參數)
    選項
        -a:  此參數的效果和同時指定"-dpR"參數相同;
        -d:  當複製符號連接時,把目標文件或目錄也建立爲符號連接,並指向與源文件或目錄連接的原始文件或目錄;
        -f:  強行復制文件或目錄,不論目標文件或目錄是否已存在;
        -i:  覆蓋既有文件之前先詢問用戶;
        -l:  對源文件建立硬連接,而非複製文件;
        -p:  保留源文件或目錄的屬性;
        -R/r:遞歸處理,將指定目錄下的所有文件與子目錄一併處理;
        -s:  對源文件建立符號連接,而非複製文件;
        -u:  使用這項參數後只會在源文件的更改時間較目標文件更新時或是名稱相互對應的目標文件並不存在時,才複製文件;
        -S:  在備份文件時,用指定的後綴“SUFFIX”代替文件的默認後綴;
        -b:  覆蓋已存在的文件目標前將目標文件備份;
        -v:  詳細顯示命令執行的操作。
    參數
        源文件:制定源文件列表。默認情況下,cp命令不能複製目錄,如果要複製目錄,則必須使用-R選項;
        目標文件:指定目標文件。當“源文件”爲多個文件時,要求“目標文件”爲指定的目錄。


(1)將一個源文件複製指定的目錄中示例演示:
        #測試文件創建
        [root@STCO6 tmp]# vi testfile0
        [root@STCO6 tmp]# ll
        total 40
        drwx------. 2 root root 4096 Aug  5 14:03 pulse-iZKjnGxygfKx
        drwx------. 2 suyi suyi 4096 Aug  5 13:26 pulse-MsDKOsKCPKzg
        drwx------. 2 gdm  gdm  4096 Aug  5 13:26 pulse-zSMgE3h1icZX
        drwxr-xr-x. 2 root root 4096 Aug 13 08:53 testA
        drwxr-xr-x. 2 root root 4096 Aug 13 08:53 testB
        drwxr-xr-x. 5 root root 4096 Aug 13 08:53 testC
        -rw-r--r--. 1 root root   15 Aug 13 09:01 testfile0
        drwx------. 2 root root 4096 Aug  5 14:03 virtual-root.8BGz6e
        drwx------. 2 suyi suyi 4096 Aug  5 13:26 virtual-suyi.5D5yx6
        drwx------. 2 suyi suyi 4096 Aug  3 19:06 virtual-suyi.9jowIw
        -rw-------. 1 root root    0 Aug  4 02:29 yum.log

        #測試文件複製到指定目錄
        [root@STCO6 tmp]# cp testfile0 /home/suyi/test
        
        #測試結果驗證
        [root@STCO6 tmp]# echo $?
        0
        [root@STCO6 tmp]# tree /home/suyi/test/
        /home/suyi/test/
        ├── testA
        ├── testB
        ├── testC
        │   ├── test1
        │   ├── test2
        │   └── test3
        ├── testfile0
        ├── testfile1
        ├── testfile2
        └── testfile3

(2)將一個源文件複製指定的文件中示例演示:
        #測試文件創建
        [root@STCO6 tmp]# vi testfile4
        [root@STCO6 tmp]# cat testfile4
        this is a test

        #測試文件複製到指定文件
        [root@STCO6 tmp]# cp testfile4 /home/suyi/test/Dfile
        
        #測試結果驗證
        [root@STCO6 tmp]# echo $?
        0
        [root@STCO6 tmp]# cat /home/suyi/test/Dfile
        this is a test
        [root@STCO6 tmp]#


(3)將多個源文件複製到指定的目錄中示例演示:        
        #測試文件創建
        [root@STCO6 tmp]# touch testA/testfile1 testB/testfile2 testC/testfile3
        [root@STCO6 tmp]# echo $?
        0
        [root@STCO6 tmp]# tree
        .
        ├── pulse-iZKjnGxygfKx
        │   ├── native
        │   └── pid
        ├── pulse-MsDKOsKCPKzg
        │   ├── native
        │   └── pid
        ├── pulse-zSMgE3h1icZX
        ├── testA
        │   └── testfile1
        ├── testB
        │   └── testfile2
        ├── testC
        │   ├── test1
        │   ├── test2
        │   ├── test3
        │   └── testfile3
        ├── virtual-root.8BGz6e
        ├── virtual-suyi.5D5yx6
        ├── virtual-suyi.9jowIw
        └── yum.log
        
        #測試文件複製到指定目錄
        [root@STCO6 tmp]# cp testA/testfile1 testB/testfile2 testC/testfile3 /home/suyi/test
        
        #測試結果驗證
        [root@STCO6 tmp]# echo $?
        0
        [root@STCO6 tmp]# tree /home/suyi
        /home/suyi
        ├── Desktop
        ├── Documents
        ├── Downloads
        ├── Music
        ├── Pictures
        ├── Public
        ├── Templates
        ├── test
        │   ├── testA
        │   ├── testB
        │   ├── testC
        │   │   ├── test1
        │   │   ├── test2
        │   │   └── test3
        │   ├── testfile1
        │   ├── testfile2
        │   └── testfile3
        └── Videos

(4)將源目錄複製到指定的目錄中示例演示:        
        #測試目錄創建
        [root@STCO6 tmp]# mkdir -p /tmp/{testA,testB,testC/{test1,test2,test3}}
        [root@STCO6 tmp]# echo $?
        0
        [root@STCO6 tmp]# tree
        .
        ├── pulse-iZKjnGxygfKx
        │   ├── native
        │   └── pid
        ├── pulse-MsDKOsKCPKzg
        │   ├── native
        │   └── pid
        ├── pulse-zSMgE3h1icZX
        ├── testA
        ├── testB
        ├── testC
        │   ├── test1
        │   ├── test2
        │   └── test3
        ├── virtual-root.8BGz6e
        ├── virtual-suyi.5D5yx6
        ├── virtual-suyi.9jowIw
        └── yum.log
        
        #測試目錄複製到指定目錄
        [root@STCO6 suyi]# cp -r /tmp/{testA,testB,testC} /home/suyi/test
        
        #測試結果驗證
        [root@STCO6 suyi]# echo $?
        0
        [root@STCO6 suyi]# tree /home/suyi
        /home/suyi
        ├── Desktop
        ├── Documents
        ├── Downloads
        ├── Music
        ├── Pictures
        ├── Public
        ├── Templates
        ├── test
        │   ├── testA
        │   ├── testB
        │   └── testC
        │       ├── test1
        │       ├── test2
        │       └── test3
        └── Videos

【mv命令】使用方法:
    語法
        mv(選項)(參數)
    選項
        --backup=<備份模式>:若需覆蓋文件,則覆蓋前先行備份;
        -b:當文件存在時,覆蓋前,爲其創建一個備份;
        -f:若目標文件或目錄與現有的文件或目錄重複,則直接覆蓋現有的文件或目錄;

        -i:交互式操作,覆蓋前先行詢問用戶,如果源文件與目標文件或目標目錄中的文件同名,則詢問用戶是否覆蓋目標文件。用戶輸入”y”,表示將覆蓋目標文件;輸入”n”,表示取消對源文件的移動。這樣可以避免誤將文件覆蓋。
        --strip-trailing-slashes:刪除源文件中的斜槓“/”;
        -S<後綴>:爲備份文件指定後綴,而不使用默認的後綴;
        --target-directory=<目錄>:指定源文件要移動到目標目錄;
        -u:當源文件比目標文件新或者目標文件不存在時,才執行移動操作。
    參數
        源文件:源文件列表。
        目標文件:如果“目標文件”是文件名則在移動文件的同時,將其改名爲“目標文件”;如果“目標文件”是目錄名則將源文件移動到“目標文件”下。    

(1)將多個源文件移動到指定的目錄中示例演示:        
        #測試文件創建
        [root@STCO6 tmp]# touch testA/testfile1 testB/testfile2 testC/testfile3
        [root@STCO6 tmp]# echo $?
        0
        [root@STCO6 tmp]# tree
        .
        ├── pulse-iZKjnGxygfKx
        │   ├── native
        │   └── pid
        ├── pulse-MsDKOsKCPKzg
        │   ├── native
        │   └── pid
        ├── pulse-zSMgE3h1icZX
        ├── testA
        │   └── testfile1
        ├── testB
        │   └── testfile2
        ├── testC
        │   ├── test1
        │   ├── test2
        │   ├── test3
        │   └── testfile3
        ├── virtual-root.8BGz6e
        ├── virtual-suyi.5D5yx6
        ├── virtual-suyi.9jowIw
        └── yum.log
        
        #測試文件移動到指定目錄
        [root@STCO6 tmp]# mv testA/testfile1 testB/testfile2 testC/testfile3 /home/suyi/test/testA
        
        #測試結果驗證
        [root@STCO6 suyi]# echo $?
        0
        [root@STCO6 tmp]# tree /home/suyi
        /home/suyi
        ├── Desktop
        ├── Documents
        ├── Downloads
        ├── Music
        ├── Pictures
        ├── Public
        ├── Templates
        ├── test
        │   ├── Dfile
        │   ├── testA
        │   │   ├── testfile1
        │   │   ├── testfile2
        │   │   └── testfile3
        │   ├── testB
        │   ├── testC
        │   │   ├── test1
        │   │   ├── test2
        │   │   └── test3
        │   ├── testfile0
        │   ├── testfile1
        │   ├── testfile2
        │   └── testfile3
        └── Videos

(2)將多個目錄移動到指定的目錄中示例演示:
        #測試目錄創建
        [root@STCO6 testB]# mkdir -p /tmp/testC/D{1,2,3/sd{1,2,3}}
        [root@STCO6 testB]# echo $?
        0
        [root@STCO6 testB]# tree /tmp
        /tmp
        ├── pulse-iZKjnGxygfKx
        │   ├── native
        │   └── pid
        ├── pulse-MsDKOsKCPKzg
        │   ├── native
        │   └── pid
        ├── pulse-zSMgE3h1icZX
        ├── testA
        ├── testB
        │   └── D3
        ├── testC
        │   ├── D1
        │   ├── D2
        │   ├── D3
        │   │   ├── sd1
        │   │   ├── sd2
        │   │   └── sd3
        │   ├── test1
        │   ├── test2
        │   └── test3
        ├── testfile0
        ├── testfile4
        ├── virtual-root.8BGz6e
        ├── virtual-suyi.5D5yx6
        ├── virtual-suyi.9jowIw
        └── yum.log

        19 directories, 7 files

        #測試目錄移動到指定目錄
        [root@STCO6 testB]# mv /tmp/testC/D{1,2,3} /home/suyi/test/testC

        #測試結果驗證
        [root@STCO6 testB]# echo $?
        0
        [root@STCO6 testB]# tree /home/suyi/test
        /home/suyi/test
        ├── Dfile
        ├── testA
        │   ├── testfile1
        │   ├── testfile2
        │   └── testfile3
        ├── testB
        │   ├── D1
        │   ├── D2
        │   ├── sd1
        │   ├── sd2
        │   └── sd3
        ├── testC
        │   ├── D1
        │   ├── D2
        │   ├── D3
        │   │   ├── sd1
        │   │   ├── sd2
        │   │   └── sd3
        │   ├── test1
        │   ├── test2
        │   └── test3
        ├── testfile0
        ├── testfile1
        ├── testfile2
        └── testfile3

        17 directories, 8 files

(3)將測試文件重命名示例演示:
        #測試文件查看
        [root@STCO6 tmp]# ll
        total 44
        drwx------. 2 root root 4096 Aug  5 14:03 pulse-iZKjnGxygfKx
        drwx------. 2 suyi suyi 4096 Aug  5 13:26 pulse-MsDKOsKCPKzg
        drwx------. 2 gdm  gdm  4096 Aug  5 13:26 pulse-zSMgE3h1icZX
        drwxr-xr-x. 2 root root 4096 Aug 13 09:39 testA
        drwxr-xr-x. 3 root root 4096 Aug 13 10:58 testB
        drwxr-xr-x. 5 root root 4096 Aug 13 11:04 testC
        -rw-r--r--. 1 root root   15 Aug 13 09:01 testfile0
        -rw-r--r--. 1 root root   15 Aug 13 09:10 testfile4
        drwx------. 2 root root 4096 Aug  5 14:03 virtual-root.8BGz6e
        drwx------. 2 suyi suyi 4096 Aug  5 13:26 virtual-suyi.5D5yx6
        drwx------. 2 suyi suyi 4096 Aug  3 19:06 virtual-suyi.9jowIw
        -rw-------. 1 root root    0 Aug  4 02:29 yum.log
        [root@STCO6 tmp]# cat testfile4
        this is a test

        #測試文件重命名
        [root@STCO6 tmp]# mv testfile4 renamefile

        #測試結果驗證
        [root@STCO6 tmp]# echo $?
        0
        [root@STCO6 tmp]# cat renamefile
        this is a test
        [root@STCO6 tmp]# ll
        total 44
        drwx------. 2 root root 4096 Aug  5 14:03 pulse-iZKjnGxygfKx
        drwx------. 2 suyi suyi 4096 Aug  5 13:26 pulse-MsDKOsKCPKzg
        drwx------. 2 gdm  gdm  4096 Aug  5 13:26 pulse-zSMgE3h1icZX
        -rw-r--r--. 1 root root   15 Aug 13 09:10 renamefile
        drwxr-xr-x. 2 root root 4096 Aug 13 09:39 testA
        drwxr-xr-x. 3 root root 4096 Aug 13 10:58 testB
        drwxr-xr-x. 5 root root 4096 Aug 13 11:04 testC
        -rw-r--r--. 1 root root   15 Aug 13 09:01 testfile0
        drwx------. 2 root root 4096 Aug  5 14:03 virtual-root.8BGz6e
        drwx------. 2 suyi suyi 4096 Aug  5 13:26 virtual-suyi.5D5yx6
        drwx------. 2 suyi suyi 4096 Aug  3 19:06 virtual-suyi.9jowIw
        -rw-------. 1 root root    0 Aug  4 02:29 yum.log

【rm命令】使用方法:
    語法
        rm (選項)(參數)
    選項
        -d:直接把欲刪除的目錄的硬連接數據刪除成0,刪除該目錄;
        -f:強制刪除文件或目錄;
        -i:刪除已有文件或目錄之前先詢問用戶;
        -r或-R:遞歸處理,將指定目錄下的所有文件與子目錄一併處理;
        --preserve-root:不對根目錄進行遞歸操作;
        -v:顯示指令的詳細執行過程。
    參數
        文件:指定被刪除的文件列表,如果參數中含有目錄,則必須加上-r或者-R選項。

(1)將測試目錄刪除示例演示:
        #測試文件及目錄創建
        [root@STCO6 tmp]# mkdir -p testrm/D{a,b,c}
        [root@STCO6 tmp]# touch testrm/Da/filerm1 testrm/Db/filerm2 testrm/Dc/filerm3
        [root@STCO6 tmp]# echo $?
        0
        [root@STCO6 tmp]# tree
        .
        ├── pulse-iZKjnGxygfKx
        │   ├── native
        │   └── pid
        ├── pulse-MsDKOsKCPKzg
        │   ├── native
        │   └── pid
        ├── pulse-zSMgE3h1icZX
        ├── renamefile
        ├── testA
        ├── testB
        │   └── D3
        ├── testC
        │   ├── test1
        │   ├── test2
        │   └── test3
        ├── testfile0
        ├── testrm
        │   ├── Da
        │   │   └── filerm1
        │   ├── Db
        │   │   └── filerm2
        │   └── Dc
        │       └── filerm3
        ├── virtual-root.8BGz6e
        ├── virtual-suyi.5D5yx6
        ├── virtual-suyi.9jowIw
        └── yum.log

        17 directories, 10 files

        #測試文件及目錄刪除
        [root@STCO6 tmp]# rm -r testrm
        rm: descend into directory `testrm'? y
        rm: descend into directory `testrm/Da'? y
        rm: remove regular empty file `testrm/Da/filerm1'? y
        rm: remove directory `testrm/Da'? y
        rm: descend into directory `testrm/Db'? y
        rm: remove regular empty file `testrm/Db/filerm2'? y
        rm: remove directory `testrm/Db'? y
        rm: descend into directory `testrm/Dc'? y
        rm: remove regular empty file `testrm/Dc/filerm3'? y
        rm: remove directory `testrm/Dc'? y
        rm: remove directory `testrm'? y

        #測試結果驗證
        [root@STCO6 tmp]# echo $?
        0
        [root@STCO6 tmp]# tree
        .
        ├── pulse-iZKjnGxygfKx
        │   ├── native
        │   └── pid
        ├── pulse-MsDKOsKCPKzg
        │   ├── native
        │   └── pid
        ├── pulse-zSMgE3h1icZX
        ├── renamefile
        ├── testA
        ├── testB
        │   └── D3
        ├── testC
        │   ├── test1
        │   ├── test2
        │   └── test3
        ├── testfile0
        ├── virtual-root.8BGz6e
        ├── virtual-suyi.5D5yx6
        ├── virtual-suyi.9jowIw
        └── yum.log

        13 directories, 7 files

2、bash的工作特性之命令執行狀態返回值和命令行展開所涉及的內容及其示例演示。
(1)bash命令執行狀態
    狀態:成功 返回值“0”
          失敗 返回值“1-255”
    示例演示:
    [root@STCO6 tmp]# cat renamefile
    this is a test
    [root@STCO6 tmp]# echo $?
    0
    [root@STCO6 tmp]# cat renamefile1
    cat: renamefile1: No such file or directory
    [root@STCO6 tmp]# echo $?
    1

(2)命令行展開
    ~: 展開爲用戶的主目錄
    ~USERNAME:展開爲指定用戶的主目錄
    {}:可承載一個以逗號分隔的列表,並將其展開爲多個路徑
    示例演示:
    [root@STCO6 tmp]# cd ~
    [root@STCO6 ~]# pwd
    /root
    
    [root@STCO6 ~]# cd ~suyi
    [root@STCO6 suyi]# pwd
    /home/suyi
    
    [root@STCO6 testB]# mkdir -p /tmp/testC/D{1,2,3/sd{1,2,3}}
    [root@STCO6 testB]# echo $?
    0
    [root@STCO6 testB]# tree /tmp
    /tmp
    ├── pulse-iZKjnGxygfKx
    │   ├── native
    │   └── pid
    ├── pulse-MsDKOsKCPKzg
    │   ├── native
    │   └── pid
    ├── pulse-zSMgE3h1icZX
    ├── testA
    ├── testB
    │   └── D3
    ├── testC
    │   ├── D1
    │   ├── D2
    │   ├── D3
    │   │   ├── sd1
    │   │   ├── sd2
    │   │   └── sd3
    │   ├── test1
    │   ├── test2
    │   └── test3
    ├── testfile0
    ├── testfile4
    ├── virtual-root.8BGz6e
    ├── virtual-suyi.5D5yx6
    ├── virtual-suyi.9jowIw
    └── yum.log

        19 directories, 7 files

3、請使用命令行展開功能來完成以下練習:

(1)、創建/tmp目錄下的:a_c, a_d, b_c, b_d
    [root@STCO6 suyi]# mkdir /tmp/{a_{c,d},b_{c,d}}
    [root@STCO6 suyi]# echo $?
    0
    [root@STCO6 suyi]# tree /tmp
    /tmp
    ├── a_c
    ├── a_d
    ├── b_c
    ├── b_d
    ├── pulse-iZKjnGxygfKx
    │   ├── native
    │   └── pid
    ├── pulse-MsDKOsKCPKzg
    │   ├── native
    │   └── pid
    ├── pulse-zSMgE3h1icZX
    ├── renamefile
    ├── testA
    ├── testB
    │   └── D3
    ├── testC
    │   ├── test1
    │   ├── test2
    │   └── test3
    ├── testfile0
    ├── virtual-root.8BGz6e
    ├── virtual-suyi.5D5yx6
    ├── virtual-suyi.9jowIw
    └── yum.log

    17 directories, 7 files


(2)、創建/tmp/mylinux目錄下的:

mylinux/

├── bin

├── boot

│   └── grub

├── dev

├── etc

│   ├── rc.d

│   │   └── init.d

│   └── sysconfig

│       └── network-scripts

├── lib

│   └── modules

├── lib64

├── proc

├── sbin

├── sys

├── tmp

├── usr

│   └── local

│       ├── bin

│       └── sbin

└── var

├── lock

├── log

└── run

    [root@STCO6 suyi]# mkdir -p /tmp/mylinux/{bin,boot/grub,dev,etc/rc.d/init.d,etc/sysconfig/network-scripts,lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var,lock,log,run}
    [root@STCO6 suyi]# tree /tmp/mylinux/
    /tmp/mylinux/
    ├── bin
    ├── boot
    │   └── grub
    ├── dev
    ├── etc
    │   ├── rc.d
    │   │   └── init.d
    │   └── sysconfig
    │       └── network-scripts
    ├── lib
    │   └── modules
    ├── lib64
    ├── lock
    ├── log
    ├── proc
    ├── run
    ├── sbin
    ├── sys
    ├── tmp
    ├── usr
    │   └── local
    │       ├── bin
    │       └── sbin
    └── var

    24 directories, 0 files

4、文件的元數據信息有哪些,分別表示什麼含義,如何查看?如何修改文件的時間戳信息。
    
    Size:文件大小        
    Blocks:塊大小
    IO Block:IO塊大小
    regular file
    Device:文件在磁盤上的位置以及磁盤在集羣中的位置
    Inode: inode編號
    Links:鏈接數量
    Access:訪問權限
    Uid:文件擁有者
    Gid:文件屬組
    Access: 訪問時間
    Modify: 修改時間
    Change: 改變時間
    查看文件的元數據信息,可以通過stat命令查看
    修改文件的時間戳信息可以通過touch命令
    touch -a:只更改存取時間;
    touch -m:只更改變動時間;  
    touch -t:<日期時間> 使用指定的日期時間,而非現在的時間;

5、如何定義一個命令的別名,如何在命令中引用另一個命令的執行結果?
(1)定義一個命令的別名,alias NAME='VALUE'
(2)可以通過管道,在命令中引用另一個命令的執行結果

6、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其它字符)的文件或目錄。
    # ls -d /var/l*[0-9]*[[:lower:]]

7、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄。
    # ls -d /etc/[0-9]*[^0-9]

8、顯示/etc目錄下,以非字母開頭,後面跟了一個字母以及其它任意長度任意字符的文件或目錄。
    # ls /etc/[^[:alpha:]][[:alpha:]]*

9、在/tmp目錄下創建以tfile開頭,後跟當前日期和時間的文件,文件名形如:tfile-2016-08-06-09-32-22。
    # touch tfile-"$(date +%Y-%m-%d-%H-%M-%S)"

10、複製/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。
    # cp -a /etc/p*[^0-9] /tmp/mystest1

11、複製/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中。
    # cp -a /etc/*.d /tmp/mytest2

12、複製/etc/目錄下所有以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中。
    # cp -a /etc/[lmn]*.conf  /tmp/mytest3

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章