I have this script that I have been using for a while and it mostly works, but there are some problems I would appreciate assistance in rectifying.

The goal is to look at an external mountpoint, check for the presence of matching source directories, and if matching, remove the source, and make a symlink from the external to the empty spot where the source was.

Of course, any suggestions on better ways of achieving the same task would be most welcome.

The first and most significant problem, is that some directories are left behind. Looking through them, I cannot see any specific pattern. Many have {} or other special characters and I think this may be the main cause, but some are very vanilla, without special characters.

Second, is that my script does not account for files, where it expects a directory.

The structure looks like this:

19823798/Test1 (789) - {456} [123]
8765862/Test2 {123} - (456) [789]
345345/Test2-ünicode (456) - {789} [123]
308/unexpected.file

and my script looks like this:

#!/bin/bash
set -e
src_dir="/var/tank"
dest_dir="/mnt/tank"
nums_and_names=$(find "$src_dir" -mindepth 2 -maxdepth 2 -type d -regextype grep -regex ".*/[0-9]\{1,10\}/.*" -printf '%P\n')
while read -r named; do
        echo "dest_dir is "${dest_dir}/$named""
        echo "find output is $named"
        [ -d "${dest_dir}/$named" ] && echo ""${dest_dir}/$named" exists!" || exit 0
        rm -rf -- "${src_dir}/$named"
        ln -s -- "${dest_dir}/$named" "${src_dir}/$named"
done <<< "$nums_and_names"
echo "all finished"
  • You have the quoting wrong in echo ""${dest_dir}/$named" exists!" (though this is not the reason that some are missing). But, why do you finish processing the entries as soon as you find a directory? All entries afterwards would be ignored. – user1934428 Nov 16 '17 at 12:21
  • thanks for the reply. seems chrome ate mine. is quoting " ' ' " more correct? as for the loop, my understanding and observation is that it loops through the find results, and exits to continue the loop if there is no match, but executes rm and ln if it does find a match. do i have the wrong end of the stick? – jakethedog Nov 16 '17 at 14:36
  • exit ends the whole script. continue would proceed with the next iteration. – user1934428 Nov 17 '17 at 5:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.