Need BASH Script Help to Move Files While Creating Directories

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

Hi,

Welcome to the Forum!

Here is one way.
Can you try the below and see if it helps:

#!/bin/bash

SOURCE="/data/Customer-Files-1"
DEST="/data/Customer-Files-Over-2-Years-Old"
PARENTDIR=${SOURCE%/*}
ROOTDIR=${SOURCE##*/}
cd $PARENTDIR

find $ROOTDIR  -type f -mtime +730 | while read line
do
GETSUBDIRNAME=$(dirname $line)
mkdir -p $DEST/$GETSUBDIRNAME 
echo "cp $line to $DEST/$GETSUBDIRNAME"
cp $line $DEST/$GETSUBDIRNAME
# rm -f $SOURCE
done

Make sure to run with cp command as shown above first and compare the files/directory structure and then you can uncomment rm -f command.

This method still uses a copy. These are large files and therefore the cp takes way longer than mv since mv just changes the file pointer and doesn't actually copy the file. Can you post it with using mv?

you can modify the code i posted before and change cp to mv

Please read

man mv

and see how it differs from cp syntax wise !

Note also that if the source and destination directories are on different filesystems, the mv utility will internally act as though cp and rm (and, possibly, chmod ) had been done instead).