I want to archive my photos into tar archives.
The directory containing photos has one dir per year and multiple dirs inside each year dir:
Photos
2005
2006
Some dir with spaces
Pic1.jpg
Pic2.jpg
...
Other dir here
...
2007
...
Target structure should also contain a directory per year, but inside a year dir I would have tar.gz files for each subdir from source dir, like this:
Backup
2005
2006
Some_dir_with_spaces.tar.gz
Other_dir_here.tar.gz
...
2007
...
Script which I'm creating to do this looks as follows:
cd ~/Photos
for y in *
do
YEAR_DIR=~/Backup/$y
mkdir -p $YEAR_DIR
pushd $y
for d in *
do
f=`echo $d | tr ' ' '_' | tr -d ',.!'` # Clean the name
t=$YEAR_DIR/$f.tar.gz
d=$(printf '%q' "$d")
# echo tar czf $t "$d" # This outputs the command as expected
tar czf $t "$d" # but executing it results in error: Cannot stat: No such file or directory
done
popd
done
As hinted in the comment, echoing the prepared command looks fine, and also if I copy that statement and execute it from terminal it works, but executing it within the script (tar czf $t "$d"
) results in an error:
tar: Some\ dir\ with\ spaces: Cannot stat: No such file or directory
What am I doing wrong?
P.S. I'm doing this on a Mac.
d=${d/ /_}
to do replacement of spaces in variables (assuming you're using bash) – Anthon May 03 '16 at 06:31