I use a simple backup bash script to copy some directories originally placed in a Debian (Testing) from a USB stick (formatted NTFS) to a MacBook. The names of directories are the same from origin to destination. Now, I need to change some names during the copy: for example, I've a hidden file (.vimrc) and a hidden directory (.abook) and I'd like to copy them as vimc and abook (not hidden), that is without point. The original code is the following one:
#!/bin/bash
declare -A BACKUP_INFOS
BACKUP_INFOS=(
[/Users/myuser/Documents]="/Volumes/CORSAIR128/.abook /Volumes/CORSAIR128/.vimrc /Volumes/CORSAIR128/articles
...
/Volumes/CORSAIR128/xindy"
)
for dest_dir in "${!BACKUP_INFOS[@]}"
do
mkdir -p "$dest_dir"
src="${BACKUP_INFOS[$dest_dir]}"
rsync -avuz --delete --delete-after --progress $src "$dest_dir"
done
My goal is for .abook/ (a directory) and .vimrc (a file) to be copied to the destination as /abook/ and vimrc (changing the name so they are no longer "hidden files").
If I add the following two lines:
mv -v $dest_dir/.vimrc $dest_dir/vimrc
mv -v $dest_dir/.abook $dest_dir/abook
In the first case, I achieve my goal, but in the second case, the command doesn't rename the original directory, but creates a new one (abook) into which it copies the old one (.abook). So I find a sort of useless (and again hidden) duplicate:
$dest_dir/abook/.abook
A solution can be found by integrating the code as follows:
mv -v $dest_dir/.abook $dest_dir/abook && rm -r $dest_dir/abook/.abook
However, it's clear that this is a completely inefficient code: copying and then deleting... Is there a way to achieve my goal in a functional way?
Thank you
-aor-Aoptions. or add those to yourlsalias if you use one.declare -pto store arrays inside an array and thenevalto extract them). But you know that, otherwise you wouldn't be trying to fake one with word splitting on the sub-array elements. I can only recommend that you either find a better way of doing it that suits bash or use a language that supports AoAs natively. e.g. perl. or python..abook) mv would fail in the way you describe is that there's already anabookdirectory in $dest_dir - so themvisn't renaming.abooktoabook, it's moving it into the existing directory. If there was an existingvimrcdirectory, then the first mv operation would fail in the same way.