LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   copy 3 files to a directory and all subdirectories? (https://www.linuxquestions.org/questions/linux-newbie-8/copy-3-files-to-a-directory-and-all-subdirectories-352079/)

snip128 08-11-2005 01:00 AM

copy 3 files to a directory and all subdirectories?
 
is it possible? i have 3 files that i need to copy to /mnt/hdb2 and all its subdirectories, and theres a lot of them....itd be nice if theres some sort of command that could do this for me...

i already tried cp -R file1 file2 file3 /mnt/hdb2/* and that failed misserbly...

thx for the help,
chris

i dont think itd matter much, but im running debian linux on a 2.4 kernel...

scuzzman 08-11-2005 01:33 AM

Try this:
Code:

for dir in `ls -d /mnt/hdb2`; do cp FILE $dir; done
Replace FILE with the filename(s) - you can still use more than one, just do it like this:
Code:

for dir in `ls -d /mnt/hdb2`; do cp FILE1 FILE2 FILE3 $dir; done

snip128 08-11-2005 10:15 AM

it didnt work...seems to be a problem with the ls command...ls -d gives me this

Code:

ghettobox:/var/www/test# ls -d /mnt/hdb2/
/mnt/hdb2/

and yes, there are several directories listed when i do ls /mnt/hdb2

ls -d actually doesnt even list the directories of /

scuzzman 08-12-2005 03:48 AM

OK, try this:

Code:

for dir in `find /mnt/hdb2/ -name '*' -type d`; do cp FILE1 FILE2 FILE3 $dir; done

snip128 08-12-2005 10:12 AM

ALMOST! some of the folders have spaces in the name...screws it up petty bad lol

it does copy it to the folders WITHOUT spaces though, so good job on that =]

tredegar 08-12-2005 11:51 AM

Well, I do not pretend to be clever with the command line, but I do know that if you have a filename (or directory name) with spaces, you can either "escape" the spaces, or, easier for you, put the "spaced name in quotes". So now you should be able to improve on scuzzman's one-liner.
I never use spaces in filenames - they are a pain.
HTH

scuzzman 08-12-2005 07:20 PM

OK - try this:
Code:

for dir in `find /mnt/hdb2/ -name '*' -type d | sed 's/ /\\ /'`; do cp FILE1 FILE2 FILE3 $dir; done
What this does is changes ' ' (a space) to '\ ' (backslash space).

Tinkster 08-12-2005 07:32 PM

What's wrong with a plain

for i in file1 file2 file3; do find /mnt/hda2 -type d -exec cp $i "{}"\; ; done

scuzzman 08-12-2005 08:44 PM

Quote:

Originally posted by Tinkster
What's wrong with a plain

for i in file1 file2 file3; do find /mnt/hda2 -type d -exec cp $i "{}"\; ; done

I'm not very good with find strings, so I did it the way I know how :)

Tinkster 08-12-2005 09:18 PM

I know what was wrong with my first solution:

It ran find three times! :}

find /mnt/hda2 -type d -exec cp file1 file2 file3 "{}"\; ; done


Cheers,
Tink


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