How can I list files having more than 100 lines in a directory and in all its sub directories? An example of such a terminal command will be very helpful.
Is there a tool to count the line numbers of given files?
You can count lines with wc
, the word count utility:
find . -type f -print0 | xargs -0 wc -l | sort -n
find
command prints out all the files in the current directory and its subdirectories. xargs
receives that list of files and passes them to a series of wc -l
commands, which print the line count of each those files. sort -n
sorts the list of files numerically by their line counts. Strictly speaking, it would be more efficient if we could tell wc
to stop counting after reaching 100 lines, but I'm not aware of a way to do that.
Use the following command:
find <folder-to-search> -name "*.txt" -type f -exec sh -c 'test `wc -l {} | cut -f1 -d" "` -gt "100"' \; -print
Also take a look at the -name
parameter, currently you will find only files that end with .txt
. You want to alter that or, just delete the parameter and the argument to find all files.
You can do it in one line with awk as well, wanted to add it as a comment to support Paul s answer but due to >> echo "1 reputation" | awk '$1<50' reputation I couldn't :D
find . -type f | xargs wc -l | awk -F "." '$1>100'
Optional --> -name that ortang mentioned with find... and -F is the delimiter, even default space can work like "awk "$1>100"