我有一个文本文件,每行一个文件名:
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 . -type f -print0 | fgrep -zFf ./file_with_filenames.txt | xargs -0 -J % cp % /path/to/destdir
find
:
filecopy.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
find
不会假设缺少路径名意味着当前目录 http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V51B_HTML/MAN/MAN1/0505____.HTM 。但在 GNU find 的情况下,它会自动为您假设,所以这里不需要,只是我喜欢保持的习惯。 - onteria_-J
选项在我的 xargs 版本中不起作用,所以我将其更改为:find . -type f -print0 | fgrep -zFf ./file_with_filenames.txt | xargs -0 -I{} cp "{}" /path/to/destdir/
-J
选项。对于传输到xargs的管道,我使用了以下命令:xargs -0 -I{} cp "{}" /path/to/destdir foo foo
- Mars Redwyne