繁体   English   中英

Linux:递归查找字典中的文件列表

[英]Linux: Find a List of Files in a Dictionary recursively


我有一个文本文件,每行一个文件名:

Interpret 1 - Song 1.mp3
Interpret 2 - Song 2.mp3
...   

(大约 200 个文件名)

现在我想在一个文件夹中递归搜索这个文件名,以获得 Filenames.txt 中每个文件名的完整路径。
这个怎么做? :)

(目的:将文件复制到我的 MP3 播放器,但其中一些已损坏,我想重新复制它们,而不需要花费数小时从我的音乐文件夹中研究它们)

最简单的方法可能如下:

cat orig_filenames.txt | while read file ; do find /dest/directory -name "$file" ; done > output_file_with_paths 

更快的方法是只运行一次 find 命令并使用 fgrep。

find . -type f -print0 | fgrep -zFf ./file_with_filenames.txt | xargs -0 -J % cp % /path/to/destdir

您可以将 while 读取循环与find一起使用:

文件复制.sh

#!/bin/bash

while read line
do
        find . -iname "$line" -exec cp '{}' /where/to/put/your/files \;
done < list_of_files.txt

其中list_of_files.txt是逐行的文件列表, /where/to/put/your/files是您要复制到的位置。 您可以在目录中像这样运行它:

$ bash filecopy.sh

+1 @jm666 答案,但-J选项不适用于我的 xargs 风格,所以我将其更改为:

find . -type f -print0 | fgrep -zFf ./file_with_filenames.txt | xargs -0 -I{} cp "{}" /path/to/destdir/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM