[SOLVED] Code to only show files with 'nana' in name and not 'nana jane' or 'nana joan'.
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
Code to only show files with 'nana' in name and not 'nana jane' or 'nana joan'.
Hi.
I want to rename my many photos to easily find all photos of a certain grandparent (not real names used in title). So any photos that only have 'nana', I want to find and rename manually to 'nana jane', etc.
I'd greatly appreciate a command line to achieve this.
The find utility can do boolean logic. See -a, -not, and -o in 'man find'. There is no XOR though. There is an implied AND between each option used when running find, so if you need an OR someday, you'll have to add in parenthesis to achieve the right grouping.
The existing answers do not give you what was actually asked for - they all wrongly exclude combinations like "joanna and nana" and wrongly include matches for results like "banana", and the obvious change would still wrongly exclude "nana joan and nana" from matching, which may or not occur but isn't difficult to cope with.
To search specifically for "nana" as a word and without jane or joan following it, you can use a regex with word boundary markers and negative lookahead: "\bnana\b(?! jane| joan)" or (useful if there might be more names): "\bnana\b(?! (jane|joan)\b)"
You can't use that with find because none of the 13 regex variants find allows have support for negative lookaheads, but you can easily filter with grep's Perl-compatible mode:
If you need to limit to JPEG files, the pattern should be suffixed with ".*\.jpe?g$" - but if these are scanned images they're as likely to be TIFFs or perhaps something else, so I've not bothered specifying the extension. If it's necessary you could use something like ".*\.(jpe?g|tiff?|tga|whatever)$"
Also, you probably want a case-insensitive search, so there's -i on the grep and -iname used for the find.
To search specifically for "nana" as a word and without jane or joan following it, you can use a regex with word boundary markers and negative lookahead: "\bnana\b(?! jane| joan)" or (useful if there might be more names): "\bnana\b(?! (jane|joan)\b)"
Looking at that requirement I see that find is missing any options at all for recognizing word boundaries, not just negative lookaheads, in its regular expression pattern matching.
It is possible to use grep with find, though it might not be so efficient:
Code:
find ~/Pictures -type f -name '*nana*' -exec sh -c 'echo {} | grep -q -P "\bnana\b(?! (jane|joan)\b)"' \; -print
Looking at that requirement I see that find is missing any options at all for recognizing word boundaries, not just negative lookaheads, in its regular expression pattern matching.
It is possible to use grep with find, though it might not be so efficient:
Code:
find ~/Pictures -type f -name '*nana*' -exec sh -c 'echo {} | grep -q -P "\bnana\b(?! (jane|joan)\b)"' \; -print
Looking at that requirement I see that find is missing any options at all for recognizing word boundaries, not just negative lookaheads, in its regular expression pattern matching.
Not sure if the default regextype varies, but -regextype sed will allow \b word boundaries with find.
Some of the others engines don't support \b but do have \< and \> for start and end boundaries respectively.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.