linux shell 腳本攻略學習16--wc命令詳解,tree命令詳解


在文本處理的工作中,統計文件的行數,單詞數和字符數非常有用。而對於開發人員本身來說,統計LOC(line of code ,代碼行數)是一件重要的工作。linux中有什么命令可以幫助我們做統計呢?沒錯,就是wc,不是廁所的意思啊,是Word Count的縮寫。

當作好統計時,又需要寫個開發的文檔,特別是項目比較大的時候,如果將目錄和文件系統以圖形化的樹狀層次結構描述,在以后的維護過程將更加清晰明了,下面將同樣將要介紹的還有tree命令.

一、wc命令詳解

首先,輸入man wc 查看wc 的指導手冊

NAME
       wc - print newline, word, and byte counts for each file

SYNOPSIS
       wc [OPTION]... [FILE]...
       wc [OPTION]... --files0-from=F

DESCRIPTION
       Print  newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.  With no FILE, or
       when FILE is -, read standard input.  A word is a non-zero-length sequence of characters delimited  by  white  space.   The
       options  below  may  be  used  to select which counts are printed, always in the following order: newline, word, character,
       byte, maximum line length.

       -c, --bytes
              print the byte counts

       -m, --chars
              print the character counts

       -l, --lines
              print the newline counts

       --files0-from=F
              read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input

       -L, --max-line-length
              print the length of the longest line

       -w, --words
              print the word counts

       --help display this help and exit

       --version
              output version information and exit

語法:

wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F

參數-實例:

下面將通過實例介紹各參數的含義和作用:

用到的test.txt

amosli@amosli-pc:~/learn/wc$ cat test.txt 
Print  newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.  With no FILE, or
       when FILE is -, read standard input.  A word is a non-zero-length sequence of characters delimited  by  white  space.   The
       options  below  may  be  used  to select which counts are printed, always in the following order: newline, word, character,
       byte, maximum line length.

1、-c參數,統計字節數,字符數

amosli@amosli-pc:~/learn/wc$ wc -c test.txt 
420 test.txt

2.-l參數,統計行數

amosli@amosli-pc:~/learn/wc$ wc -l test.txt 
4 test.txt

3.-w參數,統計單詞數

amosli@amosli-pc:~/learn/wc$ wc -w test.txt 
67 test.txt

4.-L參數,統計最長行的長度

amosli@amosli-pc:~/learn/wc$ wc -L test.txt 
130 test.txt

5.-m參數,統計字母數

amosli@amosli-pc:~/learn/wc$ wc -m test.txt 
420 test.txt

6.不加參數

amosli@amosli-pc:~/learn/wc$ wc test.txt 
  4  67 420 test.txt

分別表示行數,單詞數,字符數

7.使用標准輸入進行統計

amosli@amosli-pc:~/learn/wc$ echo "hi_amos" | wc -m
8

 

二、tree命令詳解

首先,sudo apt-get install tree安裝一下tree命令

下面將輸入tree --help查看提示信息:

