Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syncing a single file and keeping N levels of folder structure

Tags:

rsync

I have the following structure:

/Users
  /build
    /.jenkins
      /jobs
         /Job1
           config.xml
           someotherfiles.blah
         /Job2
           config.xml
           someotherfiles.blah
         /JobN
           config.xml
           someotherfiles.blah

I want to backup only the config.xml file to another folder keeping the folder structure the same in the new folder, but pruning away /User/build/.jenkins/jobs

My new folder would look like this:

backup/
  /Job1
    config.xml
  /Job2
    config.xml
  /JobN
    config.xml

Is this possible to do using rsync?

Edit: had accidentally included the someotherfiles.blah in the output, I actually don't want those. All I want is the config.xml file along with the folder structure 1 level up.

like image 633
ThaDon Avatar asked Dec 13 '13 20:12

ThaDon


People also ask

What is a folder hierarchy?

A folder hierarchy is an organizational structure of one or more folders in Oracle iFS. Folder hierarchies organize the repository so that users can browse through it easily. You can create multiple folder hierarchies to organize information in different ways to make browsing convenient for different types of users.

How do I create a directory structure diagram?

Use the Creately folder structure template, to draw a diagram to organize folders. Set up a folder for each type of document, then create subfolders for each topic under the parent folder. Place any file that does not fit into other folders, into an uncategorized folder.

How do I keep two directories in sync Linux?

To sync the contents of dir1 to dir2 on the same system, you will run rsync and use the -r flag, which stands for “recursive” and is necessary for directory syncing: rsync -r dir1/ dir2.

What is a folder within a folder called in a hierarchy?

Folders can also be stored within other folders, called subfolders, which in turn can store further subfolders. Windows Explorer displays your drives, folders and files in this hierarchical structure, making it easy to navigate your files.


1 Answers

This should work:

rsync -r --include=Job* --include=config.xml --exclude=* /full/path/to/Users/build/.jenkins/ backup

The full path is not copied to the backup directory unless you pass the -R option to rsync. If you include Job* and config.xml and then exclude * (order is important, because the first match to an include or exclude rule determines what gets copied), you end up with the structure you want. If having the explicit Job* pattern is too restrictive, the manual says you should be able to use the */ pattern:

One solution is to ask for all directories in the hierarchy to be included by using a single rule: "+ */" (put it somewhere before the "- *" rule)

See the whole INCLUDE/EXCLUDE PATTERN RULES section of the manual page for more details.

like image 156
Paulo Almeida Avatar answered May 13 '23 12:05

Paulo Almeida