What I want to do
I want to move all XML files from /source/
to /target/
How I try to do it
rsync -avz --remove-source-files /source/*.xml /target/
I am using the Symfony Process / Console Component as a wrapper for rsync.
protected function execute(InputInterface $input, OutputInterface $output){
$process = new Process([
'rsync', '-azv', '--remove-source-files',
$input->getArgument('source-path'),
$input->getArgument('target-path')
]);
}
My Problems
Calling my command by running php bin/console command:moveFiles /source/*.xml /target/
results in:
Too many arguments, expected arguments "command" "source-path" "target-path".
It seems like the * in /source/*.xml throws off Symfony (?) and does not let it recognize the correct amount of arguments provided. Escaping the * as in rsync -avz --remove-source-files /source/\*.xml /target/
results in:
rsync: link_stat "/source/*.xml" failed: No such file or directory (2)
How can I pass the wildcard GLOB to rsync wrapped by symfony? Is there another way to achieve this using the console?
So I did indeed not manage to resolve the issue but I created a workaround:
protected function configure()
{
$this->setName('jaya:moveFiles')
->addArgument('source-path')
->addArgument('target-path')
->addArgument('include-filext')
->setDescription('Sync files with option --remove-source-files from „source-path“ to „target-path“.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$src = $input->getArgument('source-path');
$target = $input->getArgument('target-path');
$include = $input->getArgument('include-filext');
$process = new Process([
'rsync', '-avz', '--remove-source-files', '--include="*.' . $include . '"', $src, $target
]);
$process->run();
}
I added another inputArgument called 'include-fileext' to which I can pass some file extention string ('pdf', 'xml', 'jpg', ...). This file extention gets concatenated with the infamous wildchard character '*'. That way I do not actually pass the wildcard character as part of the parameter and therefore I avoid having issues.
Instead of passing '/source-path/*.xml' and '/target-path/' I pass '/source-path/', 'pdf' and '/target-path/'. This results in the Symfony console executing rsync -avz --remove-source-files --include="*.pdf" /source-path/ /target-path/
and transfers only files matching my include pattern.