LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 08-11-2005, 01:00 AM   #1
snip128
Member
 
Registered: Jan 2004
Location: Delaware
Distribution: gentoo
Posts: 96

Rep: Reputation: 15
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...
 
Old 08-11-2005, 01:33 AM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
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
 
Old 08-11-2005, 10:15 AM   #3
snip128
Member
 
Registered: Jan 2004
Location: Delaware
Distribution: gentoo
Posts: 96

Original Poster
Rep: Reputation: 15
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 /
 
Old 08-12-2005, 03:48 AM   #4
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
OK, try this:

Code:
for dir in `find /mnt/hdb2/ -name '*' -type d`; do cp FILE1 FILE2 FILE3 $dir; done
 
Old 08-12-2005, 10:12 AM   #5
snip128
Member
 
Registered: Jan 2004
Location: Delaware
Distribution: gentoo
Posts: 96

Original Poster
Rep: Reputation: 15
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 =]
 
Old 08-12-2005, 11:51 AM   #6
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 6,007

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
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
 
Old 08-12-2005, 07:20 PM   #7
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
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).
 
Old 08-12-2005, 07:32 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
What's wrong with a plain

for i in file1 file2 file3; do find /mnt/hda2 -type d -exec cp $i "{}"\; ; done
 
Old 08-12-2005, 08:44 PM   #9
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
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
 
Old 08-12-2005, 09:18 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you copy hidden files from one directory to another? Akhran Linux - Newbie 19 12-29-2011 03:31 AM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 11:51 AM
Copying all files from subdirectories into one directory Hegemon Linux - General 3 01-17-2005 11:25 AM
How to know the property a directory include the files and subdirectories? Xiangbuilder Linux - Newbie 3 08-31-2003 05:48 AM


All times are GMT -5. The time now is 12:00 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration