I am attempting to copy a single specific file that is contained within 960 sub directories in a parent directory. Each file has the same name but has unique content.

I want to maintain the hierarchy of the directories and names in the new parent directory, but only want one specific file rather than all the files in each sub directory.

I also need to create each of these sub directories named processorN where N is a number from 0 to 959.

Any ideas, this has stumped me and I can't seem to find a good solution that isn't going to take me hours.

  • 3
    Hello! Please provide an example of the current and the desired directory structures. For this purpose you can create manually a parent directory and few sub directories and then you can edit and update the question with the output of the command tree /path/to/parent-dir/.
    – pa4080
    Commented Aug 31, 2018 at 13:44

1 Answer 1


rsync can do that with some tricky --include and --exclude flags:

rsync -a -v --include="*/" --include="specific-file.txt" --exclude="*" from/ to/

This will copy all directories and the specific-file.txt from from to to while skipping everything else. The command will preserve the complete directory structure below from and copy it to to. You may first want to run this with the -n switch to see what would happen, i.e.:

# check:
rsync -a -v -n --include="*/" --include="specific-file.txt" --exclude="*" from/ to/

# actually do:
rsync -a -v    --include="*/" --include="specific-file.txt" --exclude="*" from/ to/
  • I've upvoted, but maybe the second requirement "...sub directories named processorN..." is not satisfied - probably something as rename 's/.+/our $i; sprintf("processor%03d", 0+$i++)/e' * will do the job - more details.
    – pa4080
    Commented Aug 31, 2018 at 14:10
  • @pa4080 Might be. I thought those processorN directories were the 960 subdirectories the OP talks about in the first paragraph. He should really clarify that, as you already asked.
    – PerlDuck
    Commented Aug 31, 2018 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .