I've got this script to loop through all folders and move files that are more than 2 years old. I'm using the install command because it creates the necessary directories on the destination path and then I remove the source. I'd like to change the script to use the mv
command since it is much faster than copying the file then deleting it. But I don't know how to get the path without the filename to pass to the mkdir -p
command? How should I modify the script?
for d in *; do
find "$d" -type f -mtime +730 -exec sh -c '
echo "moving $d/{}" &&
install -Dp "/data/Customer Files$d/{}" "/data/Customer Files Over 2 Years Old$d/{}" &&
rm -f "/data/Customer Files$d/{}"' \;
done
Best Answer
You need
dirname
but beware, if you give it a directory name it'll give you the path till the parent dir, maybe an example is easier:Back to your script, the dirname subtlety does not come to play as you're searching for files. I'll simplified it a bit too, i didn't like the subshell, it's easier to parse for me without it. It becomes:
Obviously you have to set SRC / DST as for your needs.
BTW,
mv
is faster thencp
+rm
when you're on the same filesystem FWIK.