bashshellsearchfind

Shell find exact filename (literal)


How to find file by name without expressions/regex, just exact given name/part of name? I use it with variable so there could be special characters which will brake search. I use scratch but dont like it, need clear find/ls

find | grep -F "$name"

Using GNU find

Example that not works

name="Poweramp v3 Skin Liv Dark -  v1.7 [Proper]"
find -name "$name"

Solution

  • find's -name predicate interprets its argument as a glob(-ish) pattern. This is interpreted by find, not by the shell, so quoting it to avoid interpretation by the shell is not necessarily sufficient.

    In your particular case, find will interpret the [Proper] part of your wanted filename as a character class instead of a literal. You can obtain suitable quoting of the filename by passing it through printf %q, and quoting the result to avoid the shell dequoting it:

    find -name "$(printf %q "$name")"