批量按时间复制至对应文件夹的shell脚本


之前看到一篇blog:[http://blog.slogra.com/post-535.html]

里面的脚本比较实用,所以我就学习了一下,然后根据网上学习的方法,将博主后来提出的批量移动功能加了进去,在此做个备忘:

#!/bin/bash
DIR1=/root/backup
DIR2=/root/tmp
if [ ! -d $DIR2 ];then
	mkdir $DIR2
fi
rm -rf $DIR2/*
cp -ra $DIR1/* $DIR2/
for i in $DIR2/*;do ls --full-time $i | awk '{print $6}' >>/root/1.txt; done
for i in `awk '!a[$0]++' /root/1.txt | sort`;do
	mkdir $DIR2/$i;
	find $DIR2 -type f -newermt $i -print0 | xargs -0 mv -t $DIR2/$i
done

在此也谢谢原博主给我提供这个原始脚本和学习的机会*^_^*

,

《 “批量按时间复制至对应文件夹的shell脚本” 》 有 5 条评论

  1. 不是所有版本的`mv`命令都支持`-t`选项,所以建议将上面的命令:
    `find $DIR2 -type f -newermt $i -print0 | xargs -0 mv -t $DIR2/$i
    `
    修改成:
    `find $DIR2 -type f -newermt $i -print0 | xargs -0 -I ‘{}’ mv {} $DIR2/$i
    `

  2. 上面的find命令中`-newermt $i`的意思是:用find命令查找比文件`$i`的`modification time`更新的文件。

    `-newerXY file
    True if the current file has a more recent last access time (X=a), inode creation time (X=B), change time (X=c), or modification time (X=m) than the last access time (Y=a), inode creation time (Y=B), change time (Y=c), or modification time (Y=m) of file. In addition, if Y=t, then file is instead interpreted as a direct date specification of the form understood by cvs(1). Note that -newermm is equivalent to -newer.
    `

  3. `
    $ cat test.txt | xargs
    line1 line2 line3 line4 line5 line6 line7 line8 line9 line10 line11 line12 line13 line14 line15 line16 line17 line18 line19 line20

    $ cat test.txt | xargs -n1 printf ‘”%s” ‘
    “line1” “line2” “line3” “line4” “line5” “line6” “line7” “line8” “line9” “line10” “line11” “line12” “line13” “line14” “line15” “line16” “line17” “line18” “line19” “line20″ %

    $ cat test.txt | xargs -n1 -I @ echo -n ‘”@” ‘
    “line1” “line2” “line3” “line4” “line5” “line6” “line7” “line8” “line9” “line10” “line11” “line12” “line13” “line14” “line15” “line16” “line17” “line18” “line19” “line20” %
    `
    http://offbytwo.com/2011/06/26/things-you-didnt-know-about-xargs.html
    https://stackoverflow.com/questions/6958689/xargs-with-multiple-commands-as-argument
    https://stackoverflow.com/questions/17196230/bash-print-each-input-string-in-a-new-line
    https://unix.stackexchange.com/questions/89130/format-output-of-xargs

  4. Cross-platform backup tool for Windows, macOS & Linux with fast, incremental backups, client-side end-to-end encryption, compression and data deduplication. CLI and GUI included.
    https://github.com/kopia/kopia/
    `
    一个开源的备份工具,可以把数据定时向云端做增量备份。
    `

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注