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.

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
  • 1
    To 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 Sep 24 '14 at 16:24
  • In general I agree. – jimmij Sep 24 '14 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.