Find the answer to your Linux question:
Results 1 to 8 of 8
Hello all, I am interested in a more flexible targeted search in my directories. For example, if I am searching for all .txt .dat and .bat files (then perform an ...
  1. #1
    Just Joined!
    Join Date
    Jun 2011
    Posts
    4

    using brace expansion with find

    Hello all,

    I am interested in a more flexible targeted search in my directories. For example, if I am searching for all .txt .dat and .bat files (then perform an operation), I would think that the following would work:
    find . -name '*.{txt,dat,bat}' -exec ...

    But I get no results. I am running on Cygwin.

    I have confirmed that:
    find . -name '*.txt' -exec ...
    find . -name '*.dat' -exec ...
    find . -name '*.bat' -exec ...

    all find the aforementioned files. But,
    find . -name '*.{txt}'
    still produces no output.

    Thanks for any assistance

  2. #2
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    how bout:
    Code:
    find . -name '*.txt' -o -name '*.dat' -o -name '*.bat' -exec ...

  3. #3
    Just Joined!
    Join Date
    Jun 2011
    Posts
    4
    ateryu,

    Thanks for the suggestion. This will work.

    Though, I am still curious as to how to incorporate the brace expansion into the find statement. This will significantly simplify things as lists of file types get larger.

    Adam

  4. #4
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    okay, how bout this? it which just 'ls' every file that it finds:
    Code:
    find . -name '*.txt' -o -name '*.dat' -o -name '*.bat' -exec ls -l {} \;
    the curly braces are just a variable substitution for each file found, and the backslash semicolon is required to tell find you are done with the variable.

  5. #5
    Just Joined!
    Join Date
    Jun 2011
    Posts
    4
    This is basically what I have been doing:
    find . -name '*.txt' -exec grep -l 'Find this' {} \;

    I would like to know if there is a more elegant way to use brace expansion instead of typing out each :
    -o -name '*.txt'

    BTW how do you get your example in 'code' format? I am new to this message board.

    Thanks.

  6. #6
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    sorry, but i'm not sure what you mean by more elegant. you could just do this in straight grep though, e.g.:
    Code:
    grep -l 'Find this' *.txt
    you can make your text appear as code by highlighting your text, then clicking the '#' character in the above formatting bar.

  7. #7
    Just Joined!
    Join Date
    Jun 2011
    Posts
    4
    When I am refering to more elegant, I would like to not have to use multiple -o -name in my find command. To be able to use the brace expansion:
    Code:
    *.{dat,txt,bat,cv,irh}
    to search for all such files in the subfolders. It is not the grep part that I am concerned with right now. Just getting the aforementioned expansion to work with 'find'.

    BTW, thanks for the help with the code highlight. I needed to open the 'advanced features' button to access that capability.

    Thanks,

  8. #8
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    I don't know of another way to pass multiple searches to find, sorry.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...