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/{}"' \;


1 Answer 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

$ dirname /usr/bin

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:



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

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .