How to use find command with variables

23,877

The first double-quoted one should work:

$ touch asdfghjkl
$ var=fgh
$ find -name "*$var*"
./asdfghjkl

Within single quotes ('*$var*'), the variable is not expanded, and neither is it expanded when the dollar sign is escaped in double quotes ("*\$var*"). If you double-escape the dollar sign ("*\\$var*"), the variable is expanded but find gets a literal backslash, too. (But find seems to take the backslash as an escape again, so it doesn't change the meaning.)

So, confusing though as it is, this also works:

$ set -x
$ find -name "*\\$var*"
+ find -name '*\fgh*'
./asdfghjkl

You can try to run all the others with set -x enabled to see what arguments find actually gets.

As usual, wrap the variable name in braces {}, if it's to be followed by letters, digits or underscores, e.g. "*${prefix}somename*".

Share:
23,877

Related videos on Youtube

yael
Author by

yael

Updated on September 18, 2022

Comments

  • yael
    yael almost 2 years

    I am searching for files by finding a partial file name:

    find /script -name '*file_topicv*'
    /script/VER_file_topicv_32.2.212.1
    

    It works, but not when the partial file name is a variable:

    var=file_topicv
    

    find reported file not found, (in spite of the file existing):

    find /script -name '*$var*'
    

    What is wrong here?


    I also tried these:

    find /script -name "*$var*"
    find /script -name "*\$var*"
    find /script -name "*\\$var*"
    

    but not one of those works.


    Update:

    I think this is the problem:

    var=` find /tmp -name '*.xml' -exec sed -n 's/<Name>\([^<]*\)<\/Name>/\1/p' {} +  |  xargs `
    
    echo $var
    generateFMN
    
    ls  /script/VERSIONS | grep "$var"
    
    NO OUTPUT
    
    var=generateFMN
     ls  /script/VERSIONS | grep "$var"
    VER_generateFMN_32.2.212.1
    

    So why $var from find command cause the problem? (I removed the spaces by xargs.)

    • Jeff Schaller
      Jeff Schaller over 6 years
      The first double-quoted one didn’t work?
    • yael
      yael over 6 years
      no its not works
    • ilkkachu
      ilkkachu over 6 years
    • yael
      yael over 6 years
      ok , but why my options are not working ?
    • Alessio
      Alessio over 6 years
      what version of bash are you running? bash --version | head -1
    • yael
      yael over 6 years
      GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
    • Stéphane Chazelas
      Stéphane Chazelas over 6 years
      instead of echo $var, try printf '%s\n' "$var" | sed -n l so you can really see what it contains.
    • ilkkachu
      ilkkachu over 6 years
      @yael, do something like echo "$var" | od -c, or printf "%q\n" "$var" to see what actually goes to var.
    • ilkkachu
      ilkkachu over 6 years
      @yael, also please don't change your question after presenting, but give the context up front, it makes it much easier to answer the question you need an answer for...
    • yael
      yael over 6 years
      I get this --> printf "%q\n" "$var" then I get --> $'generateFMN\r'
    • yael
      yael over 6 years
      why $'generateFMN\r' ?? instead to get generateFMN
  • Alessio
    Alessio over 6 years
    the first double-quoted one also works for me. btw, worth mentioning that it will sometimes be necessary to use ${var} rather than just $var inside the double-quotes, depending on what comes after the variable....a * is fine, a letter/digit/underscore/etc is not.
  • yael
    yael over 6 years
    see my update , this is the problem , when $var comes from find & this isnt works
  • ilkkachu
    ilkkachu over 6 years
    @cas, they almost certainly have a CR in there, the sed here won't remove it or any other trailing garbage on the line. yael posted another question on it: unix.stackexchange.com/questions/417756/…