Obtain the number of lines of a single file
File:Test1.sh
Number of rows:20
Method 1
Awk '{print Nr}' test1.sh | tail-N1
:
Method 2
Awk 'end {print Nr} 'test1.sh
:
Method 3
Grep-n "" test1.sh | awk-F: '{print'} | tail-N1
:
Method 4
Sed-n' $ = 'test1.sh
:
Method 5
WC-l test1.sh
:
Method 6
# Cat test1.sh | WC-l
:
Obtains the number of rows of all objects in a specified directory.
CodeAs follows:
#! /Bin/ Bashfilescount = 0 Linescount = 0 Function Funcount (){ For File In ' Ls $ 1 ' Do If [-D $ 1 " / " $ File ];Then Funcount $ 1 " / " $ File Else Declare - I filelines =' Sed -N ' $ = ' $ 1 " / " $ File 'Let linescount = $ Linescount + $ Filelines let filescount = $ Filescount + 1 Fi Done } If [$ #-GT 0 ]; Then For M_dir In $ @ Do Funcount $ m_dir Done Else Funcount " . " Fi Echo " Filescount = $ filescount " Echo " Linescount = $ linescount "
Usage:
1For the local directory
./Count. Sh
2Statistics on multiple directories
./Count. sh/tmp ~
The running effect is as follows:
Obtains the number of rows of files with a specified extension in a specified directory.
The Code is as follows:
#! /Bin/ Bashextens = ( " . C " " . Cpp " " . H " " . HPP " ) Filescount = 0 Linescount = 0 Function Funcount (){ For File In ' Ls $ 1 ' Do If [-D $ 1 " / " $ File ]; Then Funcount $ 1 " / " $ File Else Filename = $ 1 " / " $ File Extension = " . " $ {Filename ##* .} Echo " Filename = $ filename extension = $ Extension " If [[ " $ {Extens [@]/$ extension /} " ! = " $ {Extens [@]} " ]; Then Declare - I filelines =' Sed -N' $ = ' $ Filename' Echo $ Filename " : " $ Filelines let linescount = $ Linescount + $ Filelines let filescount = $ Filescount + 1 Fi Fi Done } If [$ #-GT 0 ]; Then For M_dir In $ @ Do Funcount $ m_dir Done Else Funcount " . " Fi Echo " Filescount = $ filescount " Echo " Linescount = $ linescount "
Usage:
1For the local directory
./Count. Sh
2Statistics on multiple directories
./Count. sh/tmp ~
The running effect is as follows:
well, that's all. I hope it will help you.