馬哥2016全新Linux+Python高端運維班第二週做業

一、Linux上的文件管理類命令都有哪些,其經常使用的使用方法及其相關示例演示。node

  答:文件管理類命令有,cp,mv,rm   linux

   1,cpbash

      命令功能:將一個或多個源文件或目錄複製到指定的目標文件或目錄ide

      命令格式:oop

      cp [OPTION]... [-T] SOURCE DEST      //cp [選項]…[-T]源目的spa

      cp [OPTION]... SOURCE... DIRECTORY   // cp [選項]…源…目錄命令行

      cp [OPTION]... -t DIRECTORY SOURCE...  // cp [選項]…-t 目錄 源…遞歸

    經常使用選項:ip

      -i:交互式複製,即覆蓋以前提醒用戶確認it

      -f:強制覆蓋目標文件

      -r:遞歸複製目錄(大寫R也是這個功能)

      -d:--no-dereference --preserv=links 複製符號連接文件自己,而非其指向的源文件

      -a:歸檔,至關於-dR --preserve=all,archive,用於實現歸檔

    

   2, mv

      命令功能:爲文件或目錄更名,或將文件或目錄移動到其它位置

      命令格式:

       mv [OPTION]... [-T] SOURCE DEST

       mv [OPTION]... SOURCE... DIRECTORY

       mv [OPTION]... -t DIRECTORY SOURCE...

     經常使用選項:

        -i:交互式

        -f:force強制覆蓋,不提示

        -v:顯示移動過程

        -u:若目標文件已經存在,且 source 比較新,纔會更新(update)

        -b:若需覆蓋文件,則覆蓋前先行備份

        -t:即指定mv的目標目錄,該選項適用於移動多個源文件到一個目錄的狀況,此時目標目錄在前,源文件在後

   

    3,rm

      命令功能:刪除一個目錄中的一個或多個文件或目錄

      命令格式:rm [OPTION]... FILE...

      命令選項:

      -i:interactive交互式

      -f:force強制刪除

      -r:recursive遞歸刪除

      刪除目錄:rm -rf /路徑/目錄


二、bash的工做特性之命令執行狀態返回值和命令行展開所涉及的內容及其示例演示.

 答:bash命令執行完成後會有一個返回值,保存在$?中,若是正常執行,返回0,錯誤則返回值爲1-255之間的數字。當執行命令後,執行echo $?查看。

  如:

[root@localhost tmp]# ls -l  (輸入一個正確的命令查看當前目錄)
total 16
drwxr-xr-x. 2 root root 4096 Aug  7 01:44 a_c
drwxr-xr-x. 2 root root 4096 Aug  7 01:44 a_d
drwxr-xr-x. 2 root root 4096 Aug  7 01:44 b_c
drwxr-xr-x. 2 root root 4096 Aug  7 01:44 b_d
[root@localhost tmp]# echo $?  (查看,下面顯示0)
0
[root@localhost tmp]#
[root@localhost tmp]# lls -l   (輸入的錯誤命令)
-bash: lls: command not found
[root@localhost tmp]# echo $?   (查看,下面顯示127的錯誤返回值)
127
[root@localhost tmp]#


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

  (1)、建立/tmp目錄下的:a_c, a_d, b_c, b_d

[root@localhost tmp]# mkdir /tmp/{a,b}_{c,d}
[root@localhost tmp]# ll
total 16
drwxr-xr-x. 2 root root 4096 Aug  7 01:44 a_c
drwxr-xr-x. 2 root root 4096 Aug  7 01:44 a_d
drwxr-xr-x. 2 root root 4096 Aug  7 01:44 b_c
drwxr-xr-x. 2 root root 4096 Aug  7 01:44 b_d
[root@localhost tmp]#

  (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@localhost tmp]#  mkdir -p /tmp/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@localhost tmp]# 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
[root@localhost tmp]#

四、文件的元數據信息有哪些,分別表示什麼含義,如何查看?如何修改文件的時間戳信息。

 答:(1)使用stat查看

[root@localhost tmp]# stat passwd 
  File: `passwd'
  Size: 1246            Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 411220      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-07 00:35:55.925091741 -0400
Modify: 2016-08-07 00:35:55.925091741 -0400
Change: 2016-08-07 00:35:55.925091741 -0400
[root@localhost tmp]#

     (2)可使用touch修改時間戳,    

        touch [-acm][-r ref_file(參照文件)|-t time(時間值)] file(文件名)

        example:

        touch file1.txt 更新file1.txt的存取和修改時間

        touch -c file1.txt 若是file1.txt不存在,不建立文件

        touch -r ref_file file1.txt 更新file1.txt的時間戳和ref+file相同

        -a

        修改文件 file 的存取時間.

        -c

        不建立文件 file.

        -m

        修改文件 file 的修改時間

        -r ref_file

        將參照文件 ref_file 相應的時間戳記的數值做爲指定文件 file 時間戳記的新值.

        -t time

        使用指定的時間值 time 做爲指定文件 file 相應時間戳記的新值。


五、如何定義一個命令的別名,如何在命令中引用另外一個命令的執行結果? 

 答: 1,使用alias命令

     例如:[root@localhost tmp]# alias if='ifconfig'

[root@localhost tmp]# alias if='ifconfig'
[root@localhost tmp]# if
eth0      Link encap:Ethernet  HWaddr 00:0C:29:29:BF:88  
          inet addr:192.168.2.55  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe29:bf88/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1617 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1089 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:146643 (143.2 KiB)  TX bytes:163275 (159.4 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

[root@localhost tmp]#

    2,命令中引用另外一個命令,可以使用管道符「|」來實現

   上面已設置ifconfig別名,經過管道符把來網卡信息傳遞給head命令,經過head -3來顯示前3行

[root@localhost tmp]# if |head -3
eth0      Link encap:Ethernet  HWaddr 00:0C:29:29:BF:88  
          inet addr:192.168.2.55  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe29:bf88/64 Scope:Link
[root@localhost tmp]#

六、顯示/var目錄下全部以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(能夠有其它字符)的文件或目錄。

  答:

[root@localhost var]# ls -d /var/l*[0-9]*[[:lower:]]
/var/ltest8test
[root@localhost var]#

七、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄。

  答:

[root@localhost etc]# ls /etc/[0-9]*[^0-9]
/etc/12abc
[root@localhost etc]#

八、顯示/etc目錄下,以非字母開頭,後面跟了一個字母以及其它任意長度任意字符的文件或目錄。

 答:

[root@localhost etc]# ls -d /etc/[^[:alpha:]][[:alpha:]]*
/etc/1test

九、在/tmp目錄下建立以tfile開頭,後跟當前日期和時間的文件,文件名形如:tfile-2016-08-06-09-32-22。

 答:

[root@localhost tmp]# touch tfile-`date +"%Y-%m-%d-%H-%M-%S"`
[root@localhost tmp]# ll |grep tfile
-rw-r--r--.  1 root root    0 Aug  7 01:14 tfile-2016-08-07-01-14-34
[root@localhost tmp]#

十、複製/etc目錄下全部以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。

  若是沒有mytest1這個目錄,須要先手動建立這個目錄

[root@localhost tmp]# cp -a /etc/p*[^0-9] /tmp/mytest1

十一、複製/etc目錄下全部以.d結尾的文件或目錄至/tmp/mytest2目錄中。

   若是沒有mytest2這個目錄,須要先手動建立這個目錄

[root@localhost tmp]# cp -a /etc/*.d /tmp/mytest2

十二、複製/etc/目錄下全部以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中。 

 若是沒有mytest3這個目錄,須要先手動建立這個目錄   

[root@localhost tmp]# cp -a /etc/[l,m,n]*.conf /tmp/mytest3
相關文章
相關標籤/搜索