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. |
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 |
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 |
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' |
How about something like this example ....
Code:
find /home/mudd -type d -exec cp -i /home/mudd/index.htm {} \; |
Quote:
|
Homey's solution worked...thanks a ton
|
All times are GMT -5. The time now is 08:19 AM. |