Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rsync wildcard not working in bash script

I try to sync files via rsync to my raspberry pi 3.

rsync -v -P -r --size-only --remove-source-files /home/user*.mp3 [email protected]:/media/hdd/

This command works fine and only copies the mp3 files.

Since files are not all in the same place, and i don't want to type the command all the time, i did a bash script so i could change the path to source and destination.

echo "ENTER PATH" 
read -i "/home/user/*.mp3" -e path

echo "ENTER DESTINATION!"
read -i "[email protected]:/media/hdd/" -e dest

rsync -v -P -r --size-only --remove-source-files "$path" "$dest"

But this gives me the following error message

rsync: link_stat "/home/user/*.mp3" failed: No such file or directory (2)

If i do "/home/user/" the script is working, but copies all files not only mp3. So i guess the wildcard is not working in this bash script

Any clue why?

like image 318
wombat Avatar asked Mar 04 '26 07:03

wombat


1 Answers

This is not an rsync wildcard but a bash wildcard, aka glob. Bash will replace it with a list of files before rsync is ever called.

However, bash doesn't do word splitting and globbing on quoted variables:

var="/etc/*tab"
echo "Quoted:" "$var"
echo "Unquoted:" $var

Results in:

Quoted: /etc/*tab
Unquoted: /etc/crontab /etc/fstab /etc/inittab /etc/mtab

You don't normally want bash to mess around with your variables, which is why quoting is so important and kudos for doing it. However, in this case you actually do want pathname expansion, so you should carefully leave the variable unquoted:

# Prevent word splitting, so that '/path with spaces/*.mp3' still works
IFS="" 
# No quoting on $path, to allow glob expansion:
rsync -v -P -r --size-only --remove-source-files $path "$dest"
like image 139
that other guy Avatar answered Mar 06 '26 20:03

that other guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!