BASH - 备份保留

时间:2015-03-09 15:35:11

标签: string bash design-patterns

我需要一些帮助 - 我有一个备份保留和组织脚本 我有一个充满备份的文件夹,其格式为ReportsBackup-20140309-12-00

我需要的是:

1 /删除所有文件,然后让它们从04-00h开始     我这样做了

removals =\`/bin/ls | grep -v -04-00\` 
rm /mnt/backupstorage/$removals

如何基于每年和每月ReportsBackup-20140309-12-00模式隔离文件?

1 个答案:

答案 0 :(得分:0)

措辞不好的问题。我假设您要保留所有*-04-00个文件并删除其余文件

# set up some files
$ touch ReportsBackup-201403{08,09,10}-{04,12,20}-00
$ ls -1
ReportsBackup-20140308-04-00
ReportsBackup-20140308-12-00
ReportsBackup-20140308-20-00
ReportsBackup-20140309-04-00
ReportsBackup-20140309-12-00
ReportsBackup-20140309-20-00
ReportsBackup-20140310-04-00
ReportsBackup-20140310-12-00
ReportsBackup-20140310-20-00

# use extended globbing
$ shopt -s extglob

# list the ones to keep
$ ls -1 *-04-00
ReportsBackup-20140308-04-00
ReportsBackup-20140309-04-00
ReportsBackup-20140310-04-00

# list all the ones you want to delete
$ ls -1 !(*-04-00)
ReportsBackup-20140308-12-00
ReportsBackup-20140308-20-00
ReportsBackup-20140309-12-00
ReportsBackup-20140309-20-00
ReportsBackup-20140310-12-00
ReportsBackup-20140310-20-00