Web Server forum
Back To The Forum Home!Search!Private Messaging System

This is Interesting: Free IT Magazines Now Free shipping to   
Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > cp: copying a file to one directory and all subdirectories




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    cp: copying a file to one directory and all subdirectories  
John Bullock


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-21-04 03:32 AM


Hello,

I have a basic problem: I want to use a single command to copy
one file to a directory and all of its subdirectories.  How can I do
this without typing out the name of each individual subdirectory?

For example: I have foo.bar and want to copy it to /etc -- but
also to /etc/etc1, /etc/etc2, /etc/etc3, and so on.  I looked to the man
page for cp to find an easy way to do this, but to no avail.

Many thanks,
--John








[ Post a follow-up to this message ]



    Re: cp: copying a file to one directory and all subdirectories  
Alex Schuster


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-21-04 03:32 AM

john.bullock@stanford.edu (John Bullock) writes:

>         I have a basic problem: I want to use a single command to copy
> one file to a directory and all of its subdirectories.  How can I do
> this without typing out the name of each individual subdirectory?

Use find to find all files in /etc, -type d to find directories only,
and -exec to copy your file there:
find /etc -type d -exec cp foo.bar \{\} \;

Alex
--
Alex Schuster     Wonko@wonkology.org           PGP Key available
alex@pet.mpin-koeln.mpg.de






[ Post a follow-up to this message ]



    Re: cp: copying a file to one directory and all subdirectories  
Alan Connor


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-21-04 03:32 AM

On Sat, 19 Jun 2004 13:50:24 -0700, John Bullock <john.bullock@stanford.edu> wrote:
>
>
>
> Hello,
>
>         I have a basic problem: I want to use a single command to copy
> one file to a directory and all of its subdirectories.  How can I do
> this without typing out the name of each individual subdirectory?
>
>         For example: I have foo.bar and want to copy it to /etc -- but
> also to /etc/etc1, /etc/etc2, /etc/etc3, and so on.  I looked to the man
> page for cp to find an easy way to do this, but to no avail.
>                ^^^^
:-)


> Many thanks,
> --John
>
>
>


find /etc -type d -exec cp foo.bar {} \;

AC






[ Post a follow-up to this message ]



    Re: cp: copying a file to one directory and all subdirectories  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:12 PM

2004-06-19, 13:50(-07), John Bullock:
[...]
>         I have a basic problem: I want to use a single command to copy
> one file to a directory and all of its subdirectories.  How can I do
> this without typing out the name of each individual subdirectory?
>
>         For example: I have foo.bar and want to copy it to /etc -- but
> also to /etc/etc1, /etc/etc2, /etc/etc3, and so on.  I looked to the man
> page for cp to find an easy way to do this, but to no avail.
[...]

With one single command, I can only think of zsh:

zsh << EOS
< foo.bar tee /etc/**/*(/e:'REPLY=$REPLY/foo.bar' > /dev/null
EOS

But, you may run into "too many open file" problems, You'd
better do:

zsh << EOS
for d (/etc/**/*(/N)) cp foo.bar $d
EOS

--
Stephane





[ Post a follow-up to this message ]



    Re: cp: copying a file to one directory and all subdirectories  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:12 PM

On 2004-06-19, John Bullock wrote:
>
> Hello,
>
>         I have a basic problem: I want to use a single command to copy
> one file to a directory and all of its subdirectories.  How can I do
> this without typing out the name of each individual subdirectory?
>
>         For example: I have foo.bar and want to copy it to /etc -- but
> also to /etc/etc1, /etc/etc2, /etc/etc3, and so on.  I looked to the man
> page for cp to find an easy way to do this, but to no avail.

Build a new command (bash2 or ksh93):

#!/bin/bash
# NAME: mcp
IFS=$'\n'
file=$1
dir=$2
dirlist=( `printf "%s/${file##*/}\n" "$dir"/*/` )
cat $file | tee "${dirlist[@]}" > "$dir/${file##*/}"
###########

Call it with:

mcp foo.bar /etc


--
Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
 ========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License





[ Post a follow-up to this message ]



    Re: cp: copying a file to one directory and all subdirectories  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:12 PM

2004-06-21, 07:32(+00), Chris F.A. Johnson:
[...]
> #!/bin/bash
> # NAME: mcp
> IFS=$'\n'
> file=$1
> dir=$2
> dirlist=( `printf "%s/${file##*/}\n" "$dir"/*/` )
> cat $file | tee "${dirlist[@]}" > "$dir/${file##*/}"
> ###########
[...]

That fails for path names containing newlines, for filenames
with wildcards in them, and if $file contains '%' characters or
starts with "-" and also if $dir is something like
[n]=whatever/... or starts with "-".

It doesn't recurse into subdirectories.

For a bash/POSIX solution, you need something like:

#! /bin/sh -
file=$1 dir=$2
case $dir in
/*) ;;
*) dir=./$dir;; # avoid pb with filenames
# starting with "-", "(", "!"...
esac
exec find "$dir" -type d -exec cp -- "$file" '{}' ';'


# (untested)

--
Stephane







[ Post a follow-up to this message ]



    Re: cp: copying a file to one directory and all subdirectories  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:12 PM

On 2004-06-21, Stephane CHAZELAS wrote:
> 2004-06-21, 07:32(+00), Chris F.A. Johnson:
> [...] 
> [...]
>
> That fails for path names containing newlines, for filenames
> with wildcards in them, and if $file contains '%' characters or
> starts with "-" and also if $dir is something like
> [n]=whatever/... or starts with "-".

In other words, if $file is quoted, it will work 99% (or more) of
the time.

> It doesn't recurse into subdirectories.

It's not clear whether that was wanted. (The subject line suggests
that it might be; the OP's examples do not.)

> For a bash/POSIX solution, you need something like:
>
> #! /bin/sh -
> file=$1 dir=$2
> case $dir in
>  /*) ;;
>  *) dir=./$dir;; # avoid pb with filenames
>                  # starting with "-", "(", "!"...
> esac
> exec find "$dir" -type d -exec cp -- "$file" '{}' ';'
>
> # (untested)

As is often the case, there is a trade-off. Calling cp for every
directory is less efficient 99% of the time.


--
Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
 ========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License





[ Post a follow-up to this message ]



    Re: cp: copying a file to one directory and all subdirectories  
foo


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:13 PM

John Bullock wrote:

>
> Hello,
>
>         I have a basic problem: I want to use a single command to copy
> one file to a directory and all of its subdirectories.  How can I do
> this without typing out the name of each individual subdirectory?
>
>         For example: I have foo.bar and want to copy it to /etc -- but
> also to /etc/etc1, /etc/etc2, /etc/etc3, and so on.  I looked to the man
> page for cp to find an easy way to do this, but to no avail.
>
> Many thanks,
> --John

<http://freshmeat.net/projects/ff/>

$ ff -re '[ -d "$f" ] && cp foo.bar "$f"' /etc
-or-
$ find /etc -type d | ff -pe 'cp foo.bar "$f"'






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:34 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 

Back To The Top
Home | Usercp | Faq | Register