3

How to copy files from dir1 to dir2, matching pattern in a file1? for example:

file1:

T0_1
T0_2
T0_3

Dir1:

T0_1_xxx_xx
E1_1_xxx_xx
T0_2_xxx_xx
E1_2_xxx_xx
E1_3_xxx_xx
T0_2_xxx_xx

I want to copy T0_1_xxx_xx, T0_2_xxx_xx, T0_3_xxx_xx to new dir2. There are 124 names in file1, and 372 files in dir1.

2
  • When you say "I want to copy T0_1_xxx_xx" does that mean that every file that starts with "T0_1_" should be copied? You have to craft the wildcards to match all & only what you want.
    – Jeff Schaller
    Jun 4, 2021 at 14:08
  • What I mean is, a file contains half names of text files in dir. I want to copy these text files from a dir to new_dir.
    – Tomar
    Jun 8, 2021 at 11:04

1 Answer 1

1

Just iterate over the file:

while read prefix; do
    cp -- "$prefix"* new_dir_2/
done < file1

This will read each line of your file, saving it as $prefix and then will copy all files whose name starts with $prefix to new_dir_2. The -- after the cp is to tell cp that there are no more options and this ensures it will also work in the unlikely case where you have a file name starting with a -.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.