Hi_Amos
坚持每天都在进步!!

在文本处理的工作中,统计文件的行数,单词数和字符数非常有用。而对于开发人员本身来说,统计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命令手册.

 

posted on 2013-12-29 14:31  Hi_Amos  阅读(3240)  评论(0编辑  收藏  举报