Reputation: 39
Have the following bash codes to deal with filenames passed randomly with mapfile. I want to call rsync
on each file and send to a destination path dpath
.
mapfile -d '' fl < <(
find "$dpath" -maxdepth 1 -type f "${inclnm[@]}" |
shuf -z -n "$nf"
)
Or with shuf
working on arguments directly
mapfile -d '' fl < <( shuf -z -n "$nf" -e "${inclnm[@]}" )
How can I modify the two alternatives to run rsync
on each file and send to destination?
Upvotes: 0
Views: 196
Reputation: 19675
As said in comments, you don't need to mapfile
an intermediate array. Just stream the null delimited selection of files to rsync
like this:
#!/usr/bin/env bash
nf=4
inclnm=( a* b* )
# For testing purpose, destination is local host destfolder inside
# user home directory
destination="$USER@localhost:destfolder"
# Pipe the null delimited shuffled selection of files into rsync
shuf -z -n "$nf" -e "${inclnm[@]}" |
# rsync reads the null-delimited selection of from files from standard input
rsync -a -0 --files-from=- . "$destination"
If you want to collect the random selection of files and use it with rsync
then do:
#!/usr/bin/env bash
nf=4
inclnm=( a* b* )
# For testing purpose, destination is local host destfolder inside
# user home directory
destination="$USER@localhost:destfolder"
# Capture the selection of files into the fl array
mapfile -d '' fl < <( shuf -z -n "$nf" -e "${inclnm[@]}" )
# Pass the fl array elements as sources to the rsync command
rsync -a "${fl[@]}" "$destination"
Upvotes: 5