LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Copying a single file to multiple directories (https://www.linuxquestions.org/questions/linux-software-2/copying-a-single-file-to-multiple-directories-507346/)

quest49 12-04-2006 07:08 AM

Copying a single file to multiple directories
 
Hello folks,
I wanted to know if any of the users/mods know how to copy a file or folder to multiple destinations. I have a file and it needs to be copied to multiple subdirectories and its not easy to do it manually. anyway i can do it using cp or something similar.
i was thinking my last option would be writing a bash code or something. if that the case plz gimme a few pointers how to write this specific code

acid_kewpie 12-04-2006 07:11 AM

Code:

for i in /dir1 /dir2 /dir3 /dir4; do cp file $i; done

quest49 12-04-2006 07:15 AM

cool. this might work. but is there a way to do it if i don't have the directory names? and thanks!

acid_kewpie 12-04-2006 07:24 AM

well yes i'm sure there is, but you'll need some information from somewhere... bash isn't psychic!

homey 12-04-2006 07:39 AM

If you want to copy the file to subdirectories in /home , something like this may do...
Code:

find /home -type d -exec cp file.txt {} \;

quest49 12-04-2006 07:48 AM

thanks.. i think its best if i just describe the problem. i have a picture in pics directory. there are sub directories in the pics directory. i need the picture to be copied to all my sub directories. thanks homey and acid_kewpie.

quest49 12-04-2006 08:07 AM

thanks anyways..i got the solution.
here it is for the record

#!/bin/bash
#rec_copy.sh
#Usage:
# rec_copy.sh input_file target_dir
# copies 'input_file' recursively to all directories starting at 'target_dir'

if [ -z $2 ]; then
echo "usage: $0 input_file target_dir"
exit
fi

for i in $(ls -R $2| grep :$ | awk -F":" '//{print $1}' )
#for i in $(ls -R $2 | awk -F":" '/:$/{print $1}' )
do
cp $1 $i
done


All times are GMT -5. The time now is 02:22 PM.