ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I find it hard to believe this question has never been asked before on this forum:
How do you get find to print (return, echo, export, vomit, expel, proclaim; your choice of verb over mine) ONLY the first match it finds in a directory or directory hierarchy?
I'm trying to re-assemble a list of files that reside in several subdirectories of one main directory. The list I am using was generated using a script that matched a four-number substring to a label in another list; the former file had 5750 lines and the latter nearly 5300, so you can understand why I don't simply run the script, slightly tweaked, on the first list and just use a 'cut column a from b' kind of script on that one. The second list took (at minimum) 2 1/2 hours to generate in GNOME Terminal.
So approaching it from the other end -- the two lists -- I've discovered that I have a main list with filenames that match files in more than one subdirectory. This is the reason I want to use find, or a builtin that's no doubt less elegant but more useful, to match and return only the first instance. I believe the original "master" list was "run once through the sort wringer" with a -u option so only has the first instances of every filename.
Quote:
Originally Posted by Codes and Examples
'find' comes upon a line like this:
gae39-3945-306-007.jpg
and gives this as a return:
./girlswithgirls/gae39-3945-306-007.jpg ./glasses/gae39-3945-306-007.jpg
The list generated by the script I'm using now to reconstruct full-path matches had this happen nine times (not a visual count but a grep, wc -l return) before I interrupted the script with ctrl-c. At that point it had written 325 lines.
I'm not sure I understand what it is you want to accomplish, but....
If you are sure that both ./girlswithgirls/gae39-3945-306-007.jpg and ./glasses/gae39-3945-306-007.jpg are actually the same, why not use:
find . -name "whatever" | head -1
or find . -name "whatever" -print -quit
Hope this helps.
Pretty much. But as I think I mentioned, if the "master" list was run through a "sort -unique -o" kind of thing, the first file find found (try saying that with bubblegum in your mouth *G*) was more likely the one on the list and was therefore the one matched by the script that did the "labels." Not that I wouldn't want a script that labeled both, but with different paths indicated on that one line, I'd anticipate sorting them would be hell and leaving them alone would have just slowed down the script.
I didn't think of head -- thanks for the suggestion.
BZT
Last edited by SilversleevesX; 09-15-2010 at 06:45 AM.
You did mention sort in your first post, but I'm not sure how you want to accomplish that.
The find command returns hits with the (relative) path prepending the file name, which would make every entry unique even though the file name is not: ./girlswithgirls/gae39-3945-306-007.jpg and ./glasses/gae39-3945-306-007.jpg are unique, although gae39-3945-306-007.jpg is not.
You could write a script that separates the 2 and put all 3 (full path + file name, path and file name) into an array. The array can be sorted on any entry.
Another thing: Both commands given by me in post #2 do the same thing. I would go for the all find command (1 command is always better then 2 separated by a pipe).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.