LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-04-2006, 07:08 AM   #1
quest49
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Rep: Reputation: 0
Arrow 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
 
Old 12-04-2006, 07:11 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Code:
for i in /dir1 /dir2 /dir3 /dir4; do cp file $i; done
 
Old 12-04-2006, 07:15 AM   #3
quest49
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Original Poster
Rep: Reputation: 0
cool. this might work. but is there a way to do it if i don't have the directory names? and thanks!
 
Old 12-04-2006, 07:24 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well yes i'm sure there is, but you'll need some information from somewhere... bash isn't psychic!
 
Old 12-04-2006, 07:39 AM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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 {} \;
 
Old 12-04-2006, 07:48 AM   #6
quest49
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
Old 12-04-2006, 08:07 AM   #7
quest49
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Multiple Files In A Single Gz File onacorpuscle Linux - Newbie 3 08-31-2006 03:18 AM
copying multiple different files in different directories with identical file names nickleus Linux - Software 4 03-09-2006 01:25 PM
Moving or copying a directory into multiple directories oadvantage Linux - Newbie 3 03-07-2006 03:47 AM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 12:23 PM
Copying a single file to multiple directories tgolly Linux - Newbie 3 04-26-2004 03:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration