help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: finding .m files using 'locate'


From: Mike Miller
Subject: Re: finding .m files using 'locate'
Date: Fri, 15 Jul 2005 16:50:02 -0500 (CDT)

On Fri, 15 Jul 2005, Przemek Klosowski wrote:

  command.  Here is a way to get a list of every directory that contains at
  least one file with the .m extension:

  locate .m | egrep '\.m$' | perl -pe 's#(^.*)/[^/]+.m#$1#g' | sort | uniq

Or, more simply:

  locate '*.m' | xargs -i dirname '{}' | uniq

Explanation: locate treats *.m differently from simple .m by anchoring it at the end;

That's one that I didn't know, so thanks, but I conclude that this is the best approach:

locate '*.m' | perl -pe 's#(^.*)/[^/]+\.m#$1#g' | sort | uniq

Because it is fast and gives the correct answer. Your method takes about 100 times as long and it may list some directories more than once. See below for explanations.


xargs runs dirname to strip the filenames (quotes to protect against shell matching and non-alphanumerics in filenames).

It's really slow compared to perl. On Solaris, I compared your method with mine and found that yours took 111.49 seconds and mine took 0.96 seconds, but the output files were the same.

# time ; locate '*.m' | xargs -i dirname '{}' | uniq > foobar1 ; time
17.29u 15.98s 665:19:13.23 0.0%
58.47u 80.44s 665:21:04.72 0.0%
# time ; locate '*.m' | perl -pe 's#^(.*)/[^/]*.m#$1#g' | uniq > foobar2 ; time
58.47u 80.44s 665:23:49.66 0.0%
59.25u 80.61s 665:23:50.62 0.0%


Sort is not necessary because locate sorts its data.

Locate sorts the full path, including the filename. After you strip out the filename, you need to sort again before sending to uniq or you will have problems. This is output from 'locate':

/taxa1/home/ghosh/octavestuff/map_sturt.m
/taxa1/home/ghosh/octavestuff/matlab/map_haldane.m
/taxa1/home/ghosh/octavestuff/matlab/matlab_map_haldane.m
/taxa1/home/ghosh/octavestuff/merlin_data_simulator.m

After removing filenames:

/taxa1/home/ghosh/octavestuff
/taxa1/home/ghosh/octavestuff/matlab
/taxa1/home/ghosh/octavestuff/matlab
/taxa1/home/ghosh/octavestuff

After uniq:

/taxa1/home/ghosh/octavestuff
/taxa1/home/ghosh/octavestuff/matlab
/taxa1/home/ghosh/octavestuff

Thus, directories may be repeated in the output if 'sort' isn't run immediately before 'uniq.'

Mike

--
Michael B. Miller, Ph.D.
Assistant Professor
Division of Epidemiology and Community Health
and Institute of Human Genetics
University of Minnesota
http://taxa.epi.umn.edu/~mbmiller/



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]