1

I'm looking for a search tool (from Ubuntu 14.04 desktop) or method/command (Terminal) that will allow me to search in filenames for full or partial words, in any order, regardless of capitalization.

Do you know of one?

Here's an example of what I need to do. I know that somewhere under /home/myusername I have a file whose name contains the words "personal", "income" , "tax" , "state". But I can't remember if the filename is

  • state personal income tax return.doc or
  • personal Income Tax return - state.doc or
  • income tax return - State - personal.doc.

and I also can't remember if some of the words are capitalized. Also, I have a zillion other files with each of the words in them. So just doing a simple search on one of the words (like only "state", or only "tax") doesn't help me much, because it brings back too many files.

Ideally, I'd like to be able to just type in [income state return] or [state income return] and have it bring back the files with all three words in the file name, regardless of capitalization or the order of the words. A GUI tool would be preferable, but a Terminal method would be OK too.

I've tried Catfish and gnome-search-tool/"Search for Files" -- neither of them seem to work for multiple search terms unless the terms are typed in order of how they occur in the file name, with no other words in between the search terms.

David Foerster
  • 36,264
  • 56
  • 94
  • 147
gj7755
  • 13

5 Answers5

3

Don't use grep! find has a perfectly fine file name matching syntax. To use your example:

find [PATH...] -iname '*personal*' -iname '*income*' -iname '*tax*'

where [PATH...] are zero or more paths under which to search.

If you didn't disable your mlocate file name database, it may be faster to make use of it for a faster search. You can even tell it to look for multiple conjunctive search terms with the -A/--all option:

locate -i -A -b income personal tax
David Foerster
  • 36,264
  • 56
  • 94
  • 147
  • David, OP wants to search based on the contents of the files, not filenames. "tax", "personal", "states" are actually contents of the files, the names are to be found. – heemayl Feb 18 '15 at 00:50
  • @heemayl: Read the question again: “I'm looking for a search tool […] that will allow me to search in filenames” and “I have a file whose name contains the words ‘personal’, ‘income’, ‘tax’, ‘state’” (emphasis by me) – David Foerster Feb 18 '15 at 00:52
  • Ooops..my bad....then my answer is not correct..i don't know how it helped OP to be honest..actually he made his own based on mine..your answer (or Ruedinger's) should be selected as the correct one. – heemayl Feb 18 '15 at 01:00
  • @heemayl OP did ls | grep. :( (It's in the comment.) – muru Feb 18 '15 at 01:00
  • @muru: I have left a comment, hope OP checks again. I don't know what to do, should i delete my answer or modify it? – heemayl Feb 18 '15 at 01:03
  • @heemayl It's a common enough action (ls | grep), so keep the answer, just add a note that find is better at this. – muru Feb 18 '15 at 01:07
  • @muru: right. On a funnier note someone has just downvoted my answer :) – heemayl Feb 18 '15 at 01:09
  • David - thank you, the method you suggested works great. I had tried something earlier, suggested by @heemayl (slightly modified by me), that works using ls | grep -- but I like the find method also. – gj7755 Feb 18 '15 at 01:46
  • "locate doesn't support multiple conjunctive terms" is wrong, you can search mlocate -A -i income tax and it will find filenames, irrespective of case with both the words income and tax; or without that flag will find all filenames with either term. You can pass a regex too, but you have to precede each term with -r, so locate -i -r ".*\stax.*\..*" (untested) or whatever. See eg my updated answer to https://askubuntu.com/a/1078320/29073. – pbhj Aug 02 '20 at 23:40
  • @pbhj: Thanks. I updated my answer. – David Foerster Aug 03 '20 at 02:11
1

You can use grep as many times for as much words you want to look for in the filenames. For example to find out the filenames containing the words "income", "tax", "personal" and "state" you can do the following:

ls -R /home/myusername/ | grep -i "income"  | grep -i "tax" | grep -i "personal" | grep -i "state"

Here the -i switch will ensure case-insensitivity.

NOTE:

find would be better suited for this kind of scenario. Check this answer and this answer for details.

heemayl
  • 91,753
  • Thanks for clueing me into this use of grep. Since I only wanted to search within filenames (not file contents) I modified your suggestion slightly and it works great. Here's what I did:
    1. went to top directory using cd;
    2. Typed this:
    ls -R | grep -i "income" | grep -i "tax" | grep -i "state" | grep -i "personal"
    That worked quickly and gave me the results I was looking for.
    – gj7755 Feb 18 '15 at 00:38
  • @gj7755: Glad it helped. The directory in my answer is just a prototype, you need to use the correct one for your need and you did so. Also pick any of the answer that you think appropriate as the answer to your question so that others can be notified that the issue has been resolved. – heemayl Feb 18 '15 at 00:43
  • @gj7755: Please note than my answer was based on a wrong plot and hence therefore is not correct. Please check the other two answers, those are the best suited for this scenario. – heemayl Feb 18 '15 at 01:02
  • @gj7755: Since you found the final answer yourself, it would be best to write your own answer or accept one that's very similar (possibly with an edit suggestion to highlight the difference). – David Foerster Feb 18 '15 at 01:06
  • @gj7755: I have corrected my answer. – heemayl Feb 18 '15 at 01:16
  • heemayl - thanks for your updated comments about the better suitability of find as suggested by @DavidFoerster. Before reading David's post, I had gotten your original suggestion to work by using ls | grep... but I will proceed with using find. – gj7755 Feb 18 '15 at 01:51
  • @gj7755: nice. honestlly i would have also used find if i read your question more carefully at the first place.. – heemayl Feb 18 '15 at 02:00
0

You can easily do this with a fuzzy search tool called fzf. Install it from https://github.com/junegunn/fzf and you will be wondering how you managed without it.

0

If, while using caja or nautilus, you click the big search icon, type the words you speak of and click the small search icon, it will do an AND filename search of those words. The mistake of xxx-seach-tool is to prevent multiple "Name contains" option lines. Tell them.

Papou
  • 129
0

What I typically do is in my home directory find . | grep 'SEARCHTERM1\|SEARCHTERM2'

Ruediger
  • 2,182