Copy single file to multiple directories | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Copy single file to multiple directories

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-10-2007
kthatch kthatch is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 12 March 2011, 11:49 PM EST
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Copy single file to multiple directories

Please help - I need to copy a single file to multiple directories.

Dir structure:

Parent_Directoy
Filename1
Child_Directory1
Child_Directory2
Child_Directory3
Child_Directory4
....

So I need to copy Filename1 to all of the Child_Directories.

Currently I do it one by one via
cp Filename1 Child_Directory1/
cp Filename1 Child_Directory2/
....
Sponsored Links
    #2  
Old 11-10-2007
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 22 November 2014, 10:34 AM EST
Location: /dev/ph
Posts: 4,834
Thanks: 64
Thanked 433 Times in 401 Posts
Create a file (dirfile) containing a list of the directories you want to copy to, one per line
and then use a simple shell script to batch copy the file i.e.

#! /bin/sh
while read DIR
do
cp file $DIR
done < dirlist
Sponsored Links
    #3  
Old 11-11-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
 
Join Date: Nov 2007
Last Activity: 27 October 2014, 10:04 PM EDT
Location: New Zealand
Posts: 1,044
Thanks: 21
Thanked 26 Times in 25 Posts
Quote:
Originally Posted by kthatch View Post
Parent_Directoy
Filename1
Child_Directory1
Child_Directory2
Child_Directory3
Child_Directory4
....

So I need to copy Filename1 to all of the Child_Directories.
If you want to copy to all subdirectories (ie Child_Directory1,2,3,4 etc are the only subdirs), you can do the following:

Code:
find <parent directory> -type d -exec cp <parent directory>/<Filename1> {}/ \;

If you have other junk in the parent dir but a pattern that matches all subdirs (eg Child_Directory*), you can do this (bourne/ksh/bash):

Code:
for subdir in <parent directory>/<pattern> ; do cp <parent directory>/<Filename1> ${subdir}/ ; done

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Copy files/directories excluding multiple paterns ajayyadavmca Shell Programming and Scripting 0 04-25-2012 02:23 AM
Copy files from multiple directories into one directory without overwriting them shoaibjameel123 Shell Programming and Scripting 1 08-27-2011 05:14 AM
Copy one folder to multiple directories GamGom Shell Programming and Scripting 5 03-25-2011 11:10 AM
rsync multiple directories into single directory on remote wreckedred Shell Programming and Scripting 1 01-31-2011 05:58 PM
copy multiple files in different directories ken2834 UNIX for Dummies Questions & Answers 3 03-25-2007 02:35 PM



All times are GMT -4. The time now is 01:45 AM.