I am trying to write a bash script to copy files with cp or, preferably, rsync and then move the source files to the trash. I do not want to use mv because in case of an error, I want to be able to recover the source files.
This script works. It hard codes the destination folder.
for i in "$@"; do
cp -a -R "$i" '/home/userxyz/Downloads/folder1'
gio trash "$i"
done
However, this script that uses a variable for the destination folder does not work.
read -p "Enter destination folder: " destination
for i in "$@"; do
cp -a -R "$i" "$destination"
gio trash "$i"
done
Error when I enter '/home/userxyz/Downloads/folder1' as destination:
cp: cannot create regular file "'/home/userxyz/Downloads/folder1'": No such file or directory
Similarly, this works:
for i in "$@"; do
rsync "$i" '/home/userxyz/Downloads/folder1'
gio trash "$i"
done
But this does not work:
read -p "Enter destination folder: " destination
for i in "$@"; do
rsync "$i" "$destination"
gio trash "$i"
done
Error:
rsync: change_dir#3 "/home/userxyz//'/home/userxyz/Downloads" failed: No such file or directory (2)
rsync error: errors selecting input/output files, dirs (code 3) at main.c(720) [Receiver=3.1.3]
I have confirmed that '/home/userxyz/Downloads/folder1' exists. What am I doing wrong?
set -x
to the script to see what's going on there.cp -a
includes-R
, so you don't need that option (andrsync
also has an-a
/--archive
option). You also don't need that loop, replace"$i"
with"$@"
.