bash - Moving files/directories older than 7 days -


i have code find files/directories older 7 days, execute mv. realise need different command directories , files. -type not support fd - manual says supports 1 character.

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

how move both files , directories >7d /mnt/third/bt/tmp/ whilst keeping same structure had in /mnt/third/bt/uploads/?

thanks

imho, non-trivial problem correctly - @ least me :). happy, if more experienced post better solution.

the script: (must have gnu find, if "find" gnu-version change gfind 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     case $type in     f) rm "$name";;     d) rmdir "$name";;     esac done < $tmp #rm $tmp 

explanation:

  • find want move (will copy first , delete after) , store in tmpfile (find)
  • copy list of things tmpfile new place (cpio)
  • and remove old files , dirs - based on list tmpfile (while...)

the script not handling symbolic links, fifo files, etc., , print zilion errors @ deleting directories are old, they're not empty (contain new files or subdirs)

dry run first! :)


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -