Randy Johnson
asked on
Doing a copy of a file to sub-dirs
Here is a directory structure
a
b
c
d
e
f
g
all of the above are directorties
I want to copy file file.txt to each of those directories
i thought cp file.txt */ would do it but that did not work.
any ideas?
Thanks!
Randy
a
b
c
d
e
f
g
all of the above are directorties
I want to copy file file.txt to each of those directories
i thought cp file.txt */ would do it but that did not work.
any ideas?
Thanks!
Randy
ls -d a b c d e f g|xargs -n1 cp file.txt
Hi,
For rugdog's script/comand, I did some modification:
----
ls -d * |xargs -n1 cp file.txt
----
Wesly
For rugdog's script/comand, I did some modification:
----
ls -d * |xargs -n1 cp file.txt
----
Wesly
Wesly,
that's dangerous assuming file.txt, a, b, c, d, e, f and g are not the only files in the current directory. if you have more files all will end being like file.txt.
that's dangerous assuming file.txt, a, b, c, d, e, f and g are not the only files in the current directory. if you have more files all will end being like file.txt.
If you don't want to type in the directories and/or they will vary in name and quantity, do:
find /path/to/dirs -type d -maxdepth 1 -exec cp /path/to/file.txt {} \;
find /path/to/dirs -type d -maxdepth 1 -exec cp /path/to/file.txt {} \;
ASKER
TinTin,
Please describe your command so I can understand what each part does.
Thanks!
Randy
Please describe your command so I can understand what each part does.
Thanks!
Randy
based on rugdog command, if you want to copy the file to all subdirectories:
# ls -F |grep "/" |xargs -n1 cp file.txt
# ls -F |grep "/" |xargs -n1 cp file.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have learned to use perl from command line for times like this. I liken it to "Using the force"
Assuming you are in the parent directory of a->g:
perl -e 'my @arry=(a..g); for (@arry) {system("cp file.txt ./$_");}'
Highest regards,
~K Black
Irvine, Ca.
Assuming you are in the parent directory of a->g:
perl -e 'my @arry=(a..g); for (@arry) {system("cp file.txt ./$_");}'
Highest regards,
~K Black
Irvine, Ca.
rugdog's version is certainly the shortest and simpliest.
To expand of the find command:
find /path/to/dirs -type d -maxdepth 1 -exec cp /path/to/file.txt {} \;
/path/to/dirs - Path to the directory to start searching from
-type d - Only match directories
-maxdepth 1 - Don't descend to subdirectories
-exec fork a command
cp /path/to/file.txt {} \; - Copy file.txt to the matched directory, which is specified with {}
To expand of the find command:
find /path/to/dirs -type d -maxdepth 1 -exec cp /path/to/file.txt {} \;
/path/to/dirs - Path to the directory to start searching from
-type d - Only match directories
-maxdepth 1 - Don't descend to subdirectories
-exec fork a command
cp /path/to/file.txt {} \; - Copy file.txt to the matched directory, which is specified with {}
tintin, i found that your find command statement will also copy file.txt to the directory i start searching from.
I can't possibly see how you would get two different versions of file.txt being copied. Are you using correct paths?
for directory in a b c d e f g
do
cp file.txt $directory/
done