Reputation: 139
Until now when I want to gather files from a list I have been using a list that contains full paths and using:
cat pathlist.txt | xargs -I % cp % folder
However, I would like be able to recursively search through a folder and it's subfolders and copy all files that are in a plain text list of just filenames (not full paths).
How would I go about doing this?
Thanks!
Upvotes: 0
Views: 1703
Reputation: 3363
Try something like
find folder_to_search -type f | grep -f pattern_file | xargs -I % cp % folder
Upvotes: 0
Reputation: 189417
Assuming your list of file names contains bare file names, as would be suitable for passing as an argument to find -name
, you can do just that.
sed 's/^/-name /;1!s/^/-o /' pathlist.txt |
xargs -I % find folders to search -type f \( % \) -exec cp -t folder \+
If your cp
doesn't support the -t
option for specifying the destination folder before the sources, or your find
doesn't support -exec ... \+
you will need to adapt this.
Just to explain what's going on here, the input
test.txt
radish.avi
:
is being interpolated into
find folders to search -type f \( -name test.txt -o -name radish.avi \
-o name : \) -exec cp -t folder \+
Upvotes: 1
Reputation: 21965
Use the find command.
while read line
do
find /path/to/search/for -type f -name "$line" -exec cp -R {} /path/to/copy/to \;
done <plain_text_file_containing_file_names
Assumption:
The files in the list have standard names without, say newlines or special characters in them.
Note:
If the files in the list have non-standard filenames, tt will be different ballgame. For more information see find manpage and look for -print0
. In short you should be operating with null terminated strings then.
Upvotes: 0