I have multiple directories with a few thousand files each. Each directory contains files with the same names, but with different extensions.

I would like to move x files each from these sets of directories to another set of directories, such that the same file names, except the extension, are moved from each directory. Which files are moved do not matter to me.

For example, if I have directories A/, B/, and C/, each with 1000 files, and I have another set P/, Q/, R/, and say x is 100, I would like to copy 100 files from A to P, B to Q, C to R, such that the files copied have the exact same name, except the extension.

I have seen the following questions and their answers:

However, each deals with copying any x files. I tried using find as suggested, and found that different files were being copied from different directories.

How can I do this?

Thank you.

  • Come to think, Stéphane's solution here should always select the same file names. Have you tried it ? – don_crissti Apr 20 '17 at 16:18
  • @don_crissti Yes, the answer to both is yes. Thank you for the link as well - I hadn't understood what that solution does, so I didn't use it, but I will try now. – GoodDeeds Apr 20 '17 at 16:29
  • 1
    Well, the shell will expand the glob in lexicographical order and since you have the same file names (except the extension) in each directory that means they will always be expanded in the same order. – don_crissti Apr 20 '17 at 16:42
  • 1
    No problem. Try the solution I suggested and if it doesn't work let me know. It should work though and in that case once you have figured out how it works (hint: parameter expansion) you can always edit your answer to include it and explain how it works. – don_crissti Apr 20 '17 at 19:56

Your Answer

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

Browse other questions tagged or ask your own question.