LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-10-2006, 05:31 AM   #1
raj000
LQ Newbie
 
Registered: Jan 2006
Posts: 16

Rep: Reputation: 0
copy a file to all subdirectories in a folder


Hi,

Is there a way to recursively copy 1 file in all subdirectories of a parent folder. If the file already exists, it should ask me before overwriting.

I want to copy an "index.htm" file in all subfolders of my "www" directory.

I want to do this because my webhost does not allow me to "disable directory listing" for my web files . So i have to create an index.htm file in every directory.

Thanks in advance.
 
Old 03-10-2006, 06:19 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678
You could use the find command with the '-type d' option.
For example:
for dir in $(find www/ -type d); do; cp -i www/index.htm "$dir"/; done
 
Old 03-10-2006, 10:19 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
You may want to add the "-maxdepth 1" option to the find command if you want to limit the copies to
the subdirectories of the "www" folder, rather than all the subdirectories.

ie: if you have only:
www/folder1
www/folder2
...
then jschiwal's command is good enough.

If you also have:
www/folder1/level2/
www/folder2/level2/level3/
then you need to decide if you want to copy your index.html to the directories called folder1, folder2, etc only, or to all the level2s and level3s as well.
If you want to copy to all directories on all levels, use jschiwal's command.

Find's "-maxdepth level" limits the copying to a certain level, ie -maxdepth 1 for only copying to folder1, etc
-maxdepth 2 to copy also to the level2 subdirectories and so on.

The maxdepth option needs to go just before or after the "-type d" option of find, like this:
Code:
for dir in $(find www/ -type d -maxdepth 1); do; cp -i www/index.htm "$dir"/; done

Last edited by timmeke; 03-10-2006 at 10:20 AM.
 
Old 03-11-2006, 12:43 AM   #4
raj000
LQ Newbie
 
Registered: Jan 2006
Posts: 16

Original Poster
Rep: Reputation: 0
syntax error

Hi Guys,

Thanks for your replies .... did as you said but got the following error :

[root@development latestcode]# (find /home/latestcode/ -type d); do; cp -i /home/latestcode/index.html "$dir"/; done
-bash: syntax error near unexpected token `do'
 
Old 03-11-2006, 08:48 AM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
How about something like this example ....
Code:
find /home/mudd -type d -exec cp -i /home/mudd/index.htm {} \;
 
Old 03-11-2006, 09:33 AM   #6
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,616

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by raj000
[root@development latestcode]# (find /home/latestcode/ -type d); do; cp -i /home/latestcode/index.html "$dir"/; done
-bash: syntax error near unexpected token `do'
Just remove the semicolon right after "do".
 
Old 03-24-2006, 03:55 AM   #7
raj000
LQ Newbie
 
Registered: Jan 2006
Posts: 16

Original Poster
Rep: Reputation: 0
Homey's solution worked...thanks a ton
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy 3 files to a directory and all subdirectories? snip128 Linux - Newbie 9 08-12-2005 09:18 PM
starting script, init by copy/create file in (samba) folder? muab Linux - General 7 06-22-2005 06:02 PM
Cannot "Copy To" .jpg file to wallpaper folder LeanPudLou SUSE / openSUSE 5 11-08-2004 08:24 AM
can copy-paste or create new file/folder, if boot from live cd gjo Linux - Distributions 3 09-17-2004 08:43 AM
copy directories and subdirectories Red Squirrel Linux - Newbie 2 03-06-2004 09:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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