Moving files / directories older than 7 days

I have this code to find files / directories older than 7 days and then execute mv. However, I understand that I need another command for directories and files. -typealso does not support fd- the manual says that it supports only one character.

find /mnt/third/bt/uploads/ -type f -mtime +7 -exec mv {} /mnt/third/bt/tmp/ \;

How to move both files and directories> 7d to /mnt/third/bt/tmp/, keeping the same structure as in /mnt/third/bt/uploads/?

thank

+3
source share
2 answers

IMHO, this is a non-trivial problem to do it right - at least for me :). I will be happy if someone with a more experienced post becomes a better solution.

script: (GNU must be found if your "find" is the version of the GNU version that gfind needs to find)

FROMDIR="/mnt/third/bt/uploads"
TODIR="/mnt/third/bt/tmp"
tmp="/tmp/movelist.$$"

cd "$FROMDIR"
gfind . -depth -mtime +7 -printf "%Y %p\n" >$tmp
sed 's/^. //' < $tmp | cpio --quiet -pdm "$TODIR"

while read -r type name
do
    case $type in
    f) rm "$name";;
    d) rmdir "$name";;
    esac
done < $tmp
#rm $tmp

Explanation:

  • , ( ) tmpfile (find)
  • tmp (cpio)
  • , , dirs - tmpfile (while...)

script , fifo .. zilion , , ( SUBDIRS)

DRY RUN !:)

+2

, .

+1

All Articles