Linux wc命令详解

一、wc命令介绍

  如果你想统计一个文件的字数或行数,可以使用wc命令。wc命令的功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。wc命令的基本格式为:

wc [options] 文件名

如果没有给出文件名,则从标准输入读取。wc命令的选项如下表所示:

选项 功能
-c 统计字节数
-l 统计行数
-m 统计字符数(该选项不能与-c一起使用)
-w 统计字数/单词数(一个字被定义为由制表符、空格、换行符分隔的字符串)
-L 打印最长行的长度(以字节为单位)

二、wc使用示例

【例1】wc的基本统计用法

➜  test cat temp.txt
what's your name?
my name is bcy
➜ test
wc temp.txt 2 7 33 temp.txt # 没有选项,依次输出行数、单词数、字节数、文件名
➜ test
wc -l temp.txt # 统计行数 2 temp.txt
➜ test
wc -w temp.txt # 统计单词数 7 temp.txt
➜ test
wc -c temp.txt # 统计字节数 33 temp.txt
➜ test
wc -m temp.txt # 统计字符数 33 temp.txt
➜ test
wc -L temp.txt # 打印最长行的长度 17 temp.txt

【例2】使用wc命令只统计数字,不打印文件名

➜  test cat temp.txt|wc           # 使用管道符即可实现不打印文件名
      2       7      33
➜ test cat temp.txt|wc -l 2
➜ test cat temp.txt|wc -w 7
➜ test cat temp.txt|wc -c 33

【例3】统计当前目录下的文件数

➜  test ll|wc -l           # 当前目录下的文件数为:8-1=7(第一行删掉)
8
➜ test ll total 28K # 删掉本行 -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 139 Sep 20 12:23 data2.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 19 22:34 hello.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 225 Sep 20 17:31 mem.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 109 Sep 20 09:27 osix -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 25 Sep 20 17:46 teefile.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 33 Sep 21 18:08 temp.txt -rw-r--r-- 1 baichunyu.bcy baichunyu.bcy 38 Sep 20 09:26 test.txt

 

posted @ 2021-09-21 18:20  白春雨  阅读(2504)  评论(0编辑  收藏  举报