Linux: Find a List of Files in a Dictionary recurs

2019-03-31 06:17发布


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)

4条回答
Lonely孤独者°
2楼-- · 2019-03-31 06:33

+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/
查看更多
霸刀☆藐视天下
3楼-- · 2019-03-31 06:38

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
查看更多
SAY GOODBYE
4楼-- · 2019-03-31 06:50

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
查看更多
Fickle 薄情
5楼-- · 2019-03-31 06:56

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 
查看更多
登录 后发表回答