I'm trying to run find with rsync command.
I followed the answer here.
When I run it from the command line it run okay.
find ./copy1 -mtime +14 -printf %P\\0 | rsync -avc --dry-run --files-from=- --from0 ./copy1 /home/shlo/copy1/
The problem started when I run it from bash script:
RSYNC_COM='find ./copy1 -mtime +14 -printf %P\\0 | rsync -avc --dry-run --files-from=- --from0 ./copy1 /home/shlo/copy1/'
output=$($RSYNC_COM 2>&1)
I get the error:
find: paths must precede expression: | Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
It look he thinks the rsync command is parameters in the find command.
How can I fix it?
Thanks
The problem is that you assign the command to a variable. The |
is no longer parsed as pipe "operator", but as a normal string.
# echo 123 | cat
123
# a="echo 123 | cat"
# $a
123 | cat
The |
is invalid argument for find
, so it returns an error.
Use a function:
rsync_com() {
find ./copy1 -mtime +14 -printf %P\\0 |
rsync -avc --dry-run --files-from=- --from0 ./copy1 /home/shlo/copy1/
}
output=$(rsync_com 2>&1)
Remember to always quote variable expansions. And never run a command created by an unescaped variable expansion.
The convention is that upper case variables are reserved to be exported variables.