0

I have many files in my directory. I'd like to listing, copying or moving file containing 'abc' AND 'xyz' in their name. How do I do this pattern matching with AND?

The normal command: ls *abc* *xyz* only work with OR.

αғsнιη
  • 41,407

1 Answers1

1

Use this way.

ls *abc*xyz* *xyz*abc*

Using in mv or cp, you just need specify the target directory with -t option since you are looking the files with wildcards and can be more than a file to copy/move:

cp -t /path/to/dest *abc*xyz* *xyz*abc*

Or use find like:

find \( -name '*abc*' -a -name '*xyz*' \)

which is same as find -name '*abc*' -name '*xyz*' as documented in man find:

expr1 expr2
Two expressions in a row are taken to be joined with an implied "and"; expr2 is not evaluated if expr1 is false.

expr1 -a expr2
Same as expr1 expr2.

expr1 -and expr2
Same as expr1 expr2, but not POSIX compliant.

You can add -exec ... to the command above to do whatever you want to do on the files found.

find \( -name '*abc*' -a -name '*xyz*' \) -exec do-stuffs {} +
αғsнιη
  • 41,407