0

I want to copy multiple files with the same file name from different locations into the same location (by changing their names according to their original location).

For example, I want to copy,

/apple/test.txt
/banana/test.txt
/grape/test.txt

into

/fruits/apple_test.txt
/fruits/banana_test.txt
/fruits/grape_test.txt

Is there a method to do this in linux?

2

You can use a for loop:

#!/bin/bash

for fruit in apple banana grape; do
    cp "/${fruit}/test.txt" "/fruits/${fruit}_test.txt"
done

The loop executes three times; each time the variable fruit takes on one of the three values apple, banana, and grape. For each fruit, it copies the original file to the destination file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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