bashshellubuntu

How to copy a directory structure but only include certain files


I found a solution for my question in Windows but I'm using Ubuntu: How to copy a directory structure but only include certain files using Windows batch files?

As the title says, how can I recursively copy a directory structure but only include some files? For example, given the following directory structure:

folder1
  folder2
    folder3
      data.zip
      info.txt
      abc.xyz
    folder4
    folder5
      data.zip
      somefile.exe
      someotherfile.dll

The files data.zip and info.txt can appear everywhere in the directory structure. How can I copy the full directory structure, but only include files named data.zip and info.txt (all other files should be ignored)?

The resulting directory structure should look like this:

copy_of_folder1
  folder2
    folder3
      data.zip
      info.txt
    folder4
    folder5
      data.zip

Could you tell me a solution for Ubuntu?


Solution

  • I don't have a beautiful one liner, but since nobody else has answered you can always:

    find . -name 'file_name.extension' -print | cpio -pavd /path/to/receiving/folder
    

    For each specific file after copying the directories.

    (Make sure you're in the original folder first, of course! :) )