|
Home > Archive > Unix Shell > June 2004 > cp: copying a file to one directory and all subdirectories
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
cp: copying a file to one directory and all subdirectories
|
|
| John Bullock 2004-06-20, 10:32 pm |
|
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
| |
| Alex Schuster 2004-06-20, 10:32 pm |
| 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
| |
| Alan Connor 2004-06-20, 10:32 pm |
| 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
| |
| Stephane CHAZELAS 2004-06-26, 10:12 am |
| 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
| |
| Chris F.A. Johnson 2004-06-26, 10:12 am |
| 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
| |
| Stephane CHAZELAS 2004-06-26, 10:12 am |
| 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
| |
| Chris F.A. Johnson 2004-06-26, 10:12 am |
| 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
| |
|
| 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"'
|
|
|
|
|