开发者

Can find take a file as argument from stdin?

开发者 https://www.devze.com 2023-04-12 12:58 出处:网络
I have a list of 3,900 ID numbers and I need to find on our FTP server the matching files. Finding one file is quite simple e.g.

I have a list of 3,900 ID numbers and I need to find on our FTP server the matching files.

Finding one file is quite simple e.g.

find . -name "*IDNumber*" -exec ls '{}' ';' -print

but how do I do this for 3,900 IDs numbers? I created a file with t开发者_开发问答he IDs like so

028892663163
028923481973
...

but how do I pass the list of ID numbers as argument? Can you provide some pointers?

Thanks!


I would try to reduce the number of times you have to invoke find:

find . -type f -print | grep -f id.file | xargs cp -t target_dir


You may try to optimize it by running find with more than one id at a time.

With bash (100 at a time, you may try with more):

c= p=
while IFS= read -r; do
  p+=" -name '*$REPLY*' -o " 
  (( ++c )) 
  (( c % 100 )) || {
    eval find .  ${p% -o } 
    p=
    }
done < id_list_all

[[ $p ]] && 
    eval find .  ${p% -o }


Figured it out.

  • put all my 3,900 ID numbers in a file outfile
  • typed the command line:

cat outfile | while read line do find . -name "$line" -exec cp '{}' /target_directory ';' -print done

Worked awesome!


I read your question wrong the first time... arguments from find to other things. What you want is arguments from a file passed to find. So, here's the correct answer with xargs:

xargs --max-args=1 -I X -d '\n' find . -name X -exec [...] < your_list
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号