I have implemented a rsync based system to move files from different environments to others.
The problem I’m facing now is that sometimes, there are files with the same name, but different path and content.
I want to make rsync (if possible) rename duplicated files because I need and use --no-relative
option.
Duplicated files can occur in two ways:
- There was a file with same name in dest directory already.
- In the same rsync execution, we are transferring file with same name in a different location. Ex: dir1/file.txt and dir2/file.txt
Adding -b --suffix
options, allows me to have at least 1 repetition for the first duplicated file’s type mentioned.
A minimum example (for Linux based systems):
mkdir sourceDir1 sourceDir2 sourceDir3 destDir;
echo "1" >> sourceDir1/file.txt;
echo "2" >> sourceDir2/file.txt;
echo "3" >> sourceDir3/file.txt;
rsync --no-relative sourceDir1/file.txt destDir
rsync --no-relative -b --suffix="_old" sourceDir2/file.txt sourceDir3/file.txt destDir
Is there any way to achieve my requirements?
2
Answers
I don’t think that you can do it directly with
rsync
.Here’s a work-around in
bash
that does some preparation work withfind
and GNUawk
and then callsrsync
afterwards.The idea is to categorize the input files by "copy number" (for example
sourceDir1/file.txt
would be the copy #1 offile.txt
,sourceDir2/file.txt
the copy #2 andsourceDir3/file.txt
the copy #3) and generate a file per "copy number" containing the list of all the files in that category.Then, you just have to launch an
rsync
with--from-file
and a customized--suffix
per category.Pros
rsync
per file.Cons
awk
call into two).Here are the steps:
0) Use a correct shebang for
bash
in your system.1) Create a directory for storing the temporary files.
2) Categorize the input files by "duplicate number", generate the files for
rsync --from-file
(one per dup category), and get the total number of categories.