amosli@amosli-pc:~/learn$ tree --help
usage: tree [-adfghilnpqrstuvxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]
    [-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
    [--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
    [--filelimit #] [<directory list>]
  -a            All files are listed.
  -d            List directories only.
  -l            Follow symbolic links like directories.
  -f            Print the full path prefix for each file.
  -i            Don't print indentation lines.
  -q            Print non-printable characters as '?'.
  -N            Print non-printable characters as is.
  -p            Print the protections for each file.
  -u            Displays file owner or UID number.
  -g            Displays file group owner or GID number.
  -s            Print the size in bytes of each file.
  -h            Print the size in a more human readable way.
  -D            Print the date of last modification.
  -F            Appends '/', '=', '*', or '|' as per ls -F.
  -v            Sort files alphanumerically by version.
  -r            Sort files in reverse alphanumeric order.
  -t            Sort files by last modification time.
  -x            Stay on current filesystem only.
  -L level      Descend only level directories deep.
  -A            Print ANSI lines graphic indentation lines.
  -S            Print with ASCII graphics indentation lines.
  -n            Turn colorization off always (-C overrides).
  -C            Turn colorization on always.
  -P pattern    List only those files that match the pattern given.
  -I pattern    Do not list files that match the given pattern.
  -H baseHREF   Prints out HTML format with baseHREF as top directory.
  -T string     Replace the default HTML title and H1 header with string.
  -R            Rerun tree when max dir level reached.
  -o file       Output to file instead of stdout.
  --inodes      Print inode number of each file.
  --device      Print device ID number to which each file belongs.
  --noreport    Turn off file/directory count at end of tree listing.
  --nolinks     Turn off hyperlinks in HTML output.
  --dirsfirst   List directories before files.
  --charset X   Use charset X for HTML and indentation line output.
  --filelimit # Do not descend dirs with more than # files in them.

語法:

tree [-adfghilnpqrstuvxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]
    [-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
    [--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
    [--filelimit #] [<directory list>]

參數-實例:

-a 顯示所有文件和目錄。
-A 使用ASNI繪圖字符顯示樹狀圖而非以ASCII字符組合。
-C 在文件和目錄清單加上色彩,便於區分各種類型。
-d 顯示目錄名稱而非內容。
-D 列出文件或目錄的更改時間。
-f 在每個文件或目錄之前,顯示完整的相對路徑名稱。
-F 在執行文件,目錄,Socket,符號連接,管道名稱名稱,各自加上"*","/","=","@","|"號。
-g 列出文件或目錄的所屬群組名稱,沒有對應的名稱時,則顯示群組識別碼。
-i 不以階梯狀列出文件或目錄名稱。
-I 不顯示符合范本樣式的文件或目錄名稱。
-l 如遇到性質為符號連接的目錄,直接列出該連接所指向的原始目錄。
-n 不在文件和目錄清單加上色彩。
-N 直接列出文件和目錄名稱,包括控制字符。
-p 列出權限標示。
-P 只顯示符合范本樣式的文件或目錄名稱。
-q 用"?"號取代控制字符,列出文件和目錄名稱。
-s 列出文件或目錄大小。
-t 用文件和目錄的更改時間排序。
-u 列出文件或目錄的擁有者名稱,沒有對應的名稱時,則顯示用戶識別碼。
-x 將范圍局限在現行的文件系統中,若指定目錄下的某些子目錄,其存放於另一個文件系統上,則將該子目錄予以排除在尋找范圍外。

1、不輸入任何參數使用tree命令

amosli@amosli-pc:~/learn/re$ tree
.
├── d1
│   └── d2
│   └── d3
│   └── a.txt
├── d2
│   └── d2
│   └── d3
│   └── a.txt
├── db
│   └── dc
├── dd
│   └── db
├── test.sh
├── version1.txt
├── version2.txt
└── version.patch

10 directories, 6 files

 

2、-P參數,對樣式進行篩選

如,篩選.sh結尾的文件

amosli@amosli-pc:~/learn/re$ tree . -P "*.sh"
.
├── d1
│   └── d2
│       └── d3
├── d2
│   └── d2
│       └── d3
├── db
│   └── dc
├── dd
│   └── db
└── test.sh

3、-h參數,打印出文件和目錄的大小

amosli@amosli-pc:~/learn/re$ tree -h 
.
├── [4.0K]  d1
│   └── [4.0K]  d2
│       └── [4.0K]  d3
│           └── [   0]  a.txt
├── [4.0K]  d2
│   └── [4.0K]  d2
│       └── [4.0K]  d3
│           └── [   6]  a.txt
├── [4.0K]  db
│   └── [4.0K]  dc
├── [4.0K]  dd
│   └── [4.0K]  db
├── [   0]  test.sh
├── [  51]  version1.txt
├── [  51]  version2.txt
└── [ 185]  version.patch

10 directories, 6 files

4、-d參數,只顯示目錄

amosli@amosli-pc:~/learn/re$ tree -d
.
├── d1
│   └── d2
│       └── d3
├── d2
│   └── d2
│       └── d3
├── db
│   └── dc
└── dd
    └── db

10 directories

5、-D參數,顯示文件修改日期

amosli@amosli-pc:~/learn/re$ tree -D
.
├── [Dec 28  1:59]  d1
│   └── [Dec 28  1:59]  d2
│       └── [Dec 28  2:00]  d3
│           └── [Dec 28  2:00]  a.txt
├── [Dec 28  2:00]  d2
│   └── [Dec 28  2:00]  d2
│       └── [Dec 28  2:01]  d3
│           └── [Dec 28  2:01]  a.txt
├── [Dec 28  2:02]  db
│   └── [Dec 28  2:02]  dc
├── [Dec 28  2:02]  dd
│   └── [Dec 28  2:02]  db
├── [Dec 29  3:12]  test.sh
├── [Dec 28  1:54]  version1.txt
├── [Dec 28  1:37]  version2.txt
└── [Dec 28  1:54]  version.patch

10 directories, 6 files

 

6、-i參數,不以階梯狀顯示

amosli@amosli-pc:~/learn/re$ tree -i
.
d1
d2
d3
a.txt
d2
d2
d3
a.txt
db
dc
dd
db
test.sh
version1.txt
version2.txt
version.patch

10 directories, 6 files

7、其他參數可以根據自己需要去選擇,更多信息請使用man tree查看tree命令手冊.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM