Ubuntu – Finding files/folders containing 3 items of text

command linefilenamefind

I'm running Ubuntu 14.04 and currently trying to search for files and folders containing "hello", "165" and "920" (in their names – not within the files) using the command below:

    find . -name "hello 165 920" > /search/results/here/search20170224.txt

When I search a string like this in Nemo ("hello 165 920"), the results given are what I'm aiming for. If the files or folders contain the words "hello 165 920" (in any given order or amongst other text) then a result is shown.

Examples: helloxx165xx920, hello_165-920, hello165920

So what I want to do is definitely possible, but via the find command in CLI, I am only getting results for perfect matches.

I cannot work out how to do this kind of search using the find command and I need to run about 500 of these searches one after the other; so a CLI approach is needed so I don't end up having to setup each search manually and end up crashing Nemo every 5 minutes due the sheer size and depth of the searches.

Any help that anyone can give would be greatly appreciated =]

Best Answer

You need to use wildcards (*foo* instead of foo), multiple -name tests, and combine them with -and:

find . -name "*hello*" -and -name '*165*' -and -name "*920*" > search20170224.txt

If you have many different strings to search for, you can save them in a file where each string is a (space separated) column:

hello 165 920
goodbye 321 123

You can then iterate over the file and build the find query:

while read a b c; do
    find . -name "*$a*" -and -name "*$b*" -and -name "*$c*" > search."$a"_"$b"_"$c".txt
done