简体   繁体   English

如何从每行列出一个字符串的 txt 文件中移动标题内包含字符串的文件

[英]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.

相关问题 如何使用sed命令从文件中删除包含特定字符串的行 - How to remove a line containing specific string from file with sed command 在子文件夹中创建txt文件列表文件 - Create txt file listing files in subfolders 匹配两个文件中的字符串,并在第一个文件中向第二个文件的行尾添加匹配字符串 - Match strings from two files and append line with matching string from first file to end of line of second file 在Shell中每行txt.txt文件创建新的文本文件 - Create new text files per line of a txt.txt file in shell 从文件中提取包含大量字符串之一的行 - Extract lines containing one of large number of strings from file 如何将此目录中所有文件的长列表创建到名为 etcFiles.txt 的文件中(在 Linux 中) - How to Create a long listing of all the files in this directory to a file called etcFiles.txt (In Linux) 将字符串的最后一个单词从一个文件移动到另一个文件 - Move last words of strings from one file to another 在Linux中,如何在一行中编辑文件并将文件从一个目录移动到另一目录? - How to edit and move a file from one directory to another in one line in Linux? 如何使用while读取行从一个列表文件中移动另一台服务器中的文件? - How to move file in another server from one list file using while read line? 如何在Linux中通过传递变量从包含字符串的行之前的文件中删除所有数据 - How to remove all data from a file before a line containing string by passing variable in linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM