I’m trying to move some dirs from one location to another, but I need to leave one in place (all files will remain in place). I’ve tried several things, but nothing seems to work.
I have tested the value of DIR_COUNT and it works as expected. However, when used in a conditional or case statement, it doesn’t work as expected.
conditional
#!/bin/bash
DIR_COUNT=$(find path/to/dir/*[^this_dir_stays_put] -type d -maxdepth 0 | wc -l)
echo $DIR_COUNT
if [[ $DIR_COUNT > 0 ]]
then
find path/to/dir/*[^this_dir_stays_put] -type d -maxdepth 0 -exec mv {} new/location \;
echo "Moving dirs."
else
echo "No dirs to move."
fi
case
#!/bin/bash
DIR_COUNT=$(find path/to/dir/*[^this_dir_stays_put] -type d -maxdepth 0 | wc -l)
echo $DIR_COUNT
case $DIR_COUNT in
0)
echo "No dirs to move."
*)
echo "Moving dirs."
find path/to/dir/*[^this_dir_stays_put] -type d -maxdepth 0 -exec mv {} new/location \;;;
esac
With both versions of the code, everything is fine provided the directories to be moved exist, but if there aren’t any to move, I have problems.
conditional
$ sh script.sh
find: find path/to/dir/*[^this_dir_stays_put]: No such file or directory
0
No dirs to move.
case
$ sh script.sh
find: find path/to/dir/*[^this_dir_stays_put]: No such file or directory
0
Moving dirs.
find: find path/to/dir/*[^this_dir_stays_put]: No such file or directory
Skip the conditional and case statement.