0

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
chicks
  • 3,639
  • 10
  • 26
  • 36
Jeff C
  • 1
  • 1

1 Answers1

1

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:

$ dirname /usr/bin/bash
/usr/bin

$ dirname /usr/bin
/usr

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:

#!/bin/bash

SRC=./src
DST=./dst

for f in `cd $SRC && find . -type f -mtime +730`; do
    echo "moving $f"
    mkdir -p "$DST/`dirname $f`"
    mv "$SRC/$f" "$DST/$f"
done

Obviously you have to set SRC / DST as for your needs.

BTW, mv is faster then cp + rm when you're on the same filesystem FWIK.

Fredi
  • 2,227
  • 9
  • 13