0

I want to find all files (in current and all subdirectory) which end in'~' or 'pyc'. To do so I have tried the following find pattern:

find . -name '*{~,pyc}'
find . -name '{*~,*.pyc}'

but neither list any of the files present in the diectory. Why do these patterns not work? How to do it right?

When using ls instead of find (without the quotation mark) I get the expected result (except that ls only shows the current directory and not all the subdirectories).

Alex
  • 5,700

3 Answers3

6
find . -type f \( -name "*~" -o -name "*pyc" \)

This will return all files (-type f) that end with either ~ or (-o) pyc.

jasonwryan
  • 73,126
4

It would be much easier with regular expression if you have the GNU find,

find . -regextype posix-egrep -regex '.*(~|pyc)$'

P.S I guess you're using shell expansion with find? If so, that's not possible, use find . -name '*.pyc' -o -name '*~' instead.

daisy
  • 54,555
3

If it's compactness you're after, with zsh:

printf '%s\n' **/*(~|pyc)(.D)

(D for dotfiles as well, . for regular files only)

If you wanted to use shell brace expansion, you'd have to do things like:

eval 'find . -type f ! \(' '! -name \*'{~,pyc} '\)'

Which the shell expands to:

eval 'find . -type f ! \(' '! -name \*~' '! -name \*pyc' '\)'

Which is then asking the shell to evaluate this command line:

find . -type f ! \( ! -name \*~ ! -name \*pyc \)