[英]How to move files containing, inside their title, strings from a txt file listing one string per line
I need to move any file containing one of several strings in their titles from one directory to another.我需要将标题中包含多个字符串之一的任何文件从一个目录移动到另一个目录。 The strings are in one file named brh.txt and are on top of each others, one per line.
这些字符串位于一个名为 brh.txt 的文件中,并且彼此重叠,每行一个。 Here is what I have tried:
这是我尝试过的:
for i in $(cat brh.txt); do find./hehert -type f -iname '*$i*' -exec mv -t /home/bort/misc/ {} \+; done
This runs for a while, but by the end of it, no new file appears in the target directory despite having loads of files that should match.这会运行一段时间,但到最后,尽管有很多文件应该匹配,但目标目录中不会出现新文件。 Can anyone enlighten me?
任何人都可以启发我吗?
Don't use single quotes '*$i*'
for variable expansion, use double quotes "*$i*"
.不要使用单引号
'*$i*'
进行变量扩展,使用双引号"*$i*"
。 Also, it's better not to $(cat brh.txt)
because if any filenames contained spaces they would be split.此外,最好不要使用
$(cat brh.txt)
,因为如果任何文件名包含空格,它们将被拆分。 Instead, you can use mapfile
相反,您可以使用
mapfile
mapfile -t strings < brh.txt
And then use the strings array in a for loop然后在 for 循环中使用字符串数组
for i in "${strings[@]}"; do find ./hehert -type f -iname "*$i*" -exec mv -t /home/bort/misc/ {} \+; done
Or alternatively, just iterate through the lines in the file或者,只需遍历文件中的行
while IFS= read -r string; do
find ./hehert -type f -iname "*$string*" -exec mv -t /home/bort/misc/ {} \+
done < brh.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.