Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

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
0
rjohnsonjr
Asked:
rjohnsonjr
  • 3
  • 3
  • 2
  • +4
1 Solution
 
owensleftfootCommented:
#!/bin/bash

for directory in a b c d e f g
 do
cp file.txt $directory/
 done
0
 
rugdogCommented:
ls -d a b c d e f g|xargs -n1 cp file.txt
0
 
wesly_chenCommented:
Hi,

  For rugdog's script/comand, I did some modification:
----
ls -d * |xargs -n1 cp file.txt
----
Wesly
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rugdogCommented:
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.
0
 
TintinCommented:
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 {} \;
0
 
rjohnsonjrAuthor Commented:
TinTin,

Please describe your command so I can understand what each part does.

Thanks!

Randy
0
 
badrulnmCommented:
based on rugdog command, if you want to copy the file to all subdirectories:

# ls -F |grep "/" |xargs -n1 cp file.txt
0
 
rugdogCommented:
a simplified version of tintin's and badrulnm's and safer version of wesly's:

ls -d */|xargs -n1 cp file.txt
0
 
ITG-SSNACommented:
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.
0
 
TintinCommented:
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 {}
0
 
badrulnmCommented:
tintin, i found that your find command statement will also copy file.txt to the directory i start searching from.
0
 
TintinCommented:
I can't possibly see how you would get two different versions of file.txt being copied.  Are you using correct paths?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now