LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   copy a file to all subdirectories in a folder (https://www.linuxquestions.org/questions/linux-general-1/copy-a-file-to-all-subdirectories-in-a-folder-423445/)

raj000 03-10-2006 05:31 AM

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.

jschiwal 03-10-2006 06:19 AM

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

timmeke 03-10-2006 10:19 AM

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

raj000 03-11-2006 12:43 AM

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'

homey 03-11-2006 08:48 AM

How about something like this example ....
Code:

find /home/mudd -type d -exec cp -i /home/mudd/index.htm {} \;

titopoquito 03-11-2006 09:33 AM

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".

raj000 03-24-2006 03:55 AM

Homey's solution worked...thanks a ton


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