Reputation: 4915
I have a group of files in a certain directory. Now I want to search them in two different directories. I used below code:
(jumped to the directory that contains that group of files)
ls | while read name; do find ~/dir1 ~/dir2 -name {$name};done
But I guess it is too slow since for each file, dir1
and dir2
should be searched once. So the search will be do too many times.
Is my guess right? and if so, what should I write?
Upvotes: 1
Views: 216
Reputation: 20980
find
supports -o
for OR operation.
You can use this:
files=(); # Initialize an empty bash array
for i in *; do files+=(-o -name "$i"); done # Add names of all the files to the array
find dir1/ dir2/ -type f '(' "${files[@]:1}" ')' # Search for those files
e.g., consider this case:
$ touch a b c
$ ls
a b c
$ files=()
$ for i in *; do files+=(-o -name "$i"); done
$ printf '%s ' "${files[@]}"; echo
-o -name a -o -name b -o -name c
$ printf '%s ' "${files[@]:1}"; echo
-name a -o -name b -o -name c
$ printf '%s ' find dir1/ dir2/ -type f '(' "${files[@]:1}" ')'; echo
find dir1/ dir2/ -type f ( -name a -o -name b -o -name c ) # This is the command that actually runs.
Upvotes: 2