Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Compare two (or more) filenames and return what is common between them



On Tue, Mar 18, 2014 at 5:42 PM, Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Mar 18,  8:23pm, Peter Stephenson wrote:
> } Subject: Re: Compare two (or more) filenames and return what is common bet
> }
> } On Tue, 18 Mar 2014 03:05:27 -0400
> } TJ Luoma <luomat@xxxxxxxxx> wrote:
> } > What I am trying to do:
> } >
> } > Given a folder/directory full of files (and, possibly, some existing
> } > folders/directories), I want to create folders which will group files
> } > with similar files names, but which will leave folders alone.
> }
> } I'm still not quite sure after reading your description what it is you
> } want
>
> I suspect he wants something like this:
>
> http://en.wikipedia.org/wiki/Approximate_string_matching
>
> Zsh has such a function internally for "spelling" correction, but it is
> actually based on correcting for typographical mistakes on a QWERTY
> keyboard more than on traditional substring similarity.
>
> You could also try something with the (#a) glob qualifier (approximate
> matching) but it's VERY expensive for strings as long as some of your
> example file names and is entirely impossible to interrupt once it is
> started ("kill -9" territory).

I just realized that there’s a (seemingly) simpler example for what
I’d like to be able to do in a shell-script instead of in an
interactive shell. It wouldn’t have all of the features that I first
described, but I think it would be enough, and perhaps simpler to
implement.

When I have a bunch of files in a folder and need to match them, I
start typing the letters and then press {tab} for completion, and it
shows me the files that have similar “roots”

For example, right now I just did:

% ls 2[tab]

and it expanded to

% ls 2014-03-16.

even though the matching files are named

2014-03-16.Sermon.aiff
2014-03-16.worship.aiff

So here are the steps simpler version of what I’d like to do (which, I
realize, might not be possible to do in a shell script):

1. Find all of the files (not directories) in a given directory (that
directory would probably be “$@“ in most cases)

2. Type the first ~4 letters of each filename

3. Emulate {tab} for completion matching

4. Create a directory based on the output from step #3

5. Move whatever files matched step #3 into the folder created in step 4

Is that more clear and/or possible?

Thanks!

TjL



Messages sorted by: Reverse Date, Date, Thread, Author