Skip to content
Advertisement

Linux – Searching multiple filenames from a textfile and copy to location

I have a text file containing the multiple names of files. I want to search these from a specific folder and copy it to another. Which Linux commands can I use to easily do this? Does grep, find commands help with this?

Advertisement

Answer

You mean something like this?

while read -r file
do
    find /path/to/search -name "$file" -exec cp {} /new/folder/for/files ;
done < your_text_file_with_file_names

To answer your comment:

while read -r file <- here each line of your_text_file_with_file_names is assigned to the variable file.
$file outputs the value of the variable file. If you want to try it in the shell you’re using:

> some_variable=whatever
> echo $some_variable
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement