1

I have around 20 C++ files in my folder that I want to check for line length but I don't want to manually go through all of them. Is there any script/program in Linux/Windows that I can use to check an entire folder for files with long lines?

Thanks!

2 Answers 2

3

To check for files with lines long than 80 characters you can use grep:

grep -l '.\{80,\}'

-l gives you only files with matches. If you want to recurse use -R * that could be applied to a specific subfolder like src/*

You can filter files by extension (or any other regex) using --include='*.c'

$ grep  -l '.\{80,\}' -R src/* --include='*.c'
src/main.c
src/model/model.c

If you want the exact lines you can remove -l. If you want line for matches add -n:

$ grep  -n '.\{80,\}' -R src/* --include='*.c'
src/main.vala:2: * A long line comment that takes more than 80 characters and needs some kind of fix.
src/model/model.c:14: * Another long line comment that takes more than 80 characters and needs some kind of fix.

if you have ripgrep available it is faster:

$ rg '.{80,}' src
src/main.c
2: * A long line comment that takes more than 80 characters and needs some kind of fix.

src/model/model.c
14: * Another long line comment that takes more than 80 characters and needs some kind of fix.

ripgrep accepts --vimgrep for a similar output than grep.

I strongly suggest to use --color=auto if not activated by default on your system.

1

Try doing this :

find -type f -exec bash -c '
    (( $(wc -l < "$1") > 80)) && echo "$1"
' -- {} \;
1
  • 2
    Doesn't this check the number of lines rather than their length? Commented Nov 30, 2018 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .