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,




Is there a method to do this in linux?


You can use a for loop:


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

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.