簡體   English   中英

將循環變量傳遞給 BASH find -regex 參數?

[英]Passing loop variable to BASH find -regex argument?

我正在嘗試通過find復制由 REGEX 表達式選擇的圖像。 下面的命令運行但不復制文件。 如何將循環變量(例如讀取文件的行)傳遞給 find -regex 參數?

cat hit_images_regex_list.txt | while read line; 
do 
find . -regex "${line}" -exec cp --parents {} /destination_dir \;
done

我從中提取 REGEX 表達式的hit_images_regex_list.txt文件如下所示:

".*B - 12.*tif"
".*D - 09.*tif"
".*G - 03.*tif"
".*G - 12.*tif"
...

對這些 REGEX 表達式中的每一個使用find都有效,但從我的 .txt 文件中提取 REGEX 表達式的循環沒有做任何事情。

for i in $(cat hit_images_regex_list.txt); do find -name "$i" | cat -n | while read n f; do cp "$f" /newfolder/path/"$n".tig; done; done

這應該打開文件,並為每個正則表達式(行)找到所有匹配的文件並復制到新文件夾

這有幫助嗎

嘗試這個。

while IFS= read -r line; do
  find . -regex "$line" -exec cp --parents {} /destination_dir \;
done < hit_images_regex_list.txt

人們花時間嘗試並提供幫助。 請我們禮貌地評價回復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM