Linux: Find a List of Files in a Dictionary recursively

耗尽温柔 提交于 2019-12-21 05:47:15

问题



I have a Textfile with one Filename per row:

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

(About 200 Filenames)

Now I want to search a Folder recursivly for this Filenames to get the full path for each Filename in Filenames.txt.
How to do this? :)

(Purpose: Copied files to my MP3-Player but some of them are broken and i want to recopy them all without spending hours of researching them out of my music folder)


回答1:


The easiest way may be the following:

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



回答2:


Much faster way is run the find command only once and use fgrep.

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



回答3:


You can use a while read loop along with find:

filecopy.sh

#!/bin/bash

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

Where list_of_files.txt is the list of files line by line, and /where/to/put/your/files is the location you want to copy to. You can just run it like so in the directory:

$ bash filecopy.sh



回答4:


+1 for @jm666 answer, but the -J option doesn't work for my flavor of xargs, so i chaned it to:

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


来源:https://stackoverflow.com/questions/6179726/linux-find-a-list-of-files-in-a-dictionary-recursively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!