I mean to use rsync
to remove certain files (for Efficiently delete large directory containing thousands of files), in this case given to a shell script as patterns in the command line.
So far, this is what I have in my shell script rsync_del.sh
#!/bin/bash
TARGET_DIR=${1}
shift
PATTERNS="${@}"
for patt in ${PATTERNS} ; do
# Both do the same
#INCLUDE_PATTERNS="${INCLUDE_PATTERNS}"' --include='\'"${patt}"\'
INCLUDE_PATTERNS="${INCLUDE_PATTERNS} --include=\"${pattern}\""
done
EMPTYDIR=$(mktemp -d)
echo "Created empty dir ${EMPTYDIR}"
comm="rsync -a --progress --delete ${INCLUDE_PATTERNS} ${EMPTYDIR}/ ${TARGET_DIR}"
echo ${comm}
eval ${comm}
Example patterns that I would like to use are *[1-9].txt
, *000??9.txt
.
The problem is that, when executing
rsync_del.sh trg_dir '*[1-9].txt'
the command line generated is
rsync -a --progress --delete --include='*[1-9].txt' /tmp/tmp.51R9hPgkfG/ trg_dir/
(which seems ok to me), but it is matching, e.g., files like input.dat
(and I don't want that).
What is the correct way of implementing/using this? I suspect it is a matter of properly escaping patterns, but I could not make this work.
Note: I need to define the command to be executed in a variable, to echo
it prior to executing.
eval
was only to be able toecho
the command line to be used, as a reference and to possibly copy and reuse it with minor variations. – sancho.s ReinstateMonicaCellio Sep 27 '18 at 06:48for pattern in ... ; do
– sancho.s ReinstateMonicaCellio Sep 27 '18 at 06:49${INCLUDE_PATTERNS}
in the double quotes to avoid local expansion. – sancho.s ReinstateMonicaCellio Oct 02 '18 at 07:14'* *'
to match filenames that contain spaces) and a pattern that contain a filename pattern that matches a file in the current directory (maybe'*.txt'
or just'*'
). – Kusalananda Oct 02 '18 at 07:28