LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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
  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
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
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: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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



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
How do you copy hidden files from one directory to another? Akhran Linux - Newbie 22 12-13-2019 05:20 PM
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:10 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