歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux 下使用 wc 統計文件夾下所有文件的代碼行數(包括子目錄)

Linux 下使用 wc 統計文件夾下所有文件的代碼行數(包括子目錄)

日期:2017/2/28 14:22:22   编辑:Linux教程

wc 命令用於統計文件內容的行數、單詞數、字母數。但是如果想統計一個目錄下所有文件的行數,wc 並沒有提供遞歸統計。

不過,可以結合 find 命令,例如:

$ wc -l `find -name *.c`

其中,-l 參數是統計行數,find -name *.c 是查找當前目錄(包含子目錄)下所有的C文件,` ` 是 shell 中的替換命令。

當然,實現方法有很多,我只是覺得這個比較簡單易記。

測試效果如下:

最後,附一下 wc 的使用幫助(man 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

Copyright © Linux教程網 All Rights Reserved