LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-15-2010, 03:02 AM   #1
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
BASH print only first match from 'find' command.


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.

Suggestions?

BZT
 
Old 09-15-2010, 03:32 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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.
 
Old 09-15-2010, 06:43 AM   #3
SilversleevesX
Member
 
Registered: May 2009
Posts: 181

Original Poster
Blog Entries: 9

Rep: Reputation: 15
Quote:
Originally Posted by druuna View Post
Hi,

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.
 
Old 09-15-2010, 07:06 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
The sed bash command, not print backslash in Perl azkarashare Programming 8 07-12-2010 06:58 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
bash command - find |cp crackpipe Linux - Software 4 05-05-2009 02:36 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration