Is there a better way to do this?

Is there a better way to do this?

am 05.10.2007 11:13:15 von struggle

Hello,
I am now trying to count the total lines of code of all source files
in a given directory. And I use the following script:

find ./ \( -name .svn -prune \) -or \( ! -type d -print \) | tr '\n' '
' | xargs wc -l

I am sure this works well but I want to know is there a better way to
achieve this? Thanks in advance!

Regards!
Bo

Re: Is there a better way to do this?

am 05.10.2007 11:35:34 von Stephane CHAZELAS

2007-10-05, 17:13(+08), Bo Yang:
> Hello,
> I am now trying to count the total lines of code of all source files
> in a given directory. And I use the following script:
>
> find ./ \( -name .svn -prune \) -or \( ! -type d -print \) | tr '\n' '
> ' | xargs wc -l
>
> I am sure this works well but I want to know is there a better way to
> achieve this? Thanks in advance!
[...]

find . ! -name .svn -o -prune ! -type d -exec wc -l {} +

With zsh

wc -l **/*(^/)

(by default, zsh doesn't include dotfiles and doesn't descend
into dot dirs, if you don't want that):

wc -l (^.svn/)#*(D^/)

Note that by doing ! -type d or (^/), you may include device
files or pipes, or sockets or symlinks to directories. You may
prefer to include regular files or symlinks to regular files:

wc -l **/*(-.)

Unfortunately, find doesn't have any easy equivalent, you need:

find . ! -name .svn -o -prune \( -type f -o -type l -exec \
test -f {} \; \) -exec wc -l {} +


--
Stéphane

Re: Is there a better way to do this?

am 05.10.2007 13:23:14 von struggle

Stephane CHAZELAS :
> 2007-10-05, 17:13(+08), Bo Yang:
>> Hello,
>> I am now trying to count the total lines of code of all source files
>> in a given directory. And I use the following script:
>>
>> find ./ \( -name .svn -prune \) -or \( ! -type d -print \) | tr '\n' '
>> ' | xargs wc -l
>>
>> I am sure this works well but I want to know is there a better way to
>> achieve this? Thanks in advance!
> [...]
>
> find . ! -name .svn -o -prune ! -type d -exec wc -l {} +
This does not work in my shell. And I change the the command line as
find . ! -name .svn -o -prune ! -type d -exec wc -l '{}' \+
before I invoke it. But the error is :
find: missing argument to `-exec'.

Do you mind telling me the reason? Thanks!

>
> With zsh
>
> wc -l **/*(^/)
>
> (by default, zsh doesn't include dotfiles and doesn't descend
> into dot dirs, if you don't want that):
>
> wc -l (^.svn/)#*(D^/)
>
> Note that by doing ! -type d or (^/), you may include device
> files or pipes, or sockets or symlinks to directories. You may
> prefer to include regular files or symlinks to regular files:
>
> wc -l **/*(-.)
>
> Unfortunately, find doesn't have any easy equivalent, you need:
>
> find . ! -name .svn -o -prune \( -type f -o -type l -exec \
> test -f {} \; \) -exec wc -l {} +
>
>

Re: Is there a better way to do this?

am 05.10.2007 13:37:31 von Stephane CHAZELAS

2007-10-05, 19:23(+08), Bo Yang:
> Stephane CHAZELAS :
>> 2007-10-05, 17:13(+08), Bo Yang:
>>> Hello,
>>> I am now trying to count the total lines of code of all source files
>>> in a given directory. And I use the following script:
>>>
>>> find ./ \( -name .svn -prune \) -or \( ! -type d -print \) | tr '\n' '
>>> ' | xargs wc -l
>>>
>>> I am sure this works well but I want to know is there a better way to
>>> achieve this? Thanks in advance!
>> [...]
>>
>> find . ! -name .svn -o -prune ! -type d -exec wc -l {} +
> This does not work in my shell. And I change the the command line as
> find . ! -name .svn -o -prune ! -type d -exec wc -l '{}' \+
> before I invoke it. But the error is :
> find: missing argument to `-exec'.
>
> Do you mind telling me the reason? Thanks!

You seem to have a non-standard find.

Try using "command -p find" instead of "find" to be sure you get
the portable one.

If you're using GNU find, you may need to upgrade. Old versions
of GNU find were known to not support the -exec {} + feature.

Note that the + character is not special to the shell, so you
don't need to escape it.

With GNU find and xargs, you can use find ... -print0 | xargs
-r0 ... instead of find ... -exec ... {} +

--
Stéphane

Re: Is there a better way to do this?

am 05.10.2007 15:12:58 von struggle

Stephane CHAZELAS :
> 2007-10-05, 19:23(+08), Bo Yang:
>> Stephane CHAZELAS :
>>> 2007-10-05, 17:13(+08), Bo Yang:
>>>> Hello,
>>>> I am now trying to count the total lines of code of all source files
>>>> in a given directory. And I use the following script:
>>>>
>>>> find ./ \( -name .svn -prune \) -or \( ! -type d -print \) | tr '\n' '
>>>> ' | xargs wc -l
>>>>
>>>> I am sure this works well but I want to know is there a better way to
>>>> achieve this? Thanks in advance!
>>> [...]
>>>
>>> find . ! -name .svn -o -prune ! -type d -exec wc -l {} +
>> This does not work in my shell. And I change the the command line as
>> find . ! -name .svn -o -prune ! -type d -exec wc -l '{}' \+
>> before I invoke it. But the error is :
>> find: missing argument to `-exec'.
>>
>> Do you mind telling me the reason? Thanks!
>
> You seem to have a non-standard find.
>
> Try using "command -p find" instead of "find" to be sure you get
> the portable one.
>
> If you're using GNU find, you may need to upgrade. Old versions
> of GNU find were known to not support the -exec {} + feature.
>
> Note that the + character is not special to the shell, so you
> don't need to escape it.
>
> With GNU find and xargs, you can use find ... -print0 | xargs
> -r0 ... instead of find ... -exec ... {} +
>
Thank you very much!

Regards!
Bo