移动文件/目录7天以上 [英] Moving files/directories older than 7 days

查看:80
本文介绍了移动文件/目录7天以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code发现超过7天以上的文件/目录,然后执行一个MV。不过,我知道我需要的目录和文件不同的命令。 同样不支持 FD - 手动说,它仅支持一个字符

I have this code to find files/directories older than 7 days, then execute a mv. However I realise I need a different command for directories and files. -type also does not support fd - a manual says it only supports one character.

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

如何移动文件和目录> 7D到的/ mnt /三/ BT / tmp目录/ ,同时保持它们具有相同的结构到/ mnt /三/ BT /上传/

How do I move both files and directories >7d into /mnt/third/bt/tmp/ whilst keeping the same structure they had in /mnt/third/bt/uploads/?

感谢

推荐答案

恕我直言,这是一个不平凡的问题,正确地做到这一点 - 至少对我来说:)。我会很高兴,如果有人多经验丰富的后期更好的解决方案。

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

该脚本:(必须有一个GNU发现,如果你的发现是GNU版本改变gfind找到)

The script: (must have a GNU find, if your "find" is GNU-version change the gfind 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

说明:


  • 找到您想要移动(将复制第一和后删除)并将其存储在一个TMPFILE什么都(发现)

  • 从一个TMPFILE复制的事情的清单,以新的地方(的cpio)

  • 最后删除旧文件和迪尔斯 - 从基于TMPFILE名单上(虽然...)

脚本不会处​​理符号链接,FIFO文件等,将在删除目录什么是旧的打印zilion错误,但他们不为空(包含新文件或子目录)

The script does not handling symbolic links, fifo files, etc., and will print zilion errors at the deleting directories what are old, but they're not empty (contain new files or subdirs)

空转第一:!)

这篇关于移动文件/目录7天以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