How to copy a list files to a list of directories or subdirectories

0

I'm trying to figure out how to copy a list of files to a list of subdirectories.

eg. I have 300 files to copy into 300 subdirectories - 1 to 1.

file1  ->  dir1/sub
file2  ->  dir2/sub
file3  ->  dir3/sub
file4  ->  dir4/sub
.
.
file300  ->  dir/sub300

I normally have the files in the current directory where the directories are located so i would run something like

find . -type f -name "file*"   

copy to

find . -type d -name "dir*/sub"

What would be the best way to run such a script? Thanks.

13th Matrix

Posted 2014-09-24T15:48:47.467

Reputation: 73

Answers

3

file1  ->  dir1/sub
file2  ->  dir2/sub
file3  ->  dir3/sub
file4  ->  dir4/sub
.
.

The names of files and directories are with numbers as above?

for i in {1..300}; do cp file$i dir$i/sub; done

jimmij

Posted 2014-09-24T15:48:47.467

Reputation: 361

1To make people feel better about the variable expansion, use file${i} dir${i}/sub. Oh, omitting the curly braces will work fine. But the curly braces are a good habit if you're going to be adding things on the end of variables, whether or not the addition includes delimiters like slashes. – bgStack15 – 2014-09-24T16:24:47.667

In general I agree. – jimmij – 2014-09-24T16:29:29.673