How to copy a single file to multiple directories in Linux or Unix

In Linux or Unix, you can copy a single file to multiple directories using a loop in a shell script. Here’s an example script that uses the cp command to copy the file file.txt to multiple directories dir1, dir2, and dir3:

#!/bin/bash

directories=(dir1 dir2 dir3)
file=file.txt

for dir in "${directories[@]}"; do
cp "$file" "$dir"
done

This script uses an array called directories to store the names of the directories that the file should be copied to. The loop iterates over the elements in the directories array and uses the cp command to copy the file file.txt to each directory.

You can modify this script to include the names of the directories that you want to copy the file to, and to specify the name of the file that you want to copy.

Note: The cp command requires that you have the proper permissions to copy the file and write to the destination directories.

Leave a Comment