N26-第二周博客作業

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

   查看類
    pwd : print name of current/working directory  打印當前工作目錄;
         
          參數:
              -P :顯示出確實的路徑,而非使用鏈接 (link) 路徑。

          實例:
          [root@bogon tmp]# pwd          #打印當前工作目錄#
          /tmp
          [root@bogon var]# cd /var/mail
          [root@bogon mail]# pwd         
          /var/mail
          [root@bogon mail]# pwd -P    
          /var/spool/mail                #兩條相同命令,加上-P結果有很大不同。原因在于var/ mail是鏈接文件,鏈接的目標為/var/spool/mail.所以pwd -P最終顯示結果為#
          [root@bogon mail]# ls -al /var/mail
          lrwxrwxrwx. 1 root root 10 Jan 16 16:24 /var/mail -> spool/mail

          [root@bogon tmp]# echo $PWD    #顯示$PWD與使用pwd命令基本相同#

          /tmp

    cd :Change the shell working directory 切換目錄;
         
         .        代表當前目錄;
         ..       代表上一層目錄;
         –        代表前一個工作目錄;
         ~        代表當前用戶所在的家目錄;
         ~account 代表account這個用戶的家目錄;

         實例:
         [root@bogon tmp]# cd /tmp     #進入/tmp目錄
         [root@bogon tmp]#

         [root@bogon tmp]# cd ~        #回到家目錄,當前用戶是rootr所以進入的為root的家目錄#
         [root@bogon ~]#

         [root@bogon ~]# cd –          #進入上一次工作目錄#  
         /tmp
         [root@bogon tmp]#

         [root@bogon tmp]# cd ..       #回到上級目錄#
         [root@bogon tmp]#
    ls : list directory contents 顯示目錄內容;
      參數:
           -a : 顯示全部的文件,包括隱藏文件并顯示.與..目錄;
           -A :顯示全部的文件,包括隱藏文稿件但不包括.與..目錄;
           -d :只顯示目錄的本身,不顯示其目錄內容;
           -h : 例出文件的大?。?br />           -l : 顯示文件的詳細文件屬性信息;
           -n :列出 UID 與 GID 而非使用者與群組的名稱;
           -F :根據文件、目錄等信息提供附加數據結構;
              *:代表可執行文件; /:代表目錄; =:代表 socket 檔案; |:代表 FIFO 文件;@:表示鏈接文件
           -R :連同子目錄所有內容一起顯示出來;
           -S :以文件大小排序
      實例:
          [root@bogon var]# ls -alS
          total 16
          drwxr-xr-x. 25 root root 4096 Feb  7 09:48 lib
          drwxr-xr-x.  7 root root 4096 Feb  7 09:48 log
          drwxrwxrwt. 12 root root 4096 Feb  7 09:49 tmp
          drwxr-xr-x. 19 root root  267 Feb  7 09:48 .
          dr-xr-xr-x. 17 root root  224 Jan 16 16:34 ..
          -rw-r–r–.  1 root root  163 Jan 16 16:24 .updated
          drwxr-xr-x. 10 root root  118 Jan 16 16:29 spool
          drwxr-xr-x.  8 root root   92 Jan 16 16:29 cache
          drwxr-xr-x.  3 root root   34 Jan 16 16:29 db
          drwxr-xr-x.  3 root root   18 Jan 16 16:29 empty
          drwxr-xr-x.  3 root root   18 Jan 16 16:26 kerberos
          lrwxrwxrwx.  1 root root   11 Jan 16 16:24 lock -> ../run/lock
          lrwxrwxrwx.  1 root root   10 Jan 16 16:24 mail -> spool/mail
          drwxr-xr-x.  2 root root    6 Nov  5 23:38 adm

    cat :concatenate files and print on the standard output 連結文件與打印標準輸出;
          顯示文件;創建文件;將多個文件合并為一個文件
          參數:
               -n : 對輸出顯示內容從1開始編號;
               -b : 與-n相同,但不對空白行編號;
               -e : 在每行結束顯示 $ 。

          實例:
                  [root@bogon var]# cat -ne /etc/sysconfig/network-scripts/ifcfg-ens33  
                  #顯示此文件內容,對其內容進行編號并每行以$結尾 #
                     1  TYPE="Ethernet"$
                     2  BOOTPROTO="dhcp"$
                     3  DEFROUTE="yes"$
                     4  PEERDNS="yes"$
                     5  PEERROUTES="yes"$
                     6  IPV4_FAILURE_FATAL="no"$
                     7  IPV6INIT="yes"$
                     8  IPV6_AUTOCONF="yes"$
                     9  IPV6_DEFROUTE="yes"$
                    10  IPV6_PEERDNS="yes"$
                    11  IPV6_PEERROUTES="yes"$
                    12  IPV6_FAILURE_FATAL="no"$
                    13  IPV6_ADDR_GEN_MODE="stable-privacy"$
                    14  NAME="ens33"$
                    15  UUID="b0fc63a7-1b0a-4107-ae8e-07c73aa90004"$
                    16  DEVICE="ens33"$
                    17  ONBOOT="yes"$
         
                  [root@bogon var]# cat > /tmp/cat.txt <<EOF   #創建文件cat.txt,并以EOF字符結尾#
                  > Hello Everybody
                  > Welcome to the world of Linux
                  > Good Lock!
                  > EOF
                  [root@bogon var]# cat /tmp/cat.txt
                  Hello Everybody
                  Welcome to the world of Linux
                  Good Lock!

                  [root@bogon var]# cat /tmp/cat1.txt              #查看cat1.txt內容#
                  Ending
                  2017.02.07
                  [root@bogon var]# cat /tmp/cat.txt > /tmp/cat1.txt   #把cat.txt文件復制到cat1.txt中并覆蓋原cat1.txt文件內容#
                  [root@bogon var]# cat /tmp/cat1.txt                  #查看cat1.txt內容#
                  Hello Everybody
                  Welcome to the world of Linux
                  Good Lock!

                [root@localhost ~]# cat /tmp/cat.txt                  #查看cat.txt內容#
                Hello Everybody
                Welcome to the world of Linux
                Good Lock!
                [root@localhost ~]# cat /tmp/cat1.txt                 #查看cat1.txt內容#
                Ending.
                [root@localhost ~]# cat /tmp/cat1.txt >> /tmp/cat.txt  #把cat1.txt文件內容復制添中到cat.txt內容下方#
                [root@localhost ~]# cat /tmp/cat.txt                   #查看cat.txt內容#
                Hello Everybody
                Welcome to the world of Linux
                Good Lock!
                Ending.
                2017.02

      tac : concatenate and print files in reverse  反向顯示內容
             tac是將cat反寫,所以它的功能就與cat相反。cat 從第一排到最后一排,tac是從最后一排到第一排。
             [root@localhost ~]# tac /tmp/cat.txt
             2017.02
             Ending.
             Good Lock!
             Welcome to the world of Linux
             Hello Everybody

      head :output the first part of files 顯示出前N行;
          參數:
               -n :n為數字,代表要從頭開始顯示幾行。默認顯示十行,-20為顯示20行;
               [root@localhost ~]# head /etc/inittab          #顯示文件的前十行#
                # inittab is no longer used when using systemd.
                #
                # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
                #
                # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
                #
                # systemd uses 'targets' instead of runlevels. By default, there are two main targets:
                #
                # multi-user.target: analogous to runlevel 3
                # graphical.target: analogous to runlevel 5

      tail : output the last part of files 顯示文件后N行;
          參數:-n :n為數字,代表要從頭開始顯示幾行。默認顯示十行,-20為顯示20行;
          [root@localhost ~]# tail /etc/inittab
          #
          # multi-user.target: analogous to runlevel 3
          # graphical.target: analogous to runlevel 5
          #
          # To view current default target, run:
          # systemctl get-default
          #
          # To set a default target, run:
          # systemctl set-default TARGET.target
          #

      more : file perusal filter for crt viewing 翻頁查看;
          參數:
              空格鍵 (space):代表向下翻一頁;
              Enter         :代表向下翻【一行】;
              /字符串         :代表在這個顯示的內容當中,向下搜尋【字符串】;
              :f             :立刻顯示出文件名以及目前顯示的行數;
              q             :代表立刻離開 more ,不再顯示該文件內容。

      less : 與more相似,less命令允許用戶向前向后瀏覽文件,而more命令只能向前瀏覽。用PageUp鍵和Pagedown鍵翻頁,按Q退出。
          cat 連續顯示、查看文件內容
          more 分頁查看文件內容
          less 分頁可控制查看文件內容

   tree : list contents of directories in a tree-like format. 分層樹級結構顯示內容
      [root@localhost ~]# tree /tmp
      /tmp
      ├── cat1.txt
      ├── cat.txt
      ├── systemd-private-9271cc4e10aa42e18e934ce49c91826b-vmtoolsd.service-NVM6f8
      │   └── tmp
      └── systemd-private-ac6dfc77c71646bd91b02da152e18f10-vmtoolsd.service-sia9xB
          └── tmp

      4 directories, 2 files

   
   file : determine file type 查看文件件類型

        [root@bogon ~]# file /tmp/inittab
        /tmp/inittab: ASCII text

        [root@bogon ~]# file /var/mail
        /var/mail: symbolic link to `spool/mail'
   

   修改類

   touch : change file timestamps  創建空文件、改變文件時間
        -c: 指定的文件路徑不存在時不予創建;
        -a: 僅修改access time;
        -m:僅修改modify time;
        -t STAMP
          [[CC]YY]MMDDhhmm[.ss]
    實例:
    [root@bogon tmp]# touch /tmp/test/1.txt
    [root@bogon tmp]# ls -l /tmp/test
    total 0
    -rw-r–r–. 1 root root 0 Feb 16 17:32 1.txt

    mkdir:make directories 創建目錄

      -p: 自動按需創建父目錄;
      -v: verbose,顯示詳細過程;
      -m MODE:直接給定權限;
    實例:
      [root@bogon tmp]# mkdir /tmp/2017
      [root@bogon tmp]# ls -al
      total 1464
      drwxrwxrwt. 10 root root     228 Feb 16 17:49 .
      dr-xr-xr-x. 17 root root     224 Feb 12 13:41 ..
      drwxr-xr-x.  2 root root       6 Feb 16 17:49 2017

      創建多個目錄

      [root@bogon tmp]# mkdir ubuntu redhat slackware
      [root@bogon tmp]# ls -al
      total 1464
      drwxrwxrwt. 13 root root     273 Feb 16 17:54 .
      dr-xr-xr-x. 17 root root     224 Feb 12 13:41 ..
      drwxr-xr-x.  2 root root       6 Feb 16 17:49 2017
      drwxrwxrwt.  2 root root       6 Feb 16 10:51 .font-unix
      drwxrwxrwt.  2 root root       6 Feb 16 10:51 .ICE-unix
      -rw-r–r–.  1 root root     511 Feb 16 11:35 inittab
      -rw——-.  1 root root 1491821 Feb 15 22:25 messages
      drwxr-xr-x.  2 root root       6 Feb 16 17:54 redhat
      drwxr-xr-x.  2 root root       6 Feb 16 17:54 slackware
      drwxr-xr-x.  2 root root       6 Feb 16 17:54 ubuntu
   
      [root@bogon tmp]# mkdir -p ./1/2/3/
      [root@bogon tmp]# tree
      .
      ├── 1
      │   └── 2
      │       └── 3
      ├── 2017
      ├── inittab
      ├── messages
      ├── redhat
      ├── slackware
      ├── test
      │   └── 1.txt
      └── ubuntu

  使用 -m 參數,我們可以給即將生成的新目錄設置權限。
      [root@bogon ~]# mkdir -m=rw- rw
      [root@bogon ~]# ls -dl ./rw
      drw-r–r–. 2 root root 6 Feb 17 11:33 ./rw

      [root@bogon ~]# mkdir -m=777 rwx
      [root@bogon ~]# ls -dl ./rwx
      drwxrwxrwx. 2 root root 6 Feb 17 11:40 ./rwx
      [root@bogon tmp]# mkdir -p -v -m 664 d1/d2/d3/d4
      mkdir: created directory ‘d1’
      mkdir: created directory ‘d1/d2’
      mkdir: created directory ‘d1/d2/d3’
      mkdir: created directory ‘d1/d2/d3/d4’

    注意:路徑基名方為命令的作用對象;基名之前的路徑必須得存在;
    創建目錄的首要條件是, 在想要創建目錄的目標路徑下你必須具有訪問權限
        
    rmdir:remove empty directories

    rmdir [OPTION]… DIRECTORY… 刪除目錄

        -p:刪除某目錄后,如果其父目錄為空,則一并刪除之;
        -v: 顯示過程;
      [root@bogon tmp]# mkdir -p a/b/c/d
      [root@bogon tmp]# tree
      .
      ├── a
      │   └── b
      │       └── c
      │           └── d
      ├── inittab
      ├── messages
      ├── redhat
      ├── slackware
      ├── test
      │   └── 1.txt
      └── ubuntu

      10 directories, 3 files
      [root@bogon tmp]# rmdir -p -v a/b/c/d
      rmdir: removing directory, ‘a/b/c/d’
      rmdir: removing directory, ‘a/b/c’
      rmdir: removing directory, ‘a/b’
      rmdir: removing directory, ‘a’
 
   rm :  remove files or directories 刪除或者目錄

      一不小心就下崗命令:rm -rf
      -f: 強制刪除文件或目錄;
      -i: 刪除已有文件或者目錄之前先詢問用戶;
      -r: 或-R遞歸處理,將指定目錄下的所有文件與子目錄一并處理;
      -v: 顯示指令詳細過程;

    [root@bogon tmp]# rm -i -r -v 1/2/3/4
    rm: remove directory ‘1/2/3/4’? y
    removed directory: ‘1/2/3/4’

   mv : mv – move (rename) files 移動
      –backup=<備份模式>:若需覆蓋文件,則覆蓋前先行備份; -b:當文件存在時,覆蓋前,為其創建一個備份; -f:若目標文件或目錄與現有的文件或目錄重復,則直接覆蓋現有的文件或目錄;
       -i:交互式操作,覆蓋前先行詢問用戶,如果源文件與目標文件或目標目錄中的文件同名,則詢問用戶是否覆蓋目標文件。用戶輸入”y”,表示將覆蓋目標文件;輸入”n”,表示取消對源文件的移動。這樣可以避免誤將文件覆蓋。 –strip-trailing-slashes:刪除源文件中的斜杠“/”;
       -S<后綴>:為備份文件指定后綴,而不使用默認的后綴;
       –target-directory=<目錄>:指定源文件要移動到目標目錄; -u:當源文件比目標文件新或者目標文件不存在時,才執行移動操作。

    cp : mv – move (rename) files 復制
        -a:此參數的效果和同時指定"-dpR"參數相同; -d:當復制符號連接時,把目標文件或目錄也建立為符號連接,并指向與源文件或目錄連接的原始文件或目錄; -f:強行復制文件或目錄,不論目標文件或目錄是否已存在;
        -i:覆蓋既有文件之前先詢問用戶; -l:對源文件建立硬連接,而非復制文件; -p:保留源文件或目錄的屬性; -R/r:遞歸處理,將指定目錄下的所有文件與子目錄一并處理; -s:對源文件建立符號連接,而非復制文件; -u:使用這項參數后只會在源文件的更改時間較目標文件更新時或是名稱相互對應的目標文件并不存在時,才復制文件; -S:在備份文件時,用指定的后綴“SUFFIX”代替文件的默認后綴; -b:覆蓋已存在的文件目標前將目標文件備份;
         -v:詳細顯示命令執行的操作。

    file : determine file type 確定文件類型

        [root@localhost etc]# file /etc/inittab
        /etc/inittab: ASCII text

    stat : stat – display file or file system status  顯示文件元數據信息
      實例:
        [root@localhost etc]# stat /tmp
        File: ‘/tmp’
        Size: 4096        Blocks: 8          IO Block: 4096   directory
        Device: fd00h/64768d  Inode: 67160136    Links: 16
        Access: (1777/drwxrwxrwt)  Uid: (    0/    root)   Gid: (    0/    root)
        Context: system_u:object_r:tmp_t:s0
        Access: 2017-02-09 17:38:37.529189098 +0800
        Modify: 2017-02-09 17:38:28.661188955 +0800
        Change: 2017-02-09 17:38:28.661188955 +0800
         Birth: –

 

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

  l。 命令執行狀態返回值
     當系統成功執行一條命令時,會返回0值,當執行失敗時,會返回1~255,可以通過echo $? 命令查詢上一條指令是否執行成功。
     [root@localhost ~]# lls
     -bash: lls: command not found
     [root@localhost ~]# echo $?
     127
     [root@localhost ~]# ls
     anaconda-ks.cfg  manpages-zh-1.5.1  manpages-zh-1.5.1.tar.gz
     [root@localhost ~]# echo $?
     0
 

3、請使用命令行展開功能來完成以下練習:
   (1)、創建/tmp目錄下的:a_c, a_d, b_c, b_d
        [root@localhost test]# mkdir ./{a,b}_{c,d}
        [root@localhost test]# ls
        a_c  a_d  b_c  b_d

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

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}}

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
4、文件的元數據信息有哪些,分別表示什么含義,如何查看?如何修改文件的時間戳信息。
    
    元數據包括如下信息:
    File:     文件名
    Size:     文件的大小
    Blocks:   文件塊數
    IO Block:IO塊大小
    Device:   設備號
    Links:    鏈接次數
    Access:   訪問權限
    Uid:      用戶ID
    Gid:      組ID
    Access:   最后訪問時間
    Modify:   最后修改數據時間
    Change:   最后更改元數據時間

  查看元數據命令:stat

    [root@localhost tmp]# stat /etc/inittab
     File: ‘/etc/inittab’
     Size: 511         Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d  Inode: 67630852    Links: 1
    Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: system_u:object_r:etc_t:s0
    Access: 2017-02-09 14:43:18.832019412 +0800
    Modify: 2016-11-06 11:10:11.000000000 +0800
    Change: 2017-01-16 16:27:52.069749341 +0800
    Birth: –

  修改文件的時間戳:touch

      -a     修改文件 file 的存取時間.

      -c     不創建文件 file.
      -m     修改文件 file file
      -r ref_file
              將參照文件 ref_file 相應的時間戳記的數值作為指定文件 file 時間戳記的新值.
      -t time
              使用指定的時間值 time 作為指定文件 file 相應時間戳記的新值.此處的 time 規定為如下形式的十進制數:
              [[CC]YY]MMDDhhmm[.SS]

 

5、如何定義一個命令的別名,如何在命令中引用另一個命令的執行結果?
[root@localhost tmp]# alias lsah='ls -ah –color=auto'
[root@localhost tmp]# lsah /tmp/mylinux
.  ..  bin  boot  dev  etc  lib  lib64  proc  sbin  sys  tmp  usr  var

6、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其它字符)的文件或目錄。
 [root@localhost tmp]# ls -al l*[0-9]*[a-z]
-rw-r–r–. 1 root root 0 Feb  9 17:02 lweb0a
-rw-r–r–. 1 root root 0 Feb  9 17:03 lweb666a

7、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄。
 [root@localhost etc]# touch 11o
 [root@localhost etc]# ls -al [0-9]*[^0-9]
 -rw-r–r–. 1 root root 0 Feb  9 17:10 11o

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

[root@localhost etc]# ls /etc/[^a-z][a-z]*
/etc/1abcd

9、在tmp目錄下創建以tfile開頭,后跟當前日期和時間的文件,文件名形如:tfile-2016-05-27-09-32-22。
[root@localhost etc]# touch /tmp/tfile-$(date "+%Y-%m-%d-%H-%M-%S")
[root@localhost etc]# ls /tmp/
cat1.txt
cat.txt
lweb0a
lweb666a
mylinux/
mytest1/
mytest2/
mytest3/
tfile-
tfile-2017-02-09-17-38-28
.X11-unix/
.XIM-unix/

10、復制/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。
[root@localhost etc]# ls /tmp/mytest1
pam.d   passwd-  plymouth  popt.d   ppp             printcap  profile.d  python
passwd  pki      pm        postfix  prelink.conf.d  profile   protocols

11、復制/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中。

[root@localhost etc]# ls /tmp/mytest1
pam.d   passwd-  plymouth  popt.d   ppp             printcap  profile.d  python
passwd  pki      pm        postfix  prelink.conf.d  profile   protocols

12、復制/etc/目錄下所有以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中。"

[root@localhost etc]# cp [l,n,m]*.conf /tmp/mytest3
[root@localhost etc]# ls /tmp/mytest3
ld.so.conf  libaudit.conf  libuser.conf  locale.conf  logrotate.conf  man_db.conf  mke2fs.conf  nsswitch.conf

原創文章,作者:北京-且聽風吟,如若轉載,請注明出處:http://www.www58058.com/69101

(0)
北京-且聽風吟北京-且聽風吟
上一篇 2017-02-17
下一篇 2017-02-17

相關推薦

  • 一周學會shell編程之小結1

    一周學會shell編程之小結1 內容: shell腳本創建與執行 變量 條件測試 if,case判斷語句 練習   檢查錯誤: bash -n path 調試執行: bash -x path   創建shell腳本步驟: 1 添加第一行 #!/bin/bash 2 給予…

    Linux干貨 2016-08-15
  • 一個PHP程序員學習運維的轉型

         我是一位PHP開發工程師,平時負責前端、后端以及服務器端的工作,但是稱不上是個牛逼的程序員。網上熱烈討論一則傳聞,“全棧工程師”(Full Stack Engineer),要求應征者對開發堆棧的每個方面都有所掌握。那究竟何為 “全棧工程師”呢?從字面上來理解,全棧工程師必須熟悉開發堆棧的每一個層次,或者至少熟悉絕大多數…

    Linux干貨 2017-04-02
  • vim編輯器-練習題

    1 、復制/etc/profile至/tmp/目錄,用查找替換命令刪除/tmp/profile文件中的行首的空白字符 #cp /etc/profile /tmp #vim /tmp/profile :%s/^[[:space:]]\+// 2 、復制/etc/rc.d/init.d/functions 文件至/tmp 目錄,用查找替換命令為/tmp/func…

    Linux干貨 2016-08-15
  • 第五周練習

    1、顯示當前系統上root、fedora或user1用戶的默認shell; egrep “^(root|fedora|user1)” /etc/passwd | cut -d: -f7 2、找出/etc/rc.d/init.d/functions文件中某單詞后邊跟一組小括號的行,形如:hello(); grep “\<.*\>()” /etc/r…

    Linux干貨 2017-08-04
  • 用戶和組——Linux基本命令(10)

    1.     用戶和組的配置文件 Linux用戶和組的主要配置文件: /etc/passwd:用戶及其屬性信息(名稱、UID、主組ID等) /etc/group:組及其屬性信息 /etc/shadow:用戶密碼及其相關屬性 /etc/gshadow:組密碼及其相關屬性   2. /etc/passwd 在Li…

    2017-07-22

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-03-07 15:54

    完成的非常好,很用心的在做,請繼續保持,加油

欧美性久久久久