Unix Help please...
Author
Discussion

toobin

Original Poster:

1,222 posts

265 months

Sunday 5th November 2006
quotequote all
I need to create a UNIX command to find the largets c++ file in all my directories then display the largets .cc file

so far I have


set temp = find $HOME -name '*.cc' -exec wc -c | sort -nr
echo $temp[1];


can anyone help me make this work?

GreenV8S

31,027 posts

315 months

Sunday 5th November 2006
quotequote all
Rather than trying to put the results into a shell variable (I assume that what you were trying to do) you could just pipe it into something that prints the column you want. But before you get into that, does the find command produce the answer you intend?

toobin

Original Poster:

1,222 posts

265 months

Sunday 5th November 2006
quotequote all
find $HOME -name '*.cc'| sort -nr |

this works but it shows all my .cc files

I have tried
pipe head <1>
and pipe echo $1

but neither are any good

Edited by toobin on Sunday 5th November 16:32

mph999

2,780 posts

251 months

Sunday 5th November 2006
quotequote all
to get a particulat column, say column 3 from a command pipe into ...

"some command" |awk '{print $3}'

du command is quite useful for finding sizes ...

eg. du -ak, see man pages for diff. options.

or ...

find /var -size +10000

where 10000 is the no of 512K blocks it will find files equal or bigger ... just change the 10000 to whatever ...

M

toobin

Original Poster:

1,222 posts

265 months

Sunday 5th November 2006
quotequote all
Thanks Martin but that seems to print blank lines on the screen with no real output.

GreenV8S

31,027 posts

315 months

Sunday 5th November 2006
quotequote all
toobin said:
find $HOME -name '*.cc'| sort -nr |

this works but it shows all my .cc files

I have tried
pipe head <1>
and pipe echo $1

but neither are any good

Edited by toobin on Sunday 5th November 16:32


Assume they're correctly sorted biggest first. Next step select the first one e.g.
| head -1

Once you have that, select the column you want e.g. | awk '{print $3;}'

zumbruk

7,848 posts

291 months

Sunday 5th November 2006
quotequote all
GreenV8S said:
toobin said:
find $HOME -name '*.cc'| sort -nr |

this works but it shows all my .cc files

I have tried
pipe head <1>
and pipe echo $1

but neither are any good

Edited by toobin on Sunday 5th November 16:32


Assume they're correctly sorted biggest first. Next step select the first one e.g.
| head -1

Once you have that, select the column you want e.g. | awk '{print $3;}'


"sort" will do all that for you;

ls -l *.cc | sort -k 5

produces a list of the cc files in size order.

ls -l *.cc | sort -r -k 5

A reverse list.

ls -l *.cc | sort -r -k 5 | head -1

The biggest file

And finally, the name of the largest cc file;

ls -l | sort -r -k 5 | head -1 | awk '{print $9}'

Next.

toobin

Original Poster:

1,222 posts

265 months

Sunday 5th November 2006
quotequote all
zumbruk said:

Next.


Thanks Zumbruk,

But I want this to run from a c shell and I still get errors.

So far what I have looks a little like this
# searches home dir for .cc files
# uses that list (xargs) and to word count and sort it
# prints the first in the list and its size

#!/bin/csh

find $HOME -name "*.cc" | xargs wc-c | sort -nr | head -1 | awk '{print $9}'

Any ideas why it returns a black line?


zumbruk

7,848 posts

291 months

Sunday 5th November 2006
quotequote all
toobin said:
zumbruk said:

Next.


Thanks Zumbruk,

But I want this to run from a c shell and I still get errors.

So far what I have looks a little like this
# searches home dir for .cc files
# uses that list (xargs) and to word count and sort it
# prints the first in the list and its size

#!/bin/csh

find $HOME -name "*.cc" | xargs wc-c | sort -nr | head -1 | awk '{print $9}'

Any ideas why it returns a black line?




Well, firstly the "#!" needs to be the very first line in the script, else it won't work.

From your first post; "I need to create a UNIX command to find the largets c++ file in all my directories then display the largets .cc file "

I already gave you the tools to do this - all you need to do is a find . -name "*.cc" instead of the "ls", for example, this line gives you the name of the largest C file (I don't have any cc files) in the current directory and all below it;

find . -name "*.c" -exec ls -l {} ; | sort -r -k 5 | head -1 | awk '{print $9}'find . -name "*.c" -exec ls -l {} ; | sort -r -k 5 | head -1 | awk '{print $9}'

And lo, here it is;

./prog/xearth-0.92/mapdata.c

So, if you want to know how many lines the largest cc file you have is, you can do this;

wc -l `find . -name "*.cc" -exec ls -l {} ; | sort -r -k 5 | head -1 | awk '{print $9}'`

(NOTE the backticks!)

Or, did you want to know which CC file has the largest number of lines..?

find . -name "*.cc" -exec wc -l {} ; | sort -r -k 1 | head -1

huge{~}: find . -name "*.c" -exec wc -l {} ; | sort -r -k 1 | head -1
19645 ./prog/xearth-0.92/mapdata.c

And it's our old friend mapdata.c again.

I'll leave you to play with xargs to speed it up.

Regards, Z

toobin

Original Poster:

1,222 posts

265 months

Sunday 5th November 2006
quotequote all
Thanks Zumbruk, your assistance is greatly appreciated. 6 weeks into this MSc and a long time without sleep confused

ps I only commented above the #!/bin/csh to give you an idea of my logic.
I know I cannot spell largest very well but that'll be the lack of sleep

zumbruk

7,848 posts

291 months

Sunday 5th November 2006
quotequote all
toobin said:
Thanks Zumbruk, your assistance is greatly appreciated. 6 weeks into this MSc and a long time without sleep confused

ps I only commented above the #!/bin/csh to give you an idea of my logic.
I know I cannot spell largest very well but that'll be the lack of sleep


Coo, an MSc. I'm a biochemist, me. Or rather, I was.

toobin

Original Poster:

1,222 posts

265 months

Sunday 5th November 2006
quotequote all
me again

98 jaguar% wc -l `find . -name "*.cc" -exec ls -l {} ; | sort -r -k 5 | head -1 | awk '{print $9}'`
Invalid null command.

(MSc conversion course )

sorry but its got me beat

toobin

Original Poster:

1,222 posts

265 months

Sunday 5th November 2006
quotequote all
And the winner is:

find $HOME -name "*.cc" -type f -exec wc -m {} ; | sort -nr | head -1

Thanks for all the help